=================================================================== RCS file: /cvs/nagios/check_hw_sensors/check_hw_sensors,v retrieving revision 1.31 retrieving revision 1.32 diff -u -r1.31 -r1.32 --- nagios/check_hw_sensors/check_hw_sensors 2009/11/10 18:24:56 1.31 +++ nagios/check_hw_sensors/check_hw_sensors 2009/11/10 19:46:06 1.32 @@ -1,5 +1,5 @@ #!/usr/bin/perl -T -# $RedRiver: check_hw_sensors,v 1.30 2009/11/09 23:13:04 andrew Exp $ +# $RedRiver: check_hw_sensors,v 1.31 2009/11/10 18:24:56 andrew Exp $ ######################################################################## # check_hw_sensors *** A nagios check for OpenBSD sysctl hw.sensors # @@ -55,7 +55,7 @@ my $CHECK_SENSOR = $BASE; my %CHECKS; -my %SENSORS; +my @SENSORS; #Option checking my $status = GetOptions( @@ -68,6 +68,21 @@ "critical|c=s" => \$critical, ); +if ( $status == 0 ) { + print_help(); + exit $ERRORS{'OK'}; +} + +if ($opt_V) { + print_revision( $PROGNAME, '$Revision: 1.32 $ ' ); + exit $ERRORS{'OK'}; +} + +if ($opt_h) { + print_help(); + exit $ERRORS{'OK'}; +} + # set the default this way so it only happens if someone typed -f or --filename $filename = $DEFAULT_CONFIG if ( defined $filename && $filename eq '' ); @@ -112,29 +127,14 @@ }, ); -if ( $status == 0 ) { - print_help(); - exit $ERRORS{'OK'}; -} - -if ($opt_V) { - print_revision( $PROGNAME, '$Revision: 1.31 $ ' ); - exit $ERRORS{'OK'}; -} - -if ($opt_h) { - print_help(); - exit $ERRORS{'OK'}; -} - if ( defined $sensor ) { if ( $sensor !~ /^$BASE/ ) { $sensor = $BASE . '.' . $sensor; } $CHECK_SENSOR = $sensor; - $CHECKS{$sensor}{'warn'} = $warning if defined $warning; - $CHECKS{$sensor}{'crit'} = $critical if defined $critical; + $CHECKS{$sensor}{'warn'} = $warning; + $CHECKS{$sensor}{'crit'} = $critical; } elsif ( defined $filename ) { @@ -144,12 +144,10 @@ open my $sysctl, "-|", $SYSCTL, $CHECK_SENSOR or die "Couldn't open sysctl: $!"; while (<$sysctl>) { - - #while (<>) { chomp; - my ( $id, $output ) = split /=/; - my @s = split /\./, $id; - my @o = split /,\s*/, $output; + my ( $id, $output ) = split /=/xms; + my @s = split /\./xms, $id; + my @o = split /,\s*/xms, $output; my ( $type, $source, $descr, $data, $status ); @@ -158,17 +156,17 @@ if ( $OSVer >= 4.1 ) { $data = $o[0]; - if ( $data =~ s/\s+\((.*)\).*$// ) { + if ( $data =~ s/\s+\((.*)\).*$//xms ) { $descr = $1; } $status = $o[1]; - ( $source, $type ) = $id =~ /([^\.]+)\.([^\.]+?)\d+$/; + ( $source, $type ) = $id =~ /([^\.]+)\.([^\.]+?)\d+$/xms; } elsif ( $OSVer >= 4.0 ) { $data = $o[2]; $status = $o[3]; foreach my $t (@Type_Map) { - if ( $data =~ /$t->{'regex'}/ ) { + if ( $data =~ /$t->{'regex'}/xms ) { $type = $t->{'type'}; last; } @@ -182,7 +180,8 @@ $type ||= 'unknown'; - $SENSORS{$id} = { + push @SENSORS, + { id => $id, output => $output, source => $source, @@ -190,47 +189,37 @@ status => $status, type => $type, data => $data, - }; - + }; } close $sysctl; -sub as_if_numeric { - my $_a = $a; - my $_b = $b; - $_a =~ s/\D//g; - $_b =~ s/\D//g; - $_a <=> $_b; -} - -foreach my $s ( sort as_if_numeric keys %SENSORS ) { +foreach my $sensor (@SENSORS) { my ( $r, $data ); - if ( exists $CHECKS{$s} ) { - $r = check_sensor( $SENSORS{$s}, $CHECKS{$s} ); - $data = $s . '=' . $SENSORS{$s}{'output'}; + if ( exists $CHECKS{ $sensor->{id} } ) { + $r = check_sensor( $sensor, $CHECKS{ $sensor->{id} } ); + $data = $sensor->{id} . '=' . $sensor->{output}; } - elsif ( !$ignore_status ) { - $r = check_sensor( $SENSORS{$s} ); - $data = $s . '=' . $SENSORS{$s}{'output'}; + elsif ( !$ignore_status && $sensor->{status} ) { + $r = check_sensor( $sensor, { STATUS => 1 } ); + $data = $sensor->{id} . '=' . $sensor->{output}; } else { - # ignore this sensor + # ignore this sensor, theoretically you could do the check and + # that would show unknown sensors. } - next unless defined $r; - push @{ $states{$r} }, $data; + if ( defined $r ) { + push @{ $states{$r} }, $data; + } } -$state = 'OK'; my $have_results = 0; -foreach my $error ( sort { $ERRORS{$a} <=> $ERRORS{$b} } keys %ERRORS ) { - if ( exists $states{$error} ) { - $have_results++; - $state = $error; - } -} +$state = 'OK'; foreach my $error ( sort { $ERRORS{$b} <=> $ERRORS{$a} } keys %ERRORS ) { if ( exists $states{$error} ) { + $have_results++; + $state = $error if $ERRORS{$state} < $ERRORS{$error}; + if (NAGIOS_OUTPUT) { print "$error (" . scalar( @{ $states{$error} } ) . ")"; if ( $error ne 'OK' ) { @@ -284,7 +273,7 @@ my $check = shift; return unless $check; - return if $check->{'STATUS'}; + return 'STATUS' if $check->{'STATUS'}; return 'IGNORE' if $check->{'IGNORE'}; foreach my $code ( 'crit', 'warn' ) { @@ -322,18 +311,18 @@ $check = parse_check($check) if $check; if ( !$check ) { + return $result; + } + elsif ( $check eq 'STATUS' ) { # It looks like doing this should be safe, from # src/sbin/sysctl/sysctl.c - return $sensor->{'status'} if $sensor->{'status'}; - - return; + return ( $sensor->{'status'} || $result ); } elsif ( $check eq 'IGNORE' ) { return; } - $result = 'OK'; foreach my $code ( 'warn', 'crit' ) { if ( $sensor->{'type'} eq 'fan' || $sensor->{'type'} eq 'fanrpm' @@ -349,7 +338,7 @@ || $sensor->{'type'} eq 'timedelta' ) { my $data = $sensor->{'data'}; - $data =~ s/[^\d\.]//g; + $data =~ s/[^\d\.]//gxms; if ( !length $data ) { warn "INVALID DATA ($sensor->{'data'}) for '$sensor->{'id'}'\n"; @@ -361,7 +350,7 @@ { if ( defined $check->{ $code . ".low" } ) { my $c = $check->{ $code . ".low" }; - $c =~ s/[^\d\.]//g; + $c =~ s/[^\d\.]//gxms; if ( !length $c ) { warn "INVALID CHECK (" @@ -375,7 +364,7 @@ } if ( defined $check->{ $code . ".high" } ) { my $c = $check->{ $code . ".high" }; - $c =~ s/[^\d\.]//g; + $c =~ s/[^\d\.]//gxms; if ( !length $c ) { warn "INVALID CHECK (" . $check->{ $code . ".high" } @@ -386,12 +375,13 @@ $result = $errors{$code} if ( $c <= $data ); } + $result = 'OK' if $result eq 'UNKNOWN'; } elsif ( @{ $check->{$code} } ) { my $matched = 0; foreach ( @{ $check->{$code} } ) { my $c = $_; - $c =~ s/[^\d\.]//g; + $c =~ s/[^\d\.]//gxms; if ( !length $c ) { warn "INVALID CHECK (" . $_ . ") for '$sensor->{'id'}:$code'\n"; @@ -403,15 +393,19 @@ last; } } - $result = $errors{$code} unless $matched; + if ($matched) { + $result = 'OK' if $result eq 'UNKNOWN'; + } + else { + $result = $errors{$code}; + } } - } elsif ( $sensor->{'type'} eq 'temp' ) { my ( $degC, $degF ) = split /\//, $sensor->{'data'}; - $degC =~ s/[^\d\.]//g; + $degC =~ s/[^\d\.]//gxms; $degF ||= $degC * 9 / 5 + 32; - $degF =~ s/[^\d\.]//g; + $degF =~ s/[^\d\.]//gxms; if ( defined $check->{ $code . ".low" } || defined $check->{ $code . ".high" } ) { @@ -419,7 +413,7 @@ my $c = $check->{ $code . ".low" }; my $data = $degC; - $data = $degF if ( $c =~ /F/i ); + $data = $degF if ( $c =~ /F/ixms ); if ( !length $data ) { warn "INVALID DATA (" . $sensor->{'data'} @@ -427,7 +421,7 @@ next; } - $c =~ s/[^\d\.]//g; + $c =~ s/[^\d\.]//gxms; if ( !length $c ) { warn "INVALID CHECK (" . $check->{ $code . ".low" } @@ -442,7 +436,7 @@ my $c = $check->{ $code . ".high" }; my $data = $degC; - $data = $degF if ( $c =~ /F/i ); + $data = $degF if ( $c =~ /F/ixms ); if ( !length $data ) { warn "INVALID DATA (" . $sensor->{'data'} @@ -450,7 +444,7 @@ next; } - $c =~ s/[^\d\.]//g; + $c =~ s/[^\d\.]//gxms; if ( !length $c ) { warn "INVALID CHECK (" . $check->{ $code . ".high" } @@ -461,15 +455,15 @@ $result = $errors{$code} if ( $c <= $data ); } + $result = 'OK' if $result eq 'UNKNOWN'; } elsif ( @{ $check->{$code} } ) { - my $matched = 0; foreach ( @{ $check->{$code} } ) { my $c = $_; my $data = $degC; - $data = $degF if ( $c =~ /F/i ); + $data = $degF if ( $c =~ /F/ixms ); if ( !length $data ) { warn "INVALID DATA (" . $sensor->{'data'} @@ -477,7 +471,7 @@ next; } - $c =~ s/[^\d\.]//g; + $c =~ s/[^\d\.]//gxms; if ( !length $c ) { warn "INVALID CHECK (" . $_ . ") for '$sensor->{'id'}':$code\n"; @@ -489,7 +483,12 @@ last; } } - $result = $errors{$code} unless $matched; + if ($matched) { + $result = 'OK' if $result eq 'UNKNOWN'; + } + else { + $result = $errors{$code}; + } } } elsif ($sensor->{'type'} eq 'drive' @@ -504,7 +503,12 @@ last; } } - $result = $errors{$code} unless $matched; + if ($matched) { + $result = 'OK' if $result eq 'UNKNOWN'; + } + else { + $result = $errors{$code}; + } } } @@ -514,7 +518,6 @@ '=', $sensor->{'type'}, "\n"; - $result = 'UNKNOWN'; } } @@ -585,7 +588,7 @@ EOL - print_revision( $PROGNAME, '$Revision: 1.31 $' ); + print_revision( $PROGNAME, '$Revision: 1.32 $' ); print $License; }