version 1.3, 2006/05/02 02:39:23 |
version 1.7, 2006/05/02 21:03:53 |
|
|
#!/usr/bin/perl |
#!/usr/bin/perl |
# $RedRiver: check_hw_sensors,v 1.2 2006/05/02 01:29:33 andrew Exp $ |
# $RedRiver: check_hw_sensors,v 1.6 2006/05/02 19:59:47 andrew Exp $ |
######################################################################## |
######################################################################## |
# check_hw_sensors *** A nagios check for OpenBSD hw.sensors |
# check_hw_sensors *** A nagios check for OpenBSD hw.sensors |
# |
# |
# 2006.05.01 #*#*# andrew fresh <andrew@mad-techies.org> |
# 2006.05.01 #*#*# andrew fresh <andrew@mad-techies.org> |
######################################################################## |
######################################################################## |
|
# TODO: |
|
# Really need to fix the documentation issue. |
|
# |
|
# I want the ability to just check the "status" entry that is in some output. For example the OK here: |
|
# hw.sensors.1=esm0, CPU 1, OK, temp, 31.00 degC / 87.80 degF |
|
######################################################################## |
use strict; |
use strict; |
use warnings; |
use warnings; |
|
|
#use Data::Dumper; |
#use Data::Dumper; |
|
|
|
use constant NAGIOS_OUTPUT => 1; |
|
|
use POSIX; |
use POSIX; |
#use lib "/usr/local/libexec/nagios"; |
use lib "/usr/local/libexec/nagios"; |
use lib $ENV{'HOME'}; |
#use lib $ENV{'HOME'}; |
use utils qw($TIMEOUT %ERRORS &print_revision &support); |
use utils qw($TIMEOUT %ERRORS &print_revision &support); |
|
|
use Getopt::Long; |
use Getopt::Long; |
|
|
|
|
my $PROGNAME = "check_hw_sensors"; |
my $PROGNAME = "check_hw_sensors"; |
|
|
my $SYSCTL = 'sysctl'; |
my $SYSCTL = '/sbin/sysctl'; |
my $GETCAP = 'getcap'; |
my $GETCAP = '/usr/bin/getcap'; |
my $BASE = 'hw.sensors'; |
my $BASE = 'hw.sensors'; |
|
my $DEFAULT_CONFIG = '/etc/sensorsd.conf'; |
|
|
my $CHECK_SENSOR = $BASE; |
|
my %CHECKS; |
|
my %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 $filename; |
|
|
my $opt_h ; |
my $opt_h ; |
my $opt_V ; |
my $opt_V ; |
|
|
|
my $CHECK_SENSOR = $BASE; |
|
my %CHECKS; |
|
my %SENSORS; |
|
|
#Option checking |
#Option checking |
my $status = GetOptions( |
my $status = GetOptions( |
|
|
); |
); |
|
|
# set the default this way so it only happens if someone typed -f or --filename |
# set the default this way so it only happens if someone typed -f or --filename |
$filename = '/etc/sensorsd.conf' if (defined $filename && $filename eq ''); |
$filename = $DEFAULT_CONFIG if (defined $filename && $filename eq ''); |
|
|
if ($status == 0) { |
if ($status == 0) { |
print_help() ; |
print_help() ; |
|
|
$CHECK_SENSOR = $sensor; |
$CHECK_SENSOR = $sensor; |
|
|
$CHECKS{$sensor} = { |
$CHECKS{$sensor} = { |
'warning' => $warning, |
'warn' => $warning, |
'critical' => $critical, |
'crit' => $critical, |
}; |
}; |
} elsif (defined $filename) { |
} elsif (defined $filename) { |
%CHECKS = parse_file($filename); |
%CHECKS = parse_file($filename); |
|
|
#print Dumper \%states; |
#print Dumper \%states; |
|
|
$state = 'OK'; |
$state = 'OK'; |
|
my $have_results = 0; |
foreach my $error (sort { $ERRORS{$a} <=> $ERRORS{$b} } keys %ERRORS) { |
foreach my $error (sort { $ERRORS{$a} <=> $ERRORS{$b} } keys %ERRORS) { |
if (exists $states{$error}) { |
if (exists $states{$error}) { |
|
$have_results++; |
$state = $error; |
$state = $error; |
print "$error (" . scalar(@{ $states{ $error } }) . "):\n"; |
} |
foreach (@{ $states{ $error } }) { |
} |
print " $_\n"; |
if (NAGIOS_OUTPUT && $have_results) { |
|
print '<ul>'; |
|
} |
|
foreach my $error (sort { $ERRORS{$b} <=> $ERRORS{$a} } keys %ERRORS) { |
|
if (exists $states{$error}) { |
|
if (NAGIOS_OUTPUT) { |
|
print "<li>$error (" . scalar(@{ $states{ $error } }) . ")"; |
|
unless ($error eq 'OK') { |
|
print '<ul>'; |
|
foreach (@{ $states{ $error } }) { |
|
print "<li>$_</li>"; |
|
} |
|
print '</ul>'; |
|
} |
|
print "</li>" |
|
} else { |
|
print "$error (" . scalar(@{ $states{ $error } }) . "):\n"; |
|
foreach (@{ $states{ $error } }) { |
|
print " $_\n"; |
|
} |
} |
} |
} |
} |
} |
} |
|
if ($have_results == 0) { |
|
print "No results found\n"; |
|
} |
|
if (NAGIOS_OUTPUT && $have_results) { |
|
print '</ul>' . "\n"; |
|
} |
exit $ERRORS{$state}; |
exit $ERRORS{$state}; |
|
|
|
|
|
|
my $type = shift; |
my $type = shift; |
my $check = shift; |
my $check = shift; |
|
|
$check->{'warn'} = $check->{'warning'} if (!defined $check->{'warn'}); |
|
$check->{'crit'} = $check->{'critical'} if (!defined $check->{'crit'}); |
|
|
|
if (defined $check->{'warn'} && $check->{'warn'} =~ /:/) { |
if (defined $check->{'warn'} && $check->{'warn'} =~ /:/) { |
if (my ($low, $high) = split /:/, $check->{'warn'}) { |
if (my ($low, $high) = split /:/, $check->{'warn'}) { |
$check->{'warn.low'} = $low; |
$check->{'warn.low'} = $low; |
|
|
foreach my $c (@{ $check->{$code} }) { |
foreach my $c (@{ $check->{$code} }) { |
$c =~ s/[^\d\.]//g; |
$c =~ s/[^\d\.]//g; |
unless (length $c) { |
unless (length $c) { |
warn "INVALID CHECK (" . $check->{$code} . |
warn "INVALID CHECK (" . $c . |
") for '$sensor->{'id'}:$code'"; |
") for '$sensor->{'id'}:$code'"; |
next; |
next; |
} |
} |
|
|
|
|
$c =~ s/[^\d\.]//g; |
$c =~ s/[^\d\.]//g; |
unless (length $c) { |
unless (length $c) { |
warn "INVALID CHECK (" . $check->{$code} . |
warn "INVALID CHECK (" . $c . |
") for '$sensor->{'id'}':$code"; |
") for '$sensor->{'id'}':$code"; |
next; |
next; |
} |
} |