adapt a script

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
inserm
Posts: 24
Joined: Tue Jul 31, 2012 6:05 am

adapt a script

Post by inserm »

Hello,

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
}	

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
agriffin
Posts: 876
Joined: Mon May 09, 2011 9:36 am

Re: adapt a script

Post by agriffin »

This doesn't look like a bug in the script to me. You are trying to treat XML like HTML, which isn't going to work. You don't need an adaptation of this script: you need either a new script entirely or you need your temperature data in a format it can read (some specifically formatted HTML).
inserm
Posts: 24
Joined: Tue Jul 31, 2012 6:05 am

Re: adapt a script

Post by inserm »

agriffin wrote:This doesn't look like a bug in the script to me. You are trying to treat XML like HTML, which isn't going to work. You don't need an adaptation of this script: you need either a new script entirely or you need your temperature data in a format it can read (some specifically formatted HTML).
thank you
I found by adding two lines :

Code: Select all

$ value = substr ($ value, 0.3);
$ value = ~ s / ^ (\ d +) (\ d) $ / $ 1. $ 2 /;
print "$ value \ n";
instead
$value =~ s/([0-9]*).*/$1/;
slansing
Posts: 7698
Joined: Mon Apr 23, 2012 4:28 pm
Location: Travelling through time and space...

Re: adapt a script

Post by slansing »

Ah good idea, so you have solved your issue?
inserm
Posts: 24
Joined: Tue Jul 31, 2012 6:05 am

Re: adapt a script

Post by inserm »

slansing wrote:Ah good idea, so you have solved your issue?
Yes, Thanks
Locked