Plugin:check centreon ping


 * 1) ! /usr/bin/perl -w
 * 2) Copyright 2004-2011 MERETHIS
 * 3) Centreon is developped by : Julien Mathis and Romain Le Merlus under
 * 4) GPL Licence 2.0.
 * 5) This program is free software; you can redistribute it and/or modify it under
 * 6) the terms of the GNU General Public License as published by the Free Software
 * 7) Foundation ; either version 2 of the License.
 * 8) This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * 9) WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * 10) PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 11) You should have received a copy of the GNU General Public License along with
 * 12) this program; if not, see .
 * 13) Linking this program statically or dynamically with other modules is making a
 * 14) combined work based on this program. Thus, the terms and conditions of the GNU
 * 15) General Public License cover the whole combination.
 * 16) As a special exception, the copyright holders of this program give MERETHIS
 * 17) permission to link this program with independent modules to produce an executable,
 * 18) regardless of the license terms of these independent modules, and to copy and
 * 19) distribute the resulting executable under terms of MERETHIS choice, provided that
 * 20) MERETHIS also meet, for each linked independent module, the terms  and conditions
 * 21) of the license of that module. An independent module is a module which is not
 * 22) derived from this program. If you modify this program, you may extend this
 * 23) exception to your version of the program, but you are not obliged to do so. If you
 * 24) do not wish to do so, delete this exception statement from your version.
 * 25) For more information : contact@centreon.com
 * 26) SVN : $URL: http://svn.centreon.com/trunk/plugins-2.x/src/check_centreon_ping $
 * 27) SVN : $Id: check_centreon_ping 12606 2011-10-14 16:11:39Z shotamchay $
 * 28) Plugin init
 * 1) exception to your version of the program, but you are not obliged to do so. If you
 * 2) do not wish to do so, delete this exception statement from your version.
 * 3) For more information : contact@centreon.com
 * 4) SVN : $URL: http://svn.centreon.com/trunk/plugins-2.x/src/check_centreon_ping $
 * 5) SVN : $Id: check_centreon_ping 12606 2011-10-14 16:11:39Z shotamchay $
 * 6) Plugin init
 * 1) SVN : $Id: check_centreon_ping 12606 2011-10-14 16:11:39Z shotamchay $
 * 2) Plugin init
 * 1) Plugin init
 * 1) Plugin init
 * 1) Plugin init

use strict; use FindBin; use lib "$FindBin::Bin"; use lib "/usr/lib/nagios/plugins"; use utils qw($TIMEOUT %ERRORS &print_revision &support);

if (eval "require centreon" ) { use centreon qw(get_parameters); use vars qw($VERSION %centreon); %centreon = get_parameters; } else { print "Unable to load centreon perl module\n"; exit $ERRORS{'UNKNOWN'}; }

use vars qw($PROGNAME); use Getopt::Long; use vars qw($opt_V $opt_h $opt_H $opt_D $opt_w $opt_c $opt_n $opt_f $opt_i $rta_critical $rta_warning $pl_critical $pl_warning $opt_s);


 * 1) Plugin var init
 * 1) Plugin var init

my $ping = `whereis -b ping`; $ping =~ /^.*:\s(.*)$/; $ping = $1;

$PROGNAME = "$0"; sub print_help ; sub print_usage ;

Getopt::Long::Configure('bundling'); GetOptions ("h" => \$opt_h,		"help" => \$opt_h,    "V" => \$opt_V,		"version" => \$opt_V,     "w=s" => \$opt_w,		"warning=s" => \$opt_w,     "c=s" => \$opt_c,		"critical=s" => \$opt_c,     "n=s" => \$opt_n,		"number=s" => \$opt_n,     "H=s" => \$opt_H,		"hostname=s" => \$opt_H,      "i=s" => \$opt_i);

if ($opt_V) { print_revision($PROGNAME,'$Revision: 1.2 $'); exit $ERRORS{'OK'}; }

if ($opt_h) { print_help; exit $ERRORS{'OK'}; }

$opt_H = shift unless ($opt_H); (print_usage && exit $ERRORS{'OK'}) unless ($opt_H);

($opt_c) || ($opt_c = shift) || ($opt_c = "500,40%"); if ($opt_c =~ /([0-9]+),([0-9]+)%/) { $rta_critical = $1; $pl_critical = $2; }

($opt_w) || ($opt_w = shift) || ($opt_w = "200,20%"); if ($opt_w =~ /([0-9]+),([0-9]+)%/) { $rta_warning = $1; $pl_warning = $2; } if (!$rta_warning || !$rta_critical || !$pl_warning || !$pl_critical) { print "bad initialisation of Treshholds\n"; exit $ERRORS{'OK'}; }

if ( ($rta_critical <= $rta_warning) || ($pl_critical <= $pl_warning) ) { print "critical must be superior to warning\n"; exit $ERRORS{'OK'}; }

