Plugin:check centreon packetErrors


 * 1) ! /usr/bin/perl -w
 * 2) Copyright 2004-2013 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$
 * 27) SVN : $Id$
 * 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$
 * 5) SVN : $Id$
 * 6) Plugin init
 * 1) SVN : $Id$
 * 2) Plugin init
 * 1) Plugin init
 * 1) Plugin init
 * 1) Plugin init

use strict; require "/usr/lib/nagios/plugins/Centreon/SNMP/Utils.pm";

use vars qw($PROGNAME); use Getopt::Long; use vars qw($opt_V $opt_h $opt_w $opt_c);

my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3); my $centplugins_path = "/var/lib/centreon/centplugins";

$PROGNAME = "$0";
 * 1) Plugin var init
 * 1) Plugin var init

my ($row, @flg_created, @last_check_time, @last_in_errors, @last_out_errors, $result_in, $result_out, @nb_out_errors, @nb_in_errors, $update_time, $db_file);

sub print_help ; sub print_usage ;

my %OPTION = (   "host" => undef,    "snmp-community" => "public", "snmp-version" => 1, "snmp-port" => 161,     "snmp-auth-key" => undef, "snmp-auth-user" => undef, "snmp-auth-password" => undef, "snmp-auth-protocol" => "MD5",    "snmp-priv-key" => undef, "snmp-priv-password" => undef, "snmp-priv-protocol" => "DES",    "maxrepetitions" => undef, "snmptimeout" => undef,    "64-bits" => undef, );

Getopt::Long::Configure('bundling'); GetOptions (   "H|hostname|host=s"         => \$OPTION{'host'},    "C|community=s"             => \$OPTION{'snmp-community'},    "v|snmp|snmp-version=s"     => \$OPTION{'snmp-version'},    "P|snmpport|snmp-port=i"    => \$OPTION{'snmp-port'},    "u|username=s"              => \$OPTION{'snmp-auth-user'},    "p|authpassword|password=s" => \$OPTION{'snmp-auth-password'},    "k|authkey=s"               => \$OPTION{'snmp-auth-key'},    "authprotocol=s"            => \$OPTION{'snmp-auth-protocol'},    "privpassword=s"            => \$OPTION{'snmp-priv-password'},    "privkey=s"                 => \$OPTION{'snmp-priv-key'},    "privprotocol=s"            => \$OPTION{'snmp-priv-protocol'},    "maxrepetitions=s"          => \$OPTION{'maxrepetitions'},    "snmp-timeout=i"            => \$OPTION{'snmptimeout'},    "64-bits"                   => \$OPTION{'64-bits'},

"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);

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

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


 * 1)      Verify Options
 * 1)      Verify Options

my ($session_params) = Centreon::SNMP::Utils::check_snmp_options($ERRORS{'UNKNOWN'}, \%OPTION);

($opt_c) || ($opt_c = shift) || ($opt_c = 100); my $critical = $1 if ($opt_c =~ /([0-9]+)/);

($opt_w) || ($opt_w = shift) || ($opt_w = 80); my $warning = $1 if ($opt_w =~ /([0-9]+)/);

if ($critical <= $warning){ print "(--crit) must be superior to (--warn)"; print_usage; exit $ERRORS{'OK'}; }

my $start = time;


 * 1)            Plugin snmp requests
 * 1)            Plugin snmp requests

my $OID_IN_ERRORS = ".1.3.6.1.2.1.2.2.1.14"; my $OID_OUT_ERRORS = ".1.3.6.1.2.1.2.2.1.20"; my $OID_IF_DESC = ".1.3.6.1.2.1.2.2.1.2";

my $session = Centreon::SNMP::Utils::connection($ERRORS{'UNKNOWN'}, $session_params);
 * 1) create a SNMP session

$result_in = Centreon::SNMP::Utils::get_snmp_table($OID_IN_ERRORS, $session, $ERRORS{'UNKNOWN'}, \%OPTION);
 * 1)  Get IN ERRORS

foreach my $err (oid_lex_sort(keys %$result_in)) { $nb_in_errors[scalar(@nb_in_errors)] = $result_in->{$err}; }
 * 1) loop for each interface
 * 1) #######  Get OUT ERRORS

$result_out = Centreon::SNMP::Utils::get_snmp_table($OID_OUT_ERRORS, $session, $ERRORS{'UNKNOWN'}, \%OPTION); foreach my $err (oid_lex_sort(keys %$result_out)) { $nb_out_errors[scalar(@nb_out_errors)] = $result_out->{$err}; }

