This commit is contained in:
Quentin Garnier 2013-01-11 14:51:32 +01:00
parent 372504569d
commit 068bc3d089
5 changed files with 1008 additions and 1133 deletions

View File

@ -1,5 +1,5 @@
################################################################################ ################################################################################
# Copyright 2005-2011 MERETHIS # Copyright 2005-2013 MERETHIS
# Centreon is developped by : Julien Mathis and Romain Le Merlus under # Centreon is developped by : Julien Mathis and Romain Le Merlus under
# GPL Licence 2.0. # GPL Licence 2.0.
# #
@ -41,112 +41,154 @@ use warnings;
package Centreon::SNMP::Utils; package Centreon::SNMP::Utils;
# This method takes the version argument given by the user and sub load_oids {
# return true only if snmp version value is known my ($exit_status, $oid_file) = @_;
sub checkVersion{ eval 'require Config::IniFiles;';
my $self = shift;
my $snmpversion = shift;
$snmpversion =~ s/^v//;
if ($snmpversion !~ /1|2c|2|3/) {
print "Wrong SNMP version\n";
return 0;
if ($@) {
print "Could not load the Perl module 'Config::IniFiles' $@";
exit($exit_status);
} }
return 1; 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;
} }
# This method take the v3 parameter (username,authprotocol,authpassword,privprotocol,privpassword) in argument and return the session type sub check_snmp_options {
# sesssion type = 1 si snmp v1 / v2 my ($exit_status, $OPTION) = @_;
# session type = 2 si snmp v3 AuthNoPriv my %session_params;
# 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 (!defined($OPTION->{'host'})) {
if ($authprotocol ne "MD5" && $authprotocol ne "SHA1") { print "Missing parameter -H (--host).\n";
print "Wrong authentication protocol. Must be MD5 or SHA1 \n"; exit $exit_status;
return 0;
} }
$sessionType = 2;
if (defined($privpassword) && defined($privprotocol)) { $OPTION->{'snmp-version'} =~ s/^v//;
if ($privprotocol ne "DES" && $privprotocol ne "AES") { if ($OPTION->{'snmp-version'} !~ /1|2c|2|3/) {
print "Wrong encryption protocol. Must be DES or AES\n"; print "Unknown snmp version\n";
return 0; exit $exit_status;
} }
return 3;
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;
} }
} else{
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"; print "Missing parameter to open SNMPv3 session\n";
return 0; exit $exit_status;
} }
return $sessionType; 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 # Method to connect to the remote host
# This method take the hash table option as argument # This method take the hash table option as argument
# return $session if OK or 0 if not sub connection {
sub connection my ($exit_status, $session_params) = @_;
{
my $self = shift;
my $sessionType = shift;
my $options = shift;
my ($session, $error); my ($session, $error);
if ($sessionType == 1) { ($session, $error) = Net::SNMP->session(%$session_params);
if (!defined($options->{'host'}) || !defined($options->{'snmpport'}) || !defined($options->{'snmpcomm'}) || !defined($options->{'snmpversion'})) { if (!defined($session)) {
print("Error when trying to connect - mission argument(s)\n"); print "UNKNOWN: SNMP Session : $error\n";
return 0; exit $exit_status;
} }
($session, $error) = Net::SNMP->session(-hostname => $options->{'host'},
-community => $options->{'snmpcomm'},
-version => $options->{'snmpversion'},
-port => $options->{'snmpport'});
} elsif ($sessionType == 2) { $session->translate(Net::SNMP->TRANSLATE_NONE);
($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; return $session;
} }
# Test if OID passed in 2nd parameter exists sub get_snmp_table {
sub testOID{ my ($oid, $session, $exit_status, $OPTION) = @_;
my $self = shift; my $result;
my $sess = $_[0];
my $OID_toTest = $_[1]; if (defined($OPTION) && defined($OPTION->{'maxrepetitions'})) {
my $result = $sess->get_table(Baseoid => $OID_toTest); $result = $session->get_table(Baseoid => $oid, -maxrepetitions => $OPTION->{'maxrepetitions'});
if (!defined($result)) { } else {
return 0; $result = $session->get_table(Baseoid => $oid);
} else{
return 1;
} }
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; 1;

View File

@ -1,6 +1,6 @@
#! /usr/bin/perl -w #! /usr/bin/perl -w
################################################################################ ################################################################################
# Copyright 2004-2011 MERETHIS # Copyright 2004-2013 MERETHIS
# Centreon is developped by : Julien Mathis and Romain Le Merlus under # Centreon is developped by : Julien Mathis and Romain Le Merlus under
# GPL Licence 2.0. # GPL Licence 2.0.
# #
@ -31,14 +31,13 @@
# #
# For more information : contact@centreon.com # For more information : contact@centreon.com
# #
# SVN : $URL$ # SVN : $URL: http://svn.centreon.com/trunk/plugins-2.x/src/check_centreon_snmp_string $
# SVN : $Id$ # SVN : $Id: check_centreon_snmp_string 12130 2011-04-19 08:57:37Z jmathis $
# #
#################################################################################### ####################################################################################
# #
# Script init # Script init
# #
# check_value -f "The cpu is used %s %d" -m CPU -u "%"
use strict; use strict;
use Getopt::Long; use Getopt::Long;
@ -47,10 +46,13 @@ require "@NAGIOS_PLUGINS@/Centreon/SNMP/Utils.pm";
my $PROGNAME = "$0"; my $PROGNAME = "$0";
my %OPTION = ('host' => undef, 'help' => undef, my %OPTION = ("host" => undef,
'snmpversion' => 1, 'snmpcomm' => 'public', 'min' => 0, 'max' => 0, "snmp-community" => "public", "snmp-version" => 1, "snmp-port" => 161,
'host' => undef,'username' => undef, 'authpassword' => undef, 'authprotocol' => undef, "snmp-auth-key" => undef, "snmp-auth-user" => undef, "snmp-auth-password" => undef, "snmp-auth-protocol" => "MD5",
'privprotocol' => undef , 'privpassword' => undef, 'snmpport' => 161, "snmp-priv-key" => undef, "snmp-priv-password" => undef, "snmp-priv-protocol" => "DES",
"maxrepetitions" => undef,
"64-bits" => undef,
'help' => undef,
'output' => 'The value is %f'); 'output' => 'The value is %f');
my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3); my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3);
my $prefix = ""; my $prefix = "";
@ -60,31 +62,32 @@ sub print_usage ();
Getopt::Long::Configure('bundling'); Getopt::Long::Configure('bundling');
GetOptions GetOptions
("h" => \$OPTION{'help'}, "help" => \$OPTION{'help'}, (
"P=s" => \$OPTION{'snmpport'}, "snmpport=s" => \$OPTION{'snmpport'}, "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'}, "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'}, "W=s" => \$OPTION{'warning_table'}, "warning_table=s" => \$OPTION{'warning_table'},
"T=s" => \$OPTION{'critical_table'}, "critical_table=s" => \$OPTION{'critical_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{'ok_table'}, "ok_table=s" => \$OPTION{'ok_table'},
"o=s" => \$OPTION{'oid'}, "oid=s" => \$OPTION{'oid'}, "o=s" => \$OPTION{'oid'}, "oid=s" => \$OPTION{'oid'},
"debug" => \$OPTION{'debug'}, "debug" => \$OPTION{'debug'},
"f=s" => \$OPTION{'output'}, "output=s" => \$OPTION{'output'}, "f=s" => \$OPTION{'output'}, "output=s" => \$OPTION{'output'});
"m=s" => \$OPTION{'metric'}, "metric=s" => \$OPTION{'metric'});
my $metricsname = undef;
my $unit = undef; my $unit = undef;
my $output = undef; my $output = undef;
# Table used when personnal threshold are set # Table used when personnal threshold are set
my @critical_table = (); my @critical_table = ();
my @warning_table = (); my @warning_table = ();
@ -107,68 +110,39 @@ if (defined($OPTION{'help'})) {
print_help(); print_help();
exit $ERRORS{'UNKNOWN'}; exit $ERRORS{'UNKNOWN'};
} }
if (!$OPTION{'host'}) { if (!defined($OPTION{'oid'})) {
print_usage(); print "Missing parameters -o (--oid')\n";
exit $ERRORS{'UNKNOWN'};
}
if (!$OPTION{'oid'}) {
print_usage();
exit $ERRORS{'UNKNOWN'}; exit $ERRORS{'UNKNOWN'};
} }
# Store option values in simpler variables # Store option values in simpler variables
if ($OPTION{'output'} ne "" ){ if ($OPTION{'output'} eq "") {
#Output Verification? print "Output is not correctly set\n";
$output = $OPTION{'output'};
} else{
print("Output is not correctly set \n");
exit $ERRORS{'UNKNOWN'}; exit $ERRORS{'UNKNOWN'};
} }
#Output Verification
$output = $OPTION{'output'};
# Check if version passed in option exists if (!($OPTION{'oid'} =~ /^[0-9\.]+$/)) {
$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\.]+$/)) {
print "Wrong OID format\n"; print "Wrong OID format\n";
exit $ERRORS{'UNKNOWN'}; exit $ERRORS{'UNKNOWN'};
} }
# Plugin snmp connection my ($session_params) = Centreon::SNMP::Utils::check_snmp_options($ERRORS{'UNKNOWN'}, \%OPTION);
my ($session); my $session = Centreon::SNMP::Utils::connection($ERRORS{'UNKNOWN'}, $session_params);
if (!($session = Centreon::SNMP::Utils->connection($sessionType,\%OPTION))){
exit $ERRORS{'UNKNOWN'};
}
# Get the value returned by OID # Get the value returned by OID
my $result = $session->get_request(-varbindlist => [$OPTION{'oid'}]); my $result = Centreon::SNMP::Utils::get_snmp_leef([$OPTION{'oid'}], $session, $ERRORS{'UNKNOWN'});
if (!defined($result)) {
printf("UNKNOWN: %s.\n", $session->error);
$session->close;
exit $ERRORS{'UNKNOWN'};
}
my $currentValue = $result->{$OPTION{'oid'}}; my $currentValue = $result->{$OPTION{'oid'}};
# Check if value returned is a number and then save it # Check if value returned is a number and then save it
if (!defined($currentValue) || $currentValue =~ /noSuch/){ if (!defined($currentValue) || $currentValue =~ /noSuch/){
print("No instance on OID $OPTION{'oid'} \n "); print "No instance on OID $OPTION{'oid'}\n";
exit $ERRORS{'UNKNOWN'}; exit $ERRORS{'UNKNOWN'};
} }
#=== Plugin return ==== #=== Plugin return ====
if (defined($currentValue)) {
if (defined($currentValue)){
my $returnValue = $currentValue; my $returnValue = $currentValue;
my $status = "UNKNOWN"; my $status = "UNKNOWN";
my $state= "unknownState"; my $state= "unknownState";
@ -206,14 +180,14 @@ if (defined($currentValue)){
$i++; $i++;
} }
} }
if ($OPTION{'critical_table'}){ if ($OPTION{'critical_table'}) {
my $i = 0; my $i = 0;
my $max_crit= scalar(@critical_table); my $max_crit= scalar(@critical_table);
while($i < $max_crit) { while($i < $max_crit) {
print "Critical[$i] = $critical_table[$i] / returnValue = $returnValue \n" if($OPTION{'debug'}); print "Critical[$i] = $critical_table[$i] / returnValue = $returnValue \n" if($OPTION{'debug'});
if($critical_table[$i] eq $returnValue) { if($critical_table[$i] eq $returnValue) {
$status = "CRITICAL"; $status = "CRITICAL";
$state = $warning_table[$i]; $state = $critical_table[$i];
} }
$i++; $i++;
} }
@ -231,23 +205,27 @@ if (defined($currentValue)){
sub print_usage () { sub print_usage () {
print "Usage:"; print "Usage:";
print "$PROGNAME\n"; print "$PROGNAME\n";
print " -H (--hostname) \t Hostname to query - (required)\n"; print " -H (--hostname) Hostname to query (required)\n";
print " -C (--community) \t SNMP read community (defaults to public,\n"; print " -C (--community) SNMP read community (defaults to public)\n";
print " \t \t used with SNMP v1 and v2c\n"; print " used with SNMP v1 and v2c\n";
print " -v (--snmp_version) \t 1 for SNMP v1 (default)\n"; print " -v (--snmp-version) 1 for SNMP v1 (default)\n";
print " \t 2 for SNMP v2c\n"; print " 2 for SNMP v2c\n";
print " -t (--type) \t Data Source Type (GAUGE or COUNTER) (GAUGE by default)\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 " -o (--oid) \t OID to check\n";
print " -u (--username) \t snmp v3 username \n"; print " -W (--warning_table) \t Personal warning threshold : -W warningstate... \n";
print " -a (--authprotocol) \t protocol MD5/SHA1 (v3)\n"; print " -T (--critical_table) \t Personal critical threshold : -T criticalstate1,criticalstate2... \n";
print " -A (--authpassword) \t password (v3) \n"; print " -O (--ok_table) \t Personal ok threshold : -O okstate1,okstate2... \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 " -f (--output) \t Output format (ex : -f \"My metric's percentage value = %f %%\" \n";
print " -V (--version) \t Plugin version\n"; print " -V (--version) \t Plugin version\n";
print " -h (--help) \t usage help\n"; print " -h (--help) \t usage help\n";
@ -255,7 +233,7 @@ sub print_usage () {
sub print_help () { sub print_help () {
print "##############################################\n"; 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 "# Bugs to http://forge.centreon.com/ #\n";
print "##############################################\n"; print "##############################################\n";
print_usage(); print_usage();

View File

@ -40,49 +40,54 @@
# #
use strict; use strict;
use Net::SNMP qw(:snmp oid_lex_sort); require "@NAGIOS_PLUGINS@/Centreon/SNMP/Utils.pm";
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'};
}
use vars qw($PROGNAME); use vars qw($PROGNAME);
use Getopt::Long; 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); 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 # Plugin var init
my ($session, $error); 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); 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"; $PROGNAME = "$0";
sub print_help (); sub print_help ();
sub print_usage (); sub print_usage ();
my %OPTION = ( my %OPTION = (
"host" => undef, "64-bits" => undef, "host" => undef,
"snmp-community" => "public", "snmp-version" => 1, "snmp-port" => 161, "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'); Getopt::Long::Configure('bundling');
GetOptions GetOptions
( (
"H|hostname=s" => \$OPTION{'host'}, "H|hostname|host=s" => \$OPTION{'host'},
"C|community=s" => \$OPTION{'snmp-community'}, "C|community=s" => \$OPTION{'snmp-community'},
"v|snmp-version=s" => \$OPTION{'snmp-version'}, "v|snmp|snmp-version=s" => \$OPTION{'snmp-version'},
"P|snmp-port=i" => \$OPTION{'snmp-port'}, "P|snmpport|snmp-port=i" => \$OPTION{'snmp-port'},
"k|key=s" => \$OPTION{'snmp-auth-key'},
"u|username=s" => \$OPTION{'snmp-auth-user'}, "u|username=s" => \$OPTION{'snmp-auth-user'},
"p|password=s" => \$OPTION{'snmp-auth-passwd'}, "p|authpassword|password=s" => \$OPTION{'snmp-auth-password'},
"64-bits" => \$OPTION{'64-bits'}, "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'}, "maxrepetitions=s" => \$OPTION{'maxrepetitions'},
"64-bits" => \$OPTION{'64-bits'},
"disable-warn-state" => \$OPTION{'disable-warn-state'}, "disable-warn-state" => \$OPTION{'disable-warn-state'},
"h" => \$opt_h, "help" => \$opt_h, "h" => \$opt_h, "help" => \$opt_h,
"s" => \$opt_s, "show" => \$opt_s, "s" => \$opt_s, "show" => \$opt_s,
@ -106,113 +111,14 @@ if ($opt_h) {
Getopt::Long::Configure('bundling'); 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 ##### Verify Options
## ##
if (!$OPTION{'host'}) {
print_usage();
exit $ERRORS{'OK'};
}
###### ######
### SNMP Check ### SNMP Check
## ##
if (defined($OPTION{'snmp-version'}) && $OPTION{'snmp-version'} =~ /^([1-3])c?$/) { my ($session_params) = Centreon::SNMP::Utils::check_snmp_options($ERRORS{'UNKNOWN'}, \%OPTION);
$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"}
}
}
###### ######
### Others ### Others
@ -257,13 +163,13 @@ if ($critical <= $warning){
##### Plugin snmp requests ##### 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_DESC = $centreon{MIB2}{IF_DESC};
my $OID_OPERSTATUS = $centreon{MIB2}{IF_OPERSTATUS}; my $OID_OPERSTATUS = $centreon{MIB2}{IF_OPERSTATUS};
my @operstatus = ("up","down","testing", "unknown", "dormant", "notPresent", "lowerLayerDown"); 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 $result;
my $mustCreateFile = 0; my $mustCreateFile = 0;
my $countLine; my $countLine;
@ -292,7 +198,7 @@ if (-e $cacheFile) {
} }
if ($mustCreateFile) { 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)){ unless (open(FILE,">".$cacheFile)){
print "Check mod for temporary file : ".$cacheFile."...\n"; print "Check mod for temporary file : ".$cacheFile."...\n";
exit $ERRORS{"UNKNOWN"}; exit $ERRORS{"UNKNOWN"};
@ -359,7 +265,7 @@ if (defined($OPTION{'64-bits'})) {
# Get desctiption table # Get desctiption table
if ($opt_s) { if ($opt_s) {
if (!-e $cacheFile) { 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)){ unless (open(FILE,">".$cacheFile)){
print "Check mod for temporary file : ".$cacheFile."...\n"; print "Check mod for temporary file : ".$cacheFile."...\n";
exit $ERRORS{"UNKNOWN"}; exit $ERRORS{"UNKNOWN"};
@ -384,12 +290,12 @@ if ($opt_s) {
if ($countLine) { if ($countLine) {
my @resLine = split(/\;/, $row); my @resLine = split(/\;/, $row);
my $index = $resLine[0]; 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/\x00//g;
$resLine[1] =~ s/\n//g; $resLine[1] =~ s/\n//g;
print "Interface ". $index . " :: " . $resLine[1] . " :: ".$operstatus[$interface_status->{$OID_OPERSTATUS.".".$index} - 1]; print "Interface ". $index . " :: " . $resLine[1] . " :: ".$operstatus[$interface_status->{$OID_OPERSTATUS.".".$index} - 1];
if ($opt_S) { 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)) { if (!defined($link_speed)) {
printf("ERROR: Interface Speed Request : %s", $session->error); printf("ERROR: Interface Speed Request : %s", $session->error);
exit $ERRORS{'UNKNOWN'}; exit $ERRORS{'UNKNOWN'};
@ -407,16 +313,9 @@ if ($opt_s) {
exit $ERRORS{'OK'}; exit $ERRORS{'OK'};
} }
$result = $session->get_request(-varbindlist => [$OID_OPERSTATUS.".".$interface, $OID_IN, $OID_OUT, $OID_SPEED]);
if (!defined($result)) { $result = Centreon::SNMP::Utils::get_snmp_leef([$OID_OPERSTATUS.".".$interface, $OID_IN, $OID_OUT, $OID_SPEED], $session, $ERRORS{'UNKNOWN'},
printf("ERROR: SNMP Request: %s", $session->error); defined($opt_n) ? "You must specify interface name when option -n is used." : undef);
if ($opt_n) {
print " - You must specify interface name when option -n is used";
}
print ".\n";
$session->close;
exit $ERRORS{'UNKNOWN'};
}
if (!defined($result->{$OID_OPERSTATUS.".".$interface}) || $result->{$OID_OPERSTATUS.".".$interface} eq "") { if (!defined($result->{$OID_OPERSTATUS.".".$interface}) || $result->{$OID_OPERSTATUS.".".$interface} eq "") {
print "ERROR: Can't get interface '$interface' status\n"; print "ERROR: Can't get interface '$interface' status\n";
exit $ERRORS{'CRITICAL'}; exit $ERRORS{'CRITICAL'};
@ -471,8 +370,8 @@ $last_out_bits = 0;
my $flg_created = 0; my $flg_created = 0;
if (-e "@CENTPLUGINS_TMP@/traffic_if".$interface."_".$OPTION{'host'}) { if (-e "$centplugins_path/traffic_if".$interface."_".$OPTION{'host'}) {
open(FILE,"<"."@CENTPLUGINS_TMP@/traffic_if".$interface."_".$OPTION{'host'}); open(FILE,"<"."$centplugins_path/traffic_if".$interface."_".$OPTION{'host'});
while($row = <FILE>){ while($row = <FILE>){
@last_values = split(":",$row); @last_values = split(":",$row);
$last_check_time = $last_values[0]; $last_check_time = $last_values[0];
@ -487,8 +386,8 @@ if (-e "@CENTPLUGINS_TMP@/traffic_if".$interface."_".$OPTION{'host'}) {
$update_time = time(); $update_time = time();
unless (open(FILE,">"."@CENTPLUGINS_TMP@/traffic_if".$interface."_".$OPTION{'host'})){ unless (open(FILE,">"."$centplugins_path/traffic_if".$interface."_".$OPTION{'host'})){
print "Check mod for temporary file : @CENTPLUGINS_TMP@/traffic_if".$interface."_".$OPTION{'host'}. " !\n"; print "Check mod for temporary file : $centplugins_path/traffic_if".$interface."_".$OPTION{'host'}. " !\n";
exit $ERRORS{"UNKNOWN"}; exit $ERRORS{"UNKNOWN"};
} }
print FILE "$update_time:$in_bits:$out_bits"; print FILE "$update_time:$in_bits:$out_bits";
@ -647,6 +546,9 @@ sub print_usage () {
print " -k (--key) snmp V3 key\n"; print " -k (--key) snmp V3 key\n";
print " -u (--username) snmp V3 username \n"; print " -u (--username) snmp V3 username \n";
print " -p (--password) snmp V3 password\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 " --64-bits Use 64 bits OID\n";
print " --maxrepetitions To use when you have the error: 'Message size exceeded buffer maxMsgSize'\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 " 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 " -w (--warning) Signal strength at which a warning message will be generated\n";
print " (default 80)\n"; print " (default 80)\n";
print " -c (--critical) Signal strength at which a critical message will be generated\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 " -T Set maximum bandwidth\n";
print " -S Show link speed in output\n"; print " -S Show link speed in output\n";
print " -V (--version) Plugin version\n"; print " -V (--version) Plugin version\n";

View File

@ -1,6 +1,6 @@
#! /usr/bin/perl -w #! /usr/bin/perl -w
################################################################################ ################################################################################
# Copyright 2004-2011 MERETHIS # Copyright 2004-2013 MERETHIS
# Centreon is developped by : Julien Mathis and Romain Le Merlus under # Centreon is developped by : Julien Mathis and Romain Le Merlus under
# GPL Licence 2.0. # GPL Licence 2.0.
# #
@ -31,14 +31,13 @@
# #
# For more information : contact@centreon.com # For more information : contact@centreon.com
# #
# SVN : $URL$ # SVN : $URL: http://svn.centreon.com/trunk/plugins-2.x/src/check_centreon_snmp_value $
# SVN : $Id$ # SVN : $Id: check_centreon_snmp_value 13054 2012-05-21 08:02:46Z jmathis $
# #
#################################################################################### ####################################################################################
# #
# Script init # Script init
# #
# check_value -f "The cpu is used %s %d" -m CPU -u "%"
use strict; use strict;
use Getopt::Long; use Getopt::Long;
@ -47,10 +46,17 @@ require "@NAGIOS_PLUGINS@/Centreon/SNMP/Utils.pm";
my $PROGNAME = "$0"; my $PROGNAME = "$0";
my %OPTION = ('host' => undef, 'help' => undef, 'warning' => '5', 'critical' => '10', my %OPTION = (
'snmpversion' => 1, 'display' => 0, 'snmpcomm' => 'public', 'min' => 0, 'max' => 0, "host" => undef,
'host' => undef,'username' => undef, 'authpassword' => undef, 'authprotocol' => undef, "snmp-community" => "public", "snmp-version" => 1, "snmp-port" => 161,
'privprotocol' => undef , 'privpassword' => undef, 'snmpport' => 161, 'type' => 'GAUGE', 'base' => 1000, "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); 'output' => 'The value is %f', 'metric' =>'value', 'unit' => 'nounit', 'divide' => 1);
my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3); my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3);
my $prefix = ""; my $prefix = "";
@ -60,19 +66,25 @@ sub print_usage ();
Getopt::Long::Configure('bundling'); Getopt::Long::Configure('bundling');
GetOptions GetOptions
("h" => \$OPTION{'help'}, "help" => \$OPTION{'help'}, (
"P=s" => \$OPTION{'snmpport'}, "snmpport=s" => \$OPTION{'snmpport'}, "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'}, "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'}, "w=s" => \$OPTION{'warning'}, "warning=s" => \$OPTION{'warning'},
"c=s" => \$OPTION{'critical'}, "critical=s" => \$OPTION{'critical'}, "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'}, "W=s" => \$OPTION{'warning_table'}, "warning_table=s" => \$OPTION{'warning_table'},
"T=s" => \$OPTION{'critical_table'}, "critical_table=s" => \$OPTION{'critical_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{'ok_table'}, "ok_table=s" => \$OPTION{'ok_table'},
@ -85,7 +97,6 @@ GetOptions
"debug" => \$OPTION{'debug'}, "debug" => \$OPTION{'debug'},
"min=s" => \$OPTION{'min'}, "min=s" => \$OPTION{'min'},
"base=s" => \$OPTION{'base'}, "base=s" => \$OPTION{'base'},
"64-bits=s" => \$OPTION{'64-bits'},
"f=s" => \$OPTION{'output'}, "output=s" => \$OPTION{'output'}, "f=s" => \$OPTION{'output'}, "output=s" => \$OPTION{'output'},
"m=s" => \$OPTION{'metric'}, "metric=s" => \$OPTION{'metric'} "m=s" => \$OPTION{'metric'}, "metric=s" => \$OPTION{'metric'}
); );
@ -120,23 +131,18 @@ if ($OPTION{'ok_table'}){
@ok_table = split(/\,/, $OPTION{'ok_table'}); @ok_table = split(/\,/, $OPTION{'ok_table'});
} }
if (defined($OPTION{'pluginversion'})) { if (defined($OPTION{'pluginversion'})) {
print("$PROGNAME 1.1"); print "$PROGNAME 1.1\n";
exit $ERRORS{'UNKNOWN'}; exit $ERRORS{'UNKNOWN'};
} }
if (defined($OPTION{'help'})) { if (defined($OPTION{'help'})) {
print_help(); print_help();
exit $ERRORS{'UNKNOWN'}; exit $ERRORS{'UNKNOWN'};
} }
if (!$OPTION{'host'}) { if (!defined($OPTION{'oid'})) {
print_usage(); print "Missing parameters -o (--oid')\n";
exit $ERRORS{'UNKNOWN'};
}
if (!$OPTION{'oid'}) {
print_usage();
exit $ERRORS{'UNKNOWN'}; exit $ERRORS{'UNKNOWN'};
} }
my $cacheFile = "@CENTPLUGINS_TMP@/snmp_value".$OPTION{'host'}."-".$OPTION{'oid'};
# Store option values in simpler variables # 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 "") { if ($OPTION{'divide'} ne "" && $OPTION{'metric'} ne "" && $OPTION{'unit'} ne "" && $OPTION{'output'} ne "" && $OPTION{'min'} ne "" && $OPTION{'max'} ne "") {
@ -172,25 +178,11 @@ if ($OPTION{'divide'} ne "" && $OPTION{'metric'} ne "" && $OPTION{'unit'} ne ""
$min = $OPTION{'min'}; $min = $OPTION{'min'};
$max = $OPTION{'max'}; $max = $OPTION{'max'};
$divide = $OPTION{'divide'}; $divide = $OPTION{'divide'};
} else { } else {
print("One or more arguments are not set \n"); print("One or more arguments are not set \n");
exit $ERRORS{'UNKNOWN'}; 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"; my $DS_type = "GAUGE";
$DS_type = $1 if ($OPTION{'type'} =~ /(GAUGE)/ || $OPTION{'type'} =~ /(COUNTER)/); $DS_type = $1 if ($OPTION{'type'} =~ /(GAUGE)/ || $OPTION{'type'} =~ /(COUNTER)/);
@ -203,30 +195,20 @@ if ($critical < $warning){
exit $ERRORS{'UNKNOWN'}; exit $ERRORS{'UNKNOWN'};
} }
if (!$OPTION{'oid'}) { if (!($OPTION{'oid'} =~ /^[0-9\.]+$/)) {
print "Option -o needed.\n";
exit $ERRORS{'UNKNOWN'};
} elsif (!($OPTION{'oid'} =~ /^[0-9\.]+$/)) {
print "Wrong OID format\n"; print "Wrong OID format\n";
exit $ERRORS{'UNKNOWN'}; exit $ERRORS{'UNKNOWN'};
} }
# Plugin snmp connection my ($session_params) = Centreon::SNMP::Utils::check_snmp_options($ERRORS{'UNKNOWN'}, \%OPTION);
my ($session); my $session = Centreon::SNMP::Utils::connection($ERRORS{'UNKNOWN'}, $session_params);
if (!($session = Centreon::SNMP::Utils->connection($sessionType,\%OPTION))) {
exit $ERRORS{'UNKNOWN'};
}
# Get the value returned by OID # Get the value returned by OID
my $result = $session->get_request(-varbindlist => [$OPTION{'oid'}]); my $result = Centreon::SNMP::Utils::get_snmp_leef([$OPTION{'oid'}], $session, $ERRORS{'UNKNOWN'});
if (!defined($result)) {
printf("UNKNOWN: %s.\n", $session->error);
$session->close;
exit $ERRORS{'UNKNOWN'};
}
$currentValue = $result->{$OPTION{'oid'}}; $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 # Check if value returned is a number and then save it
if(!defined($currentValue) || $currentValue =~ /noSuch/){ if(!defined($currentValue) || $currentValue =~ /noSuch/){
print("No instance on OID $OPTION{'oid'} \n "); print("No instance on OID $OPTION{'oid'} \n ");
@ -241,9 +223,7 @@ if (defined($currentValue)){
# If metric type = counter, use the cache file to get the last value (or create cache file) # If metric type = counter, use the cache file to get the last value (or create cache file)
if ($DS_type eq 'COUNTER') { if ($DS_type eq 'COUNTER') {
#If file exist #If file exist
if (-e $cacheFile){ if (-e $cacheFile){
my @cache; my @cache;
open(FILE,"<".$cacheFile); open(FILE,"<".$cacheFile);
@ -257,7 +237,6 @@ if ($DS_type eq 'COUNTER') {
open(FILE,">".$cacheFile); open(FILE,">".$cacheFile);
print FILE $currentTime.";".$currentValue; print FILE $currentTime.";".$currentValue;
close(FILE); close(FILE);
} else { } else {
#If the file doesnt exist, a new file is created and values are inserted #If the file doesnt exist, a new file is created and values are inserted
unless (open(FILE,">".$cacheFile)){ unless (open(FILE,">".$cacheFile)){
@ -267,8 +246,8 @@ if ($DS_type eq 'COUNTER') {
my $currentTime = time(); my $currentTime = time();
print FILE $currentTime.";".$currentValue; print FILE $currentTime.";".$currentValue;
print("Buffer in creation . . . please wait \n"); print("Buffer in creation . . . please wait \n");
exit $ERRORS{"OK"};
close(FILE); close(FILE);
exit $ERRORS{"OK"};
} }
} }
@ -281,14 +260,12 @@ if (defined($currentValue)){
my $state= "unknownState"; my $state= "unknownState";
$returnValue = 7 if($OPTION{'debug'}); $returnValue = 7 if($OPTION{'debug'});
############################################################### ###############################################################
# If personnal tresholds are set for warning and / or critical # # If personnal tresholds are set for warning and / or critical #
############################################################### ###############################################################
if ($OPTION{'warning_table'} || $OPTION{'critical_table'} || $OPTION{'ok_table'}) { if ($OPTION{'warning_table'} || $OPTION{'critical_table'} || $OPTION{'ok_table'}) {
print "Mode personal threshold ON \n" if($OPTION{'debug'}); print "Mode personal threshold ON \n" if($OPTION{'debug'});
if ($OPTION{'ok_table'}) { if ($OPTION{'ok_table'}) {
my $max_ok= scalar(@ok_table); my $max_ok= scalar(@ok_table);
my $i = 0; my $i = 0;
@ -326,7 +303,6 @@ if (defined($currentValue)){
} }
$i = $i +2; $i = $i +2;
} }
} }
print(" Statut = $status \n ") if($OPTION{'debug'}); print(" Statut = $status \n ") if($OPTION{'debug'});
printf($output."\n",$state,$returnValue); printf($output."\n",$state,$returnValue);
@ -374,7 +350,6 @@ if (defined($currentValue)){
$critical = $OPTION{'critical'}; $critical = $OPTION{'critical'};
} }
if ($perfdata < $warning) { if ($perfdata < $warning) {
printf("OK :".$output." ".$prefix." ".$unit."|".$metricsname."=".$perfdata.$unit.";".$warning.";".$critical.";".$min.";".$max."\n",$returnValue); printf("OK :".$output." ".$prefix." ".$unit."|".$metricsname."=".$perfdata.$unit.";".$warning.";".$critical.";".$min.";".$max."\n",$returnValue);
$status = "OK"; $status = "OK";
@ -394,29 +369,33 @@ if (defined($currentValue)){
} }
# Define Common functions # Define Common functions
sub print_usage () { sub print_usage () {
print "Usage:"; print "Usage:";
print "$PROGNAME\n"; print "$PROGNAME\n";
print " -H (--hostname) \t Hostname to query - (required)\n"; print " -H (--hostname) Hostname to query (required)\n";
print " -C (--community) \t SNMP read community (defaults to public,\n"; print " -C (--community) SNMP read community (defaults to public)\n";
print " \t \t used with SNMP v1 and v2c\n"; print " used with SNMP v1 and v2c\n";
print " -v (--snmp_version) \t 1 for SNMP v1 (default)\n"; print " -v (--snmp-version) 1 for SNMP v1 (default)\n";
print " \t 2 for SNMP v2c\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 " -t (--type) \t Data Source Type (GAUGE or COUNTER) (GAUGE by default)\n";
print " -o (--oid) \t OID to check\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 " -w (--warning) \t Warning level \n";
print " -c (--critical) \t Critical level \n"; print " -c (--critical) \t Critical level \n";
print " -W (--wtreshold) \t Personal warning threshold : -W 1,normal,... \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 " -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 " --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 " -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 " -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 " -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 " --min \t \t min value for the metric (default = 0) \n";
@ -430,7 +409,7 @@ sub print_usage () {
sub print_help () { sub print_help () {
print "##############################################\n"; 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 "# Bugs to http://forge.centreon.com/ #\n";
print "##############################################\n"; print "##############################################\n";
print_usage(); print_usage();

View File

@ -1,6 +1,6 @@
#! /usr/bin/perl -w #! /usr/bin/perl -w
################################################################################ ################################################################################
# Copyright 2004-2011 MERETHIS # Copyright 2004-2013 MERETHIS
# Centreon is developped by : Julien Mathis and Romain Le Merlus under # Centreon is developped by : Julien Mathis and Romain Le Merlus under
# GPL Licence 2.0. # GPL Licence 2.0.
# #
@ -47,34 +47,43 @@ require "@NAGIOS_PLUGINS@/Centreon/SNMP/Utils.pm";
my $PROGNAME = "$0"; my $PROGNAME = "$0";
my %OPTION = ('host' => undef, 'help' => undef, 'warning' => '0', 'critical' => '0', my %OPTION = (
'snmpversion' => 1, 'display' => 0, 'snmpcomm' => 'public', 'min' => 0, 'max' => 0, "host" => undef,
'host' => undef,'username' => undef, 'authpassword' => undef, 'authprotocol' => undef, "snmp-community" => "public", "snmp-version" => 1, "snmp-port" => 161,
'privprotocol' => undef , 'privpassword' => undef, 'snmpport' => 161, 'type' => 'GAUGE', 'base' => 1000, "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); 'output' => '%.02f', 'metric' =>'value', 'unit' => 'nounit', 'divide' => 1);
my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3); my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3);
my $prefix = ""; my $prefix = "";
sub print_help (); sub print_help ();
sub print_usage (); sub print_usage ();
Getopt::Long::Configure('bundling'); Getopt::Long::Configure('bundling');
GetOptions GetOptions
("h" => \$OPTION{'help'}, "help" => \$OPTION{'help'}, (
"P=s" => \$OPTION{'snmpport'}, "snmpport=s" => \$OPTION{'snmpport'}, "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'}, "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'}, "w=s" => \$OPTION{'warning'}, "warning=s" => \$OPTION{'warning'},
"c=s" => \$OPTION{'critical'}, "critical=s" => \$OPTION{'critical'}, "c=s" => \$OPTION{'critical'}, "critical=s" => \$OPTION{'critical'},
"H=s" => \$OPTION{'host'}, "host=s" => \$OPTION{'host'},
"o=s" => \$OPTION{'oid'}, "oid=s" => \$OPTION{'oid'}, "o=s" => \$OPTION{'oid'}, "oid=s" => \$OPTION{'oid'},
"t=s" => \$OPTION{'type'}, "type=s" => \$OPTION{'type'}, "t=s" => \$OPTION{'type'}, "type=s" => \$OPTION{'type'},
"U=s" => \$OPTION{'unit'}, "unit=s" => \$OPTION{'unit'}, "U=s" => \$OPTION{'unit'}, "unit=s" => \$OPTION{'unit'},
@ -86,11 +95,9 @@ GetOptions
"min=s" => \$OPTION{'min'}, "min=s" => \$OPTION{'min'},
"max=s" => \$OPTION{'max'}, "max=s" => \$OPTION{'max'},
"base=s" => \$OPTION{'base'}, "base=s" => \$OPTION{'base'},
"64-bits" => \$OPTION{'64-bits'},
"f=s" => \$OPTION{'output'}, "output=s" => \$OPTION{'output'}, "f=s" => \$OPTION{'output'}, "output=s" => \$OPTION{'output'},
"m=s" => \$OPTION{'metric'}, "metric=s" => \$OPTION{'metric'}); "m=s" => \$OPTION{'metric'}, "metric=s" => \$OPTION{'metric'});
my $metricsname = undef; my $metricsname = undef;
my $unit = undef; my $unit = undef;
my $output = undef; my $output = undef;
@ -120,16 +127,15 @@ if($OPTION{'warning_table'}){
if($OPTION{'ok_table'}){ if($OPTION{'ok_table'}){
@ok_table = split(/\,/, $OPTION{'ok_table'}); @ok_table = split(/\,/, $OPTION{'ok_table'});
} }
if (!$OPTION{'oid'}) { if (!defined($OPTION{'oid'})) {
print "Option -o needed.\n \n"; print "Missing parameters -o (--oid')\n";
print_usage();
exit $ERRORS{'UNKNOWN'}; exit $ERRORS{'UNKNOWN'};
} elsif (!($OPTION{'oid'} =~ /^[0-9\.]+$/)) { }
if (!($OPTION{'oid'} =~ /^[0-9\.]+$/)) {
print "Wrong OID format\n"; print "Wrong OID format\n";
exit $ERRORS{'UNKNOWN'}; exit $ERRORS{'UNKNOWN'};
} }
if (defined($OPTION{'pluginversion'})) { if (defined($OPTION{'pluginversion'})) {
print("$PROGNAME 1.2"); print("$PROGNAME 1.2");
exit $ERRORS{'UNKNOWN'}; exit $ERRORS{'UNKNOWN'};
@ -138,12 +144,6 @@ if (defined($OPTION{'help'})) {
print_help(); print_help();
exit $ERRORS{'UNKNOWN'}; 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 #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 "" ){ if($OPTION{'divide'} ne "" && $OPTION{'metric'} ne "" && $OPTION{'unit'} ne "" && $OPTION{'output'} ne "" && $OPTION{'min'} ne "" && $OPTION{'max'} ne "" ){
@ -154,7 +154,6 @@ if($OPTION{'divide'} ne "" && $OPTION{'metric'} ne "" && $OPTION{'unit'} ne ""
$output = $OPTION{'output'}; $output = $OPTION{'output'};
#check parameter format #check parameter format
if ($OPTION{'base'} !~ /^[0-9]*\.?[0-9]*$/) { if ($OPTION{'base'} !~ /^[0-9]*\.?[0-9]*$/) {
print(" Base option should be a numerical \n"); print(" Base option should be a numerical \n");
exit $ERRORS{'UNKNOWN'}; exit $ERRORS{'UNKNOWN'};
@ -177,25 +176,11 @@ if($OPTION{'divide'} ne "" && $OPTION{'metric'} ne "" && $OPTION{'unit'} ne ""
$min = $OPTION{'min'}; $min = $OPTION{'min'};
$max = $OPTION{'max'}; $max = $OPTION{'max'};
$divide = $OPTION{'divide'}; $divide = $OPTION{'divide'};
} else {
}else{
print("One or more arguments are not set \n"); print("One or more arguments are not set \n");
exit $ERRORS{'UNKNOWN'}; 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"; my $DS_type = "GAUGE";
if ($OPTION{'type'} =~ m/GAUGE/i) { if ($OPTION{'type'} =~ m/GAUGE/i) {
$DS_type = "GAUGE"; $DS_type = "GAUGE";
@ -211,32 +196,22 @@ if ($critical < $warning){
print_usage(); print_usage();
exit $ERRORS{'UNKNOWN'}; 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);
my $cacheFile = "@CENTPLUGINS_TMP@/snmp_value_table".$OPTION{'host'}."-".$OPTION{'oid'};
# Plugin snmp connection
my ($session);
if (!($session = Centreon::SNMP::Utils->connection($sessionType,\%OPTION))){
exit $ERRORS{'UNKNOWN'};
}
my @oid_walk; #will contain each path to the final part of 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 @oid_list; #Will contain all final OID of the get_table request
my @cache; #Will contain cache file values my @cache; #Will contain cache file values
my %previousValues; my %previousValues;
my %currentValues; my %currentValues;
my $i =0; my $i = 0;
#Get the different finals OID returned by OID #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(); $currentTime = time();
if (!defined($result)) {
printf("UNKNOWN: %s.\n", $session->error);
$session->close;
exit $ERRORS{'UNKNOWN'};
}
foreach my $key (oid_lex_sort(keys %$result)) { foreach my $key (oid_lex_sort(keys %$result)) {
@oid_walk = split (/\./,$key); @oid_walk = split (/\./,$key);
$oid_list[$i] = pop(@oid_walk); $oid_list[$i] = pop(@oid_walk);
@ -251,7 +226,6 @@ if ($OPTION{'debug'}) {
} }
} }
#If metric type = counter, use the cache file to get the last value (or create cache file) #If metric type = counter, use the cache file to get the last value (or create cache file)
if ($DS_type eq 'COUNTER') { if ($DS_type eq 'COUNTER') {
print("COUNTER \n") if($OPTION{'debug'}); print("COUNTER \n") if($OPTION{'debug'});
@ -281,7 +255,7 @@ if ($DS_type eq 'COUNTER') {
for ($i = 0; $i < $size ; $i++) { for ($i = 0; $i < $size ; $i++) {
#Get current value for all oid at i position in table of all oid #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]]); my $result = Centreon::SNMP::Utils::get_snmp_leef([$OPTION{'oid'}.".".$oid_list[$i]], $session, $ERRORS{'UNKNOWN'});
$currentValue = $result->{$OPTION{'oid'}.".".$oid_list[$i]}; $currentValue = $result->{$OPTION{'oid'}.".".$oid_list[$i]};
$currentValues{$oid_list[$i]} = $currentValue; $currentValues{$oid_list[$i]} = $currentValue;
@ -301,7 +275,7 @@ if ($DS_type eq 'COUNTER') {
$i = 0; $i = 0;
print FILE $currentTime.";"; print FILE $currentTime.";";
for ($i = 0; $i < $size ; $i++) { for ($i = 0; $i < $size ; $i++) {
my $result = $session->get_request(-varbindlist => [$OPTION{'oid'}.".".$oid_list[$i]]); my $result = Centreon::SNMP::Utils::get_snmp_leef([$OPTION{'oid'}.".".$oid_list[$i]], $session, $ERRORS{'UNKNOWN'});
$currentValue = $result->{$OPTION{'oid'}.".".$oid_list[$i]}; $currentValue = $result->{$OPTION{'oid'}.".".$oid_list[$i]};
print FILE $oid_list[$i].";".$currentValue.";"; print FILE $oid_list[$i].";".$currentValue.";";
print("OID : $oid_list[$i] ; Current Value : $currentValue ; \n") if($OPTION{'debug'}); print("OID : $oid_list[$i] ; Current Value : $currentValue ; \n") if($OPTION{'debug'});
@ -316,7 +290,7 @@ if ($DS_type eq 'COUNTER') {
my $size= scalar(@oid_list); my $size= scalar(@oid_list);
print("Values for each OID : \n") if($OPTION{'debug'}); print("Values for each OID : \n") if($OPTION{'debug'});
for ($i = 0; $i < $size ; $i++) { for ($i = 0; $i < $size ; $i++) {
my $result = $session->get_request(-varbindlist => [$OPTION{'oid'}.".".$oid_list[$i]]); my $result = Centreon::SNMP::Utils::get_snmp_leef([$OPTION{'oid'}.".".$oid_list[$i]], $session, $ERRORS{'UNKNOWN'});
$currentValue = $result->{$OPTION{'oid'}.".".$oid_list[$i]}; $currentValue = $result->{$OPTION{'oid'}.".".$oid_list[$i]};
$currentValues{$oid_list[$i]} = $currentValue; $currentValues{$oid_list[$i]} = $currentValue;
@ -379,7 +353,6 @@ if (keys(%currentValues) > 0) {
my $i = 0; my $i = 0;
my $max_crit= scalar(@critical_table); my $max_crit= scalar(@critical_table);
while($i < $max_crit){ while($i < $max_crit){
for(my $u=0; $u < $size;$u++){ for(my $u=0; $u < $size;$u++){
print "Critical[$i] = $critical_table[$i] / returnValue = $returnValues{$oid_list[$u]} \n" if($OPTION{'debug'}); print "Critical[$i] = $critical_table[$i] / returnValue = $returnValues{$oid_list[$u]} \n" if($OPTION{'debug'});
if($critical_table[$i] == $returnValues{$oid_list[$u]}) { if($critical_table[$i] == $returnValues{$oid_list[$u]}) {
@ -421,7 +394,7 @@ if (keys(%currentValues) > 0) {
} }
} }
if($OPTION{'debug'}) { if ($OPTION{'debug'}) {
for ( my $i = 0; $i < $size ; $i++) { for ( my $i = 0; $i < $size ; $i++) {
print(" ValuetoReturn OiD: $oid_list[$i] : $returnValues{$oid_list[$i]} \n"); print(" ValuetoReturn OiD: $oid_list[$i] : $returnValues{$oid_list[$i]} \n");
} }
@ -448,7 +421,7 @@ if (keys(%currentValues) > 0) {
$Ok{$oid_list[$i]} = $returnValues{$oid_list[$i]}; $Ok{$oid_list[$i]} = $returnValues{$oid_list[$i]};
} elsif ($warning != 0 && $warning <= $returnValues{$oid_list[$i]} && $returnValues{$oid_list[$i]} < $critical) { } elsif ($warning != 0 && $warning <= $returnValues{$oid_list[$i]} && $returnValues{$oid_list[$i]} < $critical) {
$Warning{$oid_list[$i]} = $returnValues{$oid_list[$i]}; $Warning{$oid_list[$i]} = $returnValues{$oid_list[$i]};
}elsif($critical != 0 && $returnValues{$oid_list[$i]} >= $critical){ } elsif($critical != 0 && $returnValues{$oid_list[$i]} >= $critical){
$Critical{$oid_list[$i]} = $returnValues{$oid_list[$i]}; $Critical{$oid_list[$i]} = $returnValues{$oid_list[$i]};
} }
} }
@ -469,8 +442,7 @@ if (keys(%currentValues) > 0) {
my $u = 0; my $u = 0;
$prefix = ""; $prefix = "";
if($warning != 0 && $returnValues{$oid_list[$i]} > $warning){ if($warning != 0 && $returnValues{$oid_list[$i]} > $warning){
while($returnValues{$oid_list[$i]} > $OPTION{'base'}) while($returnValues{$oid_list[$i]} > $OPTION{'base'}) {
{
$returnValues{$oid_list[$i]} = $returnValues{$oid_list[$i]} / $OPTION{'base'}; $returnValues{$oid_list[$i]} = $returnValues{$oid_list[$i]} / $OPTION{'base'};
$u++; $u++;
} }
@ -495,7 +467,7 @@ if (keys(%currentValues) > 0) {
if(keys(%Critical)!=0) { if(keys(%Critical)!=0) {
$state = "CRITICAL"; $state = "CRITICAL";
}elsif(keys(%Warning)!=0) { } elsif(keys(%Warning)!=0) {
$state = "WARNING"; $state = "WARNING";
} }
print($output_message.$perf_output_message."\n"); print($output_message.$perf_output_message."\n");
@ -505,29 +477,33 @@ if (keys(%currentValues) > 0) {
exit $ERRORS{'CRITICAL'}; exit $ERRORS{'CRITICAL'};
} }
sub print_usage () { sub print_usage () {
print "Usage:"; print "Usage:";
print "$PROGNAME\n"; print "$PROGNAME\n";
print " -H (--hostname) \t Hostname to query - (required)\n"; print " -H (--hostname) Hostname to query (required)\n";
print " -C (--community) \t SNMP read community (defaults to public,\n"; print " -C (--community) SNMP read community (defaults to public)\n";
print " \t \t used with SNMP v1 and v2c\n"; print " used with SNMP v1 and v2c\n";
print " -v (--snmp_version) \t 1 for SNMP v1 (default)\n"; print " -v (--snmp-version) 1 for SNMP v1 (default)\n";
print " \t 2 for SNMP v2c\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 " -t (--type) \t Data Source Type (GAUGE or COUNTER) (GAUGE by default)\n";
print " -o (--oid) \t OID to check\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 " -w (--warning) \t Warning level \n";
print " -c (--critical) \t Critical level \n"; print " -c (--critical) \t Critical level \n";
print " -W (--wtreshold) \t Personal warning threshold : -W 1,normal,... \n"; print " -W (--warning_table) \t Personal warning threshold : -W 1,normal,... \n";
print " -T (--ctreshold) \t Personal critical threshold : -T 3,notResponding,4,NotFunctionning,... \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 " --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 " -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 " -U (--unit) \t Metric's unit ( /!\\ for % write %% ) \n";
print " -f (--output) \t Output format (ex : -f \"%0.2f \" \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 " --min \t \t min value for the metric (default = 0) \n";
@ -540,11 +516,9 @@ sub print_usage () {
sub print_help () { sub print_help () {
print "##############################################\n"; 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 "# Bugs to http://forge.centreon.com/ #\n";
print "##############################################\n"; print "##############################################\n";
print_usage(); print_usage();
print "\n"; print "\n";
} }