($opt_n) || ($opt_n = shift) || ($opt_n = 1); my $NbPing; if ($opt_n =~ /([0-9]+)/){ $NbPing = $1; } else{ print "Unknown ping number\n"; exit $ERRORS{'UNKNOWN'}; }

my $start=time;


 * 1) Plugin requests
 * 1) Plugin requests

$opt_i = 1 if (!defined($opt_i) || !$opt_i);

$_ = `$ping -n -c $NbPing -i $opt_i $opt_H 2>/dev/null`; my $return = $? / 256;


 * 1) Get Data From Ping Result
 * 1) Get Data From Ping Result

if (!$_) { print "no value returned by ping\n"; exit $ERRORS{'UNKNOWN'}; }

my $ping_result = $_; my @ping_result_array = split(/\n/,$ping_result); my @ping_subresult1_array; my @ping_subresult2_array; my $rta = 0; my $pl; my $time_answer;

if( ( $return != 0 ) || $ping_result_array[@ping_result_array -2 ] =~ /100% packet loss/) { $rta = -1; $time_answer = 0; } else { @ping_subresult1_array = split(/=/,$ping_result_array[@ping_result_array -1 ]); @ping_subresult2_array = split(/,/,$ping_result_array[@ping_result_array -2 ]); @ping_subresult1_array = split(/\//,$ping_subresult1_array[1]); @ping_subresult2_array = split(/ /,$ping_subresult2_array[2]); $rta = $ping_subresult1_array[1]; $pl = $ping_subresult2_array[1]; $time_answer = $ping_subresult1_array[1]; $pl =~ /([0-9]+)\%/; $pl = $1; }


 * 1) Plugin return code
 * 1) Plugin return code

my $result_str = "";

if( $rta == -1 ) { $ping_result_array[@ping_result_array - 2] =~ s/\%/percent/g; if (length($ping_result_array[@ping_result_array - 5]) && $ping_result_array[@ping_result_array - 5] !~ m/pipe/g) { print "PING CRITICAL - ".$ping_result_array[@ping_result_array - 5]."|time=0 ok=0\n"; } elsif (length($ping_result_array[@ping_result_array - 1]) != 0 && $ping_result_array[@ping_result_array - 1] !~ m/pipe/g) { print "PING CRITICAL - ".$ping_result_array[@ping_result_array - 1]."|time=0 ok=0\n"; } elsif (length($ping_result_array[@ping_result_array - 4]) && $ping_result_array[@ping_result_array - 4] !~ m/pipe/g) { print "PING CRITICAL - ".$ping_result_array[@ping_result_array - 4]."|time=0 ok=0\n"; } elsif (length($ping_result_array[@ping_result_array - 1]) !~ m/pipe/g) { print "PING CRITICAL - ".$ping_result_array[@ping_result_array - 1]."|time=0 ok=0\n"; } else { print "PING CRITICAL - ".$ping_result_array[@ping_result_array - 3]."|time=0 ok=0\n"; }   exit $ERRORS{'CRITICAL'}; } elsif ( ($pl >= $pl_critical) || ($rta >= $rta_critical) ) { $ping_result_array[@ping_result_array -1 ] =~ s/\%/percent/g; my @tab = split(/,/,$ping_result_array[@ping_result_array -1 ]); print "PING CRITICAL - ". $tab[0] ."|time=".$time_answer."ms;$rta_warning;$rta_critical;0; ok=1\n"; exit $ERRORS{'CRITICAL'}; } elsif ( ($pl >= $pl_warning) || ($rta >= $rta_warning) ) { $ping_result_array[@ping_result_array -1 ] =~ s/\%/percent/g; my @tab = split(/,/,$ping_result_array[@ping_result_array -1 ]); print "PING WARNING - ".$tab[0]."|time=".$time_answer."ms;$rta_warning;$rta_critical;0; ok=1\n"; exit $ERRORS{'WARNING'}; } else { $ping_result_array[@ping_result_array -1 ] =~ s/\%/percent/g; my @tab = split(/,/,$ping_result_array[@ping_result_array -1 ]); print "PING OK - ".$tab[0]."|time=".$time_answer."ms;$rta_warning;$rta_critical;0; ok=1\n"; exit $ERRORS{'OK'}; }

sub print_usage { print "Usage:\n"; print "$PROGNAME\n"; print "  -H (--hostname)   Hostname to query (Required)\n"; print "  -w (--warning)    Threshold pair (Default: 200,20%)\n"; print "  -c (--critical)   Threshold pair (Default: 500,40%)\n"; print "  -n (--number)     Number of ICMP ECHO packets to send (Default: 1)\n"; print "  -i 			    Interval between ping (Default: 1s)\n"; print "  -V (--version)    Plugin version\n"; print "  -h (--help)       Usage help\n"; }

sub print_help { print "##############################################\n"; print "#   Copyright (c) 2004-2011 Centreon        #\n"; print "#   Bugs to http://forge.centreon.com/      #\n"; print "##############################################\n"; print_usage; print "\n"; }