Annotation of nagios/check_hw_sensors/check_hw_sensors, Revision 1.9
1.2 andrew 1: #!/usr/bin/perl
1.9 ! andrew 2: # $RedRiver: check_hw_sensors,v 1.8 2006/05/02 21:23:29 andrew Exp $
1.2 andrew 3: ########################################################################
4: # check_hw_sensors *** A nagios check for OpenBSD hw.sensors
5: #
6: # 2006.05.01 #*#*# andrew fresh <andrew@mad-techies.org>
7: ########################################################################
1.4 andrew 8: # TODO:
9: # Really need to fix the documentation issue.
10: #
11: # I want the ability to just check the "status" entry that is in some output. For example the OK here:
12: # hw.sensors.1=esm0, CPU 1, OK, temp, 31.00 degC / 87.80 degF
13: ########################################################################
1.2 andrew 14: use strict;
15: use warnings;
16:
17: #use Data::Dumper;
18:
1.4 andrew 19: use constant NAGIOS_OUTPUT => 1;
20:
1.2 andrew 21: use POSIX;
1.4 andrew 22: use lib "/usr/local/libexec/nagios";
23: #use lib $ENV{'HOME'};
1.2 andrew 24: use utils qw($TIMEOUT %ERRORS &print_revision &support);
25:
26: use Getopt::Long;
27: Getopt::Long::Configure('bundling');
28:
29: my $PROGNAME = "check_hw_sensors";
30:
1.4 andrew 31: my $SYSCTL = '/sbin/sysctl';
32: my $GETCAP = '/usr/bin/getcap';
1.2 andrew 33: my $BASE = 'hw.sensors';
1.4 andrew 34: my $DEFAULT_CONFIG = '/etc/sensorsd.conf';
1.2 andrew 35:
36: my $state = 'UNKNOWN'; # tells whether the it is warning, critical, or OK
37: my %states; # This stores the count of states;
38: my $filename;
39: my $sensor;
40: my $warning;
41: my $critical;
42: my $opt_h ;
43: my $opt_V ;
44:
1.4 andrew 45: my $CHECK_SENSOR = $BASE;
46: my %CHECKS;
47: my %SENSORS;
1.2 andrew 48:
49: #Option checking
50: my $status = GetOptions(
1.3 andrew 51: "version|V" => \$opt_V,
52: "help|h" => \$opt_h,
1.2 andrew 53: "filename|f:s" => \$filename,
54: "sensor|s=s" => \$sensor,
55: "warning|w=s" => \$warning,
56: "critical|c=s" => \$critical,
57: );
58:
59: # set the default this way so it only happens if someone typed -f or --filename
1.4 andrew 60: $filename = $DEFAULT_CONFIG if (defined $filename && $filename eq '');
1.2 andrew 61:
62: if ($status == 0) {
63: print_help() ;
64: exit $ERRORS{'OK'};
65: }
66:
67: if ($opt_V) {
1.9 ! andrew 68: print_revision($PROGNAME,'$Revision: 1.8 $ ');
1.2 andrew 69: exit $ERRORS{'OK'};
70: }
71:
72: if ($opt_h) {
73: print_help();
74: exit $ERRORS{'OK'};
75: }
76:
77: unless ( (
78: defined $filename ||
79: (defined $sensor && ($warning || $critical))
80: ) &&
81: ( (!defined $filename) || (!defined $sensor) )
82: ) {
83: print_help();
84: exit $ERRORS{'OK'};
85: }
86:
87:
88: if (defined $sensor) {
89: if ($sensor !~ /^$BASE/) {
90: $sensor = $BASE . '.' . $sensor;
91: }
92: $CHECK_SENSOR = $sensor;
93:
94: $CHECKS{$sensor} = {
1.4 andrew 95: 'warn' => $warning,
96: 'crit' => $critical,
1.2 andrew 97: };
98: } elsif (defined $filename) {
99: %CHECKS = parse_file($filename);
100: }
101:
102: open my $sysctl, "-|", $SYSCTL, $CHECK_SENSOR
103: or die "Couldn't open sysctl: $!";
104: while (<$sysctl>) {
105: #while (<>) {
106: chomp;
107: my ($id, $output) = split /=/;
108: my @o = split /,\s*/, $output;
109:
110: my $source = $o[0];
111: my $descr = $o[1];
112: my $status = $o[2] if @o == 5;
113: my $type = $o[-2];
114: my $data = $o[-1];
115:
116: $SENSORS{$id} = {
117: id => $id,
118: output => $output,
119: source => $source,
120: description => $descr,
121: status => $status,
122: type => $type,
123: data => $data,
124: };
125:
126: }
127: close $sysctl;
128:
129: sub as_if_numeric {
130: my $_a = $a;
131: my $_b = $b;
132: $_a =~ s/\D//g;
133: $_b =~ s/\D//g;
134: $_a <=> $_b;
135: }
136:
137: foreach my $check (sort as_if_numeric keys %CHECKS) {
138: if (exists $SENSORS{$check}) {
139: my $r = check_sensor($CHECKS{$check}, $SENSORS{$check});
140: push @{ $states{ $r } },
141: $check . '=' . $SENSORS{$check}{'output'};
142: } else {
143: # XXX Error missing sensor
144: push @{ $states{'UNKNOWN'} }, $check . '=No sensor with this id';
145: }
146: }
147:
148: #print Dumper \%states;
149:
150: $state = 'OK';
1.5 andrew 151: my $have_results = 0;
152: foreach my $error (sort { $ERRORS{$a} <=> $ERRORS{$b} } keys %ERRORS) {
153: if (exists $states{$error}) {
154: $have_results++;
155: $state = $error;
156: }
157: }
1.4 andrew 158: foreach my $error (sort { $ERRORS{$b} <=> $ERRORS{$a} } keys %ERRORS) {
159: if (exists $states{$error}) {
160: if (NAGIOS_OUTPUT) {
1.8 andrew 161: print "$error (" . scalar(@{ $states{ $error } }) . ")";
1.5 andrew 162: unless ($error eq 'OK') {
1.9 ! andrew 163: print '<br>';
! 164: print map { " - $_<br>" } @{ $states{ $error } };
1.4 andrew 165: }
166: } else {
167: print "$error (" . scalar(@{ $states{ $error } }) . "):\n";
168: foreach (@{ $states{ $error } }) {
169: print " $_\n";
170: }
171: }
172: }
173: }
1.5 andrew 174: if ($have_results == 0) {
175: print "No results found\n";
176: }
1.2 andrew 177: exit $ERRORS{$state};
178:
179:
180: sub parse_file {
181: my $filename = shift;
182: my %contents;
183:
184: open my $fh, '-|', $GETCAP, '-a', '-f', $filename
185: or die "Couldn't open FILE '$GETCAP -a -f $filename': $!";
186: while (<$fh>) {
187: chomp;
188: my ($key, @c) = split /\:/;
189: foreach (@c) {
190: my ($k, $v) = split /\=/;
191: $contents{$key}{$k} = $v;
192: }
193: }
194: close $fh;
195:
196: return %contents;
197: }
198:
199: sub parse_check {
200: my $type = shift;
201: my $check = shift;
202:
203: if (defined $check->{'warn'} && $check->{'warn'} =~ /:/) {
204: if (my ($low, $high) = split /:/, $check->{'warn'}) {
205: $check->{'warn.low'} = $low;
206: $check->{'warn.high'} = $high;
207: }
208: delete $check->{'warn'};
209: }
210: if (defined $check->{'crit'} && $check->{'crit'} =~ /:/) {
211: if (my ($low, $high) = split /:/, $check->{'crit'}) {
212: $check->{'crit.low'} = $low;
213: $check->{'crit.high'} = $high;
214: }
215: delete $check->{'crit'};
216: }
217:
218: if (defined $check->{'low'}) {
219: $check->{'warn.low'} = $check->{'low'}
220: unless defined $check->{'warn.low'};
221: $check->{'crit.low'} = $check->{'low'}
222: unless defined $check->{'crit.low'};
223: }
224: if (defined $check->{'high'}) {
225: $check->{'warn.high'} = $check->{'high'}
226: unless defined $check->{'warn.high'};
227: $check->{'crit.high'} = $check->{'high'}
228: unless defined $check->{'crit.high'};
229: }
230:
231: no warnings 'uninitialized';
232: $check->{'warn'} = [ split /,\s*/, $check->{'warn'} ];
233: $check->{'crit'} = [ split /,\s*/, $check->{'crit'} ];
234:
235: return $check;
236: }
237:
238: sub check_sensor {
239: my $check = shift;
240: my $sensor = shift;
241: my $result = 'UNKNOWN';
242: my %errors = (
243: 'warn' => 'WARNING',
244: 'crit' => 'CRITICAL',
245: );
246:
247: return $result unless ref $sensor eq 'HASH';
248:
249: $check = parse_check($sensor->{'type'}, $check);
250: #print Dumper $check, $sensor;
251:
252: $result = 'OK';
253:
254: foreach my $code ('warn', 'crit') {
255: if (
256: $sensor->{'type'} eq 'volts_dc' ||
257: $sensor->{'type'} eq 'fanrpm' ||
258: $sensor->{'type'} eq 'raw'
259: ) {
260: my $data = $sensor->{'data'};
261: $data =~ s/[^\d\.]//g;
262: unless (length $data) {
263: warn "INVALID DATA ($sensor->{'data'}) for '$sensor->{'id'}'";
264: next;
265: }
266:
267: if (
268: defined $check->{$code . ".low"} ||
269: defined $check->{$code . ".high"}
270: ) {
271: if (defined $check->{$code . ".low"}) {
272: my $c = $check->{$code . ".low"};
273: $c =~ s/[^\d\.]//g;
274:
275: unless (length $c) {
276: warn "INVALID CHECK (" . $check->{$code . ".low"} .
277: ") for '$sensor->{'id'}:$code.low'";
278: next;
279: }
280:
281: $result = $errors{$code}
282: if ($c >= $data);
283: }
284: if (defined $check->{$code . ".high"}) {
285: my $c = $check->{$code . ".high"};
286: $c =~ s/[^\d\.]//g;
287: unless (length $c) {
288: warn "INVALID CHECK (" . $check->{$code . ".high"} .
289: ") for '$sensor->{'id'}:$code.high'";
290: next;
291: }
292:
293: $result = $errors{$code}
294: if ($c <= $data);
295: }
1.9 ! andrew 296: } elsif (@{ $check->{$code} }) {
1.2 andrew 297: my $matched = 0;
298: foreach my $c (@{ $check->{$code} }) {
299: $c =~ s/[^\d\.]//g;
300: unless (length $c) {
1.7 andrew 301: warn "INVALID CHECK (" . $c .
1.2 andrew 302: ") for '$sensor->{'id'}:$code'";
303: next;
304: }
305:
306: if ($_ eq $data) {
307: $matched = 1;
308: last;
309: }
310: }
311: $result = $errors{$code} unless $matched;
312: }
313:
314: } elsif ($sensor->{'type'} eq 'temp') {
315: my ($degC, $degF) = split /\//, $sensor->{'data'};
316: $degC =~ s/[^\d\.]//g;
317: $degF =~ s/[^\d\.]//g;
318: if (
319: defined $check->{$code . ".low"} ||
320: defined $check->{$code . ".high"}
321: ) {
322: if (defined $check->{$code . ".low"}) {
323: my $c = $check->{$code . ".low"};
324: my $data = $degC;
325:
326: $data = $degF if ($c =~ /F/i);
327: unless (length $data) {
328: warn "INVALID DATA (" . $sensor->{'data'} .
329: ") for '$sensor->{'id'}'";
330: next;
331: }
332:
333: $c =~ s/[^\d\.]//g;
334: unless (length $c) {
335: warn "INVALID CHECK (" . $check->{$code . ".low"} .
336: ") for '$sensor->{'id'}':$code.low";
337: next;
338: }
339:
340: $result = $errors{$code}
341: if ($c >= $data);
342: }
343: if (defined $check->{$code . ".high"}) {
344: my $c = $check->{$code . ".high"};
345:
346: my $data = $degC;
347: $data = $degF if ($c =~ /F/i);
348: unless (length $data) {
349: warn "INVALID DATA (" . $sensor->{'data'} .
350: ") for '$sensor->{'id'}'";
351: next;
352: }
353:
354: $c =~ s/[^\d\.]//g;
355: unless (length $c) {
356: warn "INVALID CHECK (" . $check->{$code . ".high"} .
357: ") for '$sensor->{'id'}:$code.high'";
358: next;
359: }
360:
361: $result = $errors{$code}
362: if ($c <= $data);
363: }
1.9 ! andrew 364: } elsif (@{ $check->{$code} }) {
1.2 andrew 365:
366: my $matched = 0;
367: foreach my $c (@{ $check->{$code} }) {
368: my $data = $degC;
369:
370: $data = $degF if ($c =~ /F/i);
371: unless (length $data) {
372: warn "INVALID DATA (" . $sensor->{'data'} .
373: ") for '$sensor->{'id'}'";
374: next;
375: }
376:
377: $c =~ s/[^\d\.]//g;
378: unless (length $c) {
1.7 andrew 379: warn "INVALID CHECK (" . $c .
1.2 andrew 380: ") for '$sensor->{'id'}':$code";
381: next;
382: }
383:
384: if ($_ eq $data) {
385: $matched = 1;
386: last;
387: }
388: }
389: $result = $errors{$code} unless $matched;
390: }
391:
392: } elsif (
393: $sensor->{'type'} eq 'drive' ||
394: $sensor->{'type'} eq 'indicator'
395: ) {
1.9 ! andrew 396: if (@{ $check->{$code} }) {
1.2 andrew 397: my $matched = 0;
398: foreach (@{ $check->{$code} }) {
399: if ($_ eq $sensor->{'data'}) {
400: $matched = 1;
401: last;
402: }
403: }
404: $result = $errors{$code} unless $matched;
405: }
406:
407: } else {
408: $result = 'UNKNOWN';
409: }
410:
411: }
412:
413: return $result;
414: }
415:
416: sub print_help {
417: print <<EOL;
418: $PROGNAME plugin for Nagios monitors sysctl hw.sensors on OpenBSD
419: $PROGNAME (-f [<FILENAME>]|(-s <hw.sensors id> -w limit -c limit))
420:
421: Usage:
422: -f, --filename=FILE
423: FILE to load checks from (defaults to /etc/sensorsd.conf)
424: -s, --sensor=ID
425: ID of a single sensor. "-s 0" means hw.sensors.0.
426: -w, --warning=RANGE or single ENTRY
427: Exit with WARNING status if outside of RANGE or if != ENTRY
428: -c, --critical=INTEGER
429: Exit with CRITICAL status if outside of RANGE or if != ENTRY
430:
431: -h (--help) usage help
432:
433: FILE is in the same format as sensorsd.conf(5). These additional entries in the file are ignored by sensorsd(8). $PROGNAME understands the following entries:
434: low, high, crit, warn, crit.low, crit.high, warn.low, warn.high
435:
436: An ENTRY depends on the type. The descriptions in sensorsd.conf(5) can be used when appropriate, or you can use the following:
437: volts_dc, fanrpm or raw - Anything that includes digits. Anything that isn't a digit or period is stripped from the entry and the sensor output and they are compared.
438: temp - Can be as above, but if the entry has an F in it, it compares farenheit, otherwise it uses celcius.
439: indicator or drive - does a case sensitive match of each entry in the comma separated list and if it does not match any of the entries, it matches the status.
440:
441: The entries 'crit' or 'warn' (or the -c or -w on the command line) may be a RANGE or a comma separated list of acceptable values. The comma separated list of values contains a list of things that will NOT cause the status. This is possibly counterintuitive, but you are more likely to know good values than bad values.
442:
443: A RANGE is a low ENTRY and a high ENTRY separated by a colon (:).
444:
445: EOL
446:
1.9 ! andrew 447: print_revision($PROGNAME, '$Revision: 1.8 $');
1.2 andrew 448: }
449:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>