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
# GPL Licence 2.0.
#
@ -41,112 +41,154 @@ use warnings;
package Centreon::SNMP::Utils;
# This method takes the version argument given by the user and
# return true only if snmp version value is known
sub checkVersion{
my $self = shift;
my $snmpversion = shift;
$snmpversion =~ s/^v//;
if ($snmpversion !~ /1|2c|2|3/) {
print "Wrong SNMP version\n";
return 0;
sub load_oids {
my ($exit_status, $oid_file) = @_;
eval 'require Config::IniFiles;';
if ($@) {
print "Could not load the Perl module 'Config::IniFiles' $@";
exit($exit_status);
}
return 1;
require Config::IniFiles;
unless (-e $oid_file) {
print "Unknown - In centreon.pm :: $oid_file :: $!\n";
exit($exit_status);
}
my %centreon;
tie %centreon, 'Config::IniFiles', ( -file => $oid_file );
return %centreon;
}
# This method take the v3 parameter (username,authprotocol,authpassword,privprotocol,privpassword) in argument and return the session type
# sesssion type = 1 si snmp v1 / v2
# session type = 2 si snmp v3 AuthNoPriv
# session type = 3 si snmp v3 AuthPriv
# session type = 0 si erreur;
sub checkSessiontype{
my $self = shift;
my $username = shift;
my $authprotocol = shift;
my $authpassword = shift;
my $privprotocol = shift;
my $privpassword = shift;
my $sessionType = 1;
sub check_snmp_options {
my ($exit_status, $OPTION) = @_;
my %session_params;
if (defined($authprotocol) && defined($authpassword) && defined($username)) {
if ($authprotocol ne "MD5" && $authprotocol ne "SHA1") {
print "Wrong authentication protocol. Must be MD5 or SHA1 \n";
return 0;
if (!defined($OPTION->{'host'})) {
print "Missing parameter -H (--host).\n";
exit $exit_status;
}
$sessionType = 2;
if (defined($privpassword) && defined($privprotocol)) {
if ($privprotocol ne "DES" && $privprotocol ne "AES") {
print "Wrong encryption protocol. Must be DES or AES\n";
return 0;
$OPTION->{'snmp-version'} =~ s/^v//;
if ($OPTION->{'snmp-version'} !~ /1|2c|2|3/) {
print "Unknown snmp version\n";
exit $exit_status;
}
return 3;
if ($OPTION->{'snmp-version'} eq "3") {
%session_params = (-hostname => $OPTION->{'host'}, -version => $OPTION->{'snmp-version'}, -port => $OPTION->{'snmp-port'});
if (defined($OPTION->{'snmp-auth-password'}) && defined($OPTION->{'snmp-auth-key'})) {
print "Only option -k (--authkey) or -p (--password) is needed for snmp v3\n";
exit $exit_status;
}
} else{
if (!(defined($OPTION->{'snmp-auth-protocol'}) && (defined($OPTION->{'snmp-auth-password'}) || defined($OPTION->{'snmp-auth-key'})) && defined($OPTION->{'snmp-auth-user'}))) {
print "Missing parameter to open SNMPv3 session\n";
return 0;
exit $exit_status;
}
return $sessionType;
if ($OPTION->{'snmp-auth-protocol'} ne "MD5" && $OPTION->{'snmp-auth-protocol'} ne "SHA1") {
print "Wrong authentication protocol. Must be MD5 or SHA1\n";
exit $exit_status;
}
$session_params{-username} = $OPTION->{'snmp-auth-user'};
$session_params{-authprotocol} = $OPTION->{'snmp-auth-protocol'};
if (defined($OPTION->{'snmp-auth-password'})) {
$session_params{-authpassword} = $OPTION->{'snmp-auth-password'};
} else {
$session_params{-authkey} = $OPTION->{'snmp-auth-key'};
}
if ((defined($OPTION->{'snmp-priv-password'}) || defined($OPTION->{'snmp-priv-key'})) && defined($OPTION->{'snmp-priv-protocol'})) {
if ($OPTION->{'snmp-priv-protocol'} ne "DES" && $OPTION->{'snmp-priv-protocol'} ne "AES") {
print "Wrong encryption protocol. Must be DES or AES\n";
exit $exit_status;
}
if (defined($OPTION->{'snmp-priv-password'}) && defined($OPTION->{'snmp-priv-key'})) {
print "Only option --privpassword or --privkey is needed for snmp v3\n";
exit $exit_status;
}
$session_params{-privprotocol} = $OPTION->{'snmp-priv-protocol'};
if (defined($OPTION->{'snmp-priv-password'})) {
$session_params{-privpassword} = $OPTION->{'snmp-priv-password'};
} else {
$session_params{-privkey} = $OPTION->{'snmp-priv-key'};
}
}
} else {
%session_params = (-hostname => $OPTION->{'host'},
-community => $OPTION->{'snmp-community'},
-version => $OPTION->{'snmp-version'},
-port => $OPTION->{'snmp-port'});
}
if (defined($OPTION->{'64-bits'})) {
if ($OPTION->{'snmp-version'} =~ /1/) {
print "Error : Usage : SNMP v2/v3 is required with option --64-bits\n";
exit $exit_status;
}
eval 'require bigint';
if ($@) {
print "Could not load the Perl module 'bigint' $@";
exit($exit_status);
}
require bigint;
}
return (\%session_params);
}
# Method to connect to the remote host
# This method take the hash table option as argument
# return $session if OK or 0 if not
sub connection
{
my $self = shift;
my $sessionType = shift;
my $options = shift;
sub connection {
my ($exit_status, $session_params) = @_;
my ($session, $error);
if ($sessionType == 1) {
if (!defined($options->{'host'}) || !defined($options->{'snmpport'}) || !defined($options->{'snmpcomm'}) || !defined($options->{'snmpversion'})) {
print("Error when trying to connect - mission argument(s)\n");
return 0;
($session, $error) = Net::SNMP->session(%$session_params);
if (!defined($session)) {
print "UNKNOWN: SNMP Session : $error\n";
exit $exit_status;
}
($session, $error) = Net::SNMP->session(-hostname => $options->{'host'},
-community => $options->{'snmpcomm'},
-version => $options->{'snmpversion'},
-port => $options->{'snmpport'});
} elsif ($sessionType == 2) {
($session, $error) = Net::SNMP->session(-hostname => $options->{'host'},
-version => $options->{'snmpversion'},
-username => $options->{'username'},
-authpassword => $options->{'authpassword'},
-authprotocol => $options->{'authprotocol'},
-port => $options->{'snmpport'});
} else {
($session, $error) = Net::SNMP->session(-hostname => $options->{'host'},
-version => $options->{'snmpversion'},
-username => $options->{'username'},
-authpassword => $options->{'authpassword'},
-authprotocol => $options->{'authprotocol'},
-privpassword => $options->{'privpassword'},
-privprotocol => $options->{'privprotocol'},
-port => $options->{'snmpport'});
}
if (defined($error) && $error ne "") {
print("SESSION ERROR: ".$error."\n");
return 0;
}
$session->translate(Net::SNMP->TRANSLATE_NONE);
return $session;
}
# Test if OID passed in 2nd parameter exists
sub testOID{
my $self = shift;
my $sess = $_[0];
my $OID_toTest = $_[1];
my $result = $sess->get_table(Baseoid => $OID_toTest);
if (!defined($result)) {
return 0;
} else{
return 1;
sub get_snmp_table {
my ($oid, $session, $exit_status, $OPTION) = @_;
my $result;
if (defined($OPTION) && defined($OPTION->{'maxrepetitions'})) {
$result = $session->get_table(Baseoid => $oid, -maxrepetitions => $OPTION->{'maxrepetitions'});
} else {
$result = $session->get_table(Baseoid => $oid);
}
if (!defined($result)) {
printf("SNMP TABLE ERROR : %s.\n", $session->error);
$session->close;
exit $exit_status;
}
return $result;
}
sub get_snmp_leef {
my ($oids, $session, $exit_status, $extra_msg) = @_;
my $result = $session->get_request(-varbindlist => $oids);
if (!defined($result)) {
printf("SNMP REQUEST ERROR : %s.", $session->error);
if (defined($extra_msg)) {
print $extra_msg;
}
print "\n";
$session->close;
exit $exit_status;
}
return $result;
}
1;

View File

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

View File

@ -40,49 +40,54 @@
#
use strict;
use Net::SNMP qw(:snmp oid_lex_sort);
use FindBin;
use lib "$FindBin::Bin";
use lib "@NAGIOS_PLUGINS@";
use utils qw($TIMEOUT %ERRORS &print_revision &support);
if (eval "require centreon") {
use centreon qw(get_parameters);
use vars qw(%centreon);
%centreon=get_parameters();
} else {
print "Unable to load centreon perl module\n";
exit $ERRORS{'UNKNOWN'};
}
require "@NAGIOS_PLUGINS@/Centreon/SNMP/Utils.pm";
use vars qw($PROGNAME);
use Getopt::Long;
use vars qw($opt_V $opt_h $opt_i $opt_n $opt_w $opt_c $opt_s $opt_T $opt_a $opt_r $opt_S);
my $centplugins_path = "@CENTPLUGINS_TMP@";
my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3);
my %centreon = Centreon::SNMP::Utils::load_oids($ERRORS{'UNKNOWN'}, "@NAGIOS_PLUGINS@/centreon.conf");
# Plugin var init
my ($session, $error);
my ($row, $last_check_time, $last_in_bits, $last_out_bits, @last_values, $update_time, $in_traffic, $out_traffic, $in_usage, $out_usage);
$PROGNAME = "$0";
sub print_help ();
sub print_usage ();
my %OPTION = (
"host" => undef, "64-bits" => undef,
"host" => undef,
"snmp-community" => "public", "snmp-version" => 1, "snmp-port" => 161,
"snmp-auth-key" => undef, "snmp-auth-user" => undef, "snmp-auth-passwd" => undef
"snmp-auth-key" => undef, "snmp-auth-user" => undef, "snmp-auth-password" => undef, "snmp-auth-protocol" => "MD5",
"snmp-priv-key" => undef, "snmp-priv-password" => undef, "snmp-priv-protocol" => "DES",
"maxrepetitions" => undef,
"64-bits" => undef,
"disable-warn-state" => undef
);
Getopt::Long::Configure('bundling');
GetOptions
(
"H|hostname=s" => \$OPTION{'host'},
"H|hostname|host=s" => \$OPTION{'host'},
"C|community=s" => \$OPTION{'snmp-community'},
"v|snmp-version=s" => \$OPTION{'snmp-version'},
"P|snmp-port=i" => \$OPTION{'snmp-port'},
"k|key=s" => \$OPTION{'snmp-auth-key'},
"v|snmp|snmp-version=s" => \$OPTION{'snmp-version'},
"P|snmpport|snmp-port=i" => \$OPTION{'snmp-port'},
"u|username=s" => \$OPTION{'snmp-auth-user'},
"p|password=s" => \$OPTION{'snmp-auth-passwd'},
"64-bits" => \$OPTION{'64-bits'},
"p|authpassword|password=s" => \$OPTION{'snmp-auth-password'},
"k|authkey=s" => \$OPTION{'snmp-auth-key'},
"authprotocol=s" => \$OPTION{'snmp-auth-protocol'},
"privpassword=s" => \$OPTION{'snmp-priv-password'},
"privkey=s" => \$OPTION{'snmp-priv-key'},
"privprotocol=s" => \$OPTION{'snmp-priv-protocol'},
"maxrepetitions=s" => \$OPTION{'maxrepetitions'},
"64-bits" => \$OPTION{'64-bits'},
"disable-warn-state" => \$OPTION{'disable-warn-state'},
"h" => \$opt_h, "help" => \$opt_h,
"s" => \$opt_s, "show" => \$opt_s,
@ -106,113 +111,14 @@ if ($opt_h) {
Getopt::Long::Configure('bundling');
}
##################################################
##### Functions
##
sub create_snmp_session() {
if ($OPTION{'snmp-version'} =~ /[1-2]/) {
($session, $error) = Net::SNMP->session(-hostname => $OPTION{'host'},
-community => $OPTION{'snmp-community'},
-version => $OPTION{'snmp-version'},
-port => $OPTION{'snmp-port'});
if (!defined($session)) {
print("UNKNOWN: SNMP Session : $error\n");
exit $ERRORS{'UNKNOWN'};
}
} elsif (defined($OPTION{'snmp-auth-key'})) {
($session, $error) = Net::SNMP->session(-hostname => $OPTION{'host'},
-version => $OPTION{'snmp-version'},
-username => $OPTION{'snmp-auth-user'},
-authkey => $OPTION{'snmp-auth-key'},
-port => $OPTION{'snmp-port'});
if (!defined($session)) {
print("UNKNOWN: SNMP Session : $error\n");
exit $ERRORS{'UNKNOWN'};
}
} elsif (defined($OPTION{'snmp-auth-passwd'})) {
($session, $error) = Net::SNMP->session(-hostname => $OPTION{'host'},
-version => $OPTION{'snmp-version'},
-username => $OPTION{'snmp-auth-user'},
-authpassword => $OPTION{'snmp-auth-passwd'},
-port => $OPTION{'snmp-port'});
if (!defined($session)) {
print("UNKNOWN: SNMP Session : $error\n");
exit $ERRORS{'UNKNOWN'};
}
}
if (defined($session)) {
$session->translate(Net::SNMP->TRANSLATE_NONE);
}
return $session;
}
sub get_snmp_table($$) {
my ($oid, $session) = (shift, shift);
my $result;
if (defined($OPTION{'maxrepetitions'})) {
$result = $session->get_table(Baseoid => $oid, -maxrepetitions => $OPTION{'maxrepetitions'});
} else {
$result = $session->get_table(Baseoid => $oid);
}
if (!defined($result)) {
printf("SNMP TABLE ERROR : %s.\n", $session->error);
$session->close;
exit $ERRORS{'UNKNOWN'};
}
return $result;
}
##################################################
##### Verify Options
##
if (!$OPTION{'host'}) {
print_usage();
exit $ERRORS{'OK'};
}
######
### SNMP Check
##
if (defined($OPTION{'snmp-version'}) && $OPTION{'snmp-version'} =~ /^([1-3])c?$/) {
$OPTION{'snmp-version'} = $1;
} elsif (defined($OPTION{'snmp-version'})) {
print "Unknown snmp version\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
}
if ($OPTION{'snmp-version'} eq "3") {
if (!defined($OPTION{'snmp-auth-user'})) {
print "Option -u (--username) is required for snmpV3\n";
exit $ERRORS{'UNKNOWN'};
}
if (!defined($OPTION{'snmp-auth-passwd'}) && !defined($OPTION{'snmp-auth-key'})) {
print "Option -k (--key) or -p (--password) is required for snmp v3\n";
exit $ERRORS{'UNKNOWN'};
} elsif (defined($OPTION{'snmp-auth-passwd'}) && defined($OPTION{'snmp-auth-key'})) {
print "Only option -k (--key) or -p (--password) is needed for snmp v3\n";
exit $ERRORS{'UNKNOWN'};
}
}
if (defined($OPTION{'64-bits'}) && $OPTION{'snmp-version'} !~ /2/) {
print "Error : Usage : SNMP v2 is required with option --64-bits\n";
exit $ERRORS{'UNKNOWN'};
}
if (defined($OPTION{'64-bits'})) {
if (eval "require bigint") {
use bigint;
} else {
print "ERROR : Need bigint module for 64 bit counters\n";
exit $ERRORS{"UNKNOWN"}
}
}
my ($session_params) = Centreon::SNMP::Utils::check_snmp_options($ERRORS{'UNKNOWN'}, \%OPTION);
######
### Others
@ -257,13 +163,13 @@ if ($critical <= $warning){
##### Plugin snmp requests
##
$session = create_snmp_session();
$session = Centreon::SNMP::Utils::connection($ERRORS{'UNKNOWN'}, $session_params);
my $OID_DESC = $centreon{MIB2}{IF_DESC};
my $OID_OPERSTATUS = $centreon{MIB2}{IF_OPERSTATUS};
my @operstatus = ("up","down","testing", "unknown", "dormant", "notPresent", "lowerLayerDown");
my $cacheFile = "@CENTPLUGINS_TMP@/traffic_cache_". $OPTION{'host'};
my $cacheFile = "$centplugins_path/traffic_cache_". $OPTION{'host'};
my $result;
my $mustCreateFile = 0;
my $countLine;
@ -292,7 +198,7 @@ if (-e $cacheFile) {
}
if ($mustCreateFile) {
$result = get_snmp_table($OID_DESC, $session);
$result = Centreon::SNMP::Utils::get_snmp_table($OID_DESC, $session, $ERRORS{'UNKNOWN'}, \%OPTION);
unless (open(FILE,">".$cacheFile)){
print "Check mod for temporary file : ".$cacheFile."...\n";
exit $ERRORS{"UNKNOWN"};
@ -359,7 +265,7 @@ if (defined($OPTION{'64-bits'})) {
# Get desctiption table
if ($opt_s) {
if (!-e $cacheFile) {
$result = get_snmp_table($OID_DESC, $session);
$result = Centreon::SNMP::Utils::get_snmp_table($OID_DESC, $session, $ERRORS{'UNKNOWN'}, \%OPTION);
unless (open(FILE,">".$cacheFile)){
print "Check mod for temporary file : ".$cacheFile."...\n";
exit $ERRORS{"UNKNOWN"};
@ -384,12 +290,12 @@ if ($opt_s) {
if ($countLine) {
my @resLine = split(/\;/, $row);
my $index = $resLine[0];
my $interface_status = $session->get_request(-varbindlist => [$OID_OPERSTATUS.".".$index]);
my $interface_status = Centreon::SNMP::Utils::get_snmp_leef([$OID_OPERSTATUS.".".$index], $session, $ERRORS{'UNKNOWN'});
$resLine[1] =~ s/\x00//g;
$resLine[1] =~ s/\n//g;
print "Interface ". $index . " :: " . $resLine[1] . " :: ".$operstatus[$interface_status->{$OID_OPERSTATUS.".".$index} - 1];
if ($opt_S) {
my $link_speed = $session->get_request(-varbindlist => [$OID_SPEED_BASE.".".$index]);
my $link_speed = Centreon::SNMP::Utils::get_snmp_leef([$OID_SPEED_BASE.".".$index], $session, $ERRORS{'UNKNOWN'});
if (!defined($link_speed)) {
printf("ERROR: Interface Speed Request : %s", $session->error);
exit $ERRORS{'UNKNOWN'};
@ -407,16 +313,9 @@ if ($opt_s) {
exit $ERRORS{'OK'};
}
$result = $session->get_request(-varbindlist => [$OID_OPERSTATUS.".".$interface, $OID_IN, $OID_OUT, $OID_SPEED]);
if (!defined($result)) {
printf("ERROR: SNMP Request: %s", $session->error);
if ($opt_n) {
print " - You must specify interface name when option -n is used";
}
print ".\n";
$session->close;
exit $ERRORS{'UNKNOWN'};
}
$result = Centreon::SNMP::Utils::get_snmp_leef([$OID_OPERSTATUS.".".$interface, $OID_IN, $OID_OUT, $OID_SPEED], $session, $ERRORS{'UNKNOWN'},
defined($opt_n) ? "You must specify interface name when option -n is used." : undef);
if (!defined($result->{$OID_OPERSTATUS.".".$interface}) || $result->{$OID_OPERSTATUS.".".$interface} eq "") {
print "ERROR: Can't get interface '$interface' status\n";
exit $ERRORS{'CRITICAL'};
@ -471,8 +370,8 @@ $last_out_bits = 0;
my $flg_created = 0;
if (-e "@CENTPLUGINS_TMP@/traffic_if".$interface."_".$OPTION{'host'}) {
open(FILE,"<"."@CENTPLUGINS_TMP@/traffic_if".$interface."_".$OPTION{'host'});
if (-e "$centplugins_path/traffic_if".$interface."_".$OPTION{'host'}) {
open(FILE,"<"."$centplugins_path/traffic_if".$interface."_".$OPTION{'host'});
while($row = <FILE>){
@last_values = split(":",$row);
$last_check_time = $last_values[0];
@ -487,8 +386,8 @@ if (-e "@CENTPLUGINS_TMP@/traffic_if".$interface."_".$OPTION{'host'}) {
$update_time = time();
unless (open(FILE,">"."@CENTPLUGINS_TMP@/traffic_if".$interface."_".$OPTION{'host'})){
print "Check mod for temporary file : @CENTPLUGINS_TMP@/traffic_if".$interface."_".$OPTION{'host'}. " !\n";
unless (open(FILE,">"."$centplugins_path/traffic_if".$interface."_".$OPTION{'host'})){
print "Check mod for temporary file : $centplugins_path/traffic_if".$interface."_".$OPTION{'host'}. " !\n";
exit $ERRORS{"UNKNOWN"};
}
print FILE "$update_time:$in_bits:$out_bits";
@ -647,6 +546,9 @@ sub print_usage () {
print " -k (--key) snmp V3 key\n";
print " -u (--username) snmp V3 username \n";
print " -p (--password) snmp V3 password\n";
print " --authprotocol protocol MD5/SHA1 (v3)\n";
print " --privprotocol encryption system (DES/AES)(v3) \n";
print " --privpassword passphrase (v3) \n";
print " --64-bits Use 64 bits OID\n";
print " --maxrepetitions To use when you have the error: 'Message size exceeded buffer maxMsgSize'\n";
print " Work only with SNMP v2c and v3 (Example: --maxrepetitions=1)\n";
@ -658,7 +560,7 @@ sub print_usage () {
print " -w (--warning) Signal strength at which a warning message will be generated\n";
print " (default 80)\n";
print " -c (--critical) Signal strength at which a critical message will be generated\n";
print " --disable-warn-state To use when you have the error: 'Message size exceeded buffer maxMsgSize'\n";
print " --disable-warn-state Option for not alerting on interface state\n";
print " -T Set maximum bandwidth\n";
print " -S Show link speed in output\n";
print " -V (--version) Plugin version\n";

View File

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

View File

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