summaryrefslogtreecommitdiffstats
path: root/contrib/check_digitemp.pl
blob: d2b40a104b792bb4bfe7adf51b2a840d5c0373ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/perl -w

# check_digitemp.pl Copyright (C) 2002 by Brian C. Lane <bcl@brianlane.com>
#
# This is a NetSaint plugin script to check the temperature on a local 
# machine. Remote usage may be possible with SSH
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
# ===========================================================================
# Howto Install in NetSaint (tested with v0.0.7)
#
# 1. Copy this script to /usr/local/netsaint/libexec/ or wherever you have
#    placed your NetSaint plugins
#
# 2. Create a digitemp config file in /usr/local/netsaint/etc/
#    eg. digitemp -i -s/dev/ttyS0 -c /usr/local/netsaint/etc/digitemp.conf
#
# 3. Make sure that the webserver user has permission to access the serial
#    port being used.
#
# 4. Add a command to /usr/local/netsaint/etc/commands.cfg like this:
#    command[check-temp]=$USER1$/check_digitemp.pl -w $ARG1$ -c $ARG2$ \
#    -t $ARG3$ -f $ARG4$
#    (fold into one line)
#
# 5. Tell NetSaint to monitor the temperature by adding a service line like
#    this to your hosts.cfg file:
#    service[kermit]=Temperature;0;24x7;3;5;1;home-admins;120;24x7;1;1;1;; \
#    check-temp!65!75!1!/usr/local/netsaint/etc/digitemp.conf
#    (fold into one line)
#    65 is the warning temperature
#    75 is the critical temperature
#    1 is the sensor # (as reported by digitemp -a) to monitor
#    digitemp.conf is the path to the config file
#
# 6. If you use Centigrade instead of Fahrenheit, change the commands.cfg
#    line to include the -C argument. You can then pass temperature limits in
#    Centigrade in the service line.
#
# ===========================================================================
# Howto Install in Nagios (tested with v1.0b4)
#
# 1. Copy this script to /usr/local/nagios/libexec/ or wherever you have
#    placed your Nagios plugins
#
# 2. Create a digitemp config file in /usr/local/nagios/etc/
#    eg. digitemp -i -s/dev/ttyS0 -c /usr/local/nagios/etc/digitemp.conf
#
# 3. Make sure that the webserver user has permission to access the serial
#    port being used.
#
# 4. Add a command to /usr/local/nagios/etc/checkcommands.cfg like this:
#
#    #DigiTemp temperature check command
#    define command{
#        command_name    check_temperature
#        command_line    $USER1$/check_digitemp.pl -w $ARG1$ -c $ARG2$ \
#        -t $ARG3$ -f $ARG4$
#    (fold above into one line)
#        }
#
# 5. Tell NetSaint to monitor the temperature by adding a service line like
#    this to your service.cfg file:
#
#    #DigiTemp Temperature check Service definition
#    define service{
#        use                         generic-service
#        host_name                       kermit
#        service_description             Temperature
#        is_volatile                     0
#        check_period                    24x7
#        max_check_attempts              3
#        normal_check_interval           5
#        retry_check_interval            2
#        contact_groups                  home-admins
#        notification_interval           240
#        notification_period             24x7
#        notification_options            w,u,c,r
#        check_command                   check_temperature!65!75!1!  \
#        /usr/local/nagios/etc/digitemp.conf
#        (fold into one line)
#        }
#
#    65 is the warning temperature
#    75 is the critical temperature
#    1 is the sensor # (as reported by digitemp -a) to monitor
#    digitemp.conf is the path to the config file
#
# 6. If you use Centigrade instead of Fahrenheit, change the checkcommands.cfg
#    line to include the -C argument. You can then pass temperature limits in
#    Centigrade in the service line.
#
# ===========================================================================

# Modules to use
use strict;
use Getopt::Std;

# Define all our variable usage
use vars qw($opt_c $opt_f $opt_t $opt_w $opt_F $opt_C
	    $temperature $conf_file $sensor $temp_fmt
	    $crit_level $warn_level $null
            %exit_codes
            $percent $fmt_pct 
            $verb_err $command_line);


