SNMPv3 compatibility

This commit is contained in:
Quentin Garnier 2013-01-14 11:35:59 +01:00
parent e7f55687d1
commit b893cc3cc1
2 changed files with 313 additions and 403 deletions

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.
#
@ -40,23 +40,15 @@
#
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($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_V $opt_h $opt_v $opt_C $opt_p $opt_H $opt_n $opt_k $opt_u $opt_x $opt_w $opt_c $result @result %process_list %STATUS);
use vars qw($opt_V $opt_h $opt_p $opt_n $opt_w $opt_c $result @result %process_list %STATUS);
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
@ -68,18 +60,36 @@ sub print_usage ();
%STATUS=(1=>'running',2=>'runnable',3=>'notRunnable',4=>'invalid');
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,
"u=s" => \$opt_u, "username=s" => \$opt_u,
"x=s" => \$opt_x, "password=s" => \$opt_x,
"k=s" => \$opt_k, "key=s" => \$opt_k,
(
"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,
"n" => \$opt_n, "number" => \$opt_n,
"v=s" => \$opt_v, "snmp=s" => \$opt_v,
"C=s" => \$opt_C, "community=s" => \$opt_C,
"p=s" => \$opt_p, "process=s" => \$opt_p,
"H=s" => \$opt_H, "hostname=s" => \$opt_H,
"w=s" => \$opt_w, "warning=s" => \$opt_w,
"c=s" => \$opt_c, "critical=s" => \$opt_c);
@ -93,35 +103,13 @@ if ($opt_h) {
exit $ERRORS{'OK'};
}
if (!$opt_H) {
print_usage();
exit $ERRORS{'OK'};
}
my ($session_params) = Centreon::SNMP::Utils::check_snmp_options($ERRORS{'UNKNOWN'}, \%OPTION);
if ($opt_n && (!$opt_c || !$opt_w)) {
print_usage();
exit $ERRORS{'OK'};
}
($opt_v) || ($opt_v = shift) || ($opt_v = "2");
my $snmp = $1 if ($opt_v =~ /(\d)/);
if ($snmp eq "3") {
if (!$opt_u) {
print "Option -u (--username) is required for snmpV3\n";
exit $ERRORS{'OK'};
}
if (!$opt_x && !$opt_k) {
print "Option -k (--key) or -x (--password) is required for snmpV3\n";
exit $ERRORS{'OK'};
} elsif ($opt_x && $opt_k) {
print "Only option -k (--key) or -x (--password) is needed for snmpV3\n";
exit $ERRORS{'OK'};
}
}
$opt_C = "public" if (!$opt_C);
my $process;
if(!$opt_p) {
print_usage();
@ -140,33 +128,9 @@ my $OID_SW_RunName = $centreon{MIB2}{SW_RUNNAME};
my $OID_SW_RunIndex =$centreon{MIB2}{SW_RUNINDEX};
my $OID_SW_RunStatus =$centreon{MIB2}{SW_RUNSTATUS};
my ($session, $error);
if ($snmp eq "1" || $snmp eq "2") {
($session, $error) = Net::SNMP->session(-hostname => $opt_H, -community => $opt_C, -version => $snmp);
if (!defined($session)) {
print("UNKNOWN: SNMP Session : $error\n");
exit $ERRORS{'UNKNOWN'};
}
} elsif ($opt_k) {
($session, $error) = Net::SNMP->session(-hostname => $opt_H, -version => $snmp, -username => $opt_u, -authkey => $opt_k);
if (!defined($session)) {
print("UNKNOWN: SNMP Session : $error\n");
exit $ERRORS{'UNKNOWN'};
}
} elsif ($opt_x) {
($session, $error) = Net::SNMP->session(-hostname => $opt_H, -version => $snmp, -username => $opt_u, -authpassword => $opt_x);
if (!defined($session)) {
print("UNKNOWN: SNMP Session : $error\n");
exit $ERRORS{'UNKNOWN'};
}
}
my $session = Centreon::SNMP::Utils::connection($ERRORS{'UNKNOWN'}, $session_params);
$result = $session->get_table(Baseoid => $OID_SW_RunName);
if (!defined($result)) {
printf("UNKNOWN: %s.\n", $session->error);
$session->close;
exit $ERRORS{'UNKNOWN'};
}
$result = Centreon::SNMP::Utils::get_snmp_table($OID_SW_RunName, $session, $ERRORS{'UNKNOWN'}, \%OPTION);
$proc = 0;
foreach my $key (oid_lex_sort(keys %$result)) {
@ -181,12 +145,7 @@ foreach my $key (oid_lex_sort(keys %$result)) {
if (!($opt_n)) {
if ($process_list{$process}) {
$result = $session->get_request(-varbindlist => [$OID_SW_RunStatus . "." . $process_list{$process}]);
if (!defined($result)) {
printf("UNKNOWN: %s.\n", $session->error);
$session->close;
exit $ERRORS{'UNKNOWN'};
}
$result = Centreon::SNMP::Utils::get_snmp_leef([$OID_SW_RunStatus . "." . $process_list{$process}], $session, $ERRORS{'UNKNOWN'});
$proc_run = $result->{$OID_SW_RunStatus . "." . $process_list{$process} };
}
}
@ -217,23 +176,31 @@ 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 " 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 " -n (--number) Return the number of current running processes. \n";
print " -w (--warning) Number of process that will cause a warning (only required with -n option)\n";
print " -c (--critical) Number of process that will cause an error (only required with -n option)\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 " -p (--process) Set the process name ex: by default smbd (required)\n";
print " -k (--key) snmp V3 key\n";
print " -x (--password) snmp V3 password\n";
print " -u (--username) snmp v3 username \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();

View File

@ -27,7 +27,6 @@ my $file_history=200; # number of data to keep in files.
my $delta_of_time_to_make_average=300; # 5minutes by default
use strict;
use Net::SNMP;
use Getopt::Long;
# Nagios specific
@ -36,15 +35,16 @@ use lib "@NAGIOS_PLUGINS@";
use utils qw(%ERRORS $TIMEOUT);
# centreon specific
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";
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,
);
my $session_params;
# SNMP Datas
my $process_table= '1.3.6.1.2.1.25.4.2.1';
@ -59,9 +59,6 @@ my $proc_run_state = '1.3.6.1.2.1.25.4.2.1.7';
my $Version='1.2.1';
my $o_host = undef; # hostname
my $o_community =undef; # community
my $o_port = 161; # port
my $o_descr = undef; # description filter
my $o_warn = 0; # warning limit
my @o_warnL= undef; # warning limits (min,max)
@ -74,9 +71,6 @@ my $o_noreg= undef; # Do not use Regexp for name
my $o_path= undef; # check path instead of name
my $o_inverse= undef; # checks max instead of min number of process
my $o_timeout= 5; # Default 5s Timeout
# SNMP V3 specific
my $o_login= undef; # snmp v3 login
my $o_passwd= undef; # snmp v3 passwd
# Memory & CPU
my $o_mem= undef; # checks memory (max)
my @o_memL= undef; # warn and crit level for mem
@ -85,14 +79,10 @@ my $o_cpu= undef; # checks CPU usage
my @o_cpuL= undef; # warn and crit level for cpu
my $o_delta= $delta_of_time_to_make_average; # delta time for CPU check
# Oreon specific
my $o_step= undef;
my $o_g= undef;
my $o_S= undef;
my $step= undef;
my $rrd= undef;
my $start= undef;
my $ServiceId= undef;
my @rrd_data= undef;
# functions
@ -122,14 +112,15 @@ sub read_file {
my (@last_values,@file_values,$i);
open(FILE,"<".$traffic_file) || return (1,0,0);
while($ligne = <FILE>)
{
while($ligne = <FILE>) {
chomp($ligne);
@file_values = split(":",$ligne);
#verb("@file_values");
if ($#file_values >= ($items_number-1)) {
# check if there is enough data, else ignore line
for ( $i=0 ; $i< $items_number ; $i++ ) {$last_values[$n_rows][$i]=$file_values[$i]; }
for ( $i=0 ; $i< $items_number ; $i++ ) {
$last_values[$n_rows][$i]=$file_values[$i];
}
$n_rows++;
}
}
@ -150,7 +141,9 @@ sub write_file {
for (my $i=$start_line;$i<$rows;$i++) {
for (my $j=0;$j<$item;$j++) {
print FILE2 $file_values[$i][$j];
if ($j != ($item -1)) { print FILE2 ":" };
if ($j != ($item -1)) {
print FILE2 ":"
};
}
print FILE2 "\n";
}
@ -227,13 +220,22 @@ sub check_options {
my $compat_o_cpu_sum;
Getopt::Long::Configure ("bundling");
GetOptions(
"H|hostname|host=s" => \$OPTION{'host'},
"C|community=s" => \$OPTION{'snmp-community'},
"snmp|snmp-version=s" => \$OPTION{'snmp-version'},
"p|port|P|snmpport|snmp-port=i" => \$OPTION{'snmp-port'},
"l|login|username=s" => \$OPTION{'snmp-auth-user'},
"x|passwd|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'},
'v' => \$o_verb, 'verbose' => \$o_verb,
'h' => \$o_help, 'help' => \$o_help,
'H:s' => \$o_host, 'hostname:s' => \$o_host,
'p:i' => \$o_port, 'port:i' => \$o_port,
'C:s' => \$o_community, 'community:s' => \$o_community,
'l:s' => \$o_login, 'login:s' => \$o_login,
'x:s' => \$o_passwd, 'passwd:s' => \$o_passwd,
'c:s' => \$o_crit, 'critical:s' => \$o_crit,
'w:s' => \$o_warn, 'warn:s' => \$o_warn,
't:i' => \$o_timeout, 'timeout:i' => \$o_timeout,
@ -249,11 +251,10 @@ sub check_options {
'V' => \$o_version, 'version' => \$o_version );
if (defined ($o_help)) { help(); exit $ERRORS{"UNKNOWN"}};
if (defined($o_version)) { p_version(); exit $ERRORS{"UNKNOWN"}};
# check snmp information
if ( !defined($o_community) && (!defined($o_login) || !defined($o_passwd)) )
{ print "Put snmp login info!\n"; print_usage(); exit $ERRORS{"UNKNOWN"}}
($session_params) = Centreon::SNMP::Utils::check_snmp_options($ERRORS{'UNKNOWN'}, \%OPTION);
# Check compulsory attributes
if ( ! defined($o_descr) || ! defined($o_host) ) { print_usage(); exit $ERRORS{"UNKNOWN"}};
if ( ! defined($o_descr) ) { print_usage(); exit $ERRORS{"UNKNOWN"}};
@o_warnL=split(/,/,$o_warn);
@o_critL=split(/,/,$o_crit);
#verb("$#o_warnL $#o_critL");
@ -295,11 +296,6 @@ sub check_options {
if ($o_cpuL[0]>$o_cpuL[1])
{print "Warning must be <= Critical for cpu!\n";print_usage(); exit $ERRORS{"UNKNOWN"}};
}
###### Oreon #######
if (!defined($o_step)) { $o_step="300" }
$step = $1 if ($o_step =~ /(\d+)/);
}
########## MAIN #######
@ -317,51 +313,14 @@ if (defined($TIMEOUT)) {
alarm ($o_timeout+10);
}
# Connect to host
my ($session,$error);
if ( defined($o_login) && defined($o_passwd)) {
# SNMPv3 login
verb("SNMPv3 login");
($session, $error) = Net::SNMP->session(
-hostname => $o_host,
-version => '3',
-username => $o_login,
-authpassword => $o_passwd,
-authprotocol => 'md5',
-privpassword => $o_passwd,
-timeout => $o_timeout
);
} else {
# SNMPV1 login
($session, $error) = Net::SNMP->session(
-hostname => $o_host,
-community => $o_community,
-port => $o_port,
-timeout => $o_timeout
);
}
if (!defined($session)) {
printf("ERROR: %s.\n", $error);
exit $ERRORS{"UNKNOWN"};
}
my $session = Centreon::SNMP::Utils::connection($ERRORS{'UNKNOWN'}, $session_params);
# Look for process in name or path name table
my $resultat=undef;
my $resultat;
if ( !defined ($o_path) ) {
$resultat = $session->get_table(
Baseoid => $run_name_table
);
$resultat = Centreon::SNMP::Utils::get_snmp_table($run_name_table, $session, $ERRORS{'UNKNOWN'}, \%OPTION);
} else {
$resultat = $session->get_table(
Baseoid => $run_path_table
);
}
if (!defined($resultat)) {
printf("ERROR: Process name table : %s.\n", $session->error);
$session->close;
exit $ERRORS{"UNKNOWN"};
$resultat = Centreon::SNMP::Utils::get_snmp_table($run_path_table, $session, $ERRORS{'UNKNOWN'}, \%OPTION);
}
my @tindex = undef;
@ -404,7 +363,6 @@ if ( $num_int == 0) {
my $result=undef;
my $num_int_ok=0;
my %result_cons=();
#$session->max_msg_size([214748]);
if ( $count_oid >= 50) {
my @toid=undef;
my $tmp_num=0;
@ -418,24 +376,13 @@ if ( $count_oid >= 50) {
$toid[$i]=$oids[$i+$tmp_index];
#verb("$i : $toid[$i] : $oids[$i+$tmp_index]");
}
$tmp_result = $session->get_request(
Varbindlist => \@toid
);
if (!defined($tmp_result)) { printf("ERROR: running table : %s.\n", $session->error); $session->close;
exit $ERRORS{"UNKNOWN"};
}
$tmp_result = Centreon::SNMP::Utils::get_snmp_leef(\@toid, $session, $ERRORS{'UNKNOWN'});
foreach (@toid) { $result_cons{$_}=$$tmp_result{$_}; }
$tmp_count-=$tmp_num;
$tmp_index+=$tmp_num;
}
} else {
$result = $session->get_request(
Varbindlist => \@oids
);
if (!defined($result)) { printf("ERROR: running table : %s.\n", $session->error); $session->close;
exit $ERRORS{"UNKNOWN"};
}
$result = Centreon::SNMP::Utils::get_snmp_leef(\@oids, $session, $ERRORS{'UNKNOWN'});
foreach (@oids) {$result_cons{$_}=$$result{$_};}
}
@ -448,8 +395,6 @@ for (my $i=0; $i< $num_int; $i++) {
$num_int_ok++ if (($state == 1) || ($state ==2));
}
$session->close;
my $final_status=0;
my ($res_memory,$res_cpu)=(0,0);
my $memory_print="";
@ -507,7 +452,7 @@ if (defined ($o_cpu) ) {
#### Read file
$temp_file_name=$o_descr;
$temp_file_name =~ s/ /_/g;
$temp_file_name = $o_base_dir . $o_host ."." . $temp_file_name;
$temp_file_name = $o_base_dir . $OPTION{'host'} ."." . $temp_file_name;
# First, read entire file
my @ret_array=read_file($temp_file_name,$n_items_check);
$return = shift(@ret_array);
@ -553,7 +498,6 @@ if (defined ($o_cpu) ) {
if ($final_status==0) { $final_status=3 };
$cpu_print.=", No data for CPU (".$n_rows." line(s)):UNKNOWN";
}
}
@ -591,4 +535,3 @@ if ($final_status==2) { exit $ERRORS{"CRITICAL"};}
if ($final_status==1) { exit $ERRORS{"WARNING"};}
if ($final_status==3) { exit $ERRORS{"UNKNOWN"};}
exit $ERRORS{"OK"};