my @desc_tab; my $if_description = Centreon::SNMP::Utils::get_snmp_table($OID_IF_DESC, $session, $ERRORS{'UNKNOWN'}, \%OPTION); foreach my $desc (oid_lex_sort(keys %$if_description)) { $desc_tab[scalar(@desc_tab)] = $if_description->{$desc} if ($if_description->{$desc} !~ m/StackSub/i); }
 * 1) #######  Get Interface descriptions for output


 * 1) #####      read and write in buffer file
 * 1) #####      read and write in buffer file

for (my $i = 0; $i < scalar(@nb_in_errors); $i++) { if (-e $centplugins_path . "/packet_errors_if".$i."_" . $OPTION{'host'} . ".tmp") { open(FILE,"<" . $centplugins_path . "/packet_errors_if".$i."_" . $OPTION{'host'} . ".tmp"); while($row = ){ my @last_values = split(":",$row); $last_check_time[$i] = $last_values[0]; $last_in_errors[$i] = $last_values[1]; $last_out_errors[$i] = $last_values[2]; $flg_created[$i] = 1; }       close(FILE); } else { $flg_created[$i] = 0; }

$update_time = time; unless (open(FILE,">". $centplugins_path. "/packet_errors_if".$i."_". $OPTION{'host'}. ".tmp")){ print "Unknown - $centplugins_path/packet_errors_if".$i."_". $OPTION{'host'}. ".tmp!\n"; exit $ERRORS{"UNKNOWN"}; }   print FILE "$update_time:$nb_in_errors[$i]:$nb_out_errors[$i]"; close(FILE); if ($flg_created[$i] eq 0){ print "First execution : Buffer in creation.... \n"; } }

my $status = "OK"; my @msg; my $diff_test = 0; for (my $i = 0; $i < scalar(@nb_in_errors); $i++) { my $interface = $i+1; if ($flg_created[$i]) { if (($nb_in_errors[$i] - $last_in_errors[$i] >= $critical) or ($nb_out_errors[$i] - $last_out_errors[$i] >= $critical)){ $msg[$i] = $desc_tab[$i]. ":critical "; $status = "CRITICAL"; }       if (($nb_in_errors[$i] - $last_in_errors[$i] >= $warning) or ($nb_out_errors[$i] - $last_out_errors[$i] >= $warning)){ if (!defined($msg[$i])) { $msg[$i] = $desc_tab[$i]. ":warning "; }           if ($status ne "CRITICAL") { $status = "WARNING"; }       }        $diff_test = 1; } }
 * 1) #####          return result
 * 1) #####          return result

if (!$diff_test) { exit($ERRORS{'UNKNOWN'}); } my $output = ""; for (my $i = 0; $i < scalar (@msg); $i++) { if (defined($msg[$i])) { $output .= $msg[$i]; } } if ($output ne ""){ print $output."\n"; } else { print "Status OK on all interfaces\n"; } exit($ERRORS{$status});

sub print_usage { print "\nUsage:\n"; print "$PROGNAME\n"; print "  -H (--hostname)   Hostname to query (required)\n"; print "  -C (--community)  SNMP read community (defaults to public)\n"; print "                    used with SNMP v1 and v2c\n"; print "  -v (--snmp-version)  1 for SNMP v1 (default)\n"; print "                       2 for SNMP v2c\n"; print "                       3 for SNMP v3\n"; print "  -P (--snmp-port)  SNMP port (default: 161)\n"; print "  -k (--authkey)    snmp V3 key\n"; print "  -u (--username)   snmp V3 username \n"; print "  -p (--password)   snmp V3 password\n"; print "  --authprotocol    protocol MD5/SHA  (v3)\n"; print "  --privprotocol    encryption system (DES/AES)(v3) \n"; print "  --privpassword    passphrase (v3) \n"; print "  --64-bits         Use 64 bits OID\n"; print "  --maxrepetitions  To use when you have the error: 'Message size exceeded buffer maxMsgSize'\n"; print "                    Work only with SNMP v2c and v3 (Example: --maxrepetitions=1)\n"; print "  --snmp-timeout    SNMP Timeout\n"; print "  -w (--warn)       Signal strength at which a warning message will be generated\n"; print "                    (default 80)\n"; print "  -c (--crit)       Signal strength at which a critical message will be generated\n"; print "                    (default 100)\n"; print "  -V (--version)    Plugin version\n"; print "  -h (--help)       usage help\n"; }

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