Annotation of nagios/check_hw_sensors/check_hw_sensors, Revision 1.5
1.2 andrew 1: #!/usr/bin/perl
1.5 ! andrew 2: # $RedRiver: check_hw_sensors,v 1.4 2006/05/02 15:54:42 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.5 ! andrew 68: print_revision($PROGNAME,'$Revision: 1.4 $ ');
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: }
! 158: if (NAGIOS_OUTPUT && $have_results) {
1.4 andrew 159: print '<ul>';
160: }
161: foreach my $error (sort { $ERRORS{$b} <=> $ERRORS{$a} } keys %ERRORS) {
162: if (exists $states{$error}) {
163: if (NAGIOS_OUTPUT) {
164: print "<li>$error (" . scalar(@{ $states{ $error } }) . "):<ul>";
1.5 ! andrew 165: unless ($error eq 'OK') {
! 166: foreach (@{ $states{ $error } }) {
! 167: print "<li>$_</li>";
! 168: }
1.4 andrew 169: }
170: print "</ul></li>"
171: } else {
172: print "$error (" . scalar(@{ $states{ $error } }) . "):\n";
173: foreach (@{ $states{ $error } }) {
174: print " $_\n";
175: }
176: }
177: }
178: }
1.5 ! andrew 179: if ($have_results == 0) {
! 180: print "No results found\n";
! 181: }
! 182: if (NAGIOS_OUTPUT && $have_results) {
1.4 andrew 183: print '</ul>' . "\n";
184: }
1.2 andrew 185: exit $ERRORS{$state};
186:
187:
188: sub parse_file {
189: my $filename = shift;
190: my %contents;
191:
192: open my $fh, '-|', $GETCAP, '-a', '-f', $filename
193: or die "Couldn't open FILE '$GETCAP -a -f $filename': $!";
194: while (<$fh>) {
195: chomp;
196: my ($key, @c) = split /\:/;
197: foreach (@c) {
198: my ($k, $v) = split /\=/;
199: $contents{$key}{$k} = $v;
200: }
201: }
202: close $fh;
203:
204: return %contents;
205: }
206:
207: sub parse_check {
208: my $type = shift;
209: my $check = shift;
210:
211: if (defined $check->{'warn'} && $check->{'warn'} =~ /:/) {
212: if (my ($low, $high) = split /:/, $check->{'warn'}) {
213: $check->{'warn.low'} = $low;
214: $check->{'warn.high'} = $high;
215: }
216: delete $check->{'warn'};
217: }
218: if (defined $check->{'crit'} && $check->{'crit'} =~ /:/) {
219: if (my ($low, $high) = split /:/, $check->{'crit'}) {
220: $check->{'crit.low'} = $low;
221: $check->{'crit.high'} = $high;
222: }
223: delete $check->{'crit'};
224: }
225:
226: if (defined $check->{'low'}) {
227: $check->{'warn.low'} = $check->{'low'}
228: unless defined $check->{'warn.low'};
229: $check->{'crit.low'} = $check->{'low'}
230: unless defined $check->{'crit.low'};
231: }
232: if (defined $check->{'high'}) {
233: $check->{'warn.high'} = $check->{'high'}
234: unless defined $check->{'warn.high'};
235: $check->{'crit.high'} = $check->{'high'}
236: unless defined $check->{'crit.high'};
237: }
238:
239: no warnings 'uninitialized';
240: $check->{'warn'} = [ split /,\s*/, $check->{'warn'} ];
241: $check->{'crit'} = [ split /,\s*/, $check->{'crit'} ];
242:
243: return $check;
244: }
245:
246: sub check_sensor {
247: my $check = shift;
248: my $sensor = shift;
249: my $result = 'UNKNOWN';
250: my %errors = (
251: 'warn' => 'WARNING',
252: 'crit' => 'CRITICAL',
253: );
254:
255: return $result unless ref $sensor eq 'HASH';
256:
257: $check = parse_check($sensor->{'type'}, $check);
258: #print Dumper $check, $sensor;
259:
260: $result = 'OK';
261:
262: foreach my $code ('warn', 'crit') {
263: if (
264: $sensor->{'type'} eq 'volts_dc' ||
265: $sensor->{'type'} eq 'fanrpm' ||
266: $sensor->{'type'} eq 'raw'
267: ) {
268: my $data = $sensor->{'data'};
269: $data =~ s/[^\d\.]//g;
270: unless (length $data) {
271: warn "INVALID DATA ($sensor->{'data'}) for '$sensor->{'id'}'";
272: next;
273: }
274:
275: if (
276: defined $check->{$code . ".low"} ||
277: defined $check->{$code . ".high"}
278: ) {
279: if (defined $check->{$code . ".low"}) {
280: my $c = $check->{$code . ".low"};
281: $c =~ s/[^\d\.]//g;
282:
283: unless (length $c) {
284: warn "INVALID CHECK (" . $check->{$code . ".low"} .
285: ") for '$sensor->{'id'}:$code.low'";
286: next;
287: }
288:
289: $result = $errors{$code}
290: if ($c >= $data);
291: }
292: if (defined $check->{$code . ".high"}) {
293: my $c = $check->{$code . ".high"};
294: $c =~ s/[^\d\.]//g;
295: unless (length $c) {
296: warn "INVALID CHECK (" . $check->{$code . ".high"} .
297: ") for '$sensor->{'id'}:$code.high'";
298: next;
299: }
300:
301: $result = $errors{$code}
302: if ($c <= $data);
303: }
304: } elsif (defined $check->{$code}) {
305: my $matched = 0;
306: foreach my $c (@{ $check->{$code} }) {
307: $c =~ s/[^\d\.]//g;
308: unless (length $c) {
309: warn "INVALID CHECK (" . $check->{$code} .
310: ") for '$sensor->{'id'}:$code'";
311: next;
312: }
313:
314: if ($_ eq $data) {
315: $matched = 1;
316: last;
317: }
318: }
319: $result = $errors{$code} unless $matched;
320: }
321:
322: } elsif ($sensor->{'type'} eq 'temp') {
323: my ($degC, $degF) = split /\//, $sensor->{'data'};
324: $degC =~ s/[^\d\.]//g;
325: $degF =~ s/[^\d\.]//g;
326: if (
327: defined $check->{$code . ".low"} ||
328: defined $check->{$code . ".high"}
329: ) {
330: if (defined $check->{$code . ".low"}) {
331: my $c = $check->{$code . ".low"};
332: my $data = $degC;
333:
334: $data = $degF if ($c =~ /F/i);
335: unless (length $data) {
336: warn "INVALID DATA (" . $sensor->{'data'} .
337: ") for '$sensor->{'id'}'";
338: next;
339: }
340:
341: $c =~ s/[^\d\.]//g;
342: unless (length $c) {
343: warn "INVALID CHECK (" . $check->{$code . ".low"} .
344: ") for '$sensor->{'id'}':$code.low";
345: next;
346: }
347:
348: $result = $errors{$code}
349: if ($c >= $data);
350: }
351: if (defined $check->{$code . ".high"}) {
352: my $c = $check->{$code . ".high"};
353:
354: my $data = $degC;
355: $data = $degF if ($c =~ /F/i);
356: unless (length $data) {
357: warn "INVALID DATA (" . $sensor->{'data'} .
358: ") for '$sensor->{'id'}'";
359: next;
360: }
361:
362: $c =~ s/[^\d\.]//g;
363: unless (length $c) {
364: warn "INVALID CHECK (" . $check->{$code . ".high"} .
365: ") for '$sensor->{'id'}:$code.high'";
366: next;
367: }
368:
369: $result = $errors{$code}
370: if ($c <= $data);
371: }
372: } elsif (defined $check->{$code}) {
373:
374: my $matched = 0;
375: foreach my $c (@{ $check->{$code} }) {
376: my $data = $degC;
377:
378: $data = $degF if ($c =~ /F/i);
379: unless (length $data) {
380: warn "INVALID DATA (" . $sensor->{'data'} .
381: ") for '$sensor->{'id'}'";
382: next;
383: }
384:
385: $c =~ s/[^\d\.]//g;
386: unless (length $c) {
387: warn "INVALID CHECK (" . $check->{$code} .
388: ") for '$sensor->{'id'}':$code";
389: next;
390: }
391:
392: if ($_ eq $data) {
393: $matched = 1;
394: last;
395: }
396: }
397: $result = $errors{$code} unless $matched;
398: }
399:
400: } elsif (
401: $sensor->{'type'} eq 'drive' ||
402: $sensor->{'type'} eq 'indicator'
403: ) {
404: if (defined $check->{$code}) {
405: my $matched = 0;
406: foreach (@{ $check->{$code} }) {
407: if ($_ eq $sensor->{'data'}) {
408: $matched = 1;
409: last;
410: }
411: }
412: $result = $errors{$code} unless $matched;
413: }
414:
415: } else {
416: $result = 'UNKNOWN';
417: }
418:
419: }
420:
421: return $result;
422: }
423:
424: sub print_help {
425: print <<EOL;
426: $PROGNAME plugin for Nagios monitors sysctl hw.sensors on OpenBSD
427: $PROGNAME (-f [<FILENAME>]|(-s <hw.sensors id> -w limit -c limit))
428:
429: Usage:
430: -f, --filename=FILE
431: FILE to load checks from (defaults to /etc/sensorsd.conf)
432: -s, --sensor=ID
433: ID of a single sensor. "-s 0" means hw.sensors.0.
434: -w, --warning=RANGE or single ENTRY
435: Exit with WARNING status if outside of RANGE or if != ENTRY
436: -c, --critical=INTEGER
437: Exit with CRITICAL status if outside of RANGE or if != ENTRY
438:
439: -h (--help) usage help
440:
441: 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:
442: low, high, crit, warn, crit.low, crit.high, warn.low, warn.high
443:
444: An ENTRY depends on the type. The descriptions in sensorsd.conf(5) can be used when appropriate, or you can use the following:
445: 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.
446: temp - Can be as above, but if the entry has an F in it, it compares farenheit, otherwise it uses celcius.
447: 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.
448:
449: 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.
450:
451: A RANGE is a low ENTRY and a high ENTRY separated by a colon (:).
452:
453: EOL
454:
1.5 ! andrew 455: print_revision($PROGNAME, '$Revision: 1.4 $');
1.2 andrew 456: }
457:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>