diff --git a/centreon-plugins/src/check_centreon_MS_multiple_services b/centreon-plugins/src/check_centreon_MS_multiple_services index 095ae59c9..e39c67816 100644 --- a/centreon-plugins/src/check_centreon_MS_multiple_services +++ b/centreon-plugins/src/check_centreon_MS_multiple_services @@ -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,13 @@ # 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 $result @result %process_list %STATUS); +use vars qw($opt_V $opt_h $opt_p $opt_n $result @result %process_list); + +my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3); # Plugin var init @@ -66,68 +56,60 @@ $PROGNAME = $0; sub print_help (); 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, - "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); + ( + "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, + "p=s" => \$opt_p, "process=s" => \$opt_p); if ($opt_V) { print_revision($PROGNAME,'$Revision: 1.2 $'); - exit $ERRORS{'OK'}; + exit $ERRORS{'OK'}; } if ($opt_h) { - print_help(); - exit $ERRORS{'OK'}; + print_help(); + exit $ERRORS{'OK'}; } -if (!$opt_H) { - 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(); - exit $ERRORS{'OK'}; + print_usage(); + exit $ERRORS{'OK'}; } elsif ($opt_p !~ /([-.A-Za-z0-9\,]+)/){ - print_usage(); - exit $ERRORS{'OK'}; + print_usage(); + exit $ERRORS{'OK'}; } $process = $opt_p; my @process_temp = split /\,/,$opt_p; my (@proc,@status); foreach (@process_temp) { -$status[scalar(@status)] = 0; -$proc[scalar(@proc)] = $_; + $status[scalar(@status)] = 0; + $proc[scalar(@proc)] = $_; } my $name = $0; @@ -136,60 +118,28 @@ my $OID_SW_ServiceName = "1.3.6.1.4.1.77.1.2.3.1.1"; my $OID_SW_ServiceInstallStatus = "1.3.6.1.4.1.77.1.2.3.1.2"; my $OID_SW_ServiceStatus = "1.3.6.1.4.1.77.1.2.3.1.3"; -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'}; - } -} -$result = $session->get_table(Baseoid => $OID_SW_ServiceName); -if (!defined($result)) { - printf("UNKNOWN: %s.\n", $session->error); - $session->close; - exit $ERRORS{'UNKNOWN'}; -} +my ($session_params) = Centreon::SNMP::Utils::check_snmp_options($ERRORS{'UNKNOWN'}, \%OPTION); +my $session = Centreon::SNMP::Utils::connection($ERRORS{'UNKNOWN'}, $session_params); + +$result = Centreon::SNMP::Utils::get_snmp_table($OID_SW_ServiceName, $session, $ERRORS{'UNKNOWN'}, \%OPTION); my %install_status = ( 1=>"Uninstalled", 2=> "Install-pending", 3=> "Uninstall-Pending", 4=>"Installed"); my %defined_status = ( 1=>"Active", 2=>"Continue-Pending", 3=> "Pause-Pending", 4=> "Paused"); foreach my $key (oid_lex_sort(keys %$result)) { my $found = 0; for(my $i = 0; $i < scalar(@proc) && !$found; $i++) { - my $value = substr($key, length($OID_SW_ServiceName)+1, length($key)); - if (lc $proc[$i] eq lc $result->{$key}) { - my $result2 = $session->get_request(-varbindlist => [$OID_SW_ServiceInstallStatus . "." . $value]); - if (!defined($result)) { - printf("UNKNOWN: %s.\n", $session->error); - $session->close; - exit $ERRORS{'UNKNOWN'}; - } - if ((my $temp_installStatus_val = $result2->{$OID_SW_ServiceInstallStatus . "." . $value})) { - if ($temp_installStatus_val != 1 && $temp_installStatus_val != 3) { - $result2 = $session->get_request(-varbindlist => [$OID_SW_ServiceStatus . "." . $value]); - if (!defined($result)) { - printf("UNKNOWN: %s.\n", $session->error); - $session->close; - exit $ERRORS{'UNKNOWN'}; - } - $status[$i] = $defined_status{$result2->{$OID_SW_ServiceStatus . "." . $value}}; - }else { - $status[$i] = $install_status{$temp_installStatus_val}; - } - } - $found = 1; - } + my $value = substr($key, length($OID_SW_ServiceName)+1, length($key)); + if (lc $proc[$i] eq lc $result->{$key}) { + my $result2 = Centreon::SNMP::Utils::get_snmp_leef([$OID_SW_ServiceInstallStatus . "." . $value], $session, $ERRORS{'UNKNOWN'}); + if ((my $temp_installStatus_val = $result2->{$OID_SW_ServiceInstallStatus . "." . $value})) { + if ($temp_installStatus_val != 1 && $temp_installStatus_val != 3) { + $result2 = Centreon::SNMP::Utils::get_snmp_leef([$OID_SW_ServiceStatus . "." . $value], $session, $ERRORS{'UNKNOWN'}); + $status[$i] = $defined_status{$result2->{$OID_SW_ServiceStatus . "." . $value}}; + } else { + $status[$i] = $install_status{$temp_installStatus_val}; + } + } + $found = 1; + } } } @@ -199,38 +149,38 @@ my $inactive = ""; my $unknown = ""; # Plugin return code for (my $i = 0; $i < scalar(@proc); $i++) { - if (defined($status[$i])) { - my $written = 0; - if ($final_status ne "CRITICAL" && $status[$i] eq "0") { - $status[$i] = "Unknown"; - $final_status = "WARNING"; - $written = 1; - $unknown .= $proc[$i]." - "; - } - if ($final_status ne "CRITICAL" && $status[$i] eq "Uninstall-Pending" || $status[$i] eq "Pause-Pending") { - $final_status = "WARNING"; - $written = 1; - $inactive .= $proc[$i]." - "; - } - if ($status[$i] && ($status[$i] eq "Uninstalled" || $status[$i] eq "paused")) { - $final_status = "CRITICAL"; - $written = 1; - $inactive .= $proc[$i]." - "; - } - if (!$written) { - $active .= $proc[$i]." - "; - } - } + if (defined($status[$i])) { + my $written = 0; + if ($final_status ne "CRITICAL" && $status[$i] eq "0") { + $status[$i] = "Unknown"; + $final_status = "WARNING"; + $written = 1; + $unknown .= $proc[$i]." - "; + } + if ($final_status ne "CRITICAL" && $status[$i] eq "Uninstall-Pending" || $status[$i] eq "Pause-Pending") { + $final_status = "WARNING"; + $written = 1; + $inactive .= $proc[$i]." - "; + } + if ($status[$i] && ($status[$i] eq "Uninstalled" || $status[$i] eq "paused")) { + $final_status = "CRITICAL"; + $written = 1; + $inactive .= $proc[$i]." - "; + } + if (!$written) { + $active .= $proc[$i]." - "; + } + } } my $msg = ""; if ($inactive ne "") { - $msg .= "INACTIVE : $inactive "; + $msg .= "INACTIVE : $inactive "; } if ($unknown ne "") { - $msg .= "UNKNOWN : $unknown "; + $msg .= "UNKNOWN : $unknown "; } if ($active ne "") { - $msg .= "ACTIVE : $active"; + $msg .= "ACTIVE : $active"; } $msg =~ s/\- $//; print "$msg\n"; @@ -239,22 +189,30 @@ exit $ERRORS{$final_status}; 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 " --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 " -p (--process) Set the process name ex: by default smbd\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(); diff --git a/centreon-plugins/src/check_centreon_snmp_loadaverage b/centreon-plugins/src/check_centreon_snmp_loadaverage index 9b1417935..0e0ea0e2e 100644 --- a/centreon-plugins/src/check_centreon_snmp_loadaverage +++ b/centreon-plugins/src/check_centreon_snmp_loadaverage @@ -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,149 +40,98 @@ # 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_V $opt_h $opt_v $opt_C $opt_H $opt_c $opt_w $opt_D $snmp $opt_k $opt_u $opt_p $opt_P @critical @warning); +use vars qw($opt_V $opt_h $opt_c $opt_w $opt_D @critical @warning); + +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 -my($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, - "u=s" => \$opt_u, "username=s" => \$opt_u, - "p=s" => \$opt_p, "password=s" => \$opt_p, - "k=s" => \$opt_k, "key=s" => \$opt_k, - "P=s" => \$opt_P, "snmp-port=s" => \$opt_P, - "V" => \$opt_V, "version" => \$opt_V, - "v=s" => \$opt_v, "snmp=s" => \$opt_v, - "C=s" => \$opt_C, "community=s" => \$opt_C, - "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, + "w=s" => \$opt_w, "warning=s" => \$opt_w, + "c=s" => \$opt_c, "critical=s" => \$opt_c); 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'}; } -if (!$opt_H) { - print_usage(); - exit $ERRORS{'OK'}; -} - -($opt_v) || ($opt_v = shift) || ($opt_v = "2"); -my $snmp = $1 if ($opt_v =~ /(\d)/); - if ($opt_c && $opt_c =~ /^([0-9\.]+),([0-9\.]+),([0-9\.]+)$/) { - @critical = ($1,$2,$3); + @critical = ($1,$2,$3); } elsif (!defined($opt_c) && !$opt_c){ - @critical = ('2', '4', '6'); + @critical = ('2', '4', '6'); } else { - print "Specify three critical treshold separated with a coma\n"; - exit $ERRORS{'OK'}; + print "Specify three critical treshold separated with a coma\n"; + exit $ERRORS{'OK'}; } if ($opt_w && $opt_w =~ /^([0-9]+),([0-9]+),([0-9]+)$/) { - @warning = ($1,$2,$3); + @warning = ($1,$2,$3); } elsif (!defined($opt_w) && !$opt_w) { - @warning = ('1', '3', '5'); + @warning = ('1', '3', '5'); } else { - print "Specify three warning treshold separated with a coma\n"; - exit $ERRORS{'UNKNOWN'}; + print "Specify three warning treshold separated with a coma\n"; + exit $ERRORS{'UNKNOWN'}; } for (my $i = 0; $i < scalar(@warning); $i++) { - if ($warning[$i] >= $critical[$i]) { - print "Critical tresholds must be superior to warning tresholds.\n"; - exit $ERRORS{'UNKNOWN'}; - } + if ($warning[$i] >= $critical[$i]) { + print "Critical tresholds must be superior to warning tresholds.\n"; + exit $ERRORS{'UNKNOWN'}; + } } -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{'UNKNOWN'}; - } elsif ($opt_p && $opt_k) { - print "Only option -k (--key) or -p (--password) is needed for snmpV3\n"; - exit $ERRORS{'UNKNOWN'}; - } -} - -$opt_C = "public" if (!$opt_C); - my $name = $0; $name =~ s/\.pl.*//g; # Plugin snmp requests - my $OID_CPULOAD_1 = $centreon{UNIX}{CPU_LOAD_1M}; my $OID_CPULOAD_5 = $centreon{UNIX}{CPU_LOAD_5M}; my $OID_CPULOAD_15 =$centreon{UNIX}{CPU_LOAD_15M}; -my ($session, $error); -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'}; - } -} - -my $result = $session->get_request( - -varbindlist => [$OID_CPULOAD_1, $OID_CPULOAD_5, $OID_CPULOAD_15 ] - ); -if (!defined($result)) { - printf("UNKNOWN: %s.\n", $session->error); - $session->close; - exit $ERRORS{'UNKNOWN'}; -} +my ($session_params) = Centreon::SNMP::Utils::check_snmp_options($ERRORS{'UNKNOWN'}, \%OPTION); +my $session = Centreon::SNMP::Utils::connection($ERRORS{'UNKNOWN'}, $session_params); +my $result = Centreon::SNMP::Utils::get_snmp_leef([$OID_CPULOAD_1, $OID_CPULOAD_5, $OID_CPULOAD_15], $session, $ERRORS{'UNKNOWN'}); my $un = $result->{$OID_CPULOAD_1}; my $cinq = $result->{$OID_CPULOAD_5}; my $quinze = $result->{$OID_CPULOAD_15}; @@ -200,23 +149,31 @@ exit $ERRORS{$status}; sub print_usage () { print "\nUsage:\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 " 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 " -c (--critical) Three critical tresholds (defaults : 2,4,6)\n"; print " -w (--warning) Three warning tresholds (defaults : 1,3,5)\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 " -k (--key) snmp V3 key\n"; - print " -p (--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 "##############################################\n"; + print "# Copyright (c) 2004-2013 Centreon #\n"; print "# Bugs to http://forge.centreon.com/ #\n"; print "##############################################\n"; print_usage(); diff --git a/centreon-plugins/src/check_centreon_snmp_memory b/centreon-plugins/src/check_centreon_snmp_memory index e8177619c..092a92ae6 100644 --- a/centreon-plugins/src/check_centreon_snmp_memory +++ b/centreon-plugins/src/check_centreon_snmp_memory @@ -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,25 +40,14 @@ # 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_V $opt_h $opt_g $opt_v $opt_C $opt_w $opt_c $opt_s $opt_H $opt_P @test); -my $pathtorrdbase = $centreon{GLOBAL}{DIR_RRDTOOL}; +use vars qw($opt_V $opt_h $opt_w $opt_c $opt_s); + +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 @@ -71,41 +60,49 @@ 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, - "V" => \$opt_V, "version" => \$opt_V, - "v=s" => \$opt_v, "snmp=s" => \$opt_v, - "P=s" => \$opt_P, "snmp-port=s" => \$opt_P, - "C=s" => \$opt_C, "community=s" => \$opt_C, - "w=s" => \$opt_w, "warning=s" => \$opt_w, - "c=s" => \$opt_c, "critical=s" => \$opt_c, - "s=s" => \$opt_s, "swap=s" => \$opt_s, - "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, + "w=s" => \$opt_w, "warning=s" => \$opt_w, + "c=s" => \$opt_c, "critical=s" => \$opt_c, + "s=s" => \$opt_s, "swap=s" => \$opt_s); 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'}; + 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]+)/); @@ -119,8 +116,7 @@ if ($critical <= $warning){ } my $swap_limit; -if ($opt_s) -{ +if ($opt_s) { ($opt_s) || ($opt_s = shift) || ($opt_s = 10); $swap_limit = $1 if ($opt_s =~ /([0-9]+)/); } @@ -136,24 +132,15 @@ my $name = $0; # my $OID_hrStorageUsed =$centreon{MIB2}{HR_STORAGE_USED}; # 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: SNMP Session : $error"); - exit $ERRORS{'CRITICAL'}; -} #################### #### snmp request ## my $OID_descr_storage = ".1.3.6.1.2.1.25.2.3.1.3"; #"1.3.6.1.2.1.25.2.3.1.1"; -my $result = $session->get_table(Baseoid => $OID_descr_storage); -if (!defined($result)) { - printf("ERROR: Description Table hrStorageType : %s.\n", $session->error); - $session->close; - exit $ERRORS{'UNKNOWN'}; -} +my $result = Centreon::SNMP::Utils::get_snmp_table($OID_descr_storage, $session, $ERRORS{'UNKNOWN'}, \%OPTION); my ($virt_alloc, my $virt_used, my $virt_size); @@ -163,17 +150,17 @@ my $indexC = 0; foreach my $key (oid_lex_sort(keys %$result)) { if ($result->{$key} =~ m/Swap|Virtual/) { - my @cpt = split /\./,$key; - $indexV = $cpt[scalar(@cpt) - 1];; + my @cpt = split /\./,$key; + $indexV = $cpt[scalar(@cpt) - 1];; } if ($result->{$key} =~ m/Real|Physical/) { - my @cpt = split /\./,$key; - $indexR = $cpt[scalar(@cpt)-1]; + my @cpt = split /\./,$key; + $indexR = $cpt[scalar(@cpt)-1]; } if ($result->{$key} =~ m/Cached/) { - my @cpt = split /\./,$key; - $indexC = $cpt[scalar(@cpt)-1]; - } + my @cpt = split /\./,$key; + $indexC = $cpt[scalar(@cpt)-1]; + } } if ($indexV == 0 || $indexR == 0) { printf("ERROR: cannot find ram information"); @@ -184,44 +171,29 @@ my $OID_Swap_storage_used = ".1.3.6.1.2.1.25.2.3.1.6.".$indexV; my $OID_RealM_storage_used = ".1.3.6.1.2.1.25.2.3.1.6.".$indexR; -my $used_mem = $session->get_table(Baseoid => $OID_hrStorage_used); -if (!defined($used_mem)) { - printf("ERROR: Description Table hrStorageUsed : %s.\n", $session->error); - $session->close; - exit $ERRORS{'UNKNOWN'}; -} +my $used_mem = Centreon::SNMP::Utils::get_snmp_table($OID_hrStorage_used, $session, $ERRORS{'UNKNOWN'}, \%OPTION); my $OID_hrStorage_size = ".1.3.6.1.2.1.25.2.3.1.5"; my $OID_Swap_storage_size = ".1.3.6.1.2.1.25.2.3.1.5.".$indexV; my $OID_RealM_storage_size = ".1.3.6.1.2.1.25.2.3.1.5.".$indexR; my $OID_Cache_storage_size = ".1.3.6.1.2.1.25.2.3.1.5.".$indexC; -my $total_mem = $session->get_table(Baseoid => $OID_hrStorage_size); -if (!defined($total_mem)) { - printf("ERROR: Description Table hrStorageSize : %s.\n", $session->error); - $session->close; - exit $ERRORS{'UNKNOWN'}; -} +my $total_mem = Centreon::SNMP::Utils::get_snmp_table($OID_hrStorage_size, $session, $ERRORS{'UNKNOWN'}, \%OPTION); my $OID_storage_allocationUnits = ".1.3.6.1.2.1.25.2.3.1.4"; my $OID_Swap_storage_allocationUnits = ".1.3.6.1.2.1.25.2.3.1.4.".$indexV; my $OID_RealM_storage_allocationUnits = ".1.3.6.1.2.1.25.2.3.1.4.".$indexR; my $OID_Cache_storage_allocationUnits = ".1.3.6.1.2.1.25.2.3.1.4.".$indexC; -my $alloc_units = $session->get_table(Baseoid => $OID_storage_allocationUnits); -if (!defined($alloc_units)) { - printf("ERROR: Description Table hrStorageUsed : %s.\n", $session->error); - $session->close; - exit $ERRORS{'UNKNOWN'}; -} +my $alloc_units = Centreon::SNMP::Utils::get_snmp_table($OID_storage_allocationUnits, $session, $ERRORS{'UNKNOWN'}, \%OPTION); my $swap_used = $used_mem->{$OID_Swap_storage_used} * $alloc_units->{$OID_Swap_storage_allocationUnits}; my $realM_used = $used_mem->{$OID_RealM_storage_used} * $alloc_units->{$OID_RealM_storage_allocationUnits}; my $cache_used = 0; if (defined $indexC > 0) { - if (defined($total_mem->{$OID_Cache_storage_size}) && defined($alloc_units->{$OID_Cache_storage_allocationUnits})) { - $cache_used = $total_mem->{$OID_Cache_storage_size} * $alloc_units->{$OID_Cache_storage_allocationUnits}; - } + if (defined($total_mem->{$OID_Cache_storage_size}) && defined($alloc_units->{$OID_Cache_storage_allocationUnits})) { + $cache_used = $total_mem->{$OID_Cache_storage_size} * $alloc_units->{$OID_Cache_storage_allocationUnits}; + } } my $swap_size = $total_mem->{$OID_Swap_storage_size} * $alloc_units->{$OID_Swap_storage_allocationUnits}; @@ -243,11 +215,10 @@ $percent_realM_used =~ s/\.[0-9]+//; # return -if(($opt_s) && ($opt_s =~ /([0-9]+)/)) { - if (($percent_realM_used >= 99) && ($percent_swap_used > $swap_limit)) - { - print "Swap threshold (".$opt_s."%) excedeed : total memory used : ".$percent_used."%, ram used : ".$percent_realM_used."%, swap used : ".$percent_swap_used."% | used=".$total_memory_used."o size=".$total_memory_size."o\n"; - exit $ERRORS{'CRITICAL'}; +if (($opt_s) && ($opt_s =~ /([0-9]+)/)) { + if (($percent_realM_used >= 99) && ($percent_swap_used > $swap_limit)) { + print "Swap threshold (".$opt_s."%) excedeed : total memory used : ".$percent_used."%, ram used : ".$percent_realM_used."%, swap used : ".$percent_swap_used."% | used=".$total_memory_used."o size=".$total_memory_size."o\n"; + exit $ERRORS{'CRITICAL'}; } } @@ -256,7 +227,7 @@ if ($percent_used >= $opt_c){ exit $ERRORS{'CRITICAL'}; } elsif ($percent_used >= $opt_w){ print "Threshold (".$opt_w."%) excedeed : total memory used : ".$percent_used."%, ram used : ".$percent_realM_used."%, swap used ".$percent_swap_used."% | used=".$total_memory_used."o size=".$total_memory_size."o\n"; - exit $ERRORS{'WARNING'}; + exit $ERRORS{'WARNING'}; } else { print "Total memory used : ".$percent_used."% ram used : ".$percent_realM_used."%, swap used ".$percent_swap_used."% | used=".$total_memory_used."o size=".$total_memory_size."o\n"; exit $ERRORS{'OK'}; @@ -265,12 +236,22 @@ if ($percent_used >= $opt_c){ 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 " -V (--version) Plugin version\n"; print " -h (--help) usage help\n"; print " -c (--critical) percentage of memory used at which a critical message will be generated\n"; @@ -279,10 +260,10 @@ sub print_usage () { } sub print_help () { - print "##############################################\n"; - print "# Copyright (c) 2004-2011 Centreon #\n"; + print "##############################################\n"; + print "# Copyright (c) 2004-2013 Centreon #\n"; print "# Bugs to http://forge.centreon.com/ #\n"; print "##############################################\n"; print_usage(); - print "\n"; + print "\n"; } diff --git a/centreon-plugins/src/check_centreon_snmp_multiple_process b/centreon-plugins/src/check_centreon_snmp_multiple_process index 9cba0711d..cea8dd810 100644 --- a/centreon-plugins/src/check_centreon_snmp_multiple_process +++ b/centreon-plugins/src/check_centreon_snmp_multiple_process @@ -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_d $opt_h $opt_v $opt_C $opt_p $opt_H $opt_n $opt_k $opt_u $opt_x $result @result %process_list %STATUS); +use vars qw($opt_V $opt_d $opt_h $opt_n $opt_p $result @result %process_list); + +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 @@ -66,71 +58,66 @@ $PROGNAME = $0; sub print_help (); 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, - "V" => \$opt_V, "version" => \$opt_V, - "n" => \$opt_n, "number" => \$opt_n, - "v=s" => \$opt_v, "snmp=s" => \$opt_v, - "d" => \$opt_d, "debug" => \$opt_d, - "C=s" => \$opt_C, "community=s" => \$opt_C, - "p=s" => \$opt_p, "process=s" => \$opt_p, - "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'}, + "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=s" => \$opt_p, "process=s" => \$opt_p, + "n" => \$opt_n, "number" => \$opt_n, + "d" => \$opt_d, "debug" => \$opt_d); if ($opt_V) { print_revision($PROGNAME,'$Revision: 1.2 $'); - exit $ERRORS{'OK'}; + exit $ERRORS{'OK'}; } if ($opt_h) { - print_help(); - exit $ERRORS{'OK'}; + print_help(); + exit $ERRORS{'OK'}; } -if (!$opt_H) { - 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 ($session_params) = Centreon::SNMP::Utils::check_snmp_options($ERRORS{'UNKNOWN'}, \%OPTION); my $process; if(!$opt_p) { - print_usage(); - exit $ERRORS{'OK'}; + print_usage(); + exit $ERRORS{'OK'}; } elsif ($opt_p !~ /([-.A-Za-z0-9\,]+)/){ - print_usage(); - exit $ERRORS{'OK'}; + print_usage(); + exit $ERRORS{'OK'}; } $process = $opt_p; my @process_temp = split /\,/,$opt_p; my (@proc,@status,@parameters); foreach (@process_temp) { -my @tab = split /\:/,$_; -$status[scalar(@proc)] = 0; -$proc[scalar(@proc)] = $tab[0]; -$parameters[scalar(@parameters)] = $tab[1]; + my @tab = split /\:/,$_; + $status[scalar(@proc)] = 0; + $proc[scalar(@proc)] = $tab[0]; + $parameters[scalar(@parameters)] = $tab[1]; } my $name = $0; @@ -140,63 +127,29 @@ my $OID_SW_RunParameters = $centreon{MIB2}{SW_RUNPARAMETERS}; 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'}; - } -} -$result = $session->get_table(Baseoid => $OID_SW_RunName); -if (!defined($result)) { - printf("UNKNOWN: %s.\n", $session->error); - $session->close; - exit $ERRORS{'UNKNOWN'}; -} +my $session = Centreon::SNMP::Utils::connection($ERRORS{'UNKNOWN'}, $session_params); +$result = Centreon::SNMP::Utils::get_snmp_table($OID_SW_RunName, $session, $ERRORS{'UNKNOWN'}, \%OPTION); my %defined_status = (0=>"NotPresent", 1=>"Running", 2=> "Runnable", 3=> "NotRunnable", 4=>"Invalid"); foreach my $key (oid_lex_sort(keys %$result)) { my $found = 0; for(my $i = 0; $i < scalar(@proc) && !$found; $i++) { - my @oid_list = split (/\./,$key); - my $value = pop(@oid_list); - if (lc $proc[$i] eq lc $result->{$key}) { - my $result2 = $session->get_request(-varbindlist => [$OID_SW_RunStatus . "." . $value]); - if (!defined($result)) { - printf("UNKNOWN: %s.\n", $session->error); - $session->close; - exit $ERRORS{'UNKNOWN'}; - } - my $temp_status_val = $result2->{$OID_SW_RunStatus . "." . $value}; - if (defined ($parameters[$i]) && $parameters[$i] ne "") { - $result2 = $session->get_request(-varbindlist => [$OID_SW_RunParameters . "." . $value]); - if (!defined($result)) { - printf("UNKNOWN: %s.\n", $session->error); - $session->close; - exit $ERRORS{'UNKNOWN'}; - } - if (lc $parameters[$i] eq lc $result2->{$OID_SW_RunParameters . "." . $value}) { - $status[$i] = $temp_status_val; - $found = 1; - } - }else { - $status[$i] = $temp_status_val; - $found = 1; - } - } + my @oid_list = split (/\./,$key); + my $value = pop(@oid_list); + if (lc $proc[$i] eq lc $result->{$key}) { + my $result2 = Centreon::SNMP::Utils::get_snmp_leef([$OID_SW_RunStatus . "." . $value], $session, $ERRORS{'UNKNOWN'}); + my $temp_status_val = $result2->{$OID_SW_RunStatus . "." . $value}; + if (defined ($parameters[$i]) && $parameters[$i] ne "") { + $result2 = Centreon::SNMP::Utils::get_snmp_leef([$OID_SW_RunParameters . "." . $value], $session, $ERRORS{'UNKNOWN'}); + if (lc $parameters[$i] eq lc $result2->{$OID_SW_RunParameters . "." . $value}) { + $status[$i] = $temp_status_val; + $found = 1; + } + } else { + $status[$i] = $temp_status_val; + $found = 1; + } + } } } @@ -209,34 +162,34 @@ my $OK = ""; my $unKnown = ""; #$msg =~ s/\-$//; for (my $i = 0; $i < scalar(@proc); $i++) { - if (defined($status[$i])) { - if ($status[$i] != 1 && $status[$i] != 2) { - if ($status[$i] == 0) { - $final_status = "CRITICAL"; - $notPresent .= $proc[$i]." - "; - } - if ($status[$i]) { - $final_status = "CRITICAL"; - $critical .= $proc[$i]." - "; - } - }else { - $OK .= $proc[$i]." - "; - } - }else { - $unKnown .= $proc[$i]." - "; - } + if (defined($status[$i])) { + if ($status[$i] != 1 && $status[$i] != 2) { + if ($status[$i] == 0) { + $final_status = "CRITICAL"; + $notPresent .= $proc[$i]." - "; + } + if ($status[$i]) { + $final_status = "CRITICAL"; + $critical .= $proc[$i]." - "; + } + } else { + $OK .= $proc[$i]." - "; + } + } else { + $unKnown .= $proc[$i]." - "; + } } if ($critical ne "") { -$msg = "CRITICAL : $critical"; + $msg = "CRITICAL : $critical"; } if ($notPresent ne "") { -$msg .= "NOT-PRESENT : $notPresent"; + $msg .= "NOT-PRESENT : $notPresent"; } if ($OK ne "" && $opt_d) { -$msg .= "OK : $OK"; + $msg .= "OK : $OK"; } if ($msg eq "" ) { - $msg = "All process OK"; + $msg = "All process OK"; } $msg =~ s/\- $//; print "$msg\n"; @@ -245,22 +198,30 @@ exit $ERRORS{$final_status}; 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 " -n (--number) Return the number of current running processes. \n"; - print " -p (--process) Set the process name ex: by default smbd\n"; + print " 3 for SNMP v3\n"; + print " -P (--snmp-port) SNMP port (default: 161)\n"; print " -k (--key) snmp V3 key\n"; - print " -x (--password) snmp V3 password\n"; - print " -u (--username) snmp v3 username \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 " -n (--number) Return the number of current running processes.\n"; + print " -p (--process) Set the process name ex: by default smbd\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 "##############################################\n"; + print "# Copyright (c) 2004-2013 Centreon #\n"; print "# Bugs to http://forge.centreon.com/ #\n"; print "##############################################\n"; print_usage(); diff --git a/centreon-plugins/src/check_centreon_snmp_packetErrors b/centreon-plugins/src/check_centreon_snmp_packetErrors index 23b09f71d..a5ffa51e4 100644 --- a/centreon-plugins/src/check_centreon_snmp_packetErrors +++ b/centreon-plugins/src/check_centreon_snmp_packetErrors @@ -1,7 +1,6 @@ #! /usr/bin/perl -w -# nagios: -epn ################################################################################ -# Copyright 2004-2011 MERETHIS +# Copyright 2004-2013 MERETHIS # Centreon is developped by : Julien Mathis and Romain Le Merlus under # GPL Licence 2.0. # @@ -41,29 +40,14 @@ # 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'}; -} - -use vars qw($VERSION %centreon); -use vars qw(%centreon); -$VERSION = '$Revision: 1.2 $'; -$VERSION =~ s/^\$.*:\W(.*)\W.+?$/$1/; +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_H $opt_w $opt_c); +use vars qw($opt_V $opt_h $opt_w $opt_c); + +my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3); +my $centplugins_path = "@CENTPLUGINS_TMP@"; # # Plugin var init @@ -71,20 +55,40 @@ use vars qw($opt_V $opt_h $opt_v $opt_C $opt_H $opt_w $opt_c); $PROGNAME = "$0"; my ($row, @flg_created, @last_check_time, @last_in_errors, @last_out_errors, $result_in, $result_out, @nb_out_errors, @nb_in_errors, $update_time, $db_file); -my $pathtolibexecnt = $centreon{NAGIOS_PLUGINS}; 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, - "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, + "w=s" => \$opt_w, "warning=s" => \$opt_w, + "c=s" => \$opt_c, "critical=s" => \$opt_c); if ($opt_V) { print_revision($PROGNAME,'$Revision: 1.2 $'); @@ -100,13 +104,8 @@ if ($opt_h) { ##### Verify Options ## -$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_v) || ($opt_v = shift) || ($opt_v = "1"); -my $snmp = $1 if ($opt_v =~ /(\d)/); - -($opt_C) || ($opt_C = shift) || ($opt_C = "public"); ($opt_c) || ($opt_c = shift) || ($opt_c = 100); my $critical = $1 if ($opt_c =~ /([0-9]+)/); @@ -119,8 +118,7 @@ if ($critical <= $warning){ exit $ERRORS{'OK'}; } -my $start=time; - +my $start = time; ################################################# ##### Plugin snmp requests @@ -132,21 +130,10 @@ my $OID_IF_DESC = ".1.3.6.1.2.1.2.2.1.2"; # create a SNMP session - -my ($session, $error) = Net::SNMP->session(-hostname => $opt_H, -community => $opt_C, -version => $snmp); -if (!defined($session)) { - print("UNKNOWN: SNMP Session : $error"); - exit $ERRORS{'UNKNOWN'}; -} +my $session = Centreon::SNMP::Utils::connection($ERRORS{'UNKNOWN'}, $session_params); ####### Get IN ERRORS - -$result_in = $session->get_table(Baseoid => $OID_IN_ERRORS); -if (!defined($result_in)) { - printf("ERROR: IN_ERRORS : %s.\n", $session->error); - $session->close; - exit $ERRORS{'UNKNOWN'}; -} +$result_in = Centreon::SNMP::Utils::get_snmp_table($OID_IN_ERRORS, $session, $ERRORS{'UNKNOWN'}, \%OPTION); # loop for each interface foreach my $err (oid_lex_sort(keys %$result_in)) { @@ -154,24 +141,14 @@ foreach my $err (oid_lex_sort(keys %$result_in)) { } # ####### Get OUT ERRORS -$result_out = $session->get_table(Baseoid => $OID_OUT_ERRORS); -if (!defined($result_out)) { - printf("ERROR: OUT_ERRORS : %s.\n", $session->error); - $session->close; - exit $ERRORS{'UNKNOWN'}; -} +$result_out = Centreon::SNMP::Utils::get_snmp_table($OID_OUT_ERRORS, $session, $ERRORS{'UNKNOWN'}, \%OPTION); foreach my $err (oid_lex_sort(keys %$result_out)) { $nb_out_errors[scalar(@nb_out_errors)] = $result_out->{$err}; } # ####### Get Interface descriptions for output my @desc_tab; -my $if_description = $session->get_table(Baseoid => $OID_IF_DESC); -if (!defined($if_description)) { - printf("ERROR: DESCRIPTIONS ERRORS : %s.\n", $session->error); - $session->close; - exit $ERRORS{'UNKNOWN'}; -} +my $if_description = Centreon::SNMP::Utils::get_snmp_table($OID_IF_DESC, $session, $ERRORS{'UNKNOWN'}, \%OPTION); foreach my $desc (oid_lex_sort(keys %$if_description)) { $desc_tab[scalar(@desc_tab)] = $if_description->{$desc} if ($if_description->{$desc} !~ m/StackSub/i); } @@ -182,29 +159,29 @@ foreach my $desc (oid_lex_sort(keys %$if_description)) { # ## for (my $i = 0; $i < scalar(@nb_in_errors); $i++) { - if (-e "@CENTPLUGINS_TMP@/packet_errors_if".$i."_".$opt_H.".tmp") { - open(FILE,"<"."@CENTPLUGINS_TMP@/packet_errors_if".$i."_".$opt_H.".tmp"); - while($row = ){ - my @last_values = split(":",$row); - $last_check_time[$i] = $last_values[0]; - $last_in_errors[$i] = $last_values[1]; - $last_out_errors[$i] = $last_values[2]; - $flg_created[$i] = 1; - } - close(FILE); + if (-e $centplugins_path . "/packet_errors_if".$i."_" . $OPTION{'host'} . ".tmp") { + open(FILE,"<" . $centplugins_path . "/packet_errors_if".$i."_" . $OPTION{'host'} . ".tmp"); + while($row = ){ + my @last_values = split(":",$row); + $last_check_time[$i] = $last_values[0]; + $last_in_errors[$i] = $last_values[1]; + $last_out_errors[$i] = $last_values[2]; + $flg_created[$i] = 1; + } + close(FILE); } else { - $flg_created[$i] = 0; + $flg_created[$i] = 0; } $update_time = time; - unless (open(FILE,">"."@CENTPLUGINS_TMP@/packet_errors_if".$i."_".$opt_H.".tmp")){ - print "Unknown - @CENTPLUGINS_TMP@/packet_errors_if".$i."_".$opt_H.".tmp!\n"; - exit $ERRORS{"UNKNOWN"}; -} + unless (open(FILE,">". $centplugins_path . "/packet_errors_if".$i."_" . $OPTION{'host'} . ".tmp")){ + print "Unknown - $centplugins_path/packet_errors_if".$i."_" . $OPTION{'host'} . ".tmp!\n"; + exit $ERRORS{"UNKNOWN"}; + } print FILE "$update_time:$nb_in_errors[$i]:$nb_out_errors[$i]"; close(FILE); if ($flg_created[$i] eq 0){ - print "First execution : Buffer in creation.... \n"; + print "First execution : Buffer in creation.... \n"; } } @@ -217,19 +194,19 @@ my $diff_test = 0; for (my $i = 0; $i < scalar(@nb_in_errors); $i++) { my $interface = $i+1; if ($flg_created[$i]) { - if (($nb_in_errors[$i] - $last_in_errors[$i] >= $critical) or ($nb_out_errors[$i] - $last_out_errors[$i] >= $critical)){ - $msg[$i] = $desc_tab[$i] . ":critical "; - $status = "CRITICAL"; - } - if (($nb_in_errors[$i] - $last_in_errors[$i] >= $warning) or ($nb_out_errors[$i] - $last_out_errors[$i] >= $warning)){ - if (!defined($msg[$i])) { - $msg[$i] = $desc_tab[$i] . ":warning "; - } - if ($status ne "CRITICAL") { - $status = "WARNING"; - } - } - $diff_test = 1; + if (($nb_in_errors[$i] - $last_in_errors[$i] >= $critical) or ($nb_out_errors[$i] - $last_out_errors[$i] >= $critical)){ + $msg[$i] = $desc_tab[$i] . ":critical "; + $status = "CRITICAL"; + } + if (($nb_in_errors[$i] - $last_in_errors[$i] >= $warning) or ($nb_out_errors[$i] - $last_out_errors[$i] >= $warning)){ + if (!defined($msg[$i])) { + $msg[$i] = $desc_tab[$i] . ":warning "; + } + if ($status ne "CRITICAL") { + $status = "WARNING"; + } + } + $diff_test = 1; } } @@ -239,7 +216,7 @@ if (!$diff_test) { my $output = ""; for (my $i = 0; $i < scalar (@msg); $i++) { if (defined($msg[$i])) { - $output .= $msg[$i]; + $output .= $msg[$i]; } } if ($output ne ""){ @@ -248,27 +225,39 @@ if ($output ne ""){ print "Status OK on all interfaces\n"; } exit($ERRORS{$status}); - sub print_usage () { - print "\nUsage:\n"; - print "$PROGNAME\n"; - print " -H (--hostname) Hostname to query - (required)\n"; - print " -C (--community) SNMP read community (default 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 " -w (--warn) Signal strength at which a warning message will be generated\n"; - print " (default 80)\n"; - print " -c (--crit) Signal strength at which a critical message will be generated\n"; - print " (default 100)\n"; - print " -V (--version) Plugin version\n"; - print " -h (--help) usage help\n"; + +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 " -w (--warn) Signal strength at which a warning message will be generated\n"; + print " (default 80)\n"; + print " -c (--crit) Signal strength at which a critical message will be generated\n"; + print " (default 100)\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 "##############################################\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 +}