# Predefined exit codes for NetSaint
%exit_codes   = ('UNKNOWN' ,-1,
		      'OK'      , 0,
                      'WARNING' , 1,
                      'CRITICAL', 2,);

# Default to Fahrenheit input and result (use -C to change this)
$temp_fmt = 3;


# Get the options
if ($#ARGV le 0)
{
  &usage;
} else {
  getopts('f:t:FCc:w:');
}

# Shortcircuit the switches
if (!$opt_w or $opt_w == 0 or !$opt_c or $opt_c == 0)
{
  print "*** You must define WARN and CRITICAL levels!";
  &usage;
}

# Check if levels are sane
if ($opt_w >= $opt_c)
{
  print "*** WARN level must not be greater than CRITICAL when checking temperature!";
  &usage;
}


$warn_level   = $opt_w;
$crit_level   = $opt_c;

# Default sensor to read is #0
if(!$opt_t)
{
  $sensor = 0;
} else {
  $sensor = $opt_t;
}

# Default config file is /etc/digitemp.conf
if(!$opt_f)
{
  $conf_file = "/etc/digitemp.conf";
} else {
  $conf_file = $opt_f;
}

# Check for config file
if( !-f $conf_file ) {
  print "*** You must have a digitemp.conf file\n";
  &usage;
}


if($opt_C)
{
  $temp_fmt = 2;
}

# Read the output from digitemp
# Output in form 0\troom\tattic\tdrink
open( DIGITEMP, "/usr/local/bin/digitemp -c $conf_file -t $sensor -q -o $temp_fmt |" );

# Process the output from the command
while( <DIGITEMP> )
{
#  print "$_\n";
  chomp;

  if( $_ =~ /^nanosleep/i )
  {
    print "Error reading sensor #$sensor\n";
    close(DIGITEMP);
    exit $exit_codes{'UNKNOWN'};
  } else {
    # Check for an error from digitemp, and report it instead
    if( $_ =~ /^Error.*/i ) {
      print $_;
      close(DIGITEMP);
      exit $exit_codes{'UNKNOWN'};
    } else {
      ($null,$temperature) = split(/\t/);
    }
  }
}
close( DIGITEMP );

if( $temperature and $temperature >= $crit_level )
{
  print "Temperature CRITICAL - Sensor #$sensor = $temperature ";
  if( $temp_fmt == 3 ) { print "F\n"; } else { print "C\n"; }
  exit $exit_codes{'CRITICAL'};
} elsif ($temperature and $temperature >= $warn_level ) {
  print "Temperature WARNING - Sensor #$sensor = $temperature ";
  if( $temp_fmt == 3 ) { print "F\n"; } else { print "C\n"; }
  exit $exit_codes{'WARNING'};
} elsif( $temperature ) {
  print "Temperature OK - Sensor #$sensor = $temperature ";
  if( $temp_fmt == 3 ) { print "F\n"; } else { print "C\n"; }
  exit $exit_codes{'OK'};
} else {
    print "Error parsing result for sensor #$sensor\n";
    exit $exit_codes{'UNKNOWN'};  
}

# Show usage
sub usage()
{
  print "\ncheck_digitemp.pl v1.0 - NetSaint Plugin\n";
  print "Copyright 2002 by Brian C. Lane <bcl\@brianlane.com>\n";
  print "See source for License\n";
  print "usage:\n";
  print " check_digitemp.pl -t <sensor> -f <config file> -w <warnlevel> -c <critlevel>\n\n";
  print "options:\n";
  print " -f               DigiTemp Config File\n";
  print " -t               DigiTemp Sensor #\n";
  print " -F               Temperature in Fahrenheit\n";
  print " -C               Temperature in Centigrade\n";
  print " -w temperature   temperature >= to warn\n";
  print " -c temperature   temperature >= when critical\n";

  exit $exit_codes{'UNKNOWN'}; 
}