=================================================================== RCS file: /cvs/nagios/check_hw_sensors/check_hw_sensors,v retrieving revision 1.35 retrieving revision 1.42 diff -u -r1.35 -r1.42 --- nagios/check_hw_sensors/check_hw_sensors 2009/11/10 23:15:15 1.35 +++ nagios/check_hw_sensors/check_hw_sensors 2009/11/24 17:54:54 1.42 @@ -1,5 +1,5 @@ #!/usr/bin/perl -T -# $RedRiver: check_hw_sensors,v 1.34 2009/11/10 23:12:49 andrew Exp $ +# $RedRiver: check_hw_sensors,v 1.41 2009/11/12 18:53:52 andrew Exp $ ######################################################################## # check_hw_sensors *** A nagios check for OpenBSD sysctl hw.sensors # @@ -10,8 +10,15 @@ local %ENV = (); +use POSIX; +use Config; +use Getopt::Long; +use List::Util qw/ first /; + my $NAGIOS_OUTPUT = 1; +our $VERSION = q{$Revision: 1.42 $}; $VERSION =~ s/^\D+([\d\.]+)\D+$/v$1/xms; + my $LICENSE = <<'EOL'; Copyright (c) 2009 Andrew Fresh Permission to use, copy, modify, and distribute this software for any @@ -27,22 +34,32 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. EOL -use POSIX; -use Config; -use lib '/usr/local/libexec/nagios'; -use utils qw($TIMEOUT %ERRORS &support); - -use Getopt::Long; -Getopt::Long::Configure('bundling'); - -my $PROGNAME = 'check_hw_sensors'; - +my $PROGNAME = 'check_hw_sensors'; my $SYSCTL = '/sbin/sysctl'; my $GETCAP = '/usr/bin/getcap'; my $BASE = 'hw.sensors'; my $DEFAULT_CONFIG = '/etc/sensorsd.conf'; -my $OSVer = $Config{'osvers'} || 0; +my $PREFIX; + +BEGIN { + ## no critic 'warnings' + no warnings 'uninitialized'; + $PREFIX = "${PREFIX}" || '/usr/local'; # Magic for OpenBSD ports tree +} +use lib $PREFIX . '/libexec/nagios'; +use utils qw($TIMEOUT %ERRORS &support); + +$SIG{'ALRM'} = sub { + print "ERROR: $PROGNAME timeout\n"; + exit $ERRORS{'UNKNOWN'}; +}; +alarm $TIMEOUT; + +Getopt::Long::Configure('bundling'); + +my $OSVer = $Config{'osvers'} || 0; + my $state = 'UNKNOWN'; # tells whether the it is warning, critical, or OK my $opt_V; my $opt_h; @@ -69,7 +86,7 @@ } if ($opt_V) { - print_revision( $PROGNAME, '$Revision: 1.35 $ ' ); + print_revision( $PROGNAME, $VERSION ); exit $ERRORS{'OK'}; } @@ -79,7 +96,9 @@ } # set the default this way so it only happens if someone typed -f or --filename -$FILENAME = $DEFAULT_CONFIG if ( defined $FILENAME && $FILENAME eq q{} ); +if ( defined $FILENAME && $FILENAME eq q{} ) { + $FILENAME = $DEFAULT_CONFIG; +} # Stuff is output in this file by print_sensor() # http://www.openbsd.org/cgi-bin/cvsweb/src/sbin/sysctl/sysctl.c @@ -130,9 +149,8 @@ } $CHECK_SENSOR = $SENSOR; - $CHECKS{$SENSOR}{'warn'} = $WARNING; - $CHECKS{$SENSOR}{'crit'} = $CRITICAL; - + if ($WARNING) { $CHECKS{$SENSOR}{'warn'} = $WARNING; } + if ($CRITICAL) { $CHECKS{$SENSOR}{'crit'} = $CRITICAL; } } elsif ( defined $FILENAME ) { %CHECKS = read_file($FILENAME); @@ -144,21 +162,22 @@ my $have_results = 0; $state = 'OK'; -foreach my $error ( sort { $ERRORS{$b} <=> $ERRORS{$a} } keys %ERRORS ) { +foreach + my $error ( reverse sort { $ERRORS{$a} <=> $ERRORS{$b} } keys %ERRORS ) +{ if ( exists $STATES{$error} ) { $have_results++; $state = $error if $ERRORS{$state} < $ERRORS{$error}; if ($NAGIOS_OUTPUT) { - print "$error (" . scalar( @{ $STATES{$error} } ) . ")"; + print $error . ' (' . scalar( @{ $STATES{$error} } ) . ')'; if ( $error ne 'OK' ) { - s/\s+/ /gxms; print '
'; print map {" - $_
"} @{ $STATES{$error} }; } } else { - print "$error (" . scalar( @{ $STATES{$error} } ) . "):\n"; + print $error . ' (' . scalar( @{ $STATES{$error} } ) . "):\n"; foreach ( @{ $STATES{$error} } ) { print " $_\n"; } @@ -173,7 +192,7 @@ sub read_sensors { my ($sensor) = @_; my @S; - open my $sysctl, '-|', $SYSCTL, $sensor + open my $sysctl, q{-|}, $SYSCTL, $sensor or die "Couldn't open sysctl: $!\n"; while (<$sysctl>) { chomp; @@ -191,6 +210,7 @@ sub parse_sensor { my ($sensor) = @_; + ## no critic 'literal' my ( $id, $output ) = split /=/xms, $sensor; my @s = split /\./xms, $id; my @o = split /,\s*/xms, $output; @@ -241,9 +261,9 @@ my $filename = shift; my %contents; - die "file '$filename' does not exist.\n" unless -e $filename; + die "file '$filename' does not exist.\n" if !-e $filename; - open my $fh, '-|', $GETCAP, '-a', '-f', $filename + open my $fh, q{-|}, $GETCAP, q{-a}, q{-f}, $filename or die "Couldn't open '$GETCAP -a -f $filename': $!\n"; while (<$fh>) { chomp; @@ -274,7 +294,7 @@ sub parse_check { my $check = shift; - return unless $check; + return if !$check; return 'STATUS' if $check->{'STATUS'}; return 'IGNORE' if $check->{'IGNORE'}; @@ -288,7 +308,7 @@ } foreach my $direction ( 'low', 'high' ) { - my $c = $code . '.' . $direction; + my $c = $code . q{.} . $direction; if ( defined $check->{$direction} ) { $check->{$c} ||= $check->{$direction}; } @@ -322,11 +342,11 @@ my ( $r, $data ); if ( exists $C->{ $sensor->{id} } ) { $r = check_sensor( $sensor, $C->{ $sensor->{id} } ); - $data = $sensor->{id} . '=' . $sensor->{output}; + $data = $sensor->{id} . q{=} . $sensor->{output}; } - elsif ( !$O->{ignore_status} && $sensor->{status} ) { + elsif ( $sensor->{status} && !$O->{IGNORE_STATUS} ) { $r = check_sensor( $sensor, { STATUS => 1 } ); - $data = $sensor->{id} . '=' . $sensor->{output}; + $data = $sensor->{id} . q{=} . $sensor->{output}; } else { @@ -345,7 +365,7 @@ my ( $sensor, $check ) = @_; my $result = 'UNKNOWN'; - return $result unless ref $sensor eq 'HASH'; + return $result if ref $sensor ne 'HASH'; $check = parse_check($check) if $check; if ( !$check ) { return $result; } @@ -358,7 +378,7 @@ elsif ( $check eq 'IGNORE' ) { return; } my $type = $sensor->{'type'}; - if (grep { $type eq $_ } + if (first { $type eq $_ } qw( fan fanrpm volt volts_dc @@ -370,13 +390,13 @@ { $result = check_sensor_numeric( $sensor->{'data'}, $check ); } - elsif ( grep { $type eq $_ } qw( drive indicator ) ) { + elsif ( first { $type eq $_ } qw( drive indicator ) ) { my $data = $sensor->{'data'}; $data =~ s/^drive\s+//xms; $result = check_sensor_list( $data, $check ); } else { - warn 'Unknown Sensor Type: ', $sensor->{'id'}, '=', $type, "\n"; + warn "Unknown Sensor Type: $sensor->{id} = $type\n"; } return $result; @@ -398,14 +418,14 @@ } foreach my $code ( 'warn', 'crit' ) { - if ( defined $check->{ $code . ".low" } - || defined $check->{ $code . ".high" } ) + if ( defined $check->{ $code . '.low' } + || defined $check->{ $code . '.high' } ) { - if (( defined $check->{ $code . ".low" } - && $check->{ $code . ".low" } >= $data + if (( defined $check->{ $code . '.low' } + && $check->{ $code . '.low' } >= $data ) - || ( defined $check->{ $code . ".high" } - && $check->{ $code . ".high" } <= $data ) + || ( defined $check->{ $code . '.high' } + && $check->{ $code . '.high' } <= $data ) ) { $result = $errors{$code}; @@ -418,7 +438,7 @@ my $c = $_; $c =~ s/[^\d\.]//gxms; if ( !length $c ) { - warn "INVALID CHECK (" . $_ . ") for '$code'\n"; + warn "INVALID CHECK ($_) for '$code'\n"; next; } @@ -529,7 +549,7 @@ EOL - print_revision( $PROGNAME, '$Revision: 1.35 $' ); + print_revision( $PROGNAME, $VERSION ); print $LICENSE; @@ -538,7 +558,6 @@ sub print_revision { my ( $prog, $rev ) = @_; - $rev =~ s/^\D+([\d\.]+)\D+$/v$1/xms; print "$prog $rev\n";