Skip to content

Instantly share code, notes, and snippets.

@jazzl0ver
Last active September 15, 2017 16:28
Show Gist options
  • Save jazzl0ver/0bf8c20971bfb8e0b3a4a7b16a344a6c to your computer and use it in GitHub Desktop.
Save jazzl0ver/0bf8c20971bfb8e0b3a4a7b16a344a6c to your computer and use it in GitHub Desktop.
check_centreon_ping
#! /usr/bin/perl -w
################################################################################
# Copyright 2004-2011 MERETHIS
# Centreon is developped by : Julien Mathis and Romain Le Merlus under
# GPL Licence 2.0.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation ; either version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, see <http://www.gnu.org/licenses>.
#
# Linking this program statically or dynamically with other modules is making a
# combined work based on this program. Thus, the terms and conditions of the GNU
# General Public License cover the whole combination.
#
# As a special exception, the copyright holders of this program give MERETHIS
# permission to link this program with independent modules to produce an executable,
# regardless of the license terms of these independent modules, and to copy and
# distribute the resulting executable under terms of MERETHIS choice, provided that
# MERETHIS also meet, for each linked independent module, the terms and conditions
# of the license of that module. An independent module is a module which is not
# derived from this program. If you modify this program, you may extend this
# exception to your version of the program, but you are not obliged to do so. If you
# do not wish to do so, delete this exception statement from your version.
#
# For more information : contact@centreon.com
#
# SVN : $URL: http://svn.centreon.com/trunk/plugins-2.x/src/check_centreon_ping $
# SVN : $Id: check_centreon_ping 12606 2011-10-14 16:11:39Z shotamchay $
#
####################################################################################
#
# 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 $opt_b);
#
# 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,
"b=s" => \$opt_b, "bindaddress=s" => \$opt_b,
"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'};
}
$opt_b = "-I $opt_b" if ($opt_b);
my $start=time;
#
# Plugin requests
#
$opt_i = 1 if (!defined($opt_i) || !$opt_i);
$_ = `$ping -n -c $NbPing -i $opt_i $opt_b $opt_H 2>/dev/null`;
my $return = $? / 256;
#
# 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;
}
#
# 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 " -b (--bindaddress) Bind address\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";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment