Code

Fix for regex input of '|', being output causing problems with Nagios' parsing of
[nagiosplug.git] / contrib / check_digitemp.pl
1 #!/usr/bin/perl -w
3 # check_digitemp.pl Copyright (C) 2002 by Brian C. Lane <bcl@brianlane.com>
4 #
5 # This is a NetSaint plugin script to check the temperature on a local 
6 # machine. Remote usage may be possible with SSH
7 #
8 # Permission is hereby granted, free of charge, to any person obtaining a copy
9 # of this software and associated documentation files (the "Software"), to
10 # deal in the Software without restriction, including without limitation the
11 # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 # sell copies of the Software, and to permit persons to whom the Software is
13 # furnished to do so, subject to the following conditions:
14 #
15 # The above copyright notice and this permission notice shall be included in
16 # all copies or substantial portions of the Software.
17 #
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 # IN THE SOFTWARE.
25 #
26 # ===========================================================================
27 # Howto Install in NetSaint (tested with v0.0.7)
28 #
29 # 1. Copy this script to /usr/local/netsaint/libexec/ or wherever you have
30 #    placed your NetSaint plugins
31 #
32 # 2. Create a digitemp config file in /usr/local/netsaint/etc/
33 #    eg. digitemp -i -s/dev/ttyS0 -c /usr/local/netsaint/etc/digitemp.conf
34 #
35 # 3. Make sure that the webserver user has permission to access the serial
36 #    port being used.
37 #
38 # 4. Add a command to /usr/local/netsaint/etc/commands.cfg like this:
39 #    command[check-temp]=$USER1$/check_digitemp.pl -w $ARG1$ -c $ARG2$ \
40 #    -t $ARG3$ -f $ARG4$
41 #    (fold into one line)
42 #
43 # 5. Tell NetSaint to monitor the temperature by adding a service line like
44 #    this to your hosts.cfg file:
45 #    service[kermit]=Temperature;0;24x7;3;5;1;home-admins;120;24x7;1;1;1;; \
46 #    check-temp!65!75!1!/usr/local/netsaint/etc/digitemp.conf
47 #    (fold into one line)
48 #    65 is the warning temperature
49 #    75 is the critical temperature
50 #    1 is the sensor # (as reported by digitemp -a) to monitor
51 #    digitemp.conf is the path to the config file
52 #
53 # 6. If you use Centigrade instead of Fahrenheit, change the commands.cfg
54 #    line to include the -C argument. You can then pass temperature limits in
55 #    Centigrade in the service line.
56 #
57 # ===========================================================================
58 # Howto Install in Nagios (tested with v1.0b4)
59 #
60 # 1. Copy this script to /usr/local/nagios/libexec/ or wherever you have
61 #    placed your Nagios plugins
62 #
63 # 2. Create a digitemp config file in /usr/local/nagios/etc/
64 #    eg. digitemp -i -s/dev/ttyS0 -c /usr/local/nagios/etc/digitemp.conf
65 #
66 # 3. Make sure that the webserver user has permission to access the serial
67 #    port being used.
68 #
69 # 4. Add a command to /usr/local/nagios/etc/checkcommands.cfg like this:
70 #
71 #    #DigiTemp temperature check command
72 #    define command{
73 #        command_name    check_temperature
74 #        command_line    $USER1$/check_digitemp.pl -w $ARG1$ -c $ARG2$ \
75 #        -t $ARG3$ -f $ARG4$
76 #    (fold above into one line)
77 #        }
78 #
79 # 5. Tell NetSaint to monitor the temperature by adding a service line like
80 #    this to your service.cfg file:
81 #
82 #    #DigiTemp Temperature check Service definition
83 #    define service{
84 #        use                         generic-service
85 #        host_name                       kermit
86 #        service_description             Temperature
87 #        is_volatile                     0
88 #        check_period                    24x7
89 #        max_check_attempts              3
90 #        normal_check_interval           5
91 #        retry_check_interval            2
92 #        contact_groups                  home-admins
93 #        notification_interval           240
94 #        notification_period             24x7
95 #        notification_options            w,u,c,r
96 #        check_command                   check_temperature!65!75!1!  \
97 #        /usr/local/nagios/etc/digitemp.conf
98 #        (fold into one line)
99 #        }
101 #    65 is the warning temperature
102 #    75 is the critical temperature
103 #    1 is the sensor # (as reported by digitemp -a) to monitor
104 #    digitemp.conf is the path to the config file
106 # 6. If you use Centigrade instead of Fahrenheit, change the checkcommands.cfg
107 #    line to include the -C argument. You can then pass temperature limits in
108 #    Centigrade in the service line.
110 # ===========================================================================
112 # Modules to use
113 use strict;
114 use Getopt::Std;
116 # Define all our variable usage
117 use vars qw($opt_c $opt_f $opt_t $opt_w $opt_F $opt_C
118             $temperature $conf_file $sensor $temp_fmt
119             $crit_level $warn_level $null
120             %exit_codes
121             $percent $fmt_pct 
122             $verb_err $command_line);
125 # Predefined exit codes for NetSaint
126 %exit_codes   = ('UNKNOWN' ,-1,
127                       'OK'      , 0,
128                       'WARNING' , 1,
129                       'CRITICAL', 2,);
131 # Default to Fahrenheit input and result (use -C to change this)
132 $temp_fmt = 3;
135 # Get the options
136 if ($#ARGV le 0)
138   &usage;
139 } else {
140   getopts('f:t:FCc:w:');
143 # Shortcircuit the switches
144 if (!$opt_w or $opt_w == 0 or !$opt_c or $opt_c == 0)
146   print "*** You must define WARN and CRITICAL levels!";
147   &usage;
150 # Check if levels are sane
151 if ($opt_w >= $opt_c)
153   print "*** WARN level must not be greater than CRITICAL when checking temperature!";
154   &usage;
158 $warn_level   = $opt_w;
159 $crit_level   = $opt_c;
161 # Default sensor to read is #0
162 if(!$opt_t)
164   $sensor = 0;
165 } else {
166   $sensor = $opt_t;
169 # Default config file is /etc/digitemp.conf
170 if(!$opt_f)
172   $conf_file = "/etc/digitemp.conf";
173 } else {
174   $conf_file = $opt_f;
177 # Check for config file
178 if( !-f $conf_file ) {
179   print "*** You must have a digitemp.conf file\n";
180   &usage;
184 if($opt_C)
186   $temp_fmt = 2;
189 # Read the output from digitemp
190 # Output in form 0\troom\tattic\tdrink
191 open( DIGITEMP, "/usr/local/bin/digitemp -c $conf_file -t $sensor -q -o $temp_fmt |" );
193 # Process the output from the command
194 while( <DIGITEMP> )
196 #  print "$_\n";
197   chomp;
199   if( $_ =~ /^nanosleep/i )
200   {
201     print "Error reading sensor #$sensor\n";
202     close(DIGITEMP);
203     exit $exit_codes{'UNKNOWN'};
204   } else {
205     # Check for an error from digitemp, and report it instead
206     if( $_ =~ /^Error.*/i ) {
207       print $_;
208       close(DIGITEMP);
209       exit $exit_codes{'UNKNOWN'};
210     } else {
211       ($null,$temperature) = split(/\t/);
212     }
213   }
215 close( DIGITEMP );
217 if( $temperature and $temperature >= $crit_level )
219   print "Temperature CRITICAL - Sensor #$sensor = $temperature ";
220   if( $temp_fmt == 3 ) { print "F\n"; } else { print "C\n"; }
221   exit $exit_codes{'CRITICAL'};
222 } elsif ($temperature and $temperature >= $warn_level ) {
223   print "Temperature WARNING - Sensor #$sensor = $temperature ";
224   if( $temp_fmt == 3 ) { print "F\n"; } else { print "C\n"; }
225   exit $exit_codes{'WARNING'};
226 } elsif( $temperature ) {
227   print "Temperature OK - Sensor #$sensor = $temperature ";
228   if( $temp_fmt == 3 ) { print "F\n"; } else { print "C\n"; }
229   exit $exit_codes{'OK'};
230 } else {
231     print "Error parsing result for sensor #$sensor\n";
232     exit $exit_codes{'UNKNOWN'};  
235 # Show usage
236 sub usage()
238   print "\ncheck_digitemp.pl v1.0 - NetSaint Plugin\n";
239   print "Copyright 2002 by Brian C. Lane <bcl\@brianlane.com>\n";
240   print "See source for License\n";
241   print "usage:\n";
242   print " check_digitemp.pl -t <sensor> -f <config file> -w <warnlevel> -c <critlevel>\n\n";
243   print "options:\n";
244   print " -f               DigiTemp Config File\n";
245   print " -t               DigiTemp Sensor #\n";
246   print " -F               Temperature in Fahrenheit\n";
247   print " -C               Temperature in Centigrade\n";
248   print " -w temperature   temperature >= to warn\n";
249   print " -c temperature   temperature >= when critical\n";
251   exit $exit_codes{'UNKNOWN'};