adapt a script
Posted: Thu Oct 25, 2012 8:41 am
Hello,
I tested a script it seems to work, but a view that is not well analyzed
When I run the command:
# ./check_string -H <my_ipadress>/tme.xml -s "Thermometer" -w 15 -c 25
CRITICAL Thermometer 220150250 | Thermometer=220150250;15;25
He always displays CRITICAL.
It does not parse this value: 220150250 he considers as a single value
While it should be
temperature 220
mintemperature 150
maxtemperature 250
here is my file tme.xml
<thermometer>
<title>Ethernet thermometer TME designed by Papouch s.r.o. - http://www.papouch.com</title>
<description>Thermometer</description>
<temperature>220</temperature>
<mintemperature>150</mintemperature>
<maxtemperature>250</maxtemperature>
</thermometer>
What I want if you can help me:
Display:
1.
# ./check_string -H <my_ipadress>/tme.xml -s "Thermometer" -w 15 -c 25
OK Thermometer 22 | Thermometer=22;15;25
2.
# ./check_string -H <my_ipadress>/tme.xml -s "Thermometer" -w 15 -c 25
CRITICAL Thermometer 26 | Thermometer=26;15;25
3.
# ./check_string -H <my_ipadress>/tme.xml -s "Thermometer" -w 15 -c 25
WARNING Thermometer 14 | Thermometer=14;15;25
http://exchange.nagios.org/directory/Pl ... gs/details
http://www.papouch.com/en/shop/product/ ... ermometer/
Thank you
I tested a script it seems to work, but a view that is not well analyzed
Code: Select all
#!/usr/bin/perl -w
# nagios: -epn
use strict;
use warnings;
use Switch;
use lib "/usr/lib/nagios/plugins";
use utils qw(%ERRORS $TIMEOUT);
use Getopt::Long;
use vars qw/$opt_host $opt_string $opt_sep $opt_verbose $opt_w $opt_c $opt_help/;
#### Main
get_options();
$TIMEOUT=60;
my $lynx = '/usr/bin/lynx';
my @input;
my $command="$lynx -dump http://$opt_host | grep \"$opt_string\"";
open(DATA,"$command|") || die "CRITICAL Could not execute $command";
while(<DATA>) {
chomp;
push(@input,$_);
}
close(DATA);
my $some_data_found=0;
my $value;
my $res='CRITICAL';
my $nresult;
foreach my $line (@input) {
if ( $line =~ "$opt_string" ) {
$some_data_found=1;
$value = $line;
$value =~ s/$opt_string//;
$value =~ s/$opt_sep*//;
# Extract number from $value
$value =~ s/([0-9]*).*/$1/;
}
}
if ( $value eq "" ) { print "CRITICAL Search string has no value !\n"; exit $ERRORS{'CRITICAL'}; }
if ( defined($opt_w) ) { if ($value < $opt_w) {$res='OK';} else {$res='WARNING';} }
if ( defined($opt_c) && ($res =~ 'WARNING'||!defined($opt_w)) ) { if ($value > $opt_c) {$res='CRITICAL';} }
$nresult = "$res ";
$nresult .= "$opt_string $value";
$nresult .= " | ";
$nresult .= "$opt_string=$value";
$nresult .= ";$opt_w" if ( defined($opt_w) );
$nresult .= ";$opt_c" if ( defined($opt_c) );
print ("$nresult\n");
if ( !$some_data_found || $opt_verbose ) {
print " CRITICAL No corresponding data has been found\nDebugging data: \n" if(!defined($opt_verbose));
print "$command\n";
foreach my $line (@input) {print "$line\n";}
exit $ERRORS{'CRITICAL'};
}
exit $ERRORS{"$res"};
#### END MAIN
sub get_options {
Getopt::Long::Configure( 'bundling' );
GetOptions(
'H:s' => \$opt_host, 'host' => \$opt_host,
's:s' => \$opt_string, 'string' => \$opt_string,
'k:s' => \$opt_sep, 'separator' => \$opt_sep,
'v' => \$opt_verbose, 'verbose' => \$opt_verbose,
'w:i' => \$opt_w, 'warning' => \$opt_w,
'c:i' => \$opt_c, 'critical' => \$opt_c,
'h' => \$opt_help, 'help' => \$opt_help,
);
if ( defined($opt_help) ) { &print_help; exit $ERRORS{'CRITICAL'}; }
if ( !defined($opt_string) ) { &print_help; exit $ERRORS{'CRITICAL'}; }
if ( !defined($opt_sep) ) { $opt_sep=" "; }
}
sub print_help {
print <<EOT;
Usage: check_pmta_connections -i/-o -w -c -x -d
-H --hostn=<ip's of the server>
This must be defined
-s, --string
The string that must precede the monitored value
This MUST be defined
-k, --separator
If not defined is space
-v, --verbose
print extra debugging information
-w, --warning = <warning threshold>
-c, --critical = <critical threshold>
-h, --help This help
EOT
}
# ./check_string -H <my_ipadress>/tme.xml -s "Thermometer" -w 15 -c 25
CRITICAL Thermometer 220150250 | Thermometer=220150250;15;25
He always displays CRITICAL.
It does not parse this value: 220150250 he considers as a single value
While it should be
temperature 220
mintemperature 150
maxtemperature 250
here is my file tme.xml
<thermometer>
<title>Ethernet thermometer TME designed by Papouch s.r.o. - http://www.papouch.com</title>
<description>Thermometer</description>
<temperature>220</temperature>
<mintemperature>150</mintemperature>
<maxtemperature>250</maxtemperature>
</thermometer>
What I want if you can help me:
Display:
1.
# ./check_string -H <my_ipadress>/tme.xml -s "Thermometer" -w 15 -c 25
OK Thermometer 22 | Thermometer=22;15;25
2.
# ./check_string -H <my_ipadress>/tme.xml -s "Thermometer" -w 15 -c 25
CRITICAL Thermometer 26 | Thermometer=26;15;25
3.
# ./check_string -H <my_ipadress>/tme.xml -s "Thermometer" -w 15 -c 25
WARNING Thermometer 14 | Thermometer=14;15;25
http://exchange.nagios.org/directory/Pl ... gs/details
http://www.papouch.com/en/shop/product/ ... ermometer/
Thank you