This commit is contained in:
Quentin Garnier 2013-01-11 16:45:23 +01:00
parent 068bc3d089
commit 0477dd2ff3
2 changed files with 147 additions and 172 deletions

View File

@ -89,8 +89,9 @@ sub check_snmp_options {
print "Missing parameter to open SNMPv3 session\n"; print "Missing parameter to open SNMPv3 session\n";
exit $exit_status; exit $exit_status;
} }
if ($OPTION->{'snmp-auth-protocol'} ne "MD5" && $OPTION->{'snmp-auth-protocol'} ne "SHA1") { $OPTION->{'snmp-auth-protocol'} = lc($OPTION->{'snmp-auth-protocol'});
print "Wrong authentication protocol. Must be MD5 or SHA1\n"; if ($OPTION->{'snmp-auth-protocol'} ne "md5" && $OPTION->{'snmp-auth-protocol'} ne "sha") {
print "Wrong authentication protocol. Must be MD5 or SHA\n";
exit $exit_status; exit $exit_status;
} }
$session_params{-username} = $OPTION->{'snmp-auth-user'}; $session_params{-username} = $OPTION->{'snmp-auth-user'};
@ -102,8 +103,9 @@ sub check_snmp_options {
} }
if ((defined($OPTION->{'snmp-priv-password'}) || defined($OPTION->{'snmp-priv-key'})) && defined($OPTION->{'snmp-priv-protocol'})) { 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") { $OPTION->{'snmp-priv-protocol'} = lc($OPTION->{'snmp-priv-protocol'});
print "Wrong encryption protocol. Must be DES or AES\n"; if ($OPTION->{'snmp-priv-protocol'} ne "des" && $OPTION->{'snmp-priv-protocol'} ne "aes" && $OPTION->{'snmp-priv-protocol'} ne "aes128") {
print "Wrong encryption protocol. Must be DES, AES or AES128\n";
exit $exit_status; exit $exit_status;
} }

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.
# #
@ -40,23 +40,15 @@
# #
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(%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_V $opt_t $opt_P $opt_h $opt_v $opt_f $opt_C $opt_d $opt_k $opt_u $opt_p $opt_n $opt_w $opt_c $opt_H $opt_s $opt_L $opt_M $opt_a @test); use vars qw($opt_V $opt_t $opt_h $opt_f $opt_d $opt_n $opt_w $opt_c $opt_s $opt_L $opt_M $opt_a @test);
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 # Plugin var init
@ -68,76 +60,69 @@ $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, (
"u=s" => \$opt_u, "username=s" => \$opt_u, "H|hostname|host=s" => \$OPTION{'host'},
"p=s" => \$opt_p, "password=s" => \$opt_p, "C|community=s" => \$OPTION{'snmp-community'},
"k=s" => \$opt_k, "key=s" => \$opt_k, "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'},
"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" => \$opt_V, "version" => \$opt_V,
"s" => \$opt_s, "show" => \$opt_s, "s" => \$opt_s, "show" => \$opt_s,
"v=s" => \$opt_v, "snmp=s" => \$opt_v,
"C=s" => \$opt_C, "community=s" => \$opt_C,
"d=s" => \$opt_d, "disk=s" => \$opt_d, "d=s" => \$opt_d, "disk=s" => \$opt_d,
"n" => \$opt_n, "name" => \$opt_n, "n" => \$opt_n, "name" => \$opt_n,
"w=s" => \$opt_w, "warning=s" => \$opt_w, "w=s" => \$opt_w, "warning=s" => \$opt_w,
"c=s" => \$opt_c, "critical=s" => \$opt_c, "c=s" => \$opt_c, "critical=s" => \$opt_c,
"H=s" => \$opt_H, "hostname=s" => \$opt_H,
"L" => \$opt_L, "label" => \$opt_L, "L" => \$opt_L, "label" => \$opt_L,
"a=s" => \$opt_a, "cache=s" => \$opt_a, "a=s" => \$opt_a, "cache=s" => \$opt_a,
"M" => \$opt_M, "M" => \$opt_M,
"t=s" => \$opt_t); "t=s" => \$opt_t);
if ($opt_V) { if ($opt_V) {
print_revision($PROGNAME,'$Revision: 1.3 $'); print_revision($PROGNAME,'$Revision: 1.3 $');
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'};
} }
if (!$opt_H) {
print_usage();
exit $ERRORS{'OK'};
}
if ($opt_n && !$opt_d) { if (!defined($opt_d)) {
print "Option -d (--disk) needed\n";
exit $ERRORS{'UNKNOWN'};
}
if (defined($opt_n) && !defined($opt_d)) {
print "Option -n (--name) need option -d (--disk)\n"; print "Option -n (--name) need option -d (--disk)\n";
exit $ERRORS{'UNKNOWN'}; exit $ERRORS{'UNKNOWN'};
} }
($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_p && !$opt_k) {
print "Option -k (--key) or -p (--password) is required for snmpV3\n";
exit $ERRORS{'OK'};
} elsif ($opt_p && $opt_k) {
print "Only option -k (--key) or -p (--password) is needed for snmpV3\n";
exit $ERRORS{'OK'};
}
}
$opt_C = "public" if (!$opt_C);
$opt_d = 2 if (!$opt_d);
$opt_a = 3 if (!$opt_a); $opt_a = 3 if (!$opt_a);
($opt_d) || ($opt_d = shift) || ($opt_d = 2);
my $partition = 0; my $partition = 0;
if ($opt_d =~ /([0-9]+)/ && !$opt_n){ if ($opt_d =~ /([0-9]+)/ && !defined($opt_n)) {
$partition = $1; $partition = $1;
} elsif (!$opt_n){ } elsif (!$opt_n){
print "Unknown -d number expected... or it doesn't exist, try another disk - number\n"; print "Unknown -d: number expected... try another disk - number\n";
exit $ERRORS{'UNKNOWN'}; exit $ERRORS{'UNKNOWN'};
} }
if (!$opt_c) { if (!$opt_c) {
@ -194,7 +179,6 @@ if ($criticalMB == $warningMB) {
exit $ERRORS{'OK'}; exit $ERRORS{'OK'};
} }
my $name = $0; my $name = $0;
$name =~ s/\.pl.*//g; $name =~ s/\.pl.*//g;
@ -209,31 +193,10 @@ my $OID_hrStorageAllocationUnits = $centreon{MIB2}{HR_STORAGE_ALLOCATION_UNITS};
my $OID_hrStorageSize = $centreon{MIB2}{HR_STORAGE_SIZE}; my $OID_hrStorageSize = $centreon{MIB2}{HR_STORAGE_SIZE};
my $OID_hrStorageUsed = $centreon{MIB2}{HR_STORAGE_USED}; my $OID_hrStorageUsed = $centreon{MIB2}{HR_STORAGE_USED};
# create a SNMP session my ($session_params) = Centreon::SNMP::Utils::check_snmp_options($ERRORS{'UNKNOWN'}, \%OPTION);
my ($session, $error); my $session = Centreon::SNMP::Utils::connection($ERRORS{'UNKNOWN'}, $session_params);
if ($snmp eq "1" || $snmp eq "2") {
($session, $error) = Net::SNMP->session(-hostname => $opt_H, -community => $opt_C, -version => $snmp, -port => $opt_P);
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, -port => $opt_P);
if (!defined($session)) {
print("UNKNOWN: SNMP Session : $error\n");
exit $ERRORS{'UNKNOWN'};
}
} elsif ($opt_p) {
($session, $error) = Net::SNMP->session(-hostname => $opt_H, -version => $snmp, -username => $opt_u, -authpassword => $opt_p, -port => $opt_P);
if (!defined($session)) {
print("UNKNOWN: SNMP Session : $error\n");
exit $ERRORS{'UNKNOWN'};
}
}
$session->translate(Net::SNMP->TRANSLATE_NONE) if (defined($session));
my $cacheFile = "@CENTPLUGINS_TMP@/remote_storage_cache_" . $OPTION{'host'};
my $cacheFile = "@CENTPLUGINS_TMP@/remote_storage_cache_".$opt_H;
my $result; my $result;
my $mustCreateFile = 0; my $mustCreateFile = 0;
my $row; my $row;
@ -243,37 +206,45 @@ my $countLine;
# Cache File exists, lets read it # Cache File exists, lets read it
# #
if (-e $cacheFile) { if (-e $cacheFile) {
my $one_line = 0;
open(FILE,"<".$cacheFile); open(FILE,"<".$cacheFile);
$countLine = 0; $row = <FILE>;
while ($row = <FILE>){ if (defined($row)) {
if (!$countLine) { chomp $row;
my $deltaTime = time() - $row; my $deltaTime = time() - $row;
if ($deltaTime > ($opt_a * 3600)) { if ($deltaTime > ($opt_a * 3600)) {
$mustCreateFile = 1; $mustCreateFile = 1;
} }
} $one_line = 1 if (!<FILE>);
$countLine++;
} }
close(FILE); close(FILE);
# Manage file empty or line 1 empty
if (!defined($row) || $row eq '' || $one_line == 1) {
$mustCreateFile = 1;
} }
else { } else {
$mustCreateFile = 1; $mustCreateFile = 1;
} }
if ($mustCreateFile) { if ($mustCreateFile) {
$result = $session->get_table(Baseoid => $OID_hrStorageDescr); $result = Centreon::SNMP::Utils::get_snmp_table($OID_hrStorageDescr, $session, $ERRORS{'UNKNOWN'}, \%OPTION);
unless (open(FILE,">".$cacheFile)){ unless (open(FILE,">".$cacheFile)){
print "Check mod for temporary file : ".$cacheFile."...\n"; print "Check mod for temporary file : ".$cacheFile."...\n";
exit $ERRORS{"UNKNOWN"}; exit $ERRORS{"UNKNOWN"};
} }
my $currentTime = time(); my $currentTime = time();
print FILE $currentTime."\n"; my $first = 0;
foreach my $key (oid_lex_sort(keys %$result)) { foreach my $key (oid_lex_sort(keys %$result)) {
if (defined($opt_t) && $opt_t eq "AS400"){ if (defined($opt_t) && $opt_t eq "AS400"){
$result->{$key} =~ s/\ //g; $result->{$key} =~ s/\ //g;
} }
my @oid_list = split (/\./,$key); my @oid_list = split (/\./,$key);
my $partitionIndex = pop (@oid_list); my $partitionIndex = pop (@oid_list);
if ($first == 0) {
print FILE $currentTime."\n";
$first = 1;
}
print FILE $partitionIndex.";".$result->{$key}."\n"; print FILE $partitionIndex.";".$result->{$key}."\n";
} }
close(FILE); close(FILE);
@ -288,29 +259,31 @@ if ($opt_n) {
} }
my $expr = ""; my $expr = "";
my $case_sensitive = 1;
if ($opt_d =~ m/^[A-Za-z]:/i) { if ($opt_d =~ m/^[A-Za-z]:/i) {
$opt_d =~ s/\\/\\\\/g; $opt_d =~ s/\\/\\\\/g;
$expr = "^$opt_d"; $expr = "^$opt_d";
} elsif ($opt_d =~ m/^\//) { } elsif ($opt_d =~ m/^\//) {
$expr = "$opt_d\$"; $expr = "^$opt_d\$";
} else { } else {
$case_sensitive = 0;
$expr = "$opt_d"; $expr = "$opt_d";
} }
open(FILE,"<".$cacheFile); open(FILE,"<".$cacheFile);
$countLine = 0; $countLine = 0;
while ($row = <FILE>){ while ($row = <FILE>){
chomp $row;
if ($countLine) { if ($countLine) {
my @resLine = split(/\;/, $row); my @resLine = split(/\;/, $row);
if ($resLine[1] =~ m/$expr/) { if ($case_sensitive == 1 && $resLine[1] =~ /$expr/) {
$partition = $resLine[0];
} elsif ($case_sensitive == 0 && $resLine[1] =~ /$expr/i) {
$partition = $resLine[0]; $partition = $resLine[0];
} }
} }
$countLine++; $countLine++;
} }
if ($countLine == 1) {
unlink($cacheFile);
}
close(FILE); close(FILE);
} }
@ -334,21 +307,12 @@ if ($opt_s) {
} }
$result = $session->get_request( $result = Centreon::SNMP::Utils::get_snmp_leef([$OID_hrStorageDescr.".".$partition,
-varbindlist => [$OID_hrStorageDescr.".".$partition ,
$OID_hrStorageAllocationUnits.".".$partition, $OID_hrStorageAllocationUnits.".".$partition,
$OID_hrStorageSize.".".$partition, $OID_hrStorageSize.".".$partition,
$OID_hrStorageUsed.".".$partition $OID_hrStorageUsed.".".$partition
] ], $session, $ERRORS{'UNKNOWN'},
); defined($opt_n) ? " Cases: 1) SNMP not working. 2) specify the disk name when option -n is used. 3) Disk not exist. 4) Delete cache file '$cacheFile' (maybe corrupted)" : undef);
if (!defined($result)) {
printf("ERROR: %s.\n", $session->error);
if ($opt_n) { print(" - You must specify the disk name when option -n is used");}
print ".\n";
$session->close;
exit $ERRORS{'UNKNOWN'};
}
$hrStorageDescr = $result->{$OID_hrStorageDescr.".".$partition }; $hrStorageDescr = $result->{$OID_hrStorageDescr.".".$partition };
$AllocationUnits = $result->{$OID_hrStorageAllocationUnits.".".$partition }; $AllocationUnits = $result->{$OID_hrStorageAllocationUnits.".".$partition };
@ -383,7 +347,9 @@ if (($Size =~ /([0-9]+)/) && ($AllocationUnits =~ /([0-9]+)/)){
} }
$tot = 1; $tot = 1;
$tot = $Size * $AllocationUnits; $tot = $Size * $AllocationUnits;
if (!$tot){$tot = 1;} if (!$tot) {
$tot = 1;
}
$used = $Used * $AllocationUnits; $used = $Used * $AllocationUnits;
$pourcent = ($used * 100) / $tot; $pourcent = ($used * 100) / $tot;
@ -399,7 +365,6 @@ if (($Size =~ /([0-9]+)/) && ($AllocationUnits =~ /([0-9]+)/)){
my $freeGB = $freeMB / 1024; my $freeGB = $freeMB / 1024;
# Plugin return code # Plugin return code
if ($criticalMB ? ($freeMB <= $critical) : ($pourcent >= $critical)) { if ($criticalMB ? ($freeMB <= $critical) : ($pourcent >= $critical)) {
print "Disk CRITICAL - "; print "Disk CRITICAL - ";
$return_code = 2; $return_code = 2;
@ -438,15 +403,22 @@ if (($Size =~ /([0-9]+)/) && ($AllocationUnits =~ /([0-9]+)/)){
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 (default: 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 " -P (--snmp-port) SNMP port (default: 161)\n";
print " -k (--key) snmp V3 key\n"; print " -k (--key) snmp V3 key\n";
print " -u (--username) snmp V3 username \n";
print " -p (--password) snmp V3 password\n"; print " -p (--password) snmp V3 password\n";
print " -u (--username) snmp v3 username \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 (--disk) Set the disk (number expected) ex: 1, 2,... (default: 2 )\n"; print " -d (--disk) Set the disk (number expected) ex: 1, 2,... (default: 2 )\n";
print " -n (--name) Allows to use disk name with option -d instead of disk oid index\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 " (ex: -d \"C:\" -n, -d \"E:\" -n, -d \"Swap Memory\" -n, -d \"Real Memory\" -n\n";
@ -465,6 +437,7 @@ sub print_usage () {
print " -V (--version) Plugin version\n"; print " -V (--version) Plugin version\n";
print " -L add Windows drive label to output\n"; print " -L add Windows drive label to output\n";
print " -M Shows the size in output in MB instead of GB\n"; print " -M Shows the size in output in MB instead of GB\n";
print " -t To use for AIX or AS/400 (ex. 'AIX' or 'AS/400'\n";
print " -a (--cache) Updates cache file every n hours instead of doing snmpwalk for every check (default: 3)\n"; print " -a (--cache) Updates cache file every n hours instead of doing snmpwalk for every check (default: 3)\n";
print " -h (--help) usage help\n"; print " -h (--help) usage help\n";
@ -472,7 +445,7 @@ 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();