Compatibility SNMPv3

This commit is contained in:
Quentin Garnier 2013-01-14 17:42:14 +01:00
parent 8b5ecd0bdc
commit de7b83e200
2 changed files with 138 additions and 166 deletions

View File

@ -21,43 +21,49 @@
# Plugin init
use strict;
use Net::SNMP qw(:snmp);
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 &is_valid_serviceid);
use vars qw($VERSION %centreon);
%centreon=get_parameters();
} else {
print "Unable to load centreon perl module\n";
exit $ERRORS{'UNKNOWN'};
}
require "@NAGIOS_PLUGINS@/Centreon/SNMP/Utils.pm";
my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3);
use vars qw($PROGNAME);
use Getopt::Long;
use vars qw($opt_V $opt_h $opt_g $opt_v $opt_C $opt_d $opt_n $opt_w $opt_c $opt_H $opt_S $opt_D $opt_s $opt_step $step @test $opt_f);
use vars qw($opt_V $opt_h $opt_n $opt_s);
# Plugin var init
my ($hrStorageDescr, $hrStorageAllocationUnits, $hrStorageSize, $hrStorageUsed);
my ($AllocationUnits, $Size, $Used);
my ($tot, $used, $pourcent, $return_code);
$PROGNAME = $0;
sub print_help ();
sub print_usage ();
my %OPTION = (
"host" => undef,
"snmp-community" => "public", "snmp-version" => 1, "snmp-port" => 161,
"snmp-auth-key" => undef, "snmp-auth-user" => undef, "snmp-auth-password" => undef, "snmp-auth-protocol" => "MD5",
"snmp-priv-key" => undef, "snmp-priv-password" => undef, "snmp-priv-protocol" => "DES",
"maxrepetitions" => undef,
"64-bits" => undef,
);
Getopt::Long::Configure('bundling');
GetOptions
("h" => \$opt_h, "help" => \$opt_h,
(
"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" => \$opt_h, "help" => \$opt_h,
"V" => \$opt_V, "version" => \$opt_V,
"v=s" => \$opt_v,
"s" => \$opt_s, "show" => \$opt_s,
"C=s" => \$opt_C, "community=s" => \$opt_C,
"n=s" => \$opt_n, "name=s" => \$opt_n,
"H=s" => \$opt_H, "hostname=s" => \$opt_H);
"n=s" => \$opt_n, "name=s" => \$opt_n);
if ($opt_V) {
@ -66,82 +72,46 @@ if ($opt_V) {
}
if ($opt_h) {
print_help();
exit $ERRORS{'OK'};
}
$opt_H = shift unless ($opt_H);
(print_usage() && exit $ERRORS{'OK'}) unless ($opt_H);
($opt_v) || ($opt_v = shift) || ($opt_v = "2");
my $snmp = $1 if ($opt_v =~ /(\d)/);
($opt_C) || ($opt_C = shift) || ($opt_C = "public");
($opt_c) || ($opt_c = shift) || ($opt_c = 95);
my $critical = $1 if ($opt_c =~ /([0-9]+)/);
($opt_w) || ($opt_w = shift) || ($opt_w = 80);
my $warning = $1 if ($opt_w =~ /([0-9]+)/);
if ($critical <= $warning){
print "(--crit) must be superior to (--warn)";
print_usage();
print_help();
exit $ERRORS{'OK'};
}
my ($session_params) = Centreon::SNMP::Utils::check_snmp_options($ERRORS{'UNKNOWN'}, \%OPTION);
# Plugin snmp requests
my $OID_ExecResult = ".1.3.6.1.4.1.2021.8.1.100";
my $OID_ExecDescr = ".1.3.6.1.4.1.2021.8.1.2";
my $OID_ExecOutput = ".1.3.6.1.4.1.2021.8.1.101";
# create a SNMP session
my ( $session, $error ) = Net::SNMP->session(-hostname => $opt_H,-community => $opt_C, -version => $snmp);
if ( !defined($session) ) {
print("CRITICAL: SNMP Session : $error");
exit $ERRORS{'CRITICAL'};
}
my $session = Centreon::SNMP::Utils::connection($ERRORS{'UNKNOWN'}, $session_params);
my $scriptname = "";
# getting partition using its name instead of its oid index
if ($opt_n) {
my $result = $session->get_table(Baseoid => $OID_ExecDescr);
if (!defined($result)) {
printf("ERROR: hr Exec Descr Table : %s.\n", $session->error);
$session->close;
exit $ERRORS{'UNKNOWN'};
}
my $result = Centreon::SNMP::Utils::get_snmp_table($OID_ExecDescr, $session, $ERRORS{'UNKNOWN'}, \%OPTION);
foreach my $key ( oid_lex_sort(keys %$result)) {
if ($result->{$key} =~ m/^$opt_n$/) {
my @oid_list = split (/\./,$key);
$scriptname = pop (@oid_list) ;
}
my @oid_list = split (/\./,$key);
$scriptname = pop (@oid_list) ;
}
}
}
if ($scriptname eq "") {
print "No such process, verify your snmpd configuration\n";
exit(3);
print "No such process, verify your snmpd configuration\n";
exit(3);
}
my $result = $session->get_request(-varbindlist => [$OID_ExecDescr.".".$scriptname, $OID_ExecOutput.".".$scriptname]);
if (!defined($result)) {
printf("ERROR: %s", $session->error);
if ($opt_n) { print(" - You must specify the disk name when option -n is used");}
print ".\n";
$session->close;
exit $ERRORS{'UNKNOWN'};
}
my $result = Centreon::SNMP::Utils::get_snmp_leef([$OID_ExecDescr.".".$scriptname, $OID_ExecOutput.".".$scriptname], $session, $ERRORS{'UNKNOWN'});
my $ExecResult = $result->{$OID_ExecResult.".".$scriptname };
my $ExecDescr = $result->{$OID_ExecDescr.".".$scriptname };
my $ExecOutput = $result->{$OID_ExecOutput.".".$scriptname };
if ($ExecOutput =~ /cannot\ open/) {
$ExecResult = 3;
$ExecResult = 3;
}
print $ExecOutput . "\n";
exit($ExecResult);
@ -149,11 +119,22 @@ exit($ExecResult);
sub print_usage () {
print "\nUsage:\n";
print "$PROGNAME\n";
print " -H (--hostname) Hostname to query - (required)\n";
print " -C (--community) SNMP read community (defaults to public,\n";
print " -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 " -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 " -n (--name) Allows to use disk name with option -d instead of disk oid index\n";
print " (ex: -d \"C:\" -n, -d \"E:\" -n, -d \"Swap Memory\" -n, -d \"Real Memory\" -n\n";
print " (choose an unique expression for each disk)\n";
@ -168,8 +149,8 @@ sub print_usage () {
sub print_help () {
print "##########################################\n";
print "# Copyright (c) 2004-2006 centreon #\n";
print "# Bugs to http://www.oreon-project.org/ #\n";
print "# Copyright (c) 2004-20013 centreon #\n";
print "# Bugs to http://forge.centreon.com/ #\n";
print "##########################################\n";
print_usage();
print "\n";

View File

@ -21,45 +21,50 @@
# Plugin init
use strict;
use Net::SNMP qw(:snmp);
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 &is_valid_serviceid);
use vars qw($VERSION %centreon);
%centreon=get_parameters();
} else {
print "Unable to load centreon perl module\n";
exit $ERRORS{'UNKNOWN'};
}
require "/usr/lib/nagios/plugins/Centreon/SNMP/Utils.pm";
my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3);
use vars qw($PROGNAME);
use Getopt::Long;
use vars qw($opt_V $opt_h $opt_g $opt_v $opt_C $opt_d $opt_n $opt_w $opt_c $opt_H $opt_S $opt_D $opt_s $opt_step $step @test $opt_f);
use vars qw($opt_V $opt_h $opt_n $opt_w $opt_c $opt_s);
# Plugin var init
my ($hrStorageDescr, $hrStorageAllocationUnits, $hrStorageSize, $hrStorageUsed);
my ($AllocationUnits, $Size, $Used);
my ($tot, $used, $pourcent, $return_code);
$PROGNAME = "check_snmp_remote_storage";
$PROGNAME = $0;
sub print_help ();
sub print_usage ();
Getopt::Long::Configure('bundling');
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,
);
GetOptions
("h" => \$opt_h, "help" => \$opt_h,
"V" => \$opt_V, "version" => \$opt_V,
"v=s" => \$opt_v,
"s" => \$opt_s, "show" => \$opt_s,
"C=s" => \$opt_C, "community=s" => \$opt_C,
"n=s" => \$opt_n, "name=s" => \$opt_n,
"w=s" => \$opt_w, "warning=s" => \$opt_w,
"c=s" => \$opt_c, "critical=s" => \$opt_c,
"H=s" => \$opt_H, "hostname=s" => \$opt_H);
(
"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" => \$opt_h, "help" => \$opt_h,
"V" => \$opt_V, "version" => \$opt_V,
"s" => \$opt_s, "show" => \$opt_s,
"n=s" => \$opt_n, "name=s" => \$opt_n,
"w=s" => \$opt_w, "warning=s" => \$opt_w,
"c=s" => \$opt_c, "critical=s" => \$opt_c);
if ($opt_V) {
@ -68,17 +73,11 @@ if ($opt_V) {
}
if ($opt_h) {
print_help();
exit $ERRORS{'OK'};
print_help();
exit $ERRORS{'OK'};
}
$opt_H = shift unless ($opt_H);
(print_usage() && exit $ERRORS{'OK'}) unless ($opt_H);
($opt_v) || ($opt_v = shift) || ($opt_v = "2");
my $snmp = $1 if ($opt_v =~ /(\d)/);
($opt_C) || ($opt_C = shift) || ($opt_C = "public");
my ($session_params) = Centreon::SNMP::Utils::check_snmp_options($ERRORS{'UNKNOWN'}, \%OPTION);
($opt_c) || ($opt_c = shift) || ($opt_c = 95);
my $critical = $1 if ($opt_c =~ /([0-9]+)/);
@ -97,42 +96,23 @@ my $OID_ExecDescr = ".1.3.6.1.4.1.2021.8.1.2";
my $OID_ExecOutput = ".1.3.6.1.4.1.2021.8.1.101";
# create a SNMP session
my ( $session, $error ) = Net::SNMP->session(-hostname => $opt_H,-community => $opt_C, -version => $snmp);
if ( !defined($session) ) {
print("CRITICAL: SNMP Session : $error");
exit $ERRORS{'CRITICAL'};
}
my $session = Centreon::SNMP::Utils::connection($ERRORS{'UNKNOWN'}, $session_params);
my $scriptname = "";
# getting partition using its name instead of its oid index
if ($opt_n) {
my $result = $session->get_table(Baseoid => $OID_ExecDescr);
if (!defined($result)) {
printf("ERROR: hr Exec Descr Table : %s.\n", $session->error);
$session->close;
exit $ERRORS{'UNKNOWN'};
}
my $result = Centreon::SNMP::Utils::get_snmp_table($OID_ExecDescr, $session, $ERRORS{'UNKNOWN'}, \%OPTION);
foreach my $key ( oid_lex_sort(keys %$result)) {
if ($result->{$key} =~ m/$opt_n/) {
my @oid_list = split (/\./,$key);
$scriptname = pop (@oid_list) ;
}
my @oid_list = split (/\./,$key);
$scriptname = pop (@oid_list) ;
}
}
}
my $result = $session->get_request(-varbindlist => [$OID_ExecDescr.".".$scriptname, $OID_ExecOutput.".".$scriptname]);
if (!defined($result)) {
printf("ERROR: %s", $session->error);
if ($opt_n) { print(" - You must specify the disk name when option -n is used");}
print ".\n";
$session->close;
exit $ERRORS{'UNKNOWN'};
}
my $result = Centreon::SNMP::Utils::get_snmp_leef([$OID_ExecDescr.".".$scriptname, $OID_ExecOutput.".".$scriptname], $session, $ERRORS{'UNKNOWN'});
my $ExecDescr = $result->{$OID_ExecDescr.".".$scriptname };
my $ExecOutput = $result->{$OID_ExecOutput.".".$scriptname };
@ -142,38 +122,49 @@ print "|" . $ExecOutput . "\n";
my $return = 5;
if (!defined($opt_w) && !defined($opt_c)){
$ExecOutput =~ /([0-9]*)/;
if ($1 eq 1){
print "OK : Process runnable \n";
$return = 0;
} else {
print "CRITICAL : Process runnable \n";
$return = 2;
}
$ExecOutput =~ /([0-9]*)/;
if ($1 eq 1){
print "OK : Process runnable \n";
$return = 0;
} else {
print "CRITICAL : Process runnable \n";
$return = 2;
}
} else {
if ($ExecOutput =~ /([0-9]*)/){
if ($1 >= $opt_w && $1 < $opt_c){
print "OK : $1 Process runnable \n";
$return = 1;
} elsif ($1 > $opt_c) {
print "WARNING : $1 Process runnable \n";
$return = 2;
} elsif ($1 < $opt_w) {
print "CRITICAL : Process runnable \n";
$return = 0;
}
}
}
if ($ExecOutput =~ /([0-9]*)/){
if ($1 >= $opt_w && $1 < $opt_c){
print "OK : $1 Process runnable \n";
$return = 1;
} elsif ($1 > $opt_c) {
print "WARNING : $1 Process runnable \n";
$return = 2;
} elsif ($1 < $opt_w) {
print "CRITICAL : Process runnable \n";
$return = 0;
}
}
}
exit($return);
sub print_usage () {
print "\nUsage:\n";
print "$PROGNAME\n";
print " -H (--hostname) Hostname to query - (required)\n";
print " -C (--community) SNMP read community (defaults to public,\n";
print " -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 " -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 " -n (--name) Allows to use disk name with option -d instead of disk oid index\n";
print " (ex: -d \"C:\" -n, -d \"E:\" -n, -d \"Swap Memory\" -n, -d \"Real Memory\" -n\n";
print " (choose an unique expression for each disk)\n";
@ -188,8 +179,8 @@ sub print_usage () {
sub print_help () {
print "##########################################\n";
print "# Copyright (c) 2004-2006 centreon #\n";
print "# Bugs to http://www.oreon-project.org/ #\n";
print "# Copyright (c) 2004-2013 centreon #\n";
print "# Bugs to http://forge.centreon.com/ #\n";
print "##########################################\n";
print_usage();
print "\n";