From 2429c090ccf7daf34a1bc61ec81a8d55eda1032d Mon Sep 17 00:00:00 2001 From: Quentin Garnier Date: Fri, 11 Jan 2013 14:51:32 +0100 Subject: [PATCH] Ref #2611 --- centreon-plugins/src/Centreon/SNMP/Utils.pm | 346 ++++---- .../src/check_centreon_snmp_string | 276 +++---- .../src/check_centreon_snmp_traffic | 212 ++--- .../src/check_centreon_snmp_value | 533 ++++++------ .../src/check_centreon_snmp_value_table.pl | 774 +++++++++--------- 5 files changed, 1008 insertions(+), 1133 deletions(-) diff --git a/centreon-plugins/src/Centreon/SNMP/Utils.pm b/centreon-plugins/src/Centreon/SNMP/Utils.pm index fbc1dc99c..29c26947e 100644 --- a/centreon-plugins/src/Centreon/SNMP/Utils.pm +++ b/centreon-plugins/src/Centreon/SNMP/Utils.pm @@ -1,152 +1,194 @@ -################################################################################ -# Copyright 2005-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 . -# -# 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 -# SVN : $Id -# -#################################################################################### - -use strict; -use Net::SNMP qw(:snmp); -use warnings; - -package Centreon::SNMP::Utils; - -# This method takes the version argument given by the user and -# return true only if snmp version value is known -sub checkVersion{ - my $self = shift; - my $snmpversion = shift; - $snmpversion =~ s/^v//; - - if ($snmpversion !~ /1|2c|2|3/) { - print "Wrong SNMP version\n"; - return 0; - - } - return 1; -} - - -# This method take the v3 parameter (username,authprotocol,authpassword,privprotocol,privpassword) in argument and return the session type -# sesssion type = 1 si snmp v1 / v2 -# session type = 2 si snmp v3 AuthNoPriv -# session type = 3 si snmp v3 AuthPriv -# session type = 0 si erreur; -sub checkSessiontype{ - my $self = shift; - my $username = shift; - my $authprotocol = shift; - my $authpassword = shift; - my $privprotocol = shift; - my $privpassword = shift; - my $sessionType = 1; - - if (defined($authprotocol) && defined($authpassword) && defined($username)) { - if ($authprotocol ne "MD5" && $authprotocol ne "SHA1") { - print "Wrong authentication protocol. Must be MD5 or SHA1 \n"; - return 0; - } - $sessionType = 2; - if (defined($privpassword) && defined($privprotocol)) { - if ($privprotocol ne "DES" && $privprotocol ne "AES") { - print "Wrong encryption protocol. Must be DES or AES\n"; - return 0; - } - return 3; - } - } else{ - print "Missing parameter to open SNMPv3 session\n"; - return 0; - } - return $sessionType; -} - -# Method to connect to the remote host -# This method take the hash table option as argument -# return $session if OK or 0 if not -sub connection -{ - my $self = shift; - my $sessionType = shift; - my $options = shift; - my ($session, $error); - - if ($sessionType == 1) { - if (!defined($options->{'host'}) || !defined($options->{'snmpport'}) || !defined($options->{'snmpcomm'}) || !defined($options->{'snmpversion'})) { - print("Error when trying to connect - mission argument(s)\n"); - return 0; - } - ($session, $error) = Net::SNMP->session(-hostname => $options->{'host'}, - -community => $options->{'snmpcomm'}, - -version => $options->{'snmpversion'}, - -port => $options->{'snmpport'}); - - } elsif ($sessionType == 2) { - ($session, $error) = Net::SNMP->session(-hostname => $options->{'host'}, - -version => $options->{'snmpversion'}, - -username => $options->{'username'}, - -authpassword => $options->{'authpassword'}, - -authprotocol => $options->{'authprotocol'}, - -port => $options->{'snmpport'}); - } else { - ($session, $error) = Net::SNMP->session(-hostname => $options->{'host'}, - -version => $options->{'snmpversion'}, - -username => $options->{'username'}, - -authpassword => $options->{'authpassword'}, - -authprotocol => $options->{'authprotocol'}, - -privpassword => $options->{'privpassword'}, - -privprotocol => $options->{'privprotocol'}, - -port => $options->{'snmpport'}); - - } - if (defined($error) && $error ne "") { - print("SESSION ERROR: ".$error."\n"); - return 0; - } - return $session; -} - -# Test if OID passed in 2nd parameter exists -sub testOID{ - my $self = shift; - my $sess = $_[0]; - my $OID_toTest = $_[1]; - my $result = $sess->get_table(Baseoid => $OID_toTest); - if (!defined($result)) { - return 0; - } else{ - return 1; - } -} - -1; \ No newline at end of file +################################################################################ +# Copyright 2005-2013 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 . +# +# 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 +# SVN : $Id +# +#################################################################################### + +use strict; +use Net::SNMP qw(:snmp); +use warnings; + +package Centreon::SNMP::Utils; + +sub load_oids { + my ($exit_status, $oid_file) = @_; + eval 'require Config::IniFiles;'; + + if ($@) { + print "Could not load the Perl module 'Config::IniFiles' $@"; + exit($exit_status); + } + require Config::IniFiles; + + unless (-e $oid_file) { + print "Unknown - In centreon.pm :: $oid_file :: $!\n"; + exit($exit_status); + } + + my %centreon; + tie %centreon, 'Config::IniFiles', ( -file => $oid_file ); + return %centreon; +} + + +sub check_snmp_options { + my ($exit_status, $OPTION) = @_; + my %session_params; + + if (!defined($OPTION->{'host'})) { + print "Missing parameter -H (--host).\n"; + exit $exit_status; + } + + $OPTION->{'snmp-version'} =~ s/^v//; + if ($OPTION->{'snmp-version'} !~ /1|2c|2|3/) { + print "Unknown snmp version\n"; + exit $exit_status; + } + + if ($OPTION->{'snmp-version'} eq "3") { + %session_params = (-hostname => $OPTION->{'host'}, -version => $OPTION->{'snmp-version'}, -port => $OPTION->{'snmp-port'}); + + if (defined($OPTION->{'snmp-auth-password'}) && defined($OPTION->{'snmp-auth-key'})) { + print "Only option -k (--authkey) or -p (--password) is needed for snmp v3\n"; + exit $exit_status; + } + + if (!(defined($OPTION->{'snmp-auth-protocol'}) && (defined($OPTION->{'snmp-auth-password'}) || defined($OPTION->{'snmp-auth-key'})) && defined($OPTION->{'snmp-auth-user'}))) { + print "Missing parameter to open SNMPv3 session\n"; + exit $exit_status; + } + if ($OPTION->{'snmp-auth-protocol'} ne "MD5" && $OPTION->{'snmp-auth-protocol'} ne "SHA1") { + print "Wrong authentication protocol. Must be MD5 or SHA1\n"; + exit $exit_status; + } + $session_params{-username} = $OPTION->{'snmp-auth-user'}; + $session_params{-authprotocol} = $OPTION->{'snmp-auth-protocol'}; + if (defined($OPTION->{'snmp-auth-password'})) { + $session_params{-authpassword} = $OPTION->{'snmp-auth-password'}; + } else { + $session_params{-authkey} = $OPTION->{'snmp-auth-key'}; + } + + if ((defined($OPTION->{'snmp-priv-password'}) || defined($OPTION->{'snmp-priv-key'})) && defined($OPTION->{'snmp-priv-protocol'})) { + if ($OPTION->{'snmp-priv-protocol'} ne "DES" && $OPTION->{'snmp-priv-protocol'} ne "AES") { + print "Wrong encryption protocol. Must be DES or AES\n"; + exit $exit_status; + } + + if (defined($OPTION->{'snmp-priv-password'}) && defined($OPTION->{'snmp-priv-key'})) { + print "Only option --privpassword or --privkey is needed for snmp v3\n"; + exit $exit_status; + } + + $session_params{-privprotocol} = $OPTION->{'snmp-priv-protocol'}; + if (defined($OPTION->{'snmp-priv-password'})) { + $session_params{-privpassword} = $OPTION->{'snmp-priv-password'}; + } else { + $session_params{-privkey} = $OPTION->{'snmp-priv-key'}; + } + } + } else { + %session_params = (-hostname => $OPTION->{'host'}, + -community => $OPTION->{'snmp-community'}, + -version => $OPTION->{'snmp-version'}, + -port => $OPTION->{'snmp-port'}); + } + + if (defined($OPTION->{'64-bits'})) { + if ($OPTION->{'snmp-version'} =~ /1/) { + print "Error : Usage : SNMP v2/v3 is required with option --64-bits\n"; + exit $exit_status; + } + + eval 'require bigint'; + if ($@) { + print "Could not load the Perl module 'bigint' $@"; + exit($exit_status); + } + require bigint; + } + + return (\%session_params); +} + +# Method to connect to the remote host +# This method take the hash table option as argument +sub connection { + my ($exit_status, $session_params) = @_; + my ($session, $error); + + ($session, $error) = Net::SNMP->session(%$session_params); + if (!defined($session)) { + print "UNKNOWN: SNMP Session : $error\n"; + exit $exit_status; + } + + $session->translate(Net::SNMP->TRANSLATE_NONE); + return $session; +} + +sub get_snmp_table { + my ($oid, $session, $exit_status, $OPTION) = @_; + my $result; + + if (defined($OPTION) && defined($OPTION->{'maxrepetitions'})) { + $result = $session->get_table(Baseoid => $oid, -maxrepetitions => $OPTION->{'maxrepetitions'}); + } else { + $result = $session->get_table(Baseoid => $oid); + } + if (!defined($result)) { + printf("SNMP TABLE ERROR : %s.\n", $session->error); + $session->close; + exit $exit_status; + } + return $result; +} + +sub get_snmp_leef { + my ($oids, $session, $exit_status, $extra_msg) = @_; + my $result = $session->get_request(-varbindlist => $oids); + if (!defined($result)) { + printf("SNMP REQUEST ERROR : %s.", $session->error); + if (defined($extra_msg)) { + print $extra_msg; + } + print "\n"; + $session->close; + exit $exit_status; + } + return $result; +} + +1; diff --git a/centreon-plugins/src/check_centreon_snmp_string b/centreon-plugins/src/check_centreon_snmp_string index 43dbda781..85510479b 100644 --- a/centreon-plugins/src/check_centreon_snmp_string +++ b/centreon-plugins/src/check_centreon_snmp_string @@ -1,6 +1,6 @@ #! /usr/bin/perl -w ################################################################################ -# Copyright 2004-2011 MERETHIS +# Copyright 2004-2013 MERETHIS # Centreon is developped by : Julien Mathis and Romain Le Merlus under # GPL Licence 2.0. # @@ -31,14 +31,13 @@ # # For more information : contact@centreon.com # -# SVN : $URL$ -# SVN : $Id$ +# SVN : $URL: http://svn.centreon.com/trunk/plugins-2.x/src/check_centreon_snmp_string $ +# SVN : $Id: check_centreon_snmp_string 12130 2011-04-19 08:57:37Z jmathis $ # #################################################################################### # # Script init # -# check_value -f "The cpu is used %s %d" -m CPU -u "%" use strict; use Getopt::Long; @@ -47,11 +46,14 @@ require "@NAGIOS_PLUGINS@/Centreon/SNMP/Utils.pm"; my $PROGNAME = "$0"; -my %OPTION = ('host' => undef, 'help' => undef, - 'snmpversion' => 1, 'snmpcomm' => 'public', 'min' => 0, 'max' => 0, - 'host' => undef,'username' => undef, 'authpassword' => undef, 'authprotocol' => undef, - 'privprotocol' => undef , 'privpassword' => undef, 'snmpport' => 161, - 'output' => 'The value is %f'); +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, + "64-bits" => undef, + 'help' => undef, + 'output' => 'The value is %f'); my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3); my $prefix = ""; @@ -60,44 +62,45 @@ sub print_usage (); Getopt::Long::Configure('bundling'); GetOptions - ("h" => \$OPTION{'help'}, "help" => \$OPTION{'help'}, - "P=s" => \$OPTION{'snmpport'}, "snmpport=s" => \$OPTION{'snmpport'}, - "V" => \$OPTION{'pluginversion'}, "version" => \$OPTION{'pluginversion'}, - "u=s" => \$OPTION{'username'}, "username=s" => \$OPTION{'username'}, - "a=s" => \$OPTION{'authprotocol'}, "authprotocol=s" => \$OPTION{'authprotocol'}, - "A=s" => \$OPTION{'authpassword'}, "authpassword=s" => \$OPTION{'authpassword'}, - "x=s" => \$OPTION{'privprotocol'}, "privprotocol=s" => \$OPTION{'privprotocol'}, - "X=s" => \$OPTION{'privpassword'}, "privpassword=s" => \$OPTION{'privpassword'}, - "v=s" => \$OPTION{'snmpversion'}, "snmp=s" => \$OPTION{'snmpversion'}, - "C=s" => \$OPTION{'snmpcomm'}, "community=s" => \$OPTION{'snmpcomm'}, - "H=s" => \$OPTION{'host'}, "host=s" => \$OPTION{'host'}, - "W=s" => \$OPTION{'warning_table'}, "warning_table=s" => \$OPTION{'warning_table'}, - "T=s" => \$OPTION{'critical_table'}, "critical_table=s" => \$OPTION{'critical_table'}, - "O=s" => \$OPTION{'ok_table'}, "ok_table=s" => \$OPTION{'ok_table'}, - "o=s" => \$OPTION{'oid'}, "oid=s" => \$OPTION{'oid'}, - "debug" => \$OPTION{'debug'}, - "f=s" => \$OPTION{'output'}, "output=s" => \$OPTION{'output'}, - "m=s" => \$OPTION{'metric'}, "metric=s" => \$OPTION{'metric'}); - + ( + "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'}, + "64-bits" => \$OPTION{'64-bits'}, + "h" => \$OPTION{'help'}, "help" => \$OPTION{'help'}, + "V" => \$OPTION{'pluginversion'}, "version" => \$OPTION{'pluginversion'}, + "W=s" => \$OPTION{'warning_table'}, "warning_table=s" => \$OPTION{'warning_table'}, + "T=s" => \$OPTION{'critical_table'}, "critical_table=s" => \$OPTION{'critical_table'}, + "O=s" => \$OPTION{'ok_table'}, "ok_table=s" => \$OPTION{'ok_table'}, + "o=s" => \$OPTION{'oid'}, "oid=s" => \$OPTION{'oid'}, + "debug" => \$OPTION{'debug'}, + "f=s" => \$OPTION{'output'}, "output=s" => \$OPTION{'output'}); -my $metricsname = undef; my $unit = undef; my $output = undef; - # Table used when personnal threshold are set my @critical_table = (); my @warning_table = (); my @ok_table = (); if ($OPTION{'critical_table'}) { - @critical_table = split(/\,/, $OPTION{'critical_table'}); + @critical_table = split(/\,/, $OPTION{'critical_table'}); } if ($OPTION{'warning_table'}) { - @warning_table = split(/\,/, $OPTION{'warning_table'}); + @warning_table = split(/\,/, $OPTION{'warning_table'}); } if ($OPTION{'ok_table'}) { - @ok_table = split(/\,/, $OPTION{'ok_table'}); + @ok_table = split(/\,/, $OPTION{'ok_table'}); } if (defined($OPTION{'pluginversion'})) { print("$PROGNAME 0.1"); @@ -107,121 +110,92 @@ if (defined($OPTION{'help'})) { print_help(); exit $ERRORS{'UNKNOWN'}; } -if (!$OPTION{'host'}) { - print_usage(); - exit $ERRORS{'UNKNOWN'}; -} -if (!$OPTION{'oid'}) { - print_usage(); - exit $ERRORS{'UNKNOWN'}; +if (!defined($OPTION{'oid'})) { + print "Missing parameters -o (--oid')\n"; + exit $ERRORS{'UNKNOWN'}; } # Store option values in simpler variables -if ($OPTION{'output'} ne "" ){ - #Output Verification? - $output = $OPTION{'output'}; -} else{ - print("Output is not correctly set \n"); - exit $ERRORS{'UNKNOWN'}; +if ($OPTION{'output'} eq "") { + print "Output is not correctly set\n"; + exit $ERRORS{'UNKNOWN'}; } +#Output Verification +$output = $OPTION{'output'}; -# Check if version passed in option exists -$OPTION{'snmpversion'} =~ s/v//g; -exit $ERRORS{'UNKNOWN'} if(!Centreon::SNMP::Utils->checkVersion($OPTION{'snmpversion'})); - -# Check which connection mode is used -my $sessionType = 1; -if ($OPTION{'snmpversion'} =~ /3/) { - $sessionType = Centreon::SNMP::Utils->checkSessiontype($OPTION{'username'},$OPTION{'authprotocol'},$OPTION{'authpassword'},$OPTION{'privprotocol'},$OPTION{'privpassword'}); - exit $ERRORS{'UNKNOWN'} if(!$sessionType); -} - -if (!$OPTION{'oid'}) { - print "Option -o needed.\n"; - exit $ERRORS{'UNKNOWN'}; -} elsif (!($OPTION{'oid'} =~ /^[0-9\.]+$/)) { +if (!($OPTION{'oid'} =~ /^[0-9\.]+$/)) { print "Wrong OID format\n"; exit $ERRORS{'UNKNOWN'}; } -# Plugin snmp connection -my ($session); -if (!($session = Centreon::SNMP::Utils->connection($sessionType,\%OPTION))){ - exit $ERRORS{'UNKNOWN'}; -} +my ($session_params) = Centreon::SNMP::Utils::check_snmp_options($ERRORS{'UNKNOWN'}, \%OPTION); +my $session = Centreon::SNMP::Utils::connection($ERRORS{'UNKNOWN'}, $session_params); # Get the value returned by OID -my $result = $session->get_request(-varbindlist => [$OPTION{'oid'}]); - -if (!defined($result)) { - printf("UNKNOWN: %s.\n", $session->error); - $session->close; - exit $ERRORS{'UNKNOWN'}; -} -my $currentValue = $result->{$OPTION{'oid'}}; +my $result = Centreon::SNMP::Utils::get_snmp_leef([$OPTION{'oid'}], $session, $ERRORS{'UNKNOWN'}); +my $currentValue = $result->{$OPTION{'oid'}}; # Check if value returned is a number and then save it if (!defined($currentValue) || $currentValue =~ /noSuch/){ - print("No instance on OID $OPTION{'oid'} \n "); - exit $ERRORS{'UNKNOWN'}; + print "No instance on OID $OPTION{'oid'}\n"; + exit $ERRORS{'UNKNOWN'}; } #=== Plugin return ==== +if (defined($currentValue)) { + my $returnValue = $currentValue; + my $status = "UNKNOWN"; + my $state= "unknownState"; + $returnValue = "warningvalue" if($OPTION{'debug'}); -if (defined($currentValue)){ - my $returnValue = $currentValue; - my $status = "UNKNOWN"; - my $state= "unknownState"; - $returnValue = "warningvalue" if($OPTION{'debug'}); - - - ################################################################# - # If personnal thresholds are set for warning and / or critical # - ################################################################# - if ($OPTION{'warning_table'} || $OPTION{'critical_table'} || $OPTION{'ok_table'}) { - print "Mode personal threshold ON \n" if($OPTION{'debug'}); - if ($OPTION{'ok_table'}) { - my $max_ok= scalar(@ok_table); - my $i = 0; - - while ($i < $max_ok) { - print "OK[$i]: $ok_table[$i] / returnValue = $returnValue \n" if($OPTION{'debug'}); - if($ok_table[$i] eq $returnValue) { - $status = "OK"; - $state = $ok_table[$i]; - } - $i++; - } - } - if ($OPTION{'warning_table'}) { - my $max_warn= scalar(@warning_table); - my $i = 0; - - while ($i < $max_warn) { - print "Warning[$i]: $warning_table[$i] / returnValue = $returnValue \n" if($OPTION{'debug'}); - if($warning_table[$i] eq $returnValue) { - $status = "WARNING"; - $state = $warning_table[$i]; - } - $i++; - } - } - if ($OPTION{'critical_table'}){ - my $i = 0; - my $max_crit= scalar(@critical_table); - while($i < $max_crit) { - print "Critical[$i] = $critical_table[$i] / returnValue = $returnValue \n" if($OPTION{'debug'}); - if($critical_table[$i] eq $returnValue) { - $status = "CRITICAL"; - $state = $warning_table[$i]; - } - $i++; - } - } - print(" Statut = $status \n ") if($OPTION{'debug'}); - printf($output."\n",$state,$returnValue); - exit $ERRORS{$status}; - } + + ################################################################# + # If personnal thresholds are set for warning and / or critical # + ################################################################# + if ($OPTION{'warning_table'} || $OPTION{'critical_table'} || $OPTION{'ok_table'}) { + print "Mode personal threshold ON \n" if($OPTION{'debug'}); + if ($OPTION{'ok_table'}) { + my $max_ok= scalar(@ok_table); + my $i = 0; + + while ($i < $max_ok) { + print "OK[$i]: $ok_table[$i] / returnValue = $returnValue \n" if($OPTION{'debug'}); + if($ok_table[$i] eq $returnValue) { + $status = "OK"; + $state = $ok_table[$i]; + } + $i++; + } + } + if ($OPTION{'warning_table'}) { + my $max_warn= scalar(@warning_table); + my $i = 0; + + while ($i < $max_warn) { + print "Warning[$i]: $warning_table[$i] / returnValue = $returnValue \n" if($OPTION{'debug'}); + if($warning_table[$i] eq $returnValue) { + $status = "WARNING"; + $state = $warning_table[$i]; + } + $i++; + } + } + if ($OPTION{'critical_table'}) { + my $i = 0; + my $max_crit= scalar(@critical_table); + while($i < $max_crit) { + print "Critical[$i] = $critical_table[$i] / returnValue = $returnValue \n" if($OPTION{'debug'}); + if($critical_table[$i] eq $returnValue) { + $status = "CRITICAL"; + $state = $critical_table[$i]; + } + $i++; + } + } + print(" Statut = $status \n ") if($OPTION{'debug'}); + printf($output."\n",$state,$returnValue); + exit $ERRORS{$status}; + } } else { print "CRITICAL Host unavailable\n"; exit $ERRORS{'CRITICAL'}; @@ -231,31 +205,35 @@ if (defined($currentValue)){ sub print_usage () { print "Usage:"; print "$PROGNAME\n"; - print " -H (--hostname) \t Hostname to query - (required)\n"; - print " -C (--community) \t SNMP read community (defaults to public,\n"; - print " \t \t used with SNMP v1 and v2c\n"; - print " -v (--snmp_version) \t 1 for SNMP v1 (default)\n"; - print " \t 2 for SNMP v2c\n"; - print " -t (--type) \t Data Source Type (GAUGE or COUNTER) (GAUGE by default)\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 (--key) snmp V3 key\n"; + print " -u (--username) snmp V3 username \n"; + print " -p (--password) snmp V3 password\n"; + print " --authprotocol protocol MD5/SHA1 (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 " -o (--oid) \t OID to check\n"; - print " -u (--username) \t snmp v3 username \n"; - print " -a (--authprotocol) \t protocol MD5/SHA1 (v3)\n"; - print " -A (--authpassword) \t password (v3) \n"; - print " -x (--privprotocol) \t encryption system (DES/AES)(v3) \n"; - print " -X (--privpassword)\t passphrase (v3) \n"; - print " -W (--wtreshold) \t Personal warning threshold : -W warningstate... \n"; - print " -T (--ctreshold) \t Personal critical threshold : -T criticalstate1,criticalstate2... \n"; - print " -O (--ctreshold) \t Personal critical threshold : -O okstate1,okstate2... \n"; - print " -m (--metric) \t Metric Name\n"; - print " -U (--unit) \t Metric's unit ( /!\\ for % write %% ) \n"; - print " -f (--output) \t Output format (ex : -f \"My metric's percentage value = %f %%\" \n"; - print " -V (--version) \t Plugin version\n"; + print " -W (--warning_table) \t Personal warning threshold : -W warningstate... \n"; + print " -T (--critical_table) \t Personal critical threshold : -T criticalstate1,criticalstate2... \n"; + print " -O (--ok_table) \t Personal ok threshold : -O okstate1,okstate2... \n"; + print " -f (--output) \t Output format (ex : -f \"My metric's percentage value = %f %%\" \n"; + print " -V (--version) \t Plugin version\n"; print " -h (--help) \t usage help\n"; } sub print_help () { print "##############################################\n"; - print "# Copyright (c) 2004-2011 Centreon #\n"; + print "# Copyright (c) 2004-2013 Centreon #\n"; print "# Bugs to http://forge.centreon.com/ #\n"; print "##############################################\n"; print_usage(); diff --git a/centreon-plugins/src/check_centreon_snmp_traffic b/centreon-plugins/src/check_centreon_snmp_traffic index 7967d7015..615d4d8b2 100644 --- a/centreon-plugins/src/check_centreon_snmp_traffic +++ b/centreon-plugins/src/check_centreon_snmp_traffic @@ -40,60 +40,65 @@ # use strict; -use Net::SNMP qw(:snmp oid_lex_sort); -use FindBin; -use lib "$FindBin::Bin"; -use lib "@NAGIOS_PLUGINS@"; -use utils qw($TIMEOUT %ERRORS &print_revision &support); -if (eval "require centreon") { - use centreon qw(get_parameters); - use vars qw(%centreon); - %centreon=get_parameters(); -} else { - print "Unable to load centreon perl module\n"; - exit $ERRORS{'UNKNOWN'}; -} +require "@NAGIOS_PLUGINS@/Centreon/SNMP/Utils.pm"; + use vars qw($PROGNAME); use Getopt::Long; use vars qw($opt_V $opt_h $opt_i $opt_n $opt_w $opt_c $opt_s $opt_T $opt_a $opt_r $opt_S); +my $centplugins_path = "@CENTPLUGINS_TMP@"; +my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3); + +my %centreon = Centreon::SNMP::Utils::load_oids($ERRORS{'UNKNOWN'}, "@NAGIOS_PLUGINS@/centreon.conf"); + # Plugin var init my ($session, $error); my ($row, $last_check_time, $last_in_bits, $last_out_bits, @last_values, $update_time, $in_traffic, $out_traffic, $in_usage, $out_usage); $PROGNAME = "$0"; + sub print_help (); sub print_usage (); my %OPTION = ( - "host" => undef, "64-bits" => undef, + "host" => undef, "snmp-community" => "public", "snmp-version" => 1, "snmp-port" => 161, - "snmp-auth-key" => undef, "snmp-auth-user" => undef, "snmp-auth-passwd" => undef + "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, + "64-bits" => undef, + + "disable-warn-state" => undef ); Getopt::Long::Configure('bundling'); GetOptions ( - "H|hostname=s" => \$OPTION{'host'}, - "C|community=s" => \$OPTION{'snmp-community'}, - "v|snmp-version=s" => \$OPTION{'snmp-version'}, - "P|snmp-port=i" => \$OPTION{'snmp-port'}, - "k|key=s" => \$OPTION{'snmp-auth-key'}, - "u|username=s" => \$OPTION{'snmp-auth-user'}, - "p|password=s" => \$OPTION{'snmp-auth-passwd'}, - "64-bits" => \$OPTION{'64-bits'}, - "maxrepetitions=s" => \$OPTION{'maxrepetitions'}, - "disable-warn-state" => \$OPTION{'disable-warn-state'}, - "h" => \$opt_h, "help" => \$opt_h, - "s" => \$opt_s, "show" => \$opt_s, - "V" => \$opt_V, "version" => \$opt_V, - "i=s" => \$opt_i, "interface=s" => \$opt_i, - "n" => \$opt_n, "name" => \$opt_n, - "w=s" => \$opt_w, "warning=s" => \$opt_w, - "c=s" => \$opt_c, "critical=s" => \$opt_c, - "T=s" => \$opt_T, "r" => \$opt_r, + "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'}, + "64-bits" => \$OPTION{'64-bits'}, + + "disable-warn-state" => \$OPTION{'disable-warn-state'}, + "h" => \$opt_h, "help" => \$opt_h, + "s" => \$opt_s, "show" => \$opt_s, + "V" => \$opt_V, "version" => \$opt_V, + "i=s" => \$opt_i, "interface=s" => \$opt_i, + "n" => \$opt_n, "name" => \$opt_n, + "w=s" => \$opt_w, "warning=s" => \$opt_w, + "c=s" => \$opt_c, "critical=s" => \$opt_c, + "T=s" => \$opt_T, "r" => \$opt_r, "S" => \$opt_S, - "a=i" => \$opt_a, "cache=s" => \$opt_a); + "a=i" => \$opt_a, "cache=s" => \$opt_a); if ($opt_V) { print_revision($PROGNAME,'$Revision: 1.3 $'); @@ -106,113 +111,14 @@ if ($opt_h) { Getopt::Long::Configure('bundling'); } -################################################## -##### Functions -## - -sub create_snmp_session() { - if ($OPTION{'snmp-version'} =~ /[1-2]/) { - ($session, $error) = Net::SNMP->session(-hostname => $OPTION{'host'}, - -community => $OPTION{'snmp-community'}, - -version => $OPTION{'snmp-version'}, - -port => $OPTION{'snmp-port'}); - if (!defined($session)) { - print("UNKNOWN: SNMP Session : $error\n"); - exit $ERRORS{'UNKNOWN'}; - } - } elsif (defined($OPTION{'snmp-auth-key'})) { - ($session, $error) = Net::SNMP->session(-hostname => $OPTION{'host'}, - -version => $OPTION{'snmp-version'}, - -username => $OPTION{'snmp-auth-user'}, - -authkey => $OPTION{'snmp-auth-key'}, - -port => $OPTION{'snmp-port'}); - if (!defined($session)) { - print("UNKNOWN: SNMP Session : $error\n"); - exit $ERRORS{'UNKNOWN'}; - } - } elsif (defined($OPTION{'snmp-auth-passwd'})) { - ($session, $error) = Net::SNMP->session(-hostname => $OPTION{'host'}, - -version => $OPTION{'snmp-version'}, - -username => $OPTION{'snmp-auth-user'}, - -authpassword => $OPTION{'snmp-auth-passwd'}, - -port => $OPTION{'snmp-port'}); - if (!defined($session)) { - print("UNKNOWN: SNMP Session : $error\n"); - exit $ERRORS{'UNKNOWN'}; - } - } - - if (defined($session)) { - $session->translate(Net::SNMP->TRANSLATE_NONE); - } - - return $session; -} - -sub get_snmp_table($$) { - my ($oid, $session) = (shift, shift); - my $result; - - if (defined($OPTION{'maxrepetitions'})) { - $result = $session->get_table(Baseoid => $oid, -maxrepetitions => $OPTION{'maxrepetitions'}); - } else { - $result = $session->get_table(Baseoid => $oid); - } - if (!defined($result)) { - printf("SNMP TABLE ERROR : %s.\n", $session->error); - $session->close; - exit $ERRORS{'UNKNOWN'}; - } - return $result; -} - ################################################## ##### Verify Options ## -if (!$OPTION{'host'}) { - print_usage(); - exit $ERRORS{'OK'}; -} - ###### ### SNMP Check ## -if (defined($OPTION{'snmp-version'}) && $OPTION{'snmp-version'} =~ /^([1-3])c?$/) { - $OPTION{'snmp-version'} = $1; -} elsif (defined($OPTION{'snmp-version'})) { - print "Unknown snmp version\n"; - print_usage(); - exit $ERRORS{'UNKNOWN'}; -} - -if ($OPTION{'snmp-version'} eq "3") { - if (!defined($OPTION{'snmp-auth-user'})) { - print "Option -u (--username) is required for snmpV3\n"; - exit $ERRORS{'UNKNOWN'}; - } - if (!defined($OPTION{'snmp-auth-passwd'}) && !defined($OPTION{'snmp-auth-key'})) { - print "Option -k (--key) or -p (--password) is required for snmp v3\n"; - exit $ERRORS{'UNKNOWN'}; - } elsif (defined($OPTION{'snmp-auth-passwd'}) && defined($OPTION{'snmp-auth-key'})) { - print "Only option -k (--key) or -p (--password) is needed for snmp v3\n"; - exit $ERRORS{'UNKNOWN'}; - } -} - -if (defined($OPTION{'64-bits'}) && $OPTION{'snmp-version'} !~ /2/) { - print "Error : Usage : SNMP v2 is required with option --64-bits\n"; - exit $ERRORS{'UNKNOWN'}; -} - -if (defined($OPTION{'64-bits'})) { - if (eval "require bigint") { - use bigint; - } else { - print "ERROR : Need bigint module for 64 bit counters\n"; - exit $ERRORS{"UNKNOWN"} - } -} +my ($session_params) = Centreon::SNMP::Utils::check_snmp_options($ERRORS{'UNKNOWN'}, \%OPTION); ###### ### Others @@ -257,13 +163,13 @@ if ($critical <= $warning){ ##### Plugin snmp requests ## -$session = create_snmp_session(); +$session = Centreon::SNMP::Utils::connection($ERRORS{'UNKNOWN'}, $session_params); my $OID_DESC = $centreon{MIB2}{IF_DESC}; my $OID_OPERSTATUS = $centreon{MIB2}{IF_OPERSTATUS}; my @operstatus = ("up","down","testing", "unknown", "dormant", "notPresent", "lowerLayerDown"); -my $cacheFile = "@CENTPLUGINS_TMP@/traffic_cache_". $OPTION{'host'}; +my $cacheFile = "$centplugins_path/traffic_cache_". $OPTION{'host'}; my $result; my $mustCreateFile = 0; my $countLine; @@ -292,7 +198,7 @@ if (-e $cacheFile) { } if ($mustCreateFile) { - $result = get_snmp_table($OID_DESC, $session); + $result = Centreon::SNMP::Utils::get_snmp_table($OID_DESC, $session, $ERRORS{'UNKNOWN'}, \%OPTION); unless (open(FILE,">".$cacheFile)){ print "Check mod for temporary file : ".$cacheFile."...\n"; exit $ERRORS{"UNKNOWN"}; @@ -359,7 +265,7 @@ if (defined($OPTION{'64-bits'})) { # Get desctiption table if ($opt_s) { if (!-e $cacheFile) { - $result = get_snmp_table($OID_DESC, $session); + $result = Centreon::SNMP::Utils::get_snmp_table($OID_DESC, $session, $ERRORS{'UNKNOWN'}, \%OPTION); unless (open(FILE,">".$cacheFile)){ print "Check mod for temporary file : ".$cacheFile."...\n"; exit $ERRORS{"UNKNOWN"}; @@ -384,12 +290,12 @@ if ($opt_s) { if ($countLine) { my @resLine = split(/\;/, $row); my $index = $resLine[0]; - my $interface_status = $session->get_request(-varbindlist => [$OID_OPERSTATUS.".".$index]); + my $interface_status = Centreon::SNMP::Utils::get_snmp_leef([$OID_OPERSTATUS.".".$index], $session, $ERRORS{'UNKNOWN'}); $resLine[1] =~ s/\x00//g; $resLine[1] =~ s/\n//g; print "Interface ". $index . " :: " . $resLine[1] . " :: ".$operstatus[$interface_status->{$OID_OPERSTATUS.".".$index} - 1]; if ($opt_S) { - my $link_speed = $session->get_request(-varbindlist => [$OID_SPEED_BASE.".".$index]); + my $link_speed = Centreon::SNMP::Utils::get_snmp_leef([$OID_SPEED_BASE.".".$index], $session, $ERRORS{'UNKNOWN'}); if (!defined($link_speed)) { printf("ERROR: Interface Speed Request : %s", $session->error); exit $ERRORS{'UNKNOWN'}; @@ -407,16 +313,9 @@ if ($opt_s) { exit $ERRORS{'OK'}; } -$result = $session->get_request(-varbindlist => [$OID_OPERSTATUS.".".$interface, $OID_IN, $OID_OUT, $OID_SPEED]); -if (!defined($result)) { - printf("ERROR: SNMP Request: %s", $session->error); - if ($opt_n) { - print " - You must specify interface name when option -n is used"; - } - print ".\n"; - $session->close; - exit $ERRORS{'UNKNOWN'}; -} + +$result = Centreon::SNMP::Utils::get_snmp_leef([$OID_OPERSTATUS.".".$interface, $OID_IN, $OID_OUT, $OID_SPEED], $session, $ERRORS{'UNKNOWN'}, + defined($opt_n) ? "You must specify interface name when option -n is used." : undef); if (!defined($result->{$OID_OPERSTATUS.".".$interface}) || $result->{$OID_OPERSTATUS.".".$interface} eq "") { print "ERROR: Can't get interface '$interface' status\n"; exit $ERRORS{'CRITICAL'}; @@ -471,8 +370,8 @@ $last_out_bits = 0; my $flg_created = 0; -if (-e "@CENTPLUGINS_TMP@/traffic_if".$interface."_".$OPTION{'host'}) { - open(FILE,"<"."@CENTPLUGINS_TMP@/traffic_if".$interface."_".$OPTION{'host'}); +if (-e "$centplugins_path/traffic_if".$interface."_".$OPTION{'host'}) { + open(FILE,"<"."$centplugins_path/traffic_if".$interface."_".$OPTION{'host'}); while($row = ){ @last_values = split(":",$row); $last_check_time = $last_values[0]; @@ -487,8 +386,8 @@ if (-e "@CENTPLUGINS_TMP@/traffic_if".$interface."_".$OPTION{'host'}) { $update_time = time(); -unless (open(FILE,">"."@CENTPLUGINS_TMP@/traffic_if".$interface."_".$OPTION{'host'})){ - print "Check mod for temporary file : @CENTPLUGINS_TMP@/traffic_if".$interface."_".$OPTION{'host'}. " !\n"; +unless (open(FILE,">"."$centplugins_path/traffic_if".$interface."_".$OPTION{'host'})){ + print "Check mod for temporary file : $centplugins_path/traffic_if".$interface."_".$OPTION{'host'}. " !\n"; exit $ERRORS{"UNKNOWN"}; } print FILE "$update_time:$in_bits:$out_bits"; @@ -647,6 +546,9 @@ sub print_usage () { print " -k (--key) snmp V3 key\n"; print " -u (--username) snmp V3 username \n"; print " -p (--password) snmp V3 password\n"; + print " --authprotocol protocol MD5/SHA1 (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"; @@ -658,7 +560,7 @@ sub print_usage () { print " -w (--warning) Signal strength at which a warning message will be generated\n"; print " (default 80)\n"; print " -c (--critical) Signal strength at which a critical message will be generated\n"; - print " --disable-warn-state To use when you have the error: 'Message size exceeded buffer maxMsgSize'\n"; + print " --disable-warn-state Option for not alerting on interface state\n"; print " -T Set maximum bandwidth\n"; print " -S Show link speed in output\n"; print " -V (--version) Plugin version\n"; diff --git a/centreon-plugins/src/check_centreon_snmp_value b/centreon-plugins/src/check_centreon_snmp_value index afbb2440e..c179827a9 100644 --- a/centreon-plugins/src/check_centreon_snmp_value +++ b/centreon-plugins/src/check_centreon_snmp_value @@ -1,6 +1,6 @@ #! /usr/bin/perl -w ################################################################################ -# Copyright 2004-2011 MERETHIS +# Copyright 2004-2013 MERETHIS # Centreon is developped by : Julien Mathis and Romain Le Merlus under # GPL Licence 2.0. # @@ -31,14 +31,13 @@ # # For more information : contact@centreon.com # -# SVN : $URL$ -# SVN : $Id$ +# SVN : $URL: http://svn.centreon.com/trunk/plugins-2.x/src/check_centreon_snmp_value $ +# SVN : $Id: check_centreon_snmp_value 13054 2012-05-21 08:02:46Z jmathis $ # #################################################################################### # # Script init # -# check_value -f "The cpu is used %s %d" -m CPU -u "%" use strict; use Getopt::Long; @@ -47,11 +46,18 @@ require "@NAGIOS_PLUGINS@/Centreon/SNMP/Utils.pm"; my $PROGNAME = "$0"; -my %OPTION = ('host' => undef, 'help' => undef, 'warning' => '5', 'critical' => '10', - 'snmpversion' => 1, 'display' => 0, 'snmpcomm' => 'public', 'min' => 0, 'max' => 0, - 'host' => undef,'username' => undef, 'authpassword' => undef, 'authprotocol' => undef, - 'privprotocol' => undef , 'privpassword' => undef, 'snmpport' => 161, 'type' => 'GAUGE', 'base' => 1000, - 'output' => 'The value is %f', 'metric' =>'value', 'unit' => 'nounit', 'divide' => 1); +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, + "64-bits" => undef, + + 'help' => undef, 'warning' => '5', 'critical' => '10', + 'display' => 0, 'min' => 0, 'max' => 0, + 'type' => 'GAUGE', 'base' => 1000, + 'output' => 'The value is %f', 'metric' =>'value', 'unit' => 'nounit', 'divide' => 1); my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3); my $prefix = ""; @@ -60,36 +66,41 @@ sub print_usage (); Getopt::Long::Configure('bundling'); GetOptions - ("h" => \$OPTION{'help'}, "help" => \$OPTION{'help'}, - "P=s" => \$OPTION{'snmpport'}, "snmpport=s" => \$OPTION{'snmpport'}, - "V" => \$OPTION{'pluginversion'}, "version" => \$OPTION{'pluginversion'}, - "u=s" => \$OPTION{'username'}, "username=s" => \$OPTION{'username'}, - "a=s" => \$OPTION{'authprotocol'}, "authprotocol=s" => \$OPTION{'authprotocol'}, - "A=s" => \$OPTION{'authpassword'}, "authpassword=s" => \$OPTION{'authpassword'}, - "x=s" => \$OPTION{'privprotocol'}, "privprotocol=s" => \$OPTION{'privprotocol'}, - "X=s" => \$OPTION{'privpassword'}, "privpassword=s" => \$OPTION{'privpassword'}, - "v=s" => \$OPTION{'snmpversion'}, "snmp=s" => \$OPTION{'snmpversion'}, - "C=s" => \$OPTION{'snmpcomm'}, "community=s" => \$OPTION{'snmpcomm'}, - "w=s" => \$OPTION{'warning'}, "warning=s" => \$OPTION{'warning'}, - "c=s" => \$OPTION{'critical'}, "critical=s" => \$OPTION{'critical'}, - "H=s" => \$OPTION{'host'}, "host=s" => \$OPTION{'host'}, - "W=s" => \$OPTION{'warning_table'}, "warning_table=s" => \$OPTION{'warning_table'}, - "T=s" => \$OPTION{'critical_table'}, "critical_table=s" => \$OPTION{'critical_table'}, - "O=s" => \$OPTION{'ok_table'}, "ok_table=s" => \$OPTION{'ok_table'}, - "o=s" => \$OPTION{'oid'}, "oid=s" => \$OPTION{'oid'}, - "t=s" => \$OPTION{'type'}, "type=s" => \$OPTION{'type'}, - "d=s" => \$OPTION{'divide'}, "divide=s" => \$OPTION{'divide'}, - "U=s" => \$OPTION{'unit'}, "unit=s" => \$OPTION{'unit'}, - "max=s" => \$OPTION{'max'}, - "convert" => \$OPTION{'convert'}, - "debug" => \$OPTION{'debug'}, - "min=s" => \$OPTION{'min'}, - "base=s" => \$OPTION{'base'}, - "64-bits=s" => \$OPTION{'64-bits'}, - "f=s" => \$OPTION{'output'}, "output=s" => \$OPTION{'output'}, - "m=s" => \$OPTION{'metric'}, "metric=s" => \$OPTION{'metric'} + ( + "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'}, + "64-bits" => \$OPTION{'64-bits'}, + + "h" => \$OPTION{'help'}, "help" => \$OPTION{'help'}, + "V" => \$OPTION{'pluginversion'}, "version" => \$OPTION{'pluginversion'}, + "w=s" => \$OPTION{'warning'}, "warning=s" => \$OPTION{'warning'}, + "c=s" => \$OPTION{'critical'}, "critical=s" => \$OPTION{'critical'}, + "W=s" => \$OPTION{'warning_table'}, "warning_table=s" => \$OPTION{'warning_table'}, + "T=s" => \$OPTION{'critical_table'}, "critical_table=s" => \$OPTION{'critical_table'}, + "O=s" => \$OPTION{'ok_table'}, "ok_table=s" => \$OPTION{'ok_table'}, + "o=s" => \$OPTION{'oid'}, "oid=s" => \$OPTION{'oid'}, + "t=s" => \$OPTION{'type'}, "type=s" => \$OPTION{'type'}, + "d=s" => \$OPTION{'divide'}, "divide=s" => \$OPTION{'divide'}, + "U=s" => \$OPTION{'unit'}, "unit=s" => \$OPTION{'unit'}, + "max=s" => \$OPTION{'max'}, + "convert" => \$OPTION{'convert'}, + "debug" => \$OPTION{'debug'}, + "min=s" => \$OPTION{'min'}, + "base=s" => \$OPTION{'base'}, + "f=s" => \$OPTION{'output'}, "output=s" => \$OPTION{'output'}, + "m=s" => \$OPTION{'metric'}, "metric=s" => \$OPTION{'metric'} ); - + # For counter metric type my $metricsname = undef; my $unit = undef; @@ -111,84 +122,65 @@ my @warning_table = (); my @ok_table = (); if ($OPTION{'critical_table'}){ - @critical_table = split(/\,/, $OPTION{'critical_table'}); + @critical_table = split(/\,/, $OPTION{'critical_table'}); } if ($OPTION{'warning_table'}){ - @warning_table = split(/\,/, $OPTION{'warning_table'}); + @warning_table = split(/\,/, $OPTION{'warning_table'}); } if ($OPTION{'ok_table'}){ - @ok_table = split(/\,/, $OPTION{'ok_table'}); + @ok_table = split(/\,/, $OPTION{'ok_table'}); } if (defined($OPTION{'pluginversion'})) { - print("$PROGNAME 1.1"); + print "$PROGNAME 1.1\n"; exit $ERRORS{'UNKNOWN'}; } if (defined($OPTION{'help'})) { print_help(); exit $ERRORS{'UNKNOWN'}; } -if (!$OPTION{'host'}) { - print_usage(); - exit $ERRORS{'UNKNOWN'}; -} -if (!$OPTION{'oid'}) { - print_usage(); - exit $ERRORS{'UNKNOWN'}; -} - -my $cacheFile = "@CENTPLUGINS_TMP@/snmp_value".$OPTION{'host'}."-".$OPTION{'oid'}; - -# Store option values in simpler variables -if ($OPTION{'divide'} ne "" && $OPTION{'metric'} ne "" && $OPTION{'unit'} ne "" && $OPTION{'output'} ne "" && $OPTION{'min'} ne "" && $OPTION{'max'} ne "") { - $metricsname = $OPTION{'metric'}; - $unit = $OPTION{'unit'}; - - # Output Verification? - $output = $OPTION{'output'}; - - # check parameter format - if ($OPTION{'base'} !~ /^[0-9]*\.?[0-9]*$/) { - print(" Base option should be a numerical \n"); - exit $ERRORS{'UNKNOWN'}; - } - - if ($OPTION{'min'} !~ /^[0-9]*\.?[0-9]*$/) { - print(" Min option should be a numerical \n"); - exit $ERRORS{'UNKNOWN'}; - } - if ($OPTION{'max'} !~ /^[0-9]*\.?[0-9]*$/) { - print(" Max option should be a numerical \n"); - exit $ERRORS{'UNKNOWN'}; - } - if ($OPTION{'divide'} !~ /^[0-9]*\.?[0-9]*$/) { - print(" -d option should be a numerical \n"); - exit $ERRORS{'UNKNOWN'}; - } - if ($OPTION{'warning'} !~ /^[0-9]*\.?[0-9]*$/ || $OPTION{'critical'} !~ /^[0-9]*\.?[0-9]*$/) { - print(" Option warning &/or critical should be numerical \n"); - exit $ERRORS{'UNKNOWN'}; - } - - $min = $OPTION{'min'}; - $max = $OPTION{'max'}; - $divide = $OPTION{'divide'}; - -} else { - print("One or more arguments are not set \n"); +if (!defined($OPTION{'oid'})) { + print "Missing parameters -o (--oid')\n"; exit $ERRORS{'UNKNOWN'}; } -# Check if version passed in option exists -$OPTION{'snmpversion'} =~ s/v//g; -exit $ERRORS{'UNKNOWN'} if(!Centreon::SNMP::Utils->checkVersion($OPTION{'snmpversion'})); -# Check which connection mode is used -my $sessionType = 1; -if ($OPTION{'snmpversion'} =~ /3/) { +# Store option values in simpler variables +if ($OPTION{'divide'} ne "" && $OPTION{'metric'} ne "" && $OPTION{'unit'} ne "" && $OPTION{'output'} ne "" && $OPTION{'min'} ne "" && $OPTION{'max'} ne "") { + $metricsname = $OPTION{'metric'}; + $unit = $OPTION{'unit'}; - $sessionType = Centreon::SNMP::Utils->checkSessiontype($OPTION{'username'},$OPTION{'authprotocol'},$OPTION{'authpassword'},$OPTION{'privprotocol'},$OPTION{'privpassword'}); - exit $ERRORS{'UNKNOWN'} if(!$sessionType); + # Output Verification? + $output = $OPTION{'output'}; + # check parameter format + if ($OPTION{'base'} !~ /^[0-9]*\.?[0-9]*$/) { + print(" Base option should be a numerical \n"); + exit $ERRORS{'UNKNOWN'}; + } + + if ($OPTION{'min'} !~ /^[0-9]*\.?[0-9]*$/) { + print(" Min option should be a numerical \n"); + exit $ERRORS{'UNKNOWN'}; + } + if ($OPTION{'max'} !~ /^[0-9]*\.?[0-9]*$/) { + print(" Max option should be a numerical \n"); + exit $ERRORS{'UNKNOWN'}; + } + if ($OPTION{'divide'} !~ /^[0-9]*\.?[0-9]*$/) { + print(" -d option should be a numerical \n"); + exit $ERRORS{'UNKNOWN'}; + } + if ($OPTION{'warning'} !~ /^[0-9]*\.?[0-9]*$/ || $OPTION{'critical'} !~ /^[0-9]*\.?[0-9]*$/) { + print(" Option warning &/or critical should be numerical \n"); + exit $ERRORS{'UNKNOWN'}; + } + + $min = $OPTION{'min'}; + $max = $OPTION{'max'}; + $divide = $OPTION{'divide'}; +} else { + print("One or more arguments are not set \n"); + exit $ERRORS{'UNKNOWN'}; } my $DS_type = "GAUGE"; @@ -203,190 +195,173 @@ if ($critical < $warning){ exit $ERRORS{'UNKNOWN'}; } -if (!$OPTION{'oid'}) { - print "Option -o needed.\n"; - exit $ERRORS{'UNKNOWN'}; -} elsif (!($OPTION{'oid'} =~ /^[0-9\.]+$/)) { +if (!($OPTION{'oid'} =~ /^[0-9\.]+$/)) { print "Wrong OID format\n"; exit $ERRORS{'UNKNOWN'}; } -# Plugin snmp connection -my ($session); -if (!($session = Centreon::SNMP::Utils->connection($sessionType,\%OPTION))) { - exit $ERRORS{'UNKNOWN'}; -} +my ($session_params) = Centreon::SNMP::Utils::check_snmp_options($ERRORS{'UNKNOWN'}, \%OPTION); +my $session = Centreon::SNMP::Utils::connection($ERRORS{'UNKNOWN'}, $session_params); # Get the value returned by OID -my $result = $session->get_request(-varbindlist => [$OPTION{'oid'}]); - -if (!defined($result)) { - printf("UNKNOWN: %s.\n", $session->error); - $session->close; - exit $ERRORS{'UNKNOWN'}; -} +my $result = Centreon::SNMP::Utils::get_snmp_leef([$OPTION{'oid'}], $session, $ERRORS{'UNKNOWN'}); $currentValue = $result->{$OPTION{'oid'}}; +my $cacheFile = "@CENTPLUGINS_TMP@/snmp_value".$OPTION{'host'}."-".$OPTION{'oid'}; + # Check if value returned is a number and then save it if(!defined($currentValue) || $currentValue =~ /noSuch/){ - print("No instance on OID $OPTION{'oid'} \n "); - exit $ERRORS{'UNKNOWN'}; + print("No instance on OID $OPTION{'oid'} \n "); + exit $ERRORS{'UNKNOWN'}; } if (defined($currentValue)){ - if ($currentValue !~ /^[-+]?[0-9]*\.?[0-9]*/) { - print "Snmp returned value isn't numeric (signed/float values included) .\n"; - exit $ERRORS{'UNKNOWN'}; - } + if ($currentValue !~ /^[-+]?[0-9]*\.?[0-9]*/) { + print "Snmp returned value isn't numeric (signed/float values included) .\n"; + exit $ERRORS{'UNKNOWN'}; + } } # If metric type = counter, use the cache file to get the last value (or create cache file) if ($DS_type eq 'COUNTER') { - - #If file exist - - if (-e $cacheFile){ - my @cache; - open(FILE,"<".$cacheFile); - my $countLine = 0; - my $row = ; - @cache = split(/\;/, $row); - $previousTime = $cache[0]; - $previousValue = $cache[1]; - close(FILE); - # Set new values in cache file - open(FILE,">".$cacheFile); - print FILE $currentTime.";".$currentValue; - close(FILE); - - } else { - #If the file doesnt exist, a new file is created and values are inserted - unless (open(FILE,">".$cacheFile)){ - print "Check temporary file's rights : ".$cacheFile."...\n"; - exit $ERRORS{"UNKNOWN"}; - } - my $currentTime = time(); - print FILE $currentTime.";".$currentValue; - print("Buffer in creation . . . please wait \n"); - exit $ERRORS{"OK"}; - close(FILE); - } + #If file exist + if (-e $cacheFile){ + my @cache; + open(FILE,"<".$cacheFile); + my $countLine = 0; + my $row = ; + @cache = split(/\;/, $row); + $previousTime = $cache[0]; + $previousValue = $cache[1]; + close(FILE); + # Set new values in cache file + open(FILE,">".$cacheFile); + print FILE $currentTime.";".$currentValue; + close(FILE); + } else { + #If the file doesnt exist, a new file is created and values are inserted + unless (open(FILE,">".$cacheFile)){ + print "Check temporary file's rights : ".$cacheFile."...\n"; + exit $ERRORS{"UNKNOWN"}; + } + my $currentTime = time(); + print FILE $currentTime.";".$currentValue; + print("Buffer in creation . . . please wait \n"); + close(FILE); + exit $ERRORS{"OK"}; + } } #=== Plugin return ==== if (defined($currentValue)){ - my $returnValue = $currentValue; - my $status = "UNKNOWN"; - my $state= "unknownState"; - $returnValue = 7 if($OPTION{'debug'}); - - - ############################################################### - # If personnal tresholds are set for warning and / or critical # - ############################################################### - if ($OPTION{'warning_table'} || $OPTION{'critical_table'} || $OPTION{'ok_table'}) { - - print "Mode personal threshold ON \n" if($OPTION{'debug'}); - - if ($OPTION{'ok_table'}) { - my $max_ok= scalar(@ok_table); - my $i = 0; - - while ($i < $max_ok) { - print "OK[$i]: $ok_table[$i] / returnValue = $returnValue \n" if($OPTION{'debug'}); - if($ok_table[$i] == $returnValue) { - $status = "OK"; - $state = $ok_table[$i+1]; - } - $i = $i+2; - } - } - if ($OPTION{'warning_table'}){ - my $max_warn= scalar(@warning_table); - my $i = 0; - - while ($i < $max_warn) { - print "Warning[$i]: $warning_table[$i] / returnValue = $returnValue \n" if($OPTION{'debug'}); - if($warning_table[$i] == $returnValue) { - $status = "WARNING"; - $state = $warning_table[$i+1]; - } - $i = $i+2; - } - } - if ($OPTION{'critical_table'}) { - my $i = 0; - my $max_crit= scalar(@critical_table); - while ($i < $max_crit) { - print "Critical[$i] = $critical_table[$i] / returnValue = $returnValue \n" if($OPTION{'debug'}); - if ($critical_table[$i] == $returnValue) { - $status = "CRITICAL"; - $state = $critical_table[$i+1]; - } - $i = $i +2; - } + my $returnValue = $currentValue; + my $status = "UNKNOWN"; + my $state= "unknownState"; + $returnValue = 7 if($OPTION{'debug'}); - } - print(" Statut = $status \n ") if($OPTION{'debug'}); - printf($output."\n",$state,$returnValue); - exit $ERRORS{$status}; - } + ############################################################### + # If personnal tresholds are set for warning and / or critical # + ############################################################### + if ($OPTION{'warning_table'} || $OPTION{'critical_table'} || $OPTION{'ok_table'}) { - if ($DS_type eq 'COUNTER') { - #calculate value for counter metric type - # if counter has been reseted between 2 checks - if ($currentValue - $previousValue < 0) { - if (defined($OPTION{'64-bits'})) { - $returnValue = ((18446744073709551616) - $previousValue + $currentValue)/($currentTime - $previousTime); - } else { - $returnValue = ((4294967296) - $previousValue + $currentValue) / ($currentTime - $previousTime); - } - } else { - $returnValue = ($currentValue - $previousValue) / ($currentTime - $previousTime); - } - } - - my $i =0; - my $perfdata = $returnValue; - while ($returnValue > $OPTION{'base'}) { - $returnValue = $returnValue / $OPTION{'base'}; - $i++; - } - - if($OPTION{'base'} == 1024){ - $prefix = "ki" if($i == 1); - $prefix = "Mi" if($i == 2); - $prefix = "Gi" if($i == 3); - $prefix = "Ti" if($i == 4); - } - if($OPTION{'base'} == 1000){ - $prefix = "k" if($i == 1); - $prefix = "M" if($i == 2); - $prefix = "G" if($i == 3); - $prefix = "T" if($i == 4); - } - if(defined($OPTION{'convert'})){ - $warning = ($OPTION{'warning'} * $max)/100; - $critical = ($OPTION{'critical'} * $max)/100; - } else { - $warning = $OPTION{'warning'}; - $critical = $OPTION{'critical'}; - } - + print "Mode personal threshold ON \n" if($OPTION{'debug'}); + if ($OPTION{'ok_table'}) { + my $max_ok= scalar(@ok_table); + my $i = 0; + + while ($i < $max_ok) { + print "OK[$i]: $ok_table[$i] / returnValue = $returnValue \n" if($OPTION{'debug'}); + if($ok_table[$i] == $returnValue) { + $status = "OK"; + $state = $ok_table[$i+1]; + } + $i = $i+2; + } + } + if ($OPTION{'warning_table'}){ + my $max_warn= scalar(@warning_table); + my $i = 0; + + while ($i < $max_warn) { + print "Warning[$i]: $warning_table[$i] / returnValue = $returnValue \n" if($OPTION{'debug'}); + if($warning_table[$i] == $returnValue) { + $status = "WARNING"; + $state = $warning_table[$i+1]; + } + $i = $i+2; + } + } + if ($OPTION{'critical_table'}) { + my $i = 0; + my $max_crit= scalar(@critical_table); + while ($i < $max_crit) { + print "Critical[$i] = $critical_table[$i] / returnValue = $returnValue \n" if($OPTION{'debug'}); + if ($critical_table[$i] == $returnValue) { + $status = "CRITICAL"; + $state = $critical_table[$i+1]; + } + $i = $i +2; + } + } + print(" Statut = $status \n ") if($OPTION{'debug'}); + printf($output."\n",$state,$returnValue); + exit $ERRORS{$status}; + } + + if ($DS_type eq 'COUNTER') { + #calculate value for counter metric type + # if counter has been reseted between 2 checks + if ($currentValue - $previousValue < 0) { + if (defined($OPTION{'64-bits'})) { + $returnValue = ((18446744073709551616) - $previousValue + $currentValue)/($currentTime - $previousTime); + } else { + $returnValue = ((4294967296) - $previousValue + $currentValue) / ($currentTime - $previousTime); + } + } else { + $returnValue = ($currentValue - $previousValue) / ($currentTime - $previousTime); + } + } + + my $i =0; + my $perfdata = $returnValue; + while ($returnValue > $OPTION{'base'}) { + $returnValue = $returnValue / $OPTION{'base'}; + $i++; + } + + if($OPTION{'base'} == 1024){ + $prefix = "ki" if($i == 1); + $prefix = "Mi" if($i == 2); + $prefix = "Gi" if($i == 3); + $prefix = "Ti" if($i == 4); + } + if($OPTION{'base'} == 1000){ + $prefix = "k" if($i == 1); + $prefix = "M" if($i == 2); + $prefix = "G" if($i == 3); + $prefix = "T" if($i == 4); + } + if(defined($OPTION{'convert'})){ + $warning = ($OPTION{'warning'} * $max)/100; + $critical = ($OPTION{'critical'} * $max)/100; + } else { + $warning = $OPTION{'warning'}; + $critical = $OPTION{'critical'}; + } if ($perfdata < $warning) { - printf("OK :".$output." ".$prefix." ".$unit."|".$metricsname."=".$perfdata.$unit.";".$warning.";".$critical.";".$min.";".$max."\n",$returnValue); - $status = "OK"; - exit $ERRORS{$status}; + printf("OK :".$output." ".$prefix." ".$unit."|".$metricsname."=".$perfdata.$unit.";".$warning.";".$critical.";".$min.";".$max."\n",$returnValue); + $status = "OK"; + exit $ERRORS{$status}; } elsif ($perfdata >= $warning && $perfdata < $critical) { - printf("WARNING :".$output." ".$prefix.$unit."|".$metricsname."=".$perfdata.$unit.";".$warning.";".$critical.";".$min.";".$max."\n",$returnValue); - $status = "WARNING"; - exit $ERRORS{$status}; + printf("WARNING :".$output." ".$prefix.$unit."|".$metricsname."=".$perfdata.$unit.";".$warning.";".$critical.";".$min.";".$max."\n",$returnValue); + $status = "WARNING"; + exit $ERRORS{$status}; } elsif ($perfdata >= $critical) { - printf("CRITICAL :".$output." ".$prefix.$unit."|".$metricsname."=".$perfdata.$unit.";".$warning.";".$critical.";".$min.";".$max."\n",$returnValue); - $status = "CRITICAL"; - exit $ERRORS{$status}; + printf("CRITICAL :".$output." ".$prefix.$unit."|".$metricsname."=".$perfdata.$unit.";".$warning.";".$critical.";".$min.";".$max."\n",$returnValue); + $status = "CRITICAL"; + exit $ERRORS{$status}; } } else { print "CRITICAL Host unavailable\n"; @@ -394,43 +369,47 @@ if (defined($currentValue)){ } # Define Common functions - sub print_usage () { print "Usage:"; print "$PROGNAME\n"; - print " -H (--hostname) \t Hostname to query - (required)\n"; - print " -C (--community) \t SNMP read community (defaults to public,\n"; - print " \t \t used with SNMP v1 and v2c\n"; - print " -v (--snmp_version) \t 1 for SNMP v1 (default)\n"; - print " \t 2 for SNMP v2c\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 (--key) snmp V3 key\n"; + print " -u (--username) snmp V3 username \n"; + print " -p (--password) snmp V3 password\n"; + print " --authprotocol protocol MD5/SHA1 (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 " -t (--type) \t Data Source Type (GAUGE or COUNTER) (GAUGE by default)\n"; print " -o (--oid) \t OID to check\n"; - print " -u (--username) \t snmp v3 username \n"; - print " -a (--authprotocol) \t protocol MD5/SHA1 (v3)\n"; - print " -A (--authpassword) \t password (v3) \n"; - print " -x (--privprotocol) \t encryption system (DES/AES)(v3) \n"; - print " -X (--privpassword)\t passphrase (v3) \n"; print " -w (--warning) \t Warning level \n"; print " -c (--critical) \t Critical level \n"; - print " -W (--wtreshold) \t Personal warning threshold : -W 1,normal,... \n"; - print " -T (--ctreshold) \t Personal critical threshold : -T 3,notResponding,4,NotFunctionning,... \n"; - print " --convert \t \t If critical and warning have to be converted regarding to the max value \n"; - print " -m (--metric) \t Metric Name\n"; - print " --64-bits \t \t If counter type to use = 64-bits \n"; - print " -U (--unit) \t Metric's unit ( /!\\ for % write %% ) \n"; - print " -f (--output) \t Output format (ex : -f \"My metric's percentage value = %f %%\" \n"; - print " --min \t \t min value for the metric (default = 0) \n"; - print " --max \t \t max value for the metric (default = 0)\n"; - print " --base \t \t will divide the returned number by base until it's inferior to it. \n"; + print " -W (--wtreshold) \t Personal warning threshold : -W 1,normal,... \n"; + print " -T (--ctreshold) \t Personal critical threshold : -T 3,notResponding,4,NotFunctionning,... \n"; + print " --convert \t \t If critical and warning have to be converted regarding to the max value \n"; + print " -m (--metric) \t Metric Name\n"; + print " -U (--unit) \t Metric's unit ( /!\\ for % write %% ) \n"; + print " -f (--output) \t Output format (ex : -f \"My metric's percentage value = %f %%\" \n"; + print " --min \t \t min value for the metric (default = 0) \n"; + print " --max \t \t max value for the metric (default = 0)\n"; + print " --base \t \t will divide the returned number by base until it's inferior to it. \n"; print " \t \t ex: 2000000 in base 1000 will be transformed to 2M (default 1000) \n"; print " -O (--ctreshold) \t Personal critical threshold : -O okstate1,okstate2... \n"; - print " -V (--version) \t Plugin version\n"; + print " -V (--version) \t Plugin version\n"; print " -h (--help) \t usage help\n"; } sub print_help () { print "##############################################\n"; - print "# Copyright (c) 2004-2011 Centreon #\n"; + print "# Copyright (c) 2004-2013 Centreon #\n"; print "# Bugs to http://forge.centreon.com/ #\n"; print "##############################################\n"; print_usage(); diff --git a/centreon-plugins/src/check_centreon_snmp_value_table.pl b/centreon-plugins/src/check_centreon_snmp_value_table.pl index f00a388a7..0f734a0eb 100644 --- a/centreon-plugins/src/check_centreon_snmp_value_table.pl +++ b/centreon-plugins/src/check_centreon_snmp_value_table.pl @@ -1,6 +1,6 @@ #! /usr/bin/perl -w ################################################################################ -# Copyright 2004-2011 MERETHIS +# Copyright 2004-2013 MERETHIS # Centreon is developped by : Julien Mathis and Romain Le Merlus under # GPL Licence 2.0. # @@ -47,49 +47,56 @@ require "@NAGIOS_PLUGINS@/Centreon/SNMP/Utils.pm"; my $PROGNAME = "$0"; -my %OPTION = ('host' => undef, 'help' => undef, 'warning' => '0', 'critical' => '0', - 'snmpversion' => 1, 'display' => 0, 'snmpcomm' => 'public', 'min' => 0, 'max' => 0, - 'host' => undef,'username' => undef, 'authpassword' => undef, 'authprotocol' => undef, - 'privprotocol' => undef , 'privpassword' => undef, 'snmpport' => 161, 'type' => 'GAUGE', 'base' => 1000, - 'output' => '%.02f', 'metric' =>'value', 'unit' => 'nounit', 'divide' => 1); +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, + "64-bits" => undef, + 'help' => undef, 'warning' => '0', 'critical' => '0', + 'display' => 0, 'min' => 0, 'max' => 0, + 'type' => 'GAUGE', 'base' => 1000, + 'output' => '%.02f', 'metric' =>'value', 'unit' => 'nounit', 'divide' => 1); my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3); my $prefix = ""; - - sub print_help (); sub print_usage (); Getopt::Long::Configure('bundling'); GetOptions - ("h" => \$OPTION{'help'}, "help" => \$OPTION{'help'}, - "P=s" => \$OPTION{'snmpport'}, "snmpport=s" => \$OPTION{'snmpport'}, - "V" => \$OPTION{'pluginversion'}, "version" => \$OPTION{'pluginversion'}, - "u=s" => \$OPTION{'username'}, "username=s" => \$OPTION{'username'}, - "a=s" => \$OPTION{'authprotocol'}, "authprotocol=s" => \$OPTION{'authprotocol'}, - "A=s" => \$OPTION{'authpassword'}, "authpassword=s" => \$OPTION{'authpassword'}, - "x=s" => \$OPTION{'privprotocol'}, "privprotocol=s" => \$OPTION{'privprotocol'}, - "X=s" => \$OPTION{'privpassword'}, "privpassword=s" => \$OPTION{'privpassword'}, - "v=s" => \$OPTION{'snmpversion'}, "snmp=s" => \$OPTION{'snmpversion'}, - "C=s" => \$OPTION{'snmpcomm'}, "community=s" => \$OPTION{'snmpcomm'}, - "w=s" => \$OPTION{'warning'}, "warning=s" => \$OPTION{'warning'}, - "c=s" => \$OPTION{'critical'}, "critical=s" => \$OPTION{'critical'}, - "H=s" => \$OPTION{'host'}, "host=s" => \$OPTION{'host'}, - "o=s" => \$OPTION{'oid'}, "oid=s" => \$OPTION{'oid'}, - "t=s" => \$OPTION{'type'}, "type=s" => \$OPTION{'type'}, - "U=s" => \$OPTION{'unit'}, "unit=s" => \$OPTION{'unit'}, - "W=s" => \$OPTION{'warning_table'}, "warning_table=s" => \$OPTION{'warning_table'}, - "T=s" => \$OPTION{'critical_table'}, "critical_table=s" => \$OPTION{'critical_table'}, - "O=s" => \$OPTION{'ok_table'}, "ok_table=s" => \$OPTION{'ok_table'}, - "convert" => \$OPTION{'convert'}, - "debug" => \$OPTION{'debug'}, - "min=s" => \$OPTION{'min'}, - "max=s" => \$OPTION{'max'}, - "base=s" => \$OPTION{'base'}, - "64-bits" => \$OPTION{'64-bits'}, - "f=s" => \$OPTION{'output'}, "output=s" => \$OPTION{'output'}, - "m=s" => \$OPTION{'metric'}, "metric=s" => \$OPTION{'metric'}); - + ( + "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'}, + "64-bits" => \$OPTION{'64-bits'}, + "h" => \$OPTION{'help'}, "help" => \$OPTION{'help'}, + "V" => \$OPTION{'pluginversion'}, "version" => \$OPTION{'pluginversion'}, + "w=s" => \$OPTION{'warning'}, "warning=s" => \$OPTION{'warning'}, + "c=s" => \$OPTION{'critical'}, "critical=s" => \$OPTION{'critical'}, + "o=s" => \$OPTION{'oid'}, "oid=s" => \$OPTION{'oid'}, + "t=s" => \$OPTION{'type'}, "type=s" => \$OPTION{'type'}, + "U=s" => \$OPTION{'unit'}, "unit=s" => \$OPTION{'unit'}, + "W=s" => \$OPTION{'warning_table'}, "warning_table=s" => \$OPTION{'warning_table'}, + "T=s" => \$OPTION{'critical_table'}, "critical_table=s" => \$OPTION{'critical_table'}, + "O=s" => \$OPTION{'ok_table'}, "ok_table=s" => \$OPTION{'ok_table'}, + "convert" => \$OPTION{'convert'}, + "debug" => \$OPTION{'debug'}, + "min=s" => \$OPTION{'min'}, + "max=s" => \$OPTION{'max'}, + "base=s" => \$OPTION{'base'}, + "f=s" => \$OPTION{'output'}, "output=s" => \$OPTION{'output'}, + "m=s" => \$OPTION{'metric'}, "metric=s" => \$OPTION{'metric'}); my $metricsname = undef; my $unit = undef; @@ -112,439 +119,406 @@ my @ok_table = (); #Table used when personnal threshold are set if($OPTION{'critical_table'}){ - @critical_table = split(/\,/, $OPTION{'critical_table'}); + @critical_table = split(/\,/, $OPTION{'critical_table'}); } if($OPTION{'warning_table'}){ - @warning_table = split(/\,/, $OPTION{'warning_table'}); + @warning_table = split(/\,/, $OPTION{'warning_table'}); } if($OPTION{'ok_table'}){ - @ok_table = split(/\,/, $OPTION{'ok_table'}); + @ok_table = split(/\,/, $OPTION{'ok_table'}); } -if (!$OPTION{'oid'}) { - print "Option -o needed.\n \n"; - print_usage(); - exit $ERRORS{'UNKNOWN'}; -} elsif (!($OPTION{'oid'} =~ /^[0-9\.]+$/)) { - print "Wrong OID format\n"; - exit $ERRORS{'UNKNOWN'}; +if (!defined($OPTION{'oid'})) { + print "Missing parameters -o (--oid')\n"; + exit $ERRORS{'UNKNOWN'}; +} +if (!($OPTION{'oid'} =~ /^[0-9\.]+$/)) { + print "Wrong OID format\n"; + exit $ERRORS{'UNKNOWN'}; } - if (defined($OPTION{'pluginversion'})) { - print("$PROGNAME 1.2"); - exit $ERRORS{'UNKNOWN'}; + print("$PROGNAME 1.2"); + exit $ERRORS{'UNKNOWN'}; } if (defined($OPTION{'help'})) { - print_help(); - exit $ERRORS{'UNKNOWN'}; + print_help(); + exit $ERRORS{'UNKNOWN'}; } -if (!$OPTION{'host'}) { - print_usage(); - exit $ERRORS{'UNKNOWN'}; -} - -my $cacheFile = "@CENTPLUGINS_TMP@/snmp_value_table".$OPTION{'host'}."-".$OPTION{'oid'}; #Store option values in simpler variables if($OPTION{'divide'} ne "" && $OPTION{'metric'} ne "" && $OPTION{'unit'} ne "" && $OPTION{'output'} ne "" && $OPTION{'min'} ne "" && $OPTION{'max'} ne "" ){ - $metricsname = $OPTION{'metric'}; - $unit = $OPTION{'unit'}; - - #Output Verification? - $output = $OPTION{'output'}; - - #check parameter format - - if ($OPTION{'base'} !~ /^[0-9]*\.?[0-9]*$/) { - print(" Base option should be a numerical \n"); - exit $ERRORS{'UNKNOWN'}; - } - - if ($OPTION{'min'} !~ /^[0-9]*\.?[0-9]*$/) { - print(" Min option should be a numerical \n"); - exit $ERRORS{'UNKNOWN'}; - } - if ($OPTION{'max'} !~ /^[0-9]*\.?[0-9]*$/){ - print(" Max option should be a numerical \n"); - exit $ERRORS{'UNKNOWN'}; - } + $metricsname = $OPTION{'metric'}; + $unit = $OPTION{'unit'}; - if ($OPTION{'warning'} !~ /^[0-9]*\.?[0-9]*$/ || $OPTION{'critical'} !~ /^[0-9]*\.?[0-9]*$/) { - print(" Option warning &/or critical should be numerical \n"); - exit $ERRORS{'UNKNOWN'}; - } - - $min = $OPTION{'min'}; - $max = $OPTION{'max'}; - $divide = $OPTION{'divide'}; - -}else{ - print("One or more arguments are not set \n"); - exit $ERRORS{'UNKNOWN'}; + #Output Verification? + $output = $OPTION{'output'}; + + #check parameter format + if ($OPTION{'base'} !~ /^[0-9]*\.?[0-9]*$/) { + print(" Base option should be a numerical \n"); + exit $ERRORS{'UNKNOWN'}; + } + + if ($OPTION{'min'} !~ /^[0-9]*\.?[0-9]*$/) { + print(" Min option should be a numerical \n"); + exit $ERRORS{'UNKNOWN'}; + } + if ($OPTION{'max'} !~ /^[0-9]*\.?[0-9]*$/){ + print(" Max option should be a numerical \n"); + exit $ERRORS{'UNKNOWN'}; + } + + if ($OPTION{'warning'} !~ /^[0-9]*\.?[0-9]*$/ || $OPTION{'critical'} !~ /^[0-9]*\.?[0-9]*$/) { + print(" Option warning &/or critical should be numerical \n"); + exit $ERRORS{'UNKNOWN'}; + } + + $min = $OPTION{'min'}; + $max = $OPTION{'max'}; + $divide = $OPTION{'divide'}; +} else { + print("One or more arguments are not set \n"); + exit $ERRORS{'UNKNOWN'}; } -#Check if version passed in option exists -$OPTION{'snmpversion'} =~ s/v//g; -exit $ERRORS{'UNKNOWN'} if (!Centreon::SNMP::Utils->checkVersion($OPTION{'snmpversion'})); - -#Check which connection mode is used -my $sessionType = 1; -if ($OPTION{'snmpversion'} =~ /3/) { - $sessionType = Centreon::SNMP::Utils->checkSessiontype($OPTION{'username'},$OPTION{'authprotocol'},$OPTION{'authpassword'},$OPTION{'privprotocol'},$OPTION{'privpassword'}); - exit $ERRORS{'UNKNOWN'} if(!$sessionType); -} - - - my $DS_type = "GAUGE"; if ($OPTION{'type'} =~ m/GAUGE/i) { - $DS_type = "GAUGE"; + $DS_type = "GAUGE"; } elsif ($OPTION{'type'} =~ m/COUNTER/i) { - $DS_type = "COUNTER"; + $DS_type = "COUNTER"; } my $critical = $1 if ($OPTION{'critical'} =~ /([0-9]+)/); my $warning = $1 if ($OPTION{'warning'} =~ /([0-9]+)/); if ($critical < $warning){ - print "(--critical) must be superior or equal to (--warning)"; - print_usage(); - exit $ERRORS{'UNKNOWN'}; + print "(--critical) must be superior or equal to (--warning)"; + print_usage(); + exit $ERRORS{'UNKNOWN'}; } +my ($session_params) = Centreon::SNMP::Utils::check_snmp_options($ERRORS{'UNKNOWN'}, \%OPTION); +my $session = Centreon::SNMP::Utils::connection($ERRORS{'UNKNOWN'}, $session_params); - - -# Plugin snmp connection -my ($session); -if (!($session = Centreon::SNMP::Utils->connection($sessionType,\%OPTION))){ - exit $ERRORS{'UNKNOWN'}; -} +my $cacheFile = "@CENTPLUGINS_TMP@/snmp_value_table".$OPTION{'host'}."-".$OPTION{'oid'}; my @oid_walk; #will contain each path to the final part of OID my @oid_list; #Will contain all final OID of the get_table request my @cache; #Will contain cache file values my %previousValues; my %currentValues; -my $i =0; +my $i = 0; #Get the different finals OID returned by OID -my $result = $session->get_table(Baseoid => $OPTION{'oid'}); +my $result = Centreon::SNMP::Utils::get_snmp_table($OPTION{'oid'}, $session, $ERRORS{'UNKNOWN'}, \%OPTION); $currentTime = time(); -if (!defined($result)) { - printf("UNKNOWN: %s.\n", $session->error); - $session->close; - exit $ERRORS{'UNKNOWN'}; -} - foreach my $key (oid_lex_sort(keys %$result)) { - @oid_walk = split (/\./,$key); - $oid_list[$i] = pop(@oid_walk); - $i++; + @oid_walk = split (/\./,$key); + $oid_list[$i] = pop(@oid_walk); + $i++; } if ($OPTION{'debug'}) { - my $size= scalar(@oid_list); - print(" OID received : \n"); - for ($i = 0; $i < $size ; $i++) { - print("oid_list[$i] : $oid_list[$i] \n"); - } + my $size= scalar(@oid_list); + print(" OID received : \n"); + for ($i = 0; $i < $size ; $i++) { + print("oid_list[$i] : $oid_list[$i] \n"); + } } - #If metric type = counter, use the cache file to get the last value (or create cache file) if ($DS_type eq 'COUNTER') { - print("COUNTER \n") if($OPTION{'debug'}); + print("COUNTER \n") if($OPTION{'debug'}); - #If file exist - if (-e $cacheFile) { - open(FILE,"<".$cacheFile); - my $row = ; - @cache = split(/;/, $row); - my $size= scalar(@cache); + #If file exist + if (-e $cacheFile) { + open(FILE,"<".$cacheFile); + my $row = ; + @cache = split(/;/, $row); + my $size= scalar(@cache); - print("File exist \n") if($OPTION{'debug'}); - $previousTime= $cache[0]; + print("File exist \n") if($OPTION{'debug'}); + $previousTime= $cache[0]; - #Get the previous values stored in cachefile - for ($i = 1; $i < $size ; $i=$i+2) { - print("OID : $cache[$i] ; Last Value : $cache[$i+1] ; \n") if($OPTION{'debug'}); - $previousValues{$cache[$i]} = $cache[$i+1]; - } + #Get the previous values stored in cachefile + for ($i = 1; $i < $size ; $i=$i+2) { + print("OID : $cache[$i] ; Last Value : $cache[$i+1] ; \n") if($OPTION{'debug'}); + $previousValues{$cache[$i]} = $cache[$i+1]; + } - close(FILE); - - #Set new values in cache file - open(FILE,">".$cacheFile); - $size= scalar(@oid_list); - print FILE $currentTime.";"; + close(FILE); - for ($i = 0; $i < $size ; $i++) { - #Get current value for all oid at i position in table of all oid - my $result = $session->get_request(-varbindlist => [$OPTION{'oid'}.".".$oid_list[$i]]); - $currentValue = $result->{$OPTION{'oid'}.".".$oid_list[$i]}; + #Set new values in cache file + open(FILE,">".$cacheFile); + $size= scalar(@oid_list); + print FILE $currentTime.";"; - $currentValues{$oid_list[$i]} = $currentValue; - print FILE $oid_list[$i].";".$currentValue.";"; - print("oid_list[$i] : $oid_list[$i] ; Current Value : $currentValue ; \n") if($OPTION{'debug'}); - } - close(FILE); - } else { - my $size= scalar(@oid_list); - print("File doesn't exist \n") if($OPTION{'debug'}); - #If the file doesnt exist, a new file is created and values are inserted - unless (open(FILE,">".$cacheFile)) { - print "Check temporary file's or existence rights : ".$cacheFile."...\n"; - exit $ERRORS{"UNKNOWN"}; - } - - $i = 0; - print FILE $currentTime.";"; - for ($i = 0; $i < $size ; $i++) { - my $result = $session->get_request(-varbindlist => [$OPTION{'oid'}.".".$oid_list[$i]]); - $currentValue = $result->{$OPTION{'oid'}.".".$oid_list[$i]}; - print FILE $oid_list[$i].";".$currentValue.";"; - print("OID : $oid_list[$i] ; Current Value : $currentValue ; \n") if($OPTION{'debug'}); - } + for ($i = 0; $i < $size ; $i++) { + #Get current value for all oid at i position in table of all oid + my $result = Centreon::SNMP::Utils::get_snmp_leef([$OPTION{'oid'}.".".$oid_list[$i]], $session, $ERRORS{'UNKNOWN'}); + $currentValue = $result->{$OPTION{'oid'}.".".$oid_list[$i]}; - print("Buffer in creation . . . please wait \n"); - close(FILE); - exit $ERRORS{"OK"}; - } + $currentValues{$oid_list[$i]} = $currentValue; + print FILE $oid_list[$i].";".$currentValue.";"; + print("oid_list[$i] : $oid_list[$i] ; Current Value : $currentValue ; \n") if($OPTION{'debug'}); + } + close(FILE); + } else { + my $size= scalar(@oid_list); + print("File doesn't exist \n") if($OPTION{'debug'}); + #If the file doesnt exist, a new file is created and values are inserted + unless (open(FILE,">".$cacheFile)) { + print "Check temporary file's or existence rights : ".$cacheFile."...\n"; + exit $ERRORS{"UNKNOWN"}; + } + + $i = 0; + print FILE $currentTime.";"; + for ($i = 0; $i < $size ; $i++) { + my $result = Centreon::SNMP::Utils::get_snmp_leef([$OPTION{'oid'}.".".$oid_list[$i]], $session, $ERRORS{'UNKNOWN'}); + $currentValue = $result->{$OPTION{'oid'}.".".$oid_list[$i]}; + print FILE $oid_list[$i].";".$currentValue.";"; + print("OID : $oid_list[$i] ; Current Value : $currentValue ; \n") if($OPTION{'debug'}); + } + + print("Buffer in creation . . . please wait \n"); + close(FILE); + exit $ERRORS{"OK"}; + } } else { - #Get values for each OID stored in table oid_list[] if not a counter type - my $size= scalar(@oid_list); - print("Values for each OID : \n") if($OPTION{'debug'}); - for ($i = 0; $i < $size ; $i++) { - my $result = $session->get_request(-varbindlist => [$OPTION{'oid'}.".".$oid_list[$i]]); - $currentValue = $result->{$OPTION{'oid'}.".".$oid_list[$i]}; + #Get values for each OID stored in table oid_list[] if not a counter type + my $size= scalar(@oid_list); + print("Values for each OID : \n") if($OPTION{'debug'}); + for ($i = 0; $i < $size ; $i++) { + my $result = Centreon::SNMP::Utils::get_snmp_leef([$OPTION{'oid'}.".".$oid_list[$i]], $session, $ERRORS{'UNKNOWN'}); + $currentValue = $result->{$OPTION{'oid'}.".".$oid_list[$i]}; - $currentValues{$oid_list[$i]} = $currentValue; - print("oid_list[$i] : $oid_list[$i] ; Current Value : $currentValue ; \n") if($OPTION{'debug'}); - } + $currentValues{$oid_list[$i]} = $currentValue; + print("oid_list[$i] : $oid_list[$i] ; Current Value : $currentValue ; \n") if($OPTION{'debug'}); + } } print("Taille \%currentValues : ".keys(%currentValues)."\n") if($OPTION{'debug'}); #=== Plugin returned value treatments ==== if (keys(%currentValues) > 0) { - my $status = "UNKNOWN"; - my $state= "unknownState"; - my %returnValues = %currentValues; - my %state; - my $returnValue; - my $size= keys(%currentValues); - my $output_message=""; - my $output_warning=""; - my $output_critical=""; - my $perf_output_message = "|"; - - #If personnal tresholds are set - if($OPTION{'warning_table'} || $OPTION{'critical_table'} || $OPTION{'ok_table'}) { - print "Mode personal threshold ON \n" if($OPTION{'debug'}); - - if($OPTION{'ok_table'}) { - my $max_ok= scalar(@ok_table); - my $i = 0; - - while($i < $max_ok) { - for(my $u=0; $u < $size;$u++) { - print "OK[$i]: $ok_table[$i] / returnValue = $returnValues{$oid_list[$i]} \n" if($OPTION{'debug'}); - if($ok_table[$i] == $returnValues{$oid_list[$u]}) { - $status = "OK"; - $state{$oid_list[$i]} = $ok_table[$i+1]; - } - } - $i = $i+2; - } - } - if($OPTION{'warning_table'}) { - my $max_warn= scalar(@warning_table); - my $i = 0; - while($i < $max_warn) { - for(my $u=0; $u < $size;$u++) { - print "Warning[$i]: $warning_table[$i] / returnValue = $returnValues{$oid_list[$u]} \n" if($OPTION{'debug'}); - if($warning_table[$i] == $returnValues{$oid_list[$u]}) { - print("Warning match \n") if($OPTION{'debug'}); - $status = "WARNING"; - $state{$oid_list[$u]} = $warning_table[$i+1]; - $output_warning .= "OID.".$oid_list[$u].": ".$warning_table[$i+1]."; "; - } - } - $i = $i+2; - } - $output_warning = "(Warning)".$output_warning if($status eq "WARNING"); - } - if($OPTION{'critical_table'}) { - my $i = 0; - my $max_crit= scalar(@critical_table); - while($i < $max_crit){ - - for(my $u=0; $u < $size;$u++){ - print "Critical[$i] = $critical_table[$i] / returnValue = $returnValues{$oid_list[$u]} \n" if($OPTION{'debug'}); - if($critical_table[$i] == $returnValues{$oid_list[$u]}) { - print("Critical match \n") if($OPTION{'debug'}); - $status = "CRITICAL"; - $state{$oid_list[$u]} = $critical_table[$i+1]; - $output_critical .= "OID.".$oid_list[$u].": ".$critical_table[$i+1]."; "; - } - } - $i = $i + 2; - } - $output_critical = "(Critical) ".$output_critical if($status eq "CRITICAL"); - } - if($status eq "OK") { - print("All values of table $OPTION{'oid'} are OK \n"); - exit $ERRORS{"OK"}; - } elsif($status eq "UNKNOWN") { - print("Unknown : None of the values given in argument match with OID values \n"); - exit $ERRORS{$status}; - } else { - print($output_critical." ".$output_warning."\n"); - exit $ERRORS{$status}; - } - } + my $status = "UNKNOWN"; + my $state= "unknownState"; + my %returnValues = %currentValues; + my %state; + my $returnValue; + my $size= keys(%currentValues); + my $output_message=""; + my $output_warning=""; + my $output_critical=""; + my $perf_output_message = "|"; - #calculate value for counter metric type - #if counter has been reseted between 2 checks - if ($DS_type eq 'COUNTER') { - for (my $i = 0; $i < $size ; $i++) { - if ($currentValues{$oid_list[$i]} - $previousValues{$oid_list[$i]} < 0) { - if (defined($OPTION{'64-bits'})) { - $returnValues{$oid_list[$i]} = ((18446744073709551616) - $previousValues{$oid_list[$i]} + $currentValues{$oid_list[$i]})/($currentTime - $previousTime); - } else { - $returnValues{$oid_list[$i]} = ((4294967296) - $previousValues{$oid_list[$i]} + $currentValues{$oid_list[$i]})/( $currentTime - $previousTime ); - } - } else { - $returnValues{$oid_list[$i]} = ($currentValues{$oid_list[$i]} - $previousValues{$oid_list[$i]}) / ( $currentTime - $previousTime ); - } - } - } - - if($OPTION{'debug'}) { - for ( my $i = 0; $i < $size ; $i++) { - print(" ValuetoReturn OiD: $oid_list[$i] : $returnValues{$oid_list[$i]} \n"); - } - } - - #Set in Ok Critical and Warning hashes the value corresponding to the different status. - #Finally, if Critical and Warning are empty, the plugin return OK, else , the plugin returns the value in Critical and Warning hashes - my %Ok; - my %Critical; - my %Warning; - my %perfdata; - - if(defined($OPTION{'convert'})) { - $warning = ($OPTION{'warning'} * $max)/100; - $critical = ($OPTION{'critical'} * $max)/100; - } else { - $warning = $OPTION{'warning'}; - $critical = $OPTION{'critical'}; - } - - #Test for each OID if the value returned is warning / critical or ok - for (my $i = 0; $i < $size ; $i++) { - if ($returnValues{$oid_list[$i]} < $warning) { - $Ok{$oid_list[$i]} = $returnValues{$oid_list[$i]}; - } elsif ($warning != 0 && $warning <= $returnValues{$oid_list[$i]} && $returnValues{$oid_list[$i]} < $critical) { - $Warning{$oid_list[$i]} = $returnValues{$oid_list[$i]}; - }elsif($critical != 0 && $returnValues{$oid_list[$i]} >= $critical){ - $Critical{$oid_list[$i]} = $returnValues{$oid_list[$i]}; - } - } - - if($OPTION{'debug'}){ - print("%Ok size : ".keys(%Ok)." \t %Warning size : ".keys(%Warning)." \t %Critical size : ".keys(%Critical)." \n"); - } - - if(keys(%Critical) == 0 && keys(%Warning) == 0) { - $output_message = "All values of table $OPTION{'oid'} are OK"; - $state = "OK"; - } + #If personnal tresholds are set + if($OPTION{'warning_table'} || $OPTION{'critical_table'} || $OPTION{'ok_table'}) { + print "Mode personal threshold ON \n" if($OPTION{'debug'}); - $i = 0; - %perfdata = %returnValues; - #Formating the data for the output message - for (my $i = 0; $i < $size ; $i++){ - my $u = 0; - $prefix = ""; - if($warning != 0 && $returnValues{$oid_list[$i]} > $warning){ - while($returnValues{$oid_list[$i]} > $OPTION{'base'}) - { - $returnValues{$oid_list[$i]} = $returnValues{$oid_list[$i]} / $OPTION{'base'}; - $u++; - } - if ($OPTION{'base'} == 1024) { - $prefix = "ki" if($u == 1); - $prefix = "Mi" if($u == 2); - $prefix = "Gi" if($u == 3); - $prefix = "Ti" if($u == 4); - } elsif($OPTION{'base'} == 1000){ - $prefix = "k" if($u == 1); - $prefix = "M" if($u == 2); - $prefix = "G" if($u == 3); - $prefix = "T" if($u == 4); - } - $output_message.=sprintf("OID.".$oid_list[$i]."=".$output." ".$prefix.$unit." ",$returnValues{$oid_list[$i]}); - } - } + if($OPTION{'ok_table'}) { + my $max_ok= scalar(@ok_table); + my $i = 0; - for (my $i = 0; $i < $size ; $i++){ - $perf_output_message .=$metricsname.".".$oid_list[$i]."=".$perfdata{$oid_list[$i]}.$unit.";".$warning.";".$critical.";".$min.";".$max." "; - } - - if(keys(%Critical)!=0) { - $state = "CRITICAL"; - }elsif(keys(%Warning)!=0) { - $state = "WARNING"; - } - print($output_message.$perf_output_message."\n"); - exit $ERRORS{$state}; + while($i < $max_ok) { + for(my $u=0; $u < $size;$u++) { + print "OK[$i]: $ok_table[$i] / returnValue = $returnValues{$oid_list[$i]} \n" if($OPTION{'debug'}); + if($ok_table[$i] == $returnValues{$oid_list[$u]}) { + $status = "OK"; + $state{$oid_list[$i]} = $ok_table[$i+1]; + } + } + $i = $i+2; + } + } + if($OPTION{'warning_table'}) { + my $max_warn= scalar(@warning_table); + my $i = 0; + while($i < $max_warn) { + for(my $u=0; $u < $size;$u++) { + print "Warning[$i]: $warning_table[$i] / returnValue = $returnValues{$oid_list[$u]} \n" if($OPTION{'debug'}); + if($warning_table[$i] == $returnValues{$oid_list[$u]}) { + print("Warning match \n") if($OPTION{'debug'}); + $status = "WARNING"; + $state{$oid_list[$u]} = $warning_table[$i+1]; + $output_warning .= "OID.".$oid_list[$u].": ".$warning_table[$i+1]."; "; + } + } + $i = $i+2; + } + $output_warning = "(Warning)".$output_warning if($status eq "WARNING"); + } + if($OPTION{'critical_table'}) { + my $i = 0; + my $max_crit= scalar(@critical_table); + while($i < $max_crit){ + for(my $u=0; $u < $size;$u++){ + print "Critical[$i] = $critical_table[$i] / returnValue = $returnValues{$oid_list[$u]} \n" if($OPTION{'debug'}); + if($critical_table[$i] == $returnValues{$oid_list[$u]}) { + print("Critical match \n") if($OPTION{'debug'}); + $status = "CRITICAL"; + $state{$oid_list[$u]} = $critical_table[$i+1]; + $output_critical .= "OID.".$oid_list[$u].": ".$critical_table[$i+1]."; "; + } + } + $i = $i + 2; + } + $output_critical = "(Critical) ".$output_critical if($status eq "CRITICAL"); + } + if($status eq "OK") { + print("All values of table $OPTION{'oid'} are OK \n"); + exit $ERRORS{"OK"}; + } elsif($status eq "UNKNOWN") { + print("Unknown : None of the values given in argument match with OID values \n"); + exit $ERRORS{$status}; + } else { + print($output_critical." ".$output_warning."\n"); + exit $ERRORS{$status}; + } + } + + #calculate value for counter metric type + #if counter has been reseted between 2 checks + if ($DS_type eq 'COUNTER') { + for (my $i = 0; $i < $size ; $i++) { + if ($currentValues{$oid_list[$i]} - $previousValues{$oid_list[$i]} < 0) { + if (defined($OPTION{'64-bits'})) { + $returnValues{$oid_list[$i]} = ((18446744073709551616) - $previousValues{$oid_list[$i]} + $currentValues{$oid_list[$i]})/($currentTime - $previousTime); + } else { + $returnValues{$oid_list[$i]} = ((4294967296) - $previousValues{$oid_list[$i]} + $currentValues{$oid_list[$i]})/( $currentTime - $previousTime ); + } + } else { + $returnValues{$oid_list[$i]} = ($currentValues{$oid_list[$i]} - $previousValues{$oid_list[$i]}) / ( $currentTime - $previousTime ); + } + } + } + + if ($OPTION{'debug'}) { + for ( my $i = 0; $i < $size ; $i++) { + print(" ValuetoReturn OiD: $oid_list[$i] : $returnValues{$oid_list[$i]} \n"); + } + } + + #Set in Ok Critical and Warning hashes the value corresponding to the different status. + #Finally, if Critical and Warning are empty, the plugin return OK, else , the plugin returns the value in Critical and Warning hashes + my %Ok; + my %Critical; + my %Warning; + my %perfdata; + + if(defined($OPTION{'convert'})) { + $warning = ($OPTION{'warning'} * $max)/100; + $critical = ($OPTION{'critical'} * $max)/100; + } else { + $warning = $OPTION{'warning'}; + $critical = $OPTION{'critical'}; + } + + #Test for each OID if the value returned is warning / critical or ok + for (my $i = 0; $i < $size ; $i++) { + if ($returnValues{$oid_list[$i]} < $warning) { + $Ok{$oid_list[$i]} = $returnValues{$oid_list[$i]}; + } elsif ($warning != 0 && $warning <= $returnValues{$oid_list[$i]} && $returnValues{$oid_list[$i]} < $critical) { + $Warning{$oid_list[$i]} = $returnValues{$oid_list[$i]}; + } elsif($critical != 0 && $returnValues{$oid_list[$i]} >= $critical){ + $Critical{$oid_list[$i]} = $returnValues{$oid_list[$i]}; + } + } + + if($OPTION{'debug'}){ + print("%Ok size : ".keys(%Ok)." \t %Warning size : ".keys(%Warning)." \t %Critical size : ".keys(%Critical)." \n"); + } + + if(keys(%Critical) == 0 && keys(%Warning) == 0) { + $output_message = "All values of table $OPTION{'oid'} are OK"; + $state = "OK"; + } + + $i = 0; + %perfdata = %returnValues; + #Formating the data for the output message + for (my $i = 0; $i < $size ; $i++){ + my $u = 0; + $prefix = ""; + if($warning != 0 && $returnValues{$oid_list[$i]} > $warning){ + while($returnValues{$oid_list[$i]} > $OPTION{'base'}) { + $returnValues{$oid_list[$i]} = $returnValues{$oid_list[$i]} / $OPTION{'base'}; + $u++; + } + if ($OPTION{'base'} == 1024) { + $prefix = "ki" if($u == 1); + $prefix = "Mi" if($u == 2); + $prefix = "Gi" if($u == 3); + $prefix = "Ti" if($u == 4); + } elsif($OPTION{'base'} == 1000){ + $prefix = "k" if($u == 1); + $prefix = "M" if($u == 2); + $prefix = "G" if($u == 3); + $prefix = "T" if($u == 4); + } + $output_message.=sprintf("OID.".$oid_list[$i]."=".$output." ".$prefix.$unit." ",$returnValues{$oid_list[$i]}); + } + } + + for (my $i = 0; $i < $size ; $i++){ + $perf_output_message .=$metricsname.".".$oid_list[$i]."=".$perfdata{$oid_list[$i]}.$unit.";".$warning.";".$critical.";".$min.";".$max." "; + } + + if(keys(%Critical)!=0) { + $state = "CRITICAL"; + } elsif(keys(%Warning)!=0) { + $state = "WARNING"; + } + print($output_message.$perf_output_message."\n"); + exit $ERRORS{$state}; } else { - print "CRITICAL Host unavailable\n"; - exit $ERRORS{'CRITICAL'}; + print "CRITICAL Host unavailable\n"; + exit $ERRORS{'CRITICAL'}; } - sub print_usage () { - print "Usage:"; - print "$PROGNAME\n"; - print " -H (--hostname) \t Hostname to query - (required)\n"; - print " -C (--community) \t SNMP read community (defaults to public,\n"; - print " \t \t used with SNMP v1 and v2c\n"; - print " -v (--snmp_version) \t 1 for SNMP v1 (default)\n"; - print " \t 2 for SNMP v2c\n"; - print " -t (--type) \t Data Source Type (GAUGE or COUNTER) (GAUGE by default)\n"; - print " -o (--oid) \t OID to check\n"; - print " -u (--username) \t snmp v3 username \n"; - print " -a (--authprotocol) \t protocol MD5/SHA1 (v3)\n"; - print " -A (--authpassword) \t password (v3) \n"; - print " -x (--privprotocol) \t encryption system (DES/AES)(v3) \n"; - print " -X (--privpassword)\t passphrase (v3) \n"; - print " -w (--warning) \t Warning level \n"; - print " -c (--critical) \t Critical level \n"; - print " -W (--wtreshold) \t Personal warning threshold : -W 1,normal,... \n"; - print " -T (--ctreshold) \t Personal critical threshold : -T 3,notResponding,4,NotFunctionning,... \n"; - print " --convert \t \t If critical and warning, given in %tages, have to be converted regarding to the max value \n"; - print " -m (--metric) \t Metric Name\n"; - print " --64-bits \t \t If counter type to use = 64-bits \n"; - print " -U (--unit) \t Metric's unit ( /!\\ for % write %% ) \n"; - print " -f (--output) \t Output format (ex : -f \"%0.2f \" \n"; - print " --min \t \t min value for the metric (default = 0) \n"; - print " --max \t \t max value for the metric (default = 0)\n"; - print " --base \t \t will divide the returned number by \"base\" until it's inferior to it. \n"; - print " \t \t ex: 2000000 in base 1000 will be transformed to 2M (default 1000) \n"; - print " -V (--version) \t Plugin version\n"; - print " -h (--help) \t usage help\n"; + print "Usage:"; + 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 (--key) snmp V3 key\n"; + print " -u (--username) snmp V3 username \n"; + print " -p (--password) snmp V3 password\n"; + print " --authprotocol protocol MD5/SHA1 (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 " -t (--type) \t Data Source Type (GAUGE or COUNTER) (GAUGE by default)\n"; + print " -o (--oid) \t OID to check\n"; + print " -w (--warning) \t Warning level \n"; + print " -c (--critical) \t Critical level \n"; + print " -W (--warning_table) \t Personal warning threshold : -W 1,normal,... \n"; + print " -T (--critical_table) \t Personal critical threshold : -T 3,notResponding,4,NotFunctionning,... \n"; + print " --convert \t \t If critical and warning, given in %tages, have to be converted regarding to the max value \n"; + print " -m (--metric) \t Metric Name\n"; + print " -U (--unit) \t Metric's unit ( /!\\ for % write %% ) \n"; + print " -f (--output) \t Output format (ex : -f \"%0.2f \" \n"; + print " --min \t \t min value for the metric (default = 0) \n"; + print " --max \t \t max value for the metric (default = 0)\n"; + print " --base \t \t will divide the returned number by \"base\" until it's inferior to it. \n"; + print " \t \t ex: 2000000 in base 1000 will be transformed to 2M (default 1000) \n"; + print " -V (--version) \t Plugin version\n"; + print " -h (--help) \t 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"; + print "##############################################\n"; + print "# Copyright (c) 2004-2013 Centreon #\n"; + print "# Bugs to http://forge.centreon.com/ #\n"; + print "##############################################\n"; + print_usage(); + print "\n"; } - -