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/) { if ($@) {
print "Wrong SNMP version\n"; print "Could not load the Perl module 'Config::IniFiles' $@";
return 0; exit($exit_status);
}
require Config::IniFiles;
} unless (-e $oid_file) {
return 1; 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; $OPTION->{'snmp-version'} =~ s/^v//;
if (defined($privpassword) && defined($privprotocol)) { if ($OPTION->{'snmp-version'} !~ /1|2c|2|3/) {
if ($privprotocol ne "DES" && $privprotocol ne "AES") { print "Unknown snmp version\n";
print "Wrong encryption protocol. Must be DES or AES\n"; exit $exit_status;
return 0; }
}
return 3; if ($OPTION->{'snmp-version'} eq "3") {
} %session_params = (-hostname => $OPTION->{'host'}, -version => $OPTION->{'snmp-version'}, -port => $OPTION->{'snmp-port'});
} else{
print "Missing parameter to open SNMPv3 session\n"; if (defined($OPTION->{'snmp-auth-password'}) && defined($OPTION->{'snmp-auth-key'})) {
return 0; print "Only option -k (--authkey) or -p (--password) is needed for snmp v3\n";
} exit $exit_status;
return $sessionType; }
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 # 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 ($session, $error);
my $self = shift;
my $sessionType = shift;
my $options = shift;
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'}, return $session;
-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 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'});
} else {
$result = $session->get_table(Baseoid => $oid);
}
if (!defined($result)) { if (!defined($result)) {
return 0; printf("SNMP TABLE ERROR : %s.\n", $session->error);
} else{ $session->close;
return 1; 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,11 +46,14 @@ 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",
'output' => 'The value is %f'); "maxrepetitions" => undef,
"64-bits" => undef,
'help' => undef,
'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,44 +62,45 @@ 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'},
"V" => \$OPTION{'pluginversion'}, "version" => \$OPTION{'pluginversion'}, "C|community=s" => \$OPTION{'snmp-community'},
"u=s" => \$OPTION{'username'}, "username=s" => \$OPTION{'username'}, "v|snmp|snmp-version=s" => \$OPTION{'snmp-version'},
"a=s" => \$OPTION{'authprotocol'}, "authprotocol=s" => \$OPTION{'authprotocol'}, "P|snmpport|snmp-port=i" => \$OPTION{'snmp-port'},
"A=s" => \$OPTION{'authpassword'}, "authpassword=s" => \$OPTION{'authpassword'}, "u|username=s" => \$OPTION{'snmp-auth-user'},
"x=s" => \$OPTION{'privprotocol'}, "privprotocol=s" => \$OPTION{'privprotocol'}, "p|authpassword|password=s" => \$OPTION{'snmp-auth-password'},
"X=s" => \$OPTION{'privpassword'}, "privpassword=s" => \$OPTION{'privpassword'}, "k|authkey=s" => \$OPTION{'snmp-auth-key'},
"v=s" => \$OPTION{'snmpversion'}, "snmp=s" => \$OPTION{'snmpversion'}, "authprotocol=s" => \$OPTION{'snmp-auth-protocol'},
"C=s" => \$OPTION{'snmpcomm'}, "community=s" => \$OPTION{'snmpcomm'}, "privpassword=s" => \$OPTION{'snmp-priv-password'},
"H=s" => \$OPTION{'host'}, "host=s" => \$OPTION{'host'}, "privkey=s" => \$OPTION{'snmp-priv-key'},
"W=s" => \$OPTION{'warning_table'}, "warning_table=s" => \$OPTION{'warning_table'}, "privprotocol=s" => \$OPTION{'snmp-priv-protocol'},
"T=s" => \$OPTION{'critical_table'}, "critical_table=s" => \$OPTION{'critical_table'}, "maxrepetitions=s" => \$OPTION{'maxrepetitions'},
"O=s" => \$OPTION{'ok_table'}, "ok_table=s" => \$OPTION{'ok_table'}, "64-bits" => \$OPTION{'64-bits'},
"o=s" => \$OPTION{'oid'}, "oid=s" => \$OPTION{'oid'}, "h" => \$OPTION{'help'}, "help" => \$OPTION{'help'},
"debug" => \$OPTION{'debug'}, "V" => \$OPTION{'pluginversion'}, "version" => \$OPTION{'pluginversion'},
"f=s" => \$OPTION{'output'}, "output=s" => \$OPTION{'output'}, "W=s" => \$OPTION{'warning_table'}, "warning_table=s" => \$OPTION{'warning_table'},
"m=s" => \$OPTION{'metric'}, "metric=s" => \$OPTION{'metric'}); "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 $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 = ();
my @ok_table = (); my @ok_table = ();
if ($OPTION{'critical_table'}) { if ($OPTION{'critical_table'}) {
@critical_table = split(/\,/, $OPTION{'critical_table'}); @critical_table = split(/\,/, $OPTION{'critical_table'});
} }
if ($OPTION{'warning_table'}) { if ($OPTION{'warning_table'}) {
@warning_table = split(/\,/, $OPTION{'warning_table'}); @warning_table = split(/\,/, $OPTION{'warning_table'});
} }
if ($OPTION{'ok_table'}) { 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 0.1"); print("$PROGNAME 0.1");
@ -107,121 +110,92 @@ 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'}; exit $ERRORS{'UNKNOWN'};
}
if (!$OPTION{'oid'}) {
print_usage();
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'}; exit $ERRORS{'UNKNOWN'};
} else{
print("Output is not correctly set \n");
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'});
my $currentValue = $result->{$OPTION{'oid'}};
if (!defined($result)) {
printf("UNKNOWN: %s.\n", $session->error);
$session->close;
exit $ERRORS{'UNKNOWN'};
}
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"; $returnValue = "warningvalue" if($OPTION{'debug'});
$returnValue = "warningvalue" if($OPTION{'debug'});
################################################################# #################################################################
# If personnal thresholds are set for warning and / or critical # # If personnal thresholds 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;
while ($i < $max_ok) { while ($i < $max_ok) {
print "OK[$i]: $ok_table[$i] / returnValue = $returnValue \n" if($OPTION{'debug'}); print "OK[$i]: $ok_table[$i] / returnValue = $returnValue \n" if($OPTION{'debug'});
if($ok_table[$i] eq $returnValue) { if($ok_table[$i] eq $returnValue) {
$status = "OK"; $status = "OK";
$state = $ok_table[$i]; $state = $ok_table[$i];
} }
$i++; $i++;
} }
} }
if ($OPTION{'warning_table'}) { if ($OPTION{'warning_table'}) {
my $max_warn= scalar(@warning_table); my $max_warn= scalar(@warning_table);
my $i = 0; my $i = 0;
while ($i < $max_warn) { while ($i < $max_warn) {
print "Warning[$i]: $warning_table[$i] / returnValue = $returnValue \n" if($OPTION{'debug'}); print "Warning[$i]: $warning_table[$i] / returnValue = $returnValue \n" if($OPTION{'debug'});
if($warning_table[$i] eq $returnValue) { if($warning_table[$i] eq $returnValue) {
$status = "WARNING"; $status = "WARNING";
$state = $warning_table[$i]; $state = $warning_table[$i];
} }
$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++;
} }
} }
print(" Statut = $status \n ") if($OPTION{'debug'}); print(" Statut = $status \n ") if($OPTION{'debug'});
printf($output."\n",$state,$returnValue); printf($output."\n",$state,$returnValue);
exit $ERRORS{$status}; exit $ERRORS{$status};
} }
} else { } else {
print "CRITICAL Host unavailable\n"; print "CRITICAL Host unavailable\n";
exit $ERRORS{'CRITICAL'}; exit $ERRORS{'CRITICAL'};
@ -231,31 +205,35 @@ 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 " -f (--output) \t Output format (ex : -f \"My metric's percentage value = %f %%\" \n";
print " -X (--privpassword)\t passphrase (v3) \n"; print " -V (--version) \t Plugin version\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 " -h (--help) \t usage help\n"; print " -h (--help) \t usage help\n";
} }
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,60 +40,65 @@
# #
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|authpassword|password=s" => \$OPTION{'snmp-auth-password'},
"p|password=s" => \$OPTION{'snmp-auth-passwd'}, "k|authkey=s" => \$OPTION{'snmp-auth-key'},
"64-bits" => \$OPTION{'64-bits'}, "authprotocol=s" => \$OPTION{'snmp-auth-protocol'},
"maxrepetitions=s" => \$OPTION{'maxrepetitions'}, "privpassword=s" => \$OPTION{'snmp-priv-password'},
"disable-warn-state" => \$OPTION{'disable-warn-state'}, "privkey=s" => \$OPTION{'snmp-priv-key'},
"h" => \$opt_h, "help" => \$opt_h, "privprotocol=s" => \$OPTION{'snmp-priv-protocol'},
"s" => \$opt_s, "show" => \$opt_s, "maxrepetitions=s" => \$OPTION{'maxrepetitions'},
"V" => \$opt_V, "version" => \$opt_V, "64-bits" => \$OPTION{'64-bits'},
"i=s" => \$opt_i, "interface=s" => \$opt_i,
"n" => \$opt_n, "name" => \$opt_n, "disable-warn-state" => \$OPTION{'disable-warn-state'},
"w=s" => \$opt_w, "warning=s" => \$opt_w, "h" => \$opt_h, "help" => \$opt_h,
"c=s" => \$opt_c, "critical=s" => \$opt_c, "s" => \$opt_s, "show" => \$opt_s,
"T=s" => \$opt_T, "r" => \$opt_r, "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, "S" => \$opt_S,
"a=i" => \$opt_a, "cache=s" => \$opt_a); "a=i" => \$opt_a, "cache=s" => \$opt_a);
if ($opt_V) { if ($opt_V) {
print_revision($PROGNAME,'$Revision: 1.3 $'); print_revision($PROGNAME,'$Revision: 1.3 $');
@ -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,11 +46,18 @@ 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",
'output' => 'The value is %f', 'metric' =>'value', 'unit' => 'nounit', 'divide' => 1); "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 %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3);
my $prefix = ""; my $prefix = "";
@ -60,34 +66,39 @@ 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'},
"V" => \$OPTION{'pluginversion'}, "version" => \$OPTION{'pluginversion'}, "C|community=s" => \$OPTION{'snmp-community'},
"u=s" => \$OPTION{'username'}, "username=s" => \$OPTION{'username'}, "v|snmp|snmp-version=s" => \$OPTION{'snmp-version'},
"a=s" => \$OPTION{'authprotocol'}, "authprotocol=s" => \$OPTION{'authprotocol'}, "P|snmpport|snmp-port=i" => \$OPTION{'snmp-port'},
"A=s" => \$OPTION{'authpassword'}, "authpassword=s" => \$OPTION{'authpassword'}, "u|username=s" => \$OPTION{'snmp-auth-user'},
"x=s" => \$OPTION{'privprotocol'}, "privprotocol=s" => \$OPTION{'privprotocol'}, "p|authpassword|password=s" => \$OPTION{'snmp-auth-password'},
"X=s" => \$OPTION{'privpassword'}, "privpassword=s" => \$OPTION{'privpassword'}, "k|authkey=s" => \$OPTION{'snmp-auth-key'},
"v=s" => \$OPTION{'snmpversion'}, "snmp=s" => \$OPTION{'snmpversion'}, "authprotocol=s" => \$OPTION{'snmp-auth-protocol'},
"C=s" => \$OPTION{'snmpcomm'}, "community=s" => \$OPTION{'snmpcomm'}, "privpassword=s" => \$OPTION{'snmp-priv-password'},
"w=s" => \$OPTION{'warning'}, "warning=s" => \$OPTION{'warning'}, "privkey=s" => \$OPTION{'snmp-priv-key'},
"c=s" => \$OPTION{'critical'}, "critical=s" => \$OPTION{'critical'}, "privprotocol=s" => \$OPTION{'snmp-priv-protocol'},
"H=s" => \$OPTION{'host'}, "host=s" => \$OPTION{'host'}, "maxrepetitions=s" => \$OPTION{'maxrepetitions'},
"W=s" => \$OPTION{'warning_table'}, "warning_table=s" => \$OPTION{'warning_table'}, "64-bits" => \$OPTION{'64-bits'},
"T=s" => \$OPTION{'critical_table'}, "critical_table=s" => \$OPTION{'critical_table'},
"O=s" => \$OPTION{'ok_table'}, "ok_table=s" => \$OPTION{'ok_table'}, "h" => \$OPTION{'help'}, "help" => \$OPTION{'help'},
"o=s" => \$OPTION{'oid'}, "oid=s" => \$OPTION{'oid'}, "V" => \$OPTION{'pluginversion'}, "version" => \$OPTION{'pluginversion'},
"t=s" => \$OPTION{'type'}, "type=s" => \$OPTION{'type'}, "w=s" => \$OPTION{'warning'}, "warning=s" => \$OPTION{'warning'},
"d=s" => \$OPTION{'divide'}, "divide=s" => \$OPTION{'divide'}, "c=s" => \$OPTION{'critical'}, "critical=s" => \$OPTION{'critical'},
"U=s" => \$OPTION{'unit'}, "unit=s" => \$OPTION{'unit'}, "W=s" => \$OPTION{'warning_table'}, "warning_table=s" => \$OPTION{'warning_table'},
"max=s" => \$OPTION{'max'}, "T=s" => \$OPTION{'critical_table'}, "critical_table=s" => \$OPTION{'critical_table'},
"convert" => \$OPTION{'convert'}, "O=s" => \$OPTION{'ok_table'}, "ok_table=s" => \$OPTION{'ok_table'},
"debug" => \$OPTION{'debug'}, "o=s" => \$OPTION{'oid'}, "oid=s" => \$OPTION{'oid'},
"min=s" => \$OPTION{'min'}, "t=s" => \$OPTION{'type'}, "type=s" => \$OPTION{'type'},
"base=s" => \$OPTION{'base'}, "d=s" => \$OPTION{'divide'}, "divide=s" => \$OPTION{'divide'},
"64-bits=s" => \$OPTION{'64-bits'}, "U=s" => \$OPTION{'unit'}, "unit=s" => \$OPTION{'unit'},
"f=s" => \$OPTION{'output'}, "output=s" => \$OPTION{'output'}, "max=s" => \$OPTION{'max'},
"m=s" => \$OPTION{'metric'}, "metric=s" => \$OPTION{'metric'} "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 # For counter metric type
@ -111,84 +122,65 @@ my @warning_table = ();
my @ok_table = (); my @ok_table = ();
if ($OPTION{'critical_table'}){ if ($OPTION{'critical_table'}){
@critical_table = split(/\,/, $OPTION{'critical_table'}); @critical_table = split(/\,/, $OPTION{'critical_table'});
} }
if ($OPTION{'warning_table'}){ if ($OPTION{'warning_table'}){
@warning_table = split(/\,/, $OPTION{'warning_table'}); @warning_table = split(/\,/, $OPTION{'warning_table'});
} }
if ($OPTION{'ok_table'}){ 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'};
}
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");
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 # Store option values in simpler variables
my $sessionType = 1; if ($OPTION{'divide'} ne "" && $OPTION{'metric'} ne "" && $OPTION{'unit'} ne "" && $OPTION{'output'} ne "" && $OPTION{'min'} ne "" && $OPTION{'max'} ne "") {
if ($OPTION{'snmpversion'} =~ /3/) { $metricsname = $OPTION{'metric'};
$unit = $OPTION{'unit'};
$sessionType = Centreon::SNMP::Utils->checkSessiontype($OPTION{'username'},$OPTION{'authprotocol'},$OPTION{'authpassword'},$OPTION{'privprotocol'},$OPTION{'privpassword'}); # Output Verification?
exit $ERRORS{'UNKNOWN'} if(!$sessionType); $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"; my $DS_type = "GAUGE";
@ -203,190 +195,173 @@ 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 ");
exit $ERRORS{'UNKNOWN'}; exit $ERRORS{'UNKNOWN'};
} }
if (defined($currentValue)){ if (defined($currentValue)){
if ($currentValue !~ /^[-+]?[0-9]*\.?[0-9]*/) { if ($currentValue !~ /^[-+]?[0-9]*\.?[0-9]*/) {
print "Snmp returned value isn't numeric (signed/float values included) .\n"; print "Snmp returned value isn't numeric (signed/float values included) .\n";
exit $ERRORS{'UNKNOWN'}; exit $ERRORS{'UNKNOWN'};
} }
} }
# 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){
my @cache;
if (-e $cacheFile){ open(FILE,"<".$cacheFile);
my @cache; my $countLine = 0;
open(FILE,"<".$cacheFile); my $row = <FILE>;
my $countLine = 0; @cache = split(/\;/, $row);
my $row = <FILE>; $previousTime = $cache[0];
@cache = split(/\;/, $row); $previousValue = $cache[1];
$previousTime = $cache[0]; close(FILE);
$previousValue = $cache[1]; # Set new values in cache file
close(FILE); open(FILE,">".$cacheFile);
# Set new values in cache file print FILE $currentTime.";".$currentValue;
open(FILE,">".$cacheFile); close(FILE);
print FILE $currentTime.";".$currentValue; } else {
close(FILE); #If the file doesnt exist, a new file is created and values are inserted
unless (open(FILE,">".$cacheFile)){
} else { print "Check temporary file's rights : ".$cacheFile."...\n";
#If the file doesnt exist, a new file is created and values are inserted exit $ERRORS{"UNKNOWN"};
unless (open(FILE,">".$cacheFile)){ }
print "Check temporary file's rights : ".$cacheFile."...\n"; my $currentTime = time();
exit $ERRORS{"UNKNOWN"}; print FILE $currentTime.";".$currentValue;
} print("Buffer in creation . . . please wait \n");
my $currentTime = time(); close(FILE);
print FILE $currentTime.";".$currentValue; exit $ERRORS{"OK"};
print("Buffer in creation . . . please wait \n"); }
exit $ERRORS{"OK"};
close(FILE);
}
} }
#=== 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";
$returnValue = 7 if($OPTION{'debug'}); $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 personnal tresholds are set for warning and / or critical # if ($OPTION{'ok_table'}) {
############################################################### my $max_ok= scalar(@ok_table);
if ($OPTION{'warning_table'} || $OPTION{'critical_table'} || $OPTION{'ok_table'}) { my $i = 0;
print "Mode personal threshold ON \n" if($OPTION{'debug'}); 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;
if ($OPTION{'ok_table'}) { while ($i < $max_warn) {
my $max_ok= scalar(@ok_table); print "Warning[$i]: $warning_table[$i] / returnValue = $returnValue \n" if($OPTION{'debug'});
my $i = 0; 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};
}
while ($i < $max_ok) { if ($DS_type eq 'COUNTER') {
print "OK[$i]: $ok_table[$i] / returnValue = $returnValue \n" if($OPTION{'debug'}); #calculate value for counter metric type
if($ok_table[$i] == $returnValue) { # if counter has been reseted between 2 checks
$status = "OK"; if ($currentValue - $previousValue < 0) {
$state = $ok_table[$i+1]; if (defined($OPTION{'64-bits'})) {
} $returnValue = ((18446744073709551616) - $previousValue + $currentValue)/($currentTime - $previousTime);
$i = $i+2; } else {
} $returnValue = ((4294967296) - $previousValue + $currentValue) / ($currentTime - $previousTime);
} }
if ($OPTION{'warning_table'}){ } else {
my $max_warn= scalar(@warning_table); $returnValue = ($currentValue - $previousValue) / ($currentTime - $previousTime);
my $i = 0; }
}
while ($i < $max_warn) { my $i =0;
print "Warning[$i]: $warning_table[$i] / returnValue = $returnValue \n" if($OPTION{'debug'}); my $perfdata = $returnValue;
if($warning_table[$i] == $returnValue) { while ($returnValue > $OPTION{'base'}) {
$status = "WARNING"; $returnValue = $returnValue / $OPTION{'base'};
$state = $warning_table[$i+1]; $i++;
} }
$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($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) { 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";
exit $ERRORS{$status}; exit $ERRORS{$status};
} elsif ($perfdata >= $warning && $perfdata < $critical) { } elsif ($perfdata >= $warning && $perfdata < $critical) {
printf("WARNING :".$output." ".$prefix.$unit."|".$metricsname."=".$perfdata.$unit.";".$warning.";".$critical.";".$min.";".$max."\n",$returnValue); printf("WARNING :".$output." ".$prefix.$unit."|".$metricsname."=".$perfdata.$unit.";".$warning.";".$critical.";".$min.";".$max."\n",$returnValue);
$status = "WARNING"; $status = "WARNING";
exit $ERRORS{$status}; exit $ERRORS{$status};
} elsif ($perfdata >= $critical) { } elsif ($perfdata >= $critical) {
printf("CRITICAL :".$output." ".$prefix.$unit."|".$metricsname."=".$perfdata.$unit.";".$warning.";".$critical.";".$min.";".$max."\n",$returnValue); printf("CRITICAL :".$output." ".$prefix.$unit."|".$metricsname."=".$perfdata.$unit.";".$warning.";".$critical.";".$min.";".$max."\n",$returnValue);
$status = "CRITICAL"; $status = "CRITICAL";
exit $ERRORS{$status}; exit $ERRORS{$status};
} }
} else { } else {
print "CRITICAL Host unavailable\n"; print "CRITICAL Host unavailable\n";
@ -394,43 +369,47 @@ 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"; print " --max \t \t max 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 " --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 " \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 " -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"; print " -h (--help) \t usage help\n";
} }
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,49 +47,56 @@ 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",
'output' => '%.02f', 'metric' =>'value', 'unit' => 'nounit', 'divide' => 1); "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 %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'},
"V" => \$OPTION{'pluginversion'}, "version" => \$OPTION{'pluginversion'}, "C|community=s" => \$OPTION{'snmp-community'},
"u=s" => \$OPTION{'username'}, "username=s" => \$OPTION{'username'}, "v|snmp|snmp-version=s" => \$OPTION{'snmp-version'},
"a=s" => \$OPTION{'authprotocol'}, "authprotocol=s" => \$OPTION{'authprotocol'}, "P|snmpport|snmp-port=i" => \$OPTION{'snmp-port'},
"A=s" => \$OPTION{'authpassword'}, "authpassword=s" => \$OPTION{'authpassword'}, "u|username=s" => \$OPTION{'snmp-auth-user'},
"x=s" => \$OPTION{'privprotocol'}, "privprotocol=s" => \$OPTION{'privprotocol'}, "p|authpassword|password=s" => \$OPTION{'snmp-auth-password'},
"X=s" => \$OPTION{'privpassword'}, "privpassword=s" => \$OPTION{'privpassword'}, "k|authkey=s" => \$OPTION{'snmp-auth-key'},
"v=s" => \$OPTION{'snmpversion'}, "snmp=s" => \$OPTION{'snmpversion'}, "authprotocol=s" => \$OPTION{'snmp-auth-protocol'},
"C=s" => \$OPTION{'snmpcomm'}, "community=s" => \$OPTION{'snmpcomm'}, "privpassword=s" => \$OPTION{'snmp-priv-password'},
"w=s" => \$OPTION{'warning'}, "warning=s" => \$OPTION{'warning'}, "privkey=s" => \$OPTION{'snmp-priv-key'},
"c=s" => \$OPTION{'critical'}, "critical=s" => \$OPTION{'critical'}, "privprotocol=s" => \$OPTION{'snmp-priv-protocol'},
"H=s" => \$OPTION{'host'}, "host=s" => \$OPTION{'host'}, "maxrepetitions=s" => \$OPTION{'maxrepetitions'},
"o=s" => \$OPTION{'oid'}, "oid=s" => \$OPTION{'oid'}, "64-bits" => \$OPTION{'64-bits'},
"t=s" => \$OPTION{'type'}, "type=s" => \$OPTION{'type'}, "h" => \$OPTION{'help'}, "help" => \$OPTION{'help'},
"U=s" => \$OPTION{'unit'}, "unit=s" => \$OPTION{'unit'}, "V" => \$OPTION{'pluginversion'}, "version" => \$OPTION{'pluginversion'},
"W=s" => \$OPTION{'warning_table'}, "warning_table=s" => \$OPTION{'warning_table'}, "w=s" => \$OPTION{'warning'}, "warning=s" => \$OPTION{'warning'},
"T=s" => \$OPTION{'critical_table'}, "critical_table=s" => \$OPTION{'critical_table'}, "c=s" => \$OPTION{'critical'}, "critical=s" => \$OPTION{'critical'},
"O=s" => \$OPTION{'ok_table'}, "ok_table=s" => \$OPTION{'ok_table'}, "o=s" => \$OPTION{'oid'}, "oid=s" => \$OPTION{'oid'},
"convert" => \$OPTION{'convert'}, "t=s" => \$OPTION{'type'}, "type=s" => \$OPTION{'type'},
"debug" => \$OPTION{'debug'}, "U=s" => \$OPTION{'unit'}, "unit=s" => \$OPTION{'unit'},
"min=s" => \$OPTION{'min'}, "W=s" => \$OPTION{'warning_table'}, "warning_table=s" => \$OPTION{'warning_table'},
"max=s" => \$OPTION{'max'}, "T=s" => \$OPTION{'critical_table'}, "critical_table=s" => \$OPTION{'critical_table'},
"base=s" => \$OPTION{'base'}, "O=s" => \$OPTION{'ok_table'}, "ok_table=s" => \$OPTION{'ok_table'},
"64-bits" => \$OPTION{'64-bits'}, "convert" => \$OPTION{'convert'},
"f=s" => \$OPTION{'output'}, "output=s" => \$OPTION{'output'}, "debug" => \$OPTION{'debug'},
"m=s" => \$OPTION{'metric'}, "metric=s" => \$OPTION{'metric'}); "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 $metricsname = undef;
my $unit = undef; my $unit = undef;
@ -112,439 +119,406 @@ my @ok_table = ();
#Table used when personnal threshold are set #Table used when personnal threshold are set
if($OPTION{'critical_table'}){ if($OPTION{'critical_table'}){
@critical_table = split(/\,/, $OPTION{'critical_table'}); @critical_table = split(/\,/, $OPTION{'critical_table'});
} }
if($OPTION{'warning_table'}){ if($OPTION{'warning_table'}){
@warning_table = split(/\,/, $OPTION{'warning_table'}); @warning_table = split(/\,/, $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'};
} }
if (defined($OPTION{'help'})) { 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 "" ){
$metricsname = $OPTION{'metric'}; $metricsname = $OPTION{'metric'};
$unit = $OPTION{'unit'}; $unit = $OPTION{'unit'};
#Output Verification? #Output Verification?
$output = $OPTION{'output'}; $output = $OPTION{'output'};
#check parameter format #check parameter format
if ($OPTION{'base'} !~ /^[0-9]*\.?[0-9]*$/) {
print(" Base option should be a numerical \n");
exit $ERRORS{'UNKNOWN'};
}
if ($OPTION{'base'} !~ /^[0-9]*\.?[0-9]*$/) { if ($OPTION{'min'} !~ /^[0-9]*\.?[0-9]*$/) {
print(" Base option should be a numerical \n"); print(" Min option should be a numerical \n");
exit $ERRORS{'UNKNOWN'}; exit $ERRORS{'UNKNOWN'};
} }
if ($OPTION{'max'} !~ /^[0-9]*\.?[0-9]*$/){
print(" Max option should be a numerical \n");
exit $ERRORS{'UNKNOWN'};
}
if ($OPTION{'min'} !~ /^[0-9]*\.?[0-9]*$/) { if ($OPTION{'warning'} !~ /^[0-9]*\.?[0-9]*$/ || $OPTION{'critical'} !~ /^[0-9]*\.?[0-9]*$/) {
print(" Min option should be a numerical \n"); print(" Option warning &/or critical should be numerical \n");
exit $ERRORS{'UNKNOWN'}; 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]*$/) { $min = $OPTION{'min'};
print(" Option warning &/or critical should be numerical \n"); $max = $OPTION{'max'};
exit $ERRORS{'UNKNOWN'}; $divide = $OPTION{'divide'};
} } else {
print("One or more arguments are not set \n");
$min = $OPTION{'min'}; exit $ERRORS{'UNKNOWN'};
$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"; my $DS_type = "GAUGE";
if ($OPTION{'type'} =~ m/GAUGE/i) { if ($OPTION{'type'} =~ m/GAUGE/i) {
$DS_type = "GAUGE"; $DS_type = "GAUGE";
} elsif ($OPTION{'type'} =~ m/COUNTER/i) { } elsif ($OPTION{'type'} =~ m/COUNTER/i) {
$DS_type = "COUNTER"; $DS_type = "COUNTER";
} }
my $critical = $1 if ($OPTION{'critical'} =~ /([0-9]+)/); my $critical = $1 if ($OPTION{'critical'} =~ /([0-9]+)/);
my $warning = $1 if ($OPTION{'warning'} =~ /([0-9]+)/); my $warning = $1 if ($OPTION{'warning'} =~ /([0-9]+)/);
if ($critical < $warning){ if ($critical < $warning){
print "(--critical) must be superior or equal to (--warning)"; print "(--critical) must be superior or equal to (--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);
$i++; $i++;
} }
if ($OPTION{'debug'}) { if ($OPTION{'debug'}) {
my $size= scalar(@oid_list); my $size= scalar(@oid_list);
print(" OID received : \n"); print(" OID received : \n");
for ($i = 0; $i < $size ; $i++) { for ($i = 0; $i < $size ; $i++) {
print("oid_list[$i] : $oid_list[$i] \n"); 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 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'});
#If file exist #If file exist
if (-e $cacheFile) { if (-e $cacheFile) {
open(FILE,"<".$cacheFile); open(FILE,"<".$cacheFile);
my $row = <FILE>; my $row = <FILE>;
@cache = split(/;/, $row); @cache = split(/;/, $row);
my $size= scalar(@cache); my $size= scalar(@cache);
print("File exist \n") if($OPTION{'debug'}); print("File exist \n") if($OPTION{'debug'});
$previousTime= $cache[0]; $previousTime= $cache[0];
#Get the previous values stored in cachefile #Get the previous values stored in cachefile
for ($i = 1; $i < $size ; $i=$i+2) { for ($i = 1; $i < $size ; $i=$i+2) {
print("OID : $cache[$i] ; Last Value : $cache[$i+1] ; \n") if($OPTION{'debug'}); print("OID : $cache[$i] ; Last Value : $cache[$i+1] ; \n") if($OPTION{'debug'});
$previousValues{$cache[$i]} = $cache[$i+1]; $previousValues{$cache[$i]} = $cache[$i+1];
} }
close(FILE); close(FILE);
#Set new values in cache file #Set new values in cache file
open(FILE,">".$cacheFile); open(FILE,">".$cacheFile);
$size= scalar(@oid_list); $size= scalar(@oid_list);
print FILE $currentTime.";"; print FILE $currentTime.";";
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;
print FILE $oid_list[$i].";".$currentValue.";"; print FILE $oid_list[$i].";".$currentValue.";";
print("oid_list[$i] : $oid_list[$i] ; Current Value : $currentValue ; \n") if($OPTION{'debug'}); print("oid_list[$i] : $oid_list[$i] ; Current Value : $currentValue ; \n") if($OPTION{'debug'});
} }
close(FILE); close(FILE);
} else { } else {
my $size= scalar(@oid_list); my $size= scalar(@oid_list);
print("File doesn't exist \n") if($OPTION{'debug'}); print("File doesn't exist \n") if($OPTION{'debug'});
#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)) {
print "Check temporary file's or existence rights : ".$cacheFile."...\n"; print "Check temporary file's or existence rights : ".$cacheFile."...\n";
exit $ERRORS{"UNKNOWN"}; exit $ERRORS{"UNKNOWN"};
} }
$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'});
} }
print("Buffer in creation . . . please wait \n"); print("Buffer in creation . . . please wait \n");
close(FILE); close(FILE);
exit $ERRORS{"OK"}; exit $ERRORS{"OK"};
} }
} else { } else {
#Get values for each OID stored in table oid_list[] if not a counter type #Get values for each OID stored in table oid_list[] if not a counter type
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;
print("oid_list[$i] : $oid_list[$i] ; Current Value : $currentValue ; \n") if($OPTION{'debug'}); print("oid_list[$i] : $oid_list[$i] ; Current Value : $currentValue ; \n") if($OPTION{'debug'});
} }
} }
print("Taille \%currentValues : ".keys(%currentValues)."\n") if($OPTION{'debug'}); print("Taille \%currentValues : ".keys(%currentValues)."\n") if($OPTION{'debug'});
#=== Plugin returned value treatments ==== #=== Plugin returned value treatments ====
if (keys(%currentValues) > 0) { if (keys(%currentValues) > 0) {
my $status = "UNKNOWN"; my $status = "UNKNOWN";
my $state= "unknownState"; my $state= "unknownState";
my %returnValues = %currentValues; my %returnValues = %currentValues;
my %state; my %state;
my $returnValue; my $returnValue;
my $size= keys(%currentValues); my $size= keys(%currentValues);
my $output_message=""; my $output_message="";
my $output_warning=""; my $output_warning="";
my $output_critical=""; my $output_critical="";
my $perf_output_message = "|"; my $perf_output_message = "|";
#If personnal tresholds are set #If personnal tresholds are set
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;
while($i < $max_ok) { while($i < $max_ok) {
for(my $u=0; $u < $size;$u++) { for(my $u=0; $u < $size;$u++) {
print "OK[$i]: $ok_table[$i] / returnValue = $returnValues{$oid_list[$i]} \n" if($OPTION{'debug'}); print "OK[$i]: $ok_table[$i] / returnValue = $returnValues{$oid_list[$i]} \n" if($OPTION{'debug'});
if($ok_table[$i] == $returnValues{$oid_list[$u]}) { if($ok_table[$i] == $returnValues{$oid_list[$u]}) {
$status = "OK"; $status = "OK";
$state{$oid_list[$i]} = $ok_table[$i+1]; $state{$oid_list[$i]} = $ok_table[$i+1];
} }
} }
$i = $i+2; $i = $i+2;
} }
} }
if($OPTION{'warning_table'}) { if($OPTION{'warning_table'}) {
my $max_warn= scalar(@warning_table); my $max_warn= scalar(@warning_table);
my $i = 0; my $i = 0;
while($i < $max_warn) { while($i < $max_warn) {
for(my $u=0; $u < $size;$u++) { for(my $u=0; $u < $size;$u++) {
print "Warning[$i]: $warning_table[$i] / returnValue = $returnValues{$oid_list[$u]} \n" if($OPTION{'debug'}); print "Warning[$i]: $warning_table[$i] / returnValue = $returnValues{$oid_list[$u]} \n" if($OPTION{'debug'});
if($warning_table[$i] == $returnValues{$oid_list[$u]}) { if($warning_table[$i] == $returnValues{$oid_list[$u]}) {
print("Warning match \n") if($OPTION{'debug'}); print("Warning match \n") if($OPTION{'debug'});
$status = "WARNING"; $status = "WARNING";
$state{$oid_list[$u]} = $warning_table[$i+1]; $state{$oid_list[$u]} = $warning_table[$i+1];
$output_warning .= "OID.".$oid_list[$u].": ".$warning_table[$i+1]."; "; $output_warning .= "OID.".$oid_list[$u].": ".$warning_table[$i+1]."; ";
} }
} }
$i = $i+2; $i = $i+2;
} }
$output_warning = "(Warning)".$output_warning if($status eq "WARNING"); $output_warning = "(Warning)".$output_warning if($status eq "WARNING");
} }
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){
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};
}
}
for(my $u=0; $u < $size;$u++){ #calculate value for counter metric type
print "Critical[$i] = $critical_table[$i] / returnValue = $returnValues{$oid_list[$u]} \n" if($OPTION{'debug'}); #if counter has been reseted between 2 checks
if($critical_table[$i] == $returnValues{$oid_list[$u]}) { if ($DS_type eq 'COUNTER') {
print("Critical match \n") if($OPTION{'debug'}); for (my $i = 0; $i < $size ; $i++) {
$status = "CRITICAL"; if ($currentValues{$oid_list[$i]} - $previousValues{$oid_list[$i]} < 0) {
$state{$oid_list[$u]} = $critical_table[$i+1]; if (defined($OPTION{'64-bits'})) {
$output_critical .= "OID.".$oid_list[$u].": ".$critical_table[$i+1]."; "; $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 );
$i = $i + 2; }
} } else {
$output_critical = "(Critical) ".$output_critical if($status eq "CRITICAL"); $returnValues{$oid_list[$i]} = ($currentValues{$oid_list[$i]} - $previousValues{$oid_list[$i]}) / ( $currentTime - $previousTime );
} }
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 ($OPTION{'debug'}) {
#if counter has been reseted between 2 checks for ( my $i = 0; $i < $size ; $i++) {
if ($DS_type eq 'COUNTER') { print(" ValuetoReturn OiD: $oid_list[$i] : $returnValues{$oid_list[$i]} \n");
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'}) { #Set in Ok Critical and Warning hashes the value corresponding to the different status.
for ( my $i = 0; $i < $size ; $i++) { #Finally, if Critical and Warning are empty, the plugin return OK, else , the plugin returns the value in Critical and Warning hashes
print(" ValuetoReturn OiD: $oid_list[$i] : $returnValues{$oid_list[$i]} \n"); my %Ok;
} my %Critical;
} my %Warning;
my %perfdata;
#Set in Ok Critical and Warning hashes the value corresponding to the different status. if(defined($OPTION{'convert'})) {
#Finally, if Critical and Warning are empty, the plugin return OK, else , the plugin returns the value in Critical and Warning hashes $warning = ($OPTION{'warning'} * $max)/100;
my %Ok; $critical = ($OPTION{'critical'} * $max)/100;
my %Critical; } else {
my %Warning; $warning = $OPTION{'warning'};
my %perfdata; $critical = $OPTION{'critical'};
}
if(defined($OPTION{'convert'})) { #Test for each OID if the value returned is warning / critical or ok
$warning = ($OPTION{'warning'} * $max)/100; for (my $i = 0; $i < $size ; $i++) {
$critical = ($OPTION{'critical'} * $max)/100; if ($returnValues{$oid_list[$i]} < $warning) {
} else { $Ok{$oid_list[$i]} = $returnValues{$oid_list[$i]};
$warning = $OPTION{'warning'}; } elsif ($warning != 0 && $warning <= $returnValues{$oid_list[$i]} && $returnValues{$oid_list[$i]} < $critical) {
$critical = $OPTION{'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]};
}
}
#Test for each OID if the value returned is warning / critical or ok if($OPTION{'debug'}){
for (my $i = 0; $i < $size ; $i++) { print("%Ok size : ".keys(%Ok)." \t %Warning size : ".keys(%Warning)." \t %Critical size : ".keys(%Critical)." \n");
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'}){ if(keys(%Critical) == 0 && keys(%Warning) == 0) {
print("%Ok size : ".keys(%Ok)." \t %Warning size : ".keys(%Warning)." \t %Critical size : ".keys(%Critical)." \n"); $output_message = "All values of table $OPTION{'oid'} are OK";
} $state = "OK";
}
if(keys(%Critical) == 0 && keys(%Warning) == 0) { $i = 0;
$output_message = "All values of table $OPTION{'oid'} are OK"; %perfdata = %returnValues;
$state = "OK"; #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]});
}
}
$i = 0; for (my $i = 0; $i < $size ; $i++){
%perfdata = %returnValues; $perf_output_message .=$metricsname.".".$oid_list[$i]."=".$perfdata{$oid_list[$i]}.$unit.";".$warning.";".$critical.";".$min.";".$max." ";
#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++){ if(keys(%Critical)!=0) {
$perf_output_message .=$metricsname.".".$oid_list[$i]."=".$perfdata{$oid_list[$i]}.$unit.";".$warning.";".$critical.";".$min.";".$max." "; $state = "CRITICAL";
} } elsif(keys(%Warning)!=0) {
$state = "WARNING";
if(keys(%Critical)!=0) { }
$state = "CRITICAL"; print($output_message.$perf_output_message."\n");
}elsif(keys(%Warning)!=0) { exit $ERRORS{$state};
$state = "WARNING";
}
print($output_message.$perf_output_message."\n");
exit $ERRORS{$state};
} else { } else {
print "CRITICAL Host unavailable\n"; print "CRITICAL Host unavailable\n";
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 " -t (--type) \t Data Source Type (GAUGE or COUNTER) (GAUGE by default)\n"; print " 3 for SNMP v3\n";
print " -o (--oid) \t OID to check\n"; print " -P (--snmp-port) SNMP port (default: 161)\n";
print " -u (--username) \t snmp v3 username \n"; print " -k (--key) snmp V3 key\n";
print " -a (--authprotocol) \t protocol MD5/SHA1 (v3)\n"; print " -u (--username) snmp V3 username \n";
print " -A (--authpassword) \t password (v3) \n"; print " -p (--password) snmp V3 password\n";
print " -x (--privprotocol) \t encryption system (DES/AES)(v3) \n"; print " --authprotocol protocol MD5/SHA1 (v3)\n";
print " -X (--privpassword)\t passphrase (v3) \n"; print " --privprotocol encryption system (DES/AES)(v3) \n";
print " -w (--warning) \t Warning level \n"; print " --privpassword passphrase (v3) \n";
print " -c (--critical) \t Critical level \n"; print " --64-bits Use 64 bits OID\n";
print " -W (--wtreshold) \t Personal warning threshold : -W 1,normal,... \n"; print " --maxrepetitions To use when you have the error: 'Message size exceeded buffer maxMsgSize'\n";
print " -T (--ctreshold) \t Personal critical threshold : -T 3,notResponding,4,NotFunctionning,... \n"; print " Work only with SNMP v2c and v3 (Example: --maxrepetitions=1)\n";
print " --convert \t \t If critical and warning, given in %tages, have to be converted regarding to the max value \n"; print " -t (--type) \t Data Source Type (GAUGE or COUNTER) (GAUGE by default)\n";
print " -m (--metric) \t Metric Name\n"; print " -o (--oid) \t OID to check\n";
print " --64-bits \t \t If counter type to use = 64-bits \n"; print " -w (--warning) \t Warning level \n";
print " -U (--unit) \t Metric's unit ( /!\\ for % write %% ) \n"; print " -c (--critical) \t Critical level \n";
print " -f (--output) \t Output format (ex : -f \"%0.2f \" \n"; print " -W (--warning_table) \t Personal warning threshold : -W 1,normal,... \n";
print " --min \t \t min value for the metric (default = 0) \n"; print " -T (--critical_table) \t Personal critical threshold : -T 3,notResponding,4,NotFunctionning,... \n";
print " --max \t \t max value for the metric (default = 0)\n"; print " --convert \t \t If critical and warning, given in %tages, have to be converted regarding to the max value \n";
print " --base \t \t will divide the returned number by \"base\" until it's inferior to it. \n"; print " -m (--metric) \t Metric Name\n";
print " \t \t ex: 2000000 in base 1000 will be transformed to 2M (default 1000) \n"; print " -U (--unit) \t Metric's unit ( /!\\ for % write %% ) \n";
print " -V (--version) \t Plugin version\n"; print " -f (--output) \t Output format (ex : -f \"%0.2f \" \n";
print " -h (--help) \t usage help\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 () { 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";
} }