From 7ec16b9a828b52fd137fd99c9dc5be30e871cf2f Mon Sep 17 00:00:00 2001 From: Quentin Garnier Date: Mon, 14 Jan 2013 15:51:01 +0100 Subject: [PATCH] Compatibility SNMPv3 Fix #2201 #29 --- .../src/check_centreon_snmp_TcpConn | 117 ++++++++------- .../src/check_centreon_snmp_traffic | 2 +- .../src/check_centreon_snmp_uptime | 139 +++++++++--------- 3 files changed, 128 insertions(+), 130 deletions(-) diff --git a/centreon-plugins/src/check_centreon_snmp_TcpConn b/centreon-plugins/src/check_centreon_snmp_TcpConn index b725a5c26..52231d8d5 100644 --- a/centreon-plugins/src/check_centreon_snmp_TcpConn +++ b/centreon-plugins/src/check_centreon_snmp_TcpConn @@ -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. # @@ -37,25 +37,15 @@ #################################################################################### # # 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); - 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"; + use vars qw($PROGNAME); 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($snmp); +use vars qw($opt_h $opt_V $opt_p $opt_c $opt_w); + +my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3); my $Revision = 1.2.1; @@ -63,15 +53,36 @@ $PROGNAME = "ckeck_TcpConn"; 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, - "v=s" => \$opt_v, "snmp_version=s" => \$opt_v, - "V" => \$opt_V, "version" => \$opt_V, - "H=s" => \$opt_H, "Hostname=s" => \$opt_H, - "p=i" => \$opt_p, "port=s" => \$opt_p, - "C=s" => \$opt_C, "Community=s" => \$opt_C, - "c=s"=> \$opt_c, "w=s"=> \$opt_w + ( + "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'}, + "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) { @@ -84,44 +95,28 @@ if ($opt_h) { exit $ERRORS{'OK'}; } -$opt_H = shift unless ($opt_H); -(print_usage() && exit $ERRORS{'OK'}) unless ($opt_H); +my ($session_params) = Centreon::SNMP::Utils::check_snmp_options($ERRORS{'UNKNOWN'}, \%OPTION); $opt_p = shift 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; $name =~ s/\.pl.*//g; my $day = 0; #=== create a SNMP session ==== # 1.3.6.1.4.1.232.1.2.2.1.1.6 - -my ($session, $error) = Net::SNMP->session(-hostname => $opt_H,-community => $opt_C, -version => $snmp); -if (!defined($session)) { - print("CRITICAL: $error"); - exit $ERRORS{'CRITICAL'}; -} +my $session = Centreon::SNMP::Utils::connection($ERRORS{'UNKNOWN'}, $session_params); my $OID_TCP_PORT = ".1.3.6.1.2.1.6.13.1.3"; -my $result = $session->get_table(Baseoid => $OID_TCP_PORT); -if (!defined($result)) { - printf("ERROR: Description Table : %s.\n", $session->error); - $session->close; - exit $ERRORS{'UNKNOWN'}; -} +my $result = Centreon::SNMP::Utils::get_snmp_table($OID_TCP_PORT, $session, $ERRORS{'UNKNOWN'}, \%OPTION); my $cpt = 0; foreach my $key (oid_lex_sort(keys %$result)) { - if ($result->{$key} == $opt_p) { - $cpt++; - } + if ($result->{$key} == $opt_p) { + $cpt++; + } } 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"; if ($cpt >= $opt_w && $cpt < $opt_c){ - exit $ERRORS{'WARNING'}; + exit $ERRORS{'WARNING'}; } elsif ($cpt >= $opt_c){ - exit $ERRORS{'CRITICAL'}; + exit $ERRORS{'CRITICAL'}; } else { - exit $ERRORS{'OK'}; + exit $ERRORS{'OK'}; } - sub print_usage () { print "\nUsage:\n"; print "$PROGNAME\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 " -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 " --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 " -h (--help) usage help\n"; } 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"; -} \ No newline at end of file +} diff --git a/centreon-plugins/src/check_centreon_snmp_traffic b/centreon-plugins/src/check_centreon_snmp_traffic index 484a2910b..1e411ac30 100644 --- a/centreon-plugins/src/check_centreon_snmp_traffic +++ b/centreon-plugins/src/check_centreon_snmp_traffic @@ -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. # diff --git a/centreon-plugins/src/check_centreon_snmp_uptime b/centreon-plugins/src/check_centreon_snmp_uptime index 6261ca8ed..0ef1792b6 100644 --- a/centreon-plugins/src/check_centreon_snmp_uptime +++ b/centreon-plugins/src/check_centreon_snmp_uptime @@ -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. # @@ -39,85 +39,74 @@ # 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); - 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"; use vars qw($PROGNAME); +use POSIX; 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; 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, - "V" => \$opt_V, "version" => \$opt_V, - "v=s" => \$opt_v, "snmp=s" => \$opt_v, - "C=s" => \$opt_C, "community=s" => \$opt_C, - "P=s" => \$opt_P, "snmp-port=s" => \$opt_P, - "d" => \$opt_d, "day" => \$opt_d, - "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, + "d" => \$opt_d, "day" => \$opt_d, + ); if ($opt_V) { print_revision($PROGNAME,'$Revision: 1.2 $'); exit $ERRORS{'OK'}; } -if (!defined($opt_P)) { - $opt_P = 161; -} 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 = "1"); -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); my $start=time; my $name = $0; $name =~ s/\.pl.*//g; my $day = 0; - # Plugin snmp requests my $OID_OBJECTID =$centreon{MIB2}{OBJECTID}; my $OID_UPTIME_WINDOWS =$centreon{MIB2}{UPTIME_WINDOWS}; my $OID_UPTIME_OTHER =$centreon{MIB2}{UPTIME_OTHER}; -# create a SNMP session -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 $session = Centreon::SNMP::Utils::connection($ERRORS{'UNKNOWN'}, $session_params); +my $result = Centreon::SNMP::Utils::get_snmp_leef([$OID_OBJECTID], $session, $ERRORS{'UNKNOWN'}); my $return_result = $result->{$OID_OBJECTID}; my $OID = ""; if ($return_result =~ /.*Windows.*/i ) { @@ -126,28 +115,24 @@ if ($return_result =~ /.*Windows.*/i ) { $OID = $OID_UPTIME_OTHER; } -$result = $session->get_request(-varbindlist => [$OID]); -if (!defined($result)) { - printf("UNKNOWN: %s.\n", $session->error); - $session->close; - exit $ERRORS{'UNKNOWN'}; -} +$result = Centreon::SNMP::Utils::get_snmp_leef([$OID], $session, $ERRORS{'UNKNOWN'}); -my $un = 0; +my $un = $result->{$OID}; $return_result = $result->{$OID}; 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; - $day = $1; + $un = $5 + $4 * 100 + $3 * 100 * 60 + $2 * 100 * 60 * 60 + $1 * 100 * 60 * 60 * 24; + 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 ($opt_d) { @@ -156,22 +141,30 @@ if ($un || ( $un == 0) ){ print "OK - Uptime (in hundredths of a second): $un|uptime=".$un."hs\n"; } exit $ERRORS{'OK'}; -} -else{ +} else { print "CRITICAL Host unavailable\n"; exit $ERRORS{'CRITICAL'}; } - 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 " -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 " -V (--version) Plugin version\n"; print " -h (--help) usage help\n"; @@ -180,9 +173,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"; -} \ No newline at end of file +}