version 1.26, 2009/11/09 21:25:07 |
version 1.28, 2009/11/09 22:58:41 |
|
|
#!/usr/bin/perl -T |
#!/usr/bin/perl -T |
# $RedRiver: check_hw_sensors,v 1.25 2008/03/10 16:21:53 andrew Exp $ |
# $RedRiver: check_hw_sensors,v 1.27 2009/11/09 22:51:44 andrew Exp $ |
######################################################################## |
######################################################################## |
# check_hw_sensors *** A nagios check for OpenBSD sysctl hw.sensors |
# check_hw_sensors *** A nagios check for OpenBSD sysctl hw.sensors |
# |
# |
|
|
|
|
my $state = 'UNKNOWN'; # tells whether the it is warning, critical, or OK |
my $state = 'UNKNOWN'; # tells whether the it is warning, critical, or OK |
my %states; # This stores the count of states; |
my %states; # This stores the count of states; |
my $filename; |
my $opt_V; |
|
my $opt_h; |
my $ignore_status; |
my $ignore_status; |
|
my $filename; |
my $sensor; |
my $sensor; |
my $warning; |
my $warning; |
my $critical; |
my $critical; |
my $opt_h; |
|
my $opt_V; |
|
|
|
my $CHECK_SENSOR = $BASE; |
my $CHECK_SENSOR = $BASE; |
my %CHECKS; |
my %CHECKS; |
|
|
#Option checking |
#Option checking |
my $status = GetOptions( |
my $status = GetOptions( |
"version|V" => \$opt_V, |
"version|V" => \$opt_V, |
"filename|f:s" => \$filename, |
"help|h" => \$opt_h, |
"ignore-status|i" => \$ignore_status, |
"ignore-status|i" => \$ignore_status, |
|
"filename|f:s" => \$filename, |
"sensor|s=s" => \$sensor, |
"sensor|s=s" => \$sensor, |
"warning|w=s" => \$warning, |
"warning|w=s" => \$warning, |
"critical|c=s" => \$critical, |
"critical|c=s" => \$critical, |
|
|
exit $ERRORS{'OK'}; |
exit $ERRORS{'OK'}; |
} |
} |
|
|
if ( |
if ($opt_h |
$opt_h |
|| ( ( !defined $filename ) |
|| ( ( !defined $filename ) && ( !defined $sensor ) ) |
&& ( !defined $sensor ) |
|
&& ( !defined $ignore_status ) ) |
|
|
#|| ( defined $sensor && !( $warning || $critical ) ) |
#|| ( defined $sensor && !( $warning || $critical ) ) |
) |
) |
{ |
{ |
|
|
$r = check_sensor( $SENSORS{$s}, $CHECKS{$s} ); |
$r = check_sensor( $SENSORS{$s}, $CHECKS{$s} ); |
$data = $s . '=' . $SENSORS{$s}{'output'}; |
$data = $s . '=' . $SENSORS{$s}{'output'}; |
} |
} |
elsif ( not $ignore_status ) { |
elsif ( !$ignore_status ) { |
$r = check_sensor( $SENSORS{$s} ); |
$r = check_sensor( $SENSORS{$s} ); |
$data = $s . '=' . $SENSORS{$s}{'output'}; |
$data = $s . '=' . $SENSORS{$s}{'output'}; |
} |
} |
|
|
return $result unless ref $sensor eq 'HASH'; |
return $result unless ref $sensor eq 'HASH'; |
$check = parse_check($check) if $check; |
$check = parse_check($check) if $check; |
|
|
if (! $check) { |
if ( !$check ) { |
|
|
# It looks like doing this should be safe, from |
# It looks like doing this should be safe, from |
# src/sbin/sysctl/sysctl.c |
# src/sbin/sysctl/sysctl.c |
return $sensor->{'status'} if $sensor->{'status'}; |
return $sensor->{'status'} if $sensor->{'status'}; |
|
|
return; |
return; |
} |
} |
elsif ($check eq 'IGNORE') { |
elsif ( $check eq 'IGNORE' ) { |
return; |
return; |
} |
} |
|
|
use YAML; |
|
print Dump $check; |
|
|
|
$result = 'OK'; |
$result = 'OK'; |
foreach my $code ( 'warn', 'crit' ) { |
foreach my $code ( 'warn', 'crit' ) { |
if ( $sensor->{'type'} eq 'fan' |
if ( $sensor->{'type'} eq 'fan' |
|
|
{ |
{ |
my $data = $sensor->{'data'}; |
my $data = $sensor->{'data'}; |
$data =~ s/[^\d\.]//g; |
$data =~ s/[^\d\.]//g; |
if (! length $data ) { |
if ( !length $data ) { |
warn "INVALID DATA ($sensor->{'data'}) for '$sensor->{'id'}'"; |
warn |
|
"INVALID DATA ($sensor->{'data'}) for '$sensor->{'id'}'\n"; |
next; |
next; |
} |
} |
|
|
|
|
my $c = $check->{ $code . ".low" }; |
my $c = $check->{ $code . ".low" }; |
$c =~ s/[^\d\.]//g; |
$c =~ s/[^\d\.]//g; |
|
|
if (! length $c ) { |
if ( !length $c ) { |
warn "INVALID CHECK (" |
warn "INVALID CHECK (" |
. $check->{ $code . ".low" } |
. $check->{ $code . ".low" } |
. ") for '$sensor->{'id'}:$code.low'"; |
. ") for '$sensor->{'id'}:$code.low'\n"; |
next; |
next; |
} |
} |
|
|
|
|
if ( defined $check->{ $code . ".high" } ) { |
if ( defined $check->{ $code . ".high" } ) { |
my $c = $check->{ $code . ".high" }; |
my $c = $check->{ $code . ".high" }; |
$c =~ s/[^\d\.]//g; |
$c =~ s/[^\d\.]//g; |
if (! length $c ) { |
if ( !length $c ) { |
warn "INVALID CHECK (" |
warn "INVALID CHECK (" |
. $check->{ $code . ".high" } |
. $check->{ $code . ".high" } |
. ") for '$sensor->{'id'}:$code.high'"; |
. ") for '$sensor->{'id'}:$code.high'\n"; |
next; |
next; |
} |
} |
|
|
|
|
} |
} |
elsif ( @{ $check->{$code} } ) { |
elsif ( @{ $check->{$code} } ) { |
my $matched = 0; |
my $matched = 0; |
foreach my $c ( @{ $check->{$code} } ) { |
foreach ( @{ $check->{$code} } ) { |
|
my $c = $_; |
$c =~ s/[^\d\.]//g; |
$c =~ s/[^\d\.]//g; |
if (! length $c ) { |
if ( !length $c ) { |
warn "INVALID CHECK (" . $c |
warn "INVALID CHECK (" . $_ |
. ") for '$sensor->{'id'}:$code'"; |
. ") for '$sensor->{'id'}:$code'\n"; |
next; |
next; |
} |
} |
|
|
|
|
my $data = $degC; |
my $data = $degC; |
|
|
$data = $degF if ( $c =~ /F/i ); |
$data = $degF if ( $c =~ /F/i ); |
if (! length $data ) { |
if ( !length $data ) { |
warn "INVALID DATA (" |
warn "INVALID DATA (" |
. $sensor->{'data'} |
. $sensor->{'data'} |
. ") for '$sensor->{'id'}'"; |
. ") for '$sensor->{'id'}'\n"; |
next; |
next; |
} |
} |
|
|
$c =~ s/[^\d\.]//g; |
$c =~ s/[^\d\.]//g; |
if (! length $c ) { |
if ( !length $c ) { |
warn "INVALID CHECK (" |
warn "INVALID CHECK (" |
. $check->{ $code . ".low" } |
. $check->{ $code . ".low" } |
. ") for '$sensor->{'id'}':$code.low"; |
. ") for '$sensor->{'id'}':$code.low\n"; |
next; |
next; |
} |
} |
|
|
|
|
|
|
my $data = $degC; |
my $data = $degC; |
$data = $degF if ( $c =~ /F/i ); |
$data = $degF if ( $c =~ /F/i ); |
if (! length $data ) { |
if ( !length $data ) { |
warn "INVALID DATA (" |
warn "INVALID DATA (" |
. $sensor->{'data'} |
. $sensor->{'data'} |
. ") for '$sensor->{'id'}'"; |
. ") for '$sensor->{'id'}'\n"; |
next; |
next; |
} |
} |
|
|
$c =~ s/[^\d\.]//g; |
$c =~ s/[^\d\.]//g; |
if (! length $c ) { |
if ( !length $c ) { |
warn "INVALID CHECK (" |
warn "INVALID CHECK (" |
. $check->{ $code . ".high" } |
. $check->{ $code . ".high" } |
. ") for '$sensor->{'id'}:$code.high'"; |
. ") for '$sensor->{'id'}:$code.high'\n"; |
next; |
next; |
} |
} |
|
|
|
|
elsif ( @{ $check->{$code} } ) { |
elsif ( @{ $check->{$code} } ) { |
|
|
my $matched = 0; |
my $matched = 0; |
foreach my $c ( @{ $check->{$code} } ) { |
foreach ( @{ $check->{$code} } ) { |
|
my $c = $_; |
my $data = $degC; |
my $data = $degC; |
|
|
$data = $degF if ( $c =~ /F/i ); |
$data = $degF if ( $c =~ /F/i ); |
if (! length $data ) { |
if ( !length $data ) { |
warn "INVALID DATA (" |
warn "INVALID DATA (" |
. $sensor->{'data'} |
. $sensor->{'data'} |
. ") for '$sensor->{'id'}'"; |
. ") for '$sensor->{'id'}'\n"; |
next; |
next; |
} |
} |
|
|
$c =~ s/[^\d\.]//g; |
$c =~ s/[^\d\.]//g; |
if (! length $c ) { |
if ( !length $c ) { |
warn "INVALID CHECK (" . $c |
warn "INVALID CHECK (" . $_ |
. ") for '$sensor->{'id'}':$code"; |
. ") for '$sensor->{'id'}':$code\n"; |
next; |
next; |
} |
} |
|
|
|
|
} |
} |
$result = $errors{$code} unless $matched; |
$result = $errors{$code} unless $matched; |
} |
} |
|
|
} |
} |
elsif ($sensor->{'type'} eq 'drive' |
elsif ($sensor->{'type'} eq 'drive' |
|| $sensor->{'type'} eq 'indicator' ) |
|| $sensor->{'type'} eq 'indicator' ) |
|
|
|
|
} |
} |
else { |
else { |
print STDERR 'Unknown Sensor Type: ', |
warn 'Unknown Sensor Type: ', |
$sensor->{'id'}, |
$sensor->{'id'}, |
'=', |
'=', |
$sensor->{'type'}, |
$sensor->{'type'}, |
|
|
FILE to load checks from (defaults to /etc/sensorsd.conf) |
FILE to load checks from (defaults to /etc/sensorsd.conf) |
-s, --sensor=ID |
-s, --sensor=ID |
ID of a single sensor. "-s kate0.temp0" means hw.sensors.kate0.temp0 |
ID of a single sensor. "-s kate0.temp0" means hw.sensors.kate0.temp0 |
|
Overrides --filename. |
-w, --warning=RANGE or single ENTRY |
-w, --warning=RANGE or single ENTRY |
Exit with WARNING status if outside of RANGE or if != ENTRY |
Exit with WARNING status if outside of RANGE or if != ENTRY |
-c, --critical=RANGE or single ENTRY |
-c, --critical=RANGE or single ENTRY |
|
|
print $License; |
print $License; |
} |
} |
|
|
|
|
sub print_revision { |
sub print_revision { |
my ($prog, $rev) = @_; |
my ( $prog, $rev ) = @_; |
$rev =~ s/^\D+([\d\.]+)\D+$/v$1/xms; |
$rev =~ s/^\D+([\d\.]+)\D+$/v$1/xms; |
|
|
print "$prog $rev\n"; |
print "$prog $rev\n"; |
} |
} |
|
|
|
|
|
|