Compatibility SNMPv3

Fix #2201 #29
This commit is contained in:
Quentin Garnier 2013-01-14 15:51:01 +01:00
parent 9a0115647b
commit 7ec16b9a82
3 changed files with 128 additions and 130 deletions

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.
# #
@ -37,25 +37,15 @@
#################################################################################### ####################################################################################
# #
# Plugin init # Plugin init
#
use strict; use strict;
use Net::SNMP qw(:snmp); 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($VERSION %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_h $opt_V $opt_H $opt_C $opt_v $opt_p $opt_c $opt_w); use vars qw($opt_h $opt_V $opt_p $opt_c $opt_w);
use vars qw($snmp);
my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3);
my $Revision = 1.2.1; my $Revision = 1.2.1;
@ -63,15 +53,36 @@ $PROGNAME = "ckeck_TcpConn";
sub print_help (); sub print_help ();
sub print_usage (); 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'); Getopt::Long::Configure('bundling');
GetOptions GetOptions
("h" => \$opt_h, "help" => \$opt_h, (
"v=s" => \$opt_v, "snmp_version=s" => \$opt_v, "H|hostname|host=s" => \$OPTION{'host'},
"V" => \$opt_V, "version" => \$opt_V, "C|community=s" => \$OPTION{'snmp-community'},
"H=s" => \$opt_H, "Hostname=s" => \$opt_H, "v|snmp|snmp-version=s" => \$OPTION{'snmp-version'},
"p=i" => \$opt_p, "port=s" => \$opt_p, "P|snmpport|snmp-port=i" => \$OPTION{'snmp-port'},
"C=s" => \$opt_C, "Community=s" => \$opt_C, "u|username=s" => \$OPTION{'snmp-auth-user'},
"c=s"=> \$opt_c, "w=s"=> \$opt_w "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,
"p=i" => \$opt_p, "port=s" => \$opt_p,
"c=s" => \$opt_c, "w=s" => \$opt_w
); );
if ($opt_V) { if ($opt_V) {
@ -84,44 +95,28 @@ if ($opt_h) {
exit $ERRORS{'OK'}; exit $ERRORS{'OK'};
} }
$opt_H = shift unless ($opt_H); my ($session_params) = Centreon::SNMP::Utils::check_snmp_options($ERRORS{'UNKNOWN'}, \%OPTION);
(print_usage() && exit $ERRORS{'OK'}) unless ($opt_H);
$opt_p = shift unless ($opt_p); $opt_p = shift unless ($opt_p);
(print_usage() && exit $ERRORS{'OK'}) unless ($opt_p); (print_usage() && exit $ERRORS{'OK'}) unless ($opt_p);
($opt_v) || ($opt_v = shift) || ($opt_v = "v1");
my $snmp = $1 if ($opt_v =~ /(\d)/);
($opt_C) || ($opt_C = shift) || ($opt_C = "public");
my $name = $0; my $name = $0;
$name =~ s/\.pl.*//g; $name =~ s/\.pl.*//g;
my $day = 0; my $day = 0;
#=== create a SNMP session ==== #=== create a SNMP session ====
# 1.3.6.1.4.1.232.1.2.2.1.1.6 # 1.3.6.1.4.1.232.1.2.2.1.1.6
my $session = Centreon::SNMP::Utils::connection($ERRORS{'UNKNOWN'}, $session_params);
my ($session, $error) = Net::SNMP->session(-hostname => $opt_H,-community => $opt_C, -version => $snmp);
if (!defined($session)) {
print("CRITICAL: $error");
exit $ERRORS{'CRITICAL'};
}
my $OID_TCP_PORT = ".1.3.6.1.2.1.6.13.1.3"; my $OID_TCP_PORT = ".1.3.6.1.2.1.6.13.1.3";
my $result = $session->get_table(Baseoid => $OID_TCP_PORT); my $result = Centreon::SNMP::Utils::get_snmp_table($OID_TCP_PORT, $session, $ERRORS{'UNKNOWN'}, \%OPTION);
if (!defined($result)) {
printf("ERROR: Description Table : %s.\n", $session->error);
$session->close;
exit $ERRORS{'UNKNOWN'};
}
my $cpt = 0; my $cpt = 0;
foreach my $key (oid_lex_sort(keys %$result)) { foreach my $key (oid_lex_sort(keys %$result)) {
if ($result->{$key} == $opt_p) { if ($result->{$key} == $opt_p) {
$cpt++; $cpt++;
} }
} }
if (!defined($opt_w)){$opt_w = 20;} if (!defined($opt_w)){$opt_w = 20;}
@ -129,32 +124,42 @@ if (!defined($opt_c)){$opt_c = 30;}
print "Number of connections on port $opt_p : $cpt |nb_conn=$cpt\n"; print "Number of connections on port $opt_p : $cpt |nb_conn=$cpt\n";
if ($cpt >= $opt_w && $cpt < $opt_c){ if ($cpt >= $opt_w && $cpt < $opt_c){
exit $ERRORS{'WARNING'}; exit $ERRORS{'WARNING'};
} elsif ($cpt >= $opt_c){ } elsif ($cpt >= $opt_c){
exit $ERRORS{'CRITICAL'}; exit $ERRORS{'CRITICAL'};
} else { } else {
exit $ERRORS{'OK'}; exit $ERRORS{'OK'};
} }
sub print_usage () { sub print_usage () {
print "\nUsage:\n"; print "\nUsage:\n";
print "$PROGNAME\n"; print "$PROGNAME\n";
print " -H (--hostname) Hostname to query - (required)\n"; print " -H (--hostname) Hostname to query (required)\n";
print " -p (--port) port you want to check - (required)\n"; print " -C (--community) SNMP read community (defaults to public)\n";
print " -C (--community) SNMP read community (defaults to public,\n";
print " used with SNMP v1 and v2c\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 " 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 " --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 " -p (--port) port you want to check - (required)\n";
print " -V (--version) Plugin version\n"; print " -V (--version) Plugin version\n";
print " -h (--help) usage help\n"; print " -h (--help) 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";
} }

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.
# #

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.
# #
@ -39,85 +39,74 @@
# Plugin init # Plugin init
# #
use strict; use strict;
use Net::SNMP qw(:snmp); 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($VERSION %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 POSIX;
use Getopt::Long; use Getopt::Long;
use vars qw($opt_h $opt_V $opt_H $opt_C $opt_v $opt_d $opt_P $day); use vars qw($opt_h $opt_V $opt_d $day);
my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3);
my %centreon = Centreon::SNMP::Utils::load_oids($ERRORS{'UNKNOWN'}, "@NAGIOS_PLUGINS@/centreon.conf");
$PROGNAME = $0; $PROGNAME = $0;
sub print_help (); sub print_help ();
sub print_usage (); 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'); Getopt::Long::Configure('bundling');
GetOptions GetOptions
("h" => \$opt_h, "help" => \$opt_h, (
"V" => \$opt_V, "version" => \$opt_V, "H|hostname|host=s" => \$OPTION{'host'},
"v=s" => \$opt_v, "snmp=s" => \$opt_v, "C|community=s" => \$OPTION{'snmp-community'},
"C=s" => \$opt_C, "community=s" => \$opt_C, "v|snmp|snmp-version=s" => \$OPTION{'snmp-version'},
"P=s" => \$opt_P, "snmp-port=s" => \$opt_P, "P|snmpport|snmp-port=i" => \$OPTION{'snmp-port'},
"d" => \$opt_d, "day" => \$opt_d, "u|username=s" => \$OPTION{'snmp-auth-user'},
"H=s" => \$opt_H, "hostname=s" => \$opt_H); "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,
"d" => \$opt_d, "day" => \$opt_d,
);
if ($opt_V) { if ($opt_V) {
print_revision($PROGNAME,'$Revision: 1.2 $'); print_revision($PROGNAME,'$Revision: 1.2 $');
exit $ERRORS{'OK'}; exit $ERRORS{'OK'};
} }
if (!defined($opt_P)) {
$opt_P = 161;
}
if ($opt_h) { if ($opt_h) {
print_help(); print_help();
exit $ERRORS{'OK'}; exit $ERRORS{'OK'};
} }
$opt_H = shift unless ($opt_H); my ($session_params) = Centreon::SNMP::Utils::check_snmp_options($ERRORS{'UNKNOWN'}, \%OPTION);
(print_usage() && exit $ERRORS{'OK'}) unless ($opt_H);
($opt_v) || ($opt_v = shift) || ($opt_v = "1");
my $snmp = $1 if ($opt_v =~ /(\d)/);
($opt_C) || ($opt_C = shift) || ($opt_C = "public");
my $start=time; my $start=time;
my $name = $0; my $name = $0;
$name =~ s/\.pl.*//g; $name =~ s/\.pl.*//g;
my $day = 0; my $day = 0;
# Plugin snmp requests # Plugin snmp requests
my $OID_OBJECTID =$centreon{MIB2}{OBJECTID}; my $OID_OBJECTID =$centreon{MIB2}{OBJECTID};
my $OID_UPTIME_WINDOWS =$centreon{MIB2}{UPTIME_WINDOWS}; my $OID_UPTIME_WINDOWS =$centreon{MIB2}{UPTIME_WINDOWS};
my $OID_UPTIME_OTHER =$centreon{MIB2}{UPTIME_OTHER}; my $OID_UPTIME_OTHER =$centreon{MIB2}{UPTIME_OTHER};
# create a SNMP session my $session = Centreon::SNMP::Utils::connection($ERRORS{'UNKNOWN'}, $session_params);
my ( $session, $error ) = Net::SNMP->session(-hostname => $opt_H,-community => $opt_C, -version => $snmp, -port => $opt_P);
if ( !defined($session) ) {
print("CRITICAL: $error");
exit $ERRORS{'CRITICAL'};
}
my $result = $session->get_request(-varbindlist => [$OID_OBJECTID]);
if (!defined($result)) {
printf("UNKNOWN: %s.\n", $session->error);
$session->close;
exit $ERRORS{'UNKNOWN'};
}
my $result = Centreon::SNMP::Utils::get_snmp_leef([$OID_OBJECTID], $session, $ERRORS{'UNKNOWN'});
my $return_result = $result->{$OID_OBJECTID}; my $return_result = $result->{$OID_OBJECTID};
my $OID = ""; my $OID = "";
if ($return_result =~ /.*Windows.*/i ) { if ($return_result =~ /.*Windows.*/i ) {
@ -126,28 +115,24 @@ if ($return_result =~ /.*Windows.*/i ) {
$OID = $OID_UPTIME_OTHER; $OID = $OID_UPTIME_OTHER;
} }
$result = $session->get_request(-varbindlist => [$OID]); $result = Centreon::SNMP::Utils::get_snmp_leef([$OID], $session, $ERRORS{'UNKNOWN'});
if (!defined($result)) {
printf("UNKNOWN: %s.\n", $session->error);
$session->close;
exit $ERRORS{'UNKNOWN'};
}
my $un = 0; my $un = $result->{$OID};
$return_result = $result->{$OID}; $return_result = $result->{$OID};
if ( $return_result =~ m/(\d*) day[s]?,\s*(\d*):(\d*):(\d*).(\d*)/ ) { if ( $return_result =~ m/(\d*) day[s]?,\s*(\d*):(\d*):(\d*).(\d*)/ ) {
$un = $5 + $4 * 100 + $3 * 100 * 60 + $2 * 100 * 60 * 60 + $1 * 100 * 60 * 60 * 24; $un = $5 + $4 * 100 + $3 * 100 * 60 + $2 * 100 * 60 * 60 + $1 * 100 * 60 * 60 * 24;
$day = $1; if ($opt_d) {
$un = $1;
}
} elsif ( $return_result =~ m/(\d*) hour.*(\d*):(\d*).(\d*)/ ) {
$un = $4 + $3 * 100 + $3 * 100 * 60 + $1 * 100 * 60 * 60 ;
} else {
if ($opt_d) {
$un = floor($un / 60 / 60 / 24 / 100);
}
} }
if ( $return_result =~ m/(\d*) hour.*(\d*):(\d*).(\d*)/ ) {
$un = $4 + $3 * 100 + $3 * 100 * 60 + $1 * 100 * 60 * 60 ;
}
if ($opt_d) {
$un = $day;
}
if ($un || ( $un == 0) ){ if ($un || ( $un == 0) ){
if ($opt_d) { if ($opt_d) {
@ -156,22 +141,30 @@ if ($un || ( $un == 0) ){
print "OK - Uptime (in hundredths of a second): $un|uptime=".$un."hs\n"; print "OK - Uptime (in hundredths of a second): $un|uptime=".$un."hs\n";
} }
exit $ERRORS{'OK'}; exit $ERRORS{'OK'};
} } 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 "\nUsage:\n"; print "\nUsage:\n";
print "$PROGNAME\n"; print "$PROGNAME\n";
print " -H (--hostname) Hostname to query - (required)\n"; print " -H (--hostname) Hostname to query (required)\n";
print " -C (--community) SNMP read community (defaults to public,\n"; print " -C (--community) SNMP read community (defaults to public)\n";
print " used with SNMP v1 and v2c\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 " 2 for SNMP v2c\n";
print " -P (--snmp-port) SNMP port (default: 161)\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 " -d (--day) Uptime in day\n"; print " -d (--day) Uptime in day\n";
print " -V (--version) Plugin version\n"; print " -V (--version) Plugin version\n";
print " -h (--help) usage help\n"; print " -h (--help) usage help\n";
@ -180,9 +173,9 @@ sub print_usage () {
sub print_help () { sub print_help () {
print "##############################################\n"; print "##############################################\n";
print "# Copyright (c) 2004-2011 Centreon #\n"; print "# Copyright (c) 2004-2013 Centreon #\n";
print "# Bugs to http://forge.centreon.com/ #\n"; print "# Bugs to http://forge.centreon.com/ #\n";
print "##############################################\n"; print "##############################################\n";
print_usage(); print_usage();
print "\n"; print "\n";
} }