[Nagiosplug-devel] Help wanted: take my hack and make it a real plugin ;-}
Richard Colley
rcolley at cardaccess.com.au
Mon Jan 13 23:40:04 CET 2003
I've got a simple perl script for checking CPU temp and any other variable
output by the lm_sensors package. But it doesn't really conform to the
Nagios plugin guidelines. I'd be really happy if anyone was interested in
making it conform. Unfortunately, I don't have the time to do this. Feel
free to use it anyway.
The general usage is:
check_sensors <chip> <field1> <disp1> <warn1> <crit1> ....
'field1' is the name of the field output by 'sensors'.
'disp1' is what to print back to nagios as the name of this field.
'warn1' is an expression that defines when a warning will be generated for
this field
'crit1' is an expression that defines when a critical will be generated for
this field
The 'warn1' and 'crit1' values can be numbers e.g. '35.5' or expressions
e.g. '<20'. No attempt is made to ensure the warning and critical
expressions are logically consistent.
Output is a single line with the values of each of the sensor checks
requested, and an indication if they are above the critical or warning
thresholds.
An example:
check_sensors via686a-isa-0c00 'CPU Temp' 'CPU Temp' 38 42 'SYS
Temp' 'SYS Temp' 30 35 'CPU core' 'CPU core' '<=1.80' '<1.79' 'CPU Fan' 'CPU
Fan' '<3500' '<3000'
... Generates a warning if CPU temp >=38, and critical if >=42
... Generates a warning if SYS temp >=30, and critical if >=35
... Generates a warning if CPU core V <=1.80, and critical if <1.79
... Generates a warning if CPU fan RPM <3500, and critical if <3000
The output generated for the above would be something like:
CPU core=1.67(C<1.79):CPU Fan=5625:SYS Temp=30.1:CPU Temp=34.3:
An obvious extension is to use nagios performance data logging output
format. But alas, no time ;-}
Feel free to email with questions. (Please - no flames on my poor Perl
coding - I know it's bad :-)
Richard
---------------- check_sensors
#!/usr/bin/env perl
#
# check sensors according to lm_sensors output
#
# (c)2003 Richard Colley <Richard.Colley at rcolley.com>
# License: GPL v2
#
#### Some paths
$SENSOR_PROG="/usr/local/bin/sensors";
###################
$progname=$0;
$RC_OK=0;
$RC_WARN=1;
$RC_CRITICAL=2;
sub usage {
my ($msg) = shift;
print "$msg\n";
print "Usage: $progname <chip> <field1> <disp1> <warn threshold1>
<critical threshold1> ...\n";
print "\twhere:\t<chip> corresponds to an entry in
/etc/sensors.conf\n";
print "\t\t<field1> is the name of a field output by 'sensors'\n";
print "\t\t<disp1> is a string to use while displaying this
field\n";
print "\t\t<warn threshold1> is the warning threshold value of the
field output by 'sensors'\n";
print "\t\t<critical threshold1> is the critical threshold value of
the field output by 'sensors'\n";
print "\t\tmultiple triples are permitted\n";
print "\n\t\tNB: a threshold value can be optionally prefixed by an
operator character.\n";
print "\t\tValid operators are currently '>', '<', '==', '>=', '<='.
The default is '>='.\n";
print "\t\t\te.g. if the warn threshold is <30, then a warning is
raised if the value is less than 30.\n";
print "\t\tNo attempt is made to ensure the resultant logic is
sensible\n";
exit $RC_CRITICAL;
}
if( $#ARGV < 4 ) {
usage "Not enough args";
}
$chip=$ARGV[0];
shift @ARGV;
%fields={};
while( $#ARGV >= 3 ) {
$i=0;
$field=$ARGV[$i];
$i++;
$fieldDisp=$ARGV[$i];
$i++;
$ARGV[$i] =~ /^([=<>]*)([0-9.]+)/ or usage "Invalid warning
threshold format: $ARGV[$i]";
$warn = $2;
$warnop = $1 ne "" ? $1 : ">=";
$i++;
$ARGV[$i] =~ /^([=<>]*)([0-9.]+)/ or usage "Invalid critical
threshold format: $ARGV[$i]";
$critical = $2;
$criticalop = $1 ne "" ? $1 : ">=";
$i++;
$config{ $field } = [ $fieldDisp, $warn, $warnop, $critical,
$criticalop ];
shift @ARGV;
shift @ARGV;
shift @ARGV;
shift @ARGV;
}
sub eval_simple {
my( $value, $op, $threshold ) = @_;
eval "$value $op $threshold";
}
$output="";
$retcode = $RC_OK;
open( SENSOR, "$SENSOR_PROG $chip|" ) or usage "Failed to run
$SENSOR_PROG\n";
while( <SENSOR> ) {
if( /^([^:]+)[^0-9]*([0-9.]+)/ ) {
$field = $1;
$value = $2;
if( defined( $config{ $field } ) ) {
$fieldDisp = ${$config{ $field }}[0];
$warn = ${$config{ $field }}[1];
$warnop = ${$config{ $field }}[2];
$critical = ${$config{ $field }}[3];
$criticalop = ${$config{ $field }}[4];
$output .= "$fieldDisp=$value";
if( eval_simple( $value, $criticalop, $critical ) )
{
$output .= "(C$criticalop$critical)";
$retcode = $RC_CRITICAL;
} elsif( $retcode == $RC_OK and eval_simple( $value,
$warnop, $warn ) ) {
$output .= "(W$warnop$warn)";
$retcode = $RC_WARN;
}
$output .= ";";
}
}
}
close( SENSOR );
print "$output\n";
exit $retcode;
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.443 / Virus Database: 248 - Release Date: 10/01/2003
More information about the Devel
mailing list