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 #! /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 oid_lex_sort); require "@NAGIOS_PLUGINS@/Centreon/SNMP/Utils.pm";
use FindBin;
use lib "$FindBin::Bin";
use lib "@NAGIOS_PLUGINS@";
use utils qw($TIMEOUT %ERRORS &print_revision &support);
if (eval "require centreon" ) {
use centreon qw(get_parameters);
use vars qw($VERSION %centreon);
%centreon = get_parameters();
} else {
print "Unable to load centreon perl module\n";
exit $ERRORS{'UNKNOWN'};
}
use vars qw($PROGNAME); use vars qw($PROGNAME);
use Getopt::Long; use Getopt::Long;
use vars qw($opt_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 # Plugin var init
@ -68,67 +60,63 @@ sub print_usage ();
%STATUS=(1=>'running',2=>'runnable',3=>'notRunnable',4=>'invalid'); %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'); 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'},
"x=s" => \$opt_x, "password=s" => \$opt_x, "C|community=s" => \$OPTION{'snmp-community'},
"k=s" => \$opt_k, "key=s" => \$opt_k, "v|snmp|snmp-version=s" => \$OPTION{'snmp-version'},
"V" => \$opt_V, "version" => \$opt_V, "P|snmpport|snmp-port=i" => \$OPTION{'snmp-port'},
"n" => \$opt_n, "number" => \$opt_n, "u|username=s" => \$OPTION{'snmp-auth-user'},
"v=s" => \$opt_v, "snmp=s" => \$opt_v, "authpassword|password=s" => \$OPTION{'snmp-auth-password'},
"C=s" => \$opt_C, "community=s" => \$opt_C, "k|authkey=s" => \$OPTION{'snmp-auth-key'},
"p=s" => \$opt_p, "process=s" => \$opt_p, "authprotocol=s" => \$OPTION{'snmp-auth-protocol'},
"H=s" => \$opt_H, "hostname=s" => \$opt_H, "privpassword=s" => \$OPTION{'snmp-priv-password'},
"w=s" => \$opt_w, "warning=s" => \$opt_w, "privkey=s" => \$OPTION{'snmp-priv-key'},
"c=s" => \$opt_c, "critical=s" => \$opt_c); "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,
"w=s" => \$opt_w, "warning=s" => \$opt_w,
"c=s" => \$opt_c, "critical=s" => \$opt_c);
if ($opt_V) { if ($opt_V) {
print_revision($PROGNAME,'$Revision: 1.2 $'); print_revision($PROGNAME,'$Revision: 1.2 $');
exit $ERRORS{'OK'}; exit $ERRORS{'OK'};
} }
if ($opt_h) { if ($opt_h) {
print_help(); print_help();
exit $ERRORS{'OK'}; exit $ERRORS{'OK'};
} }
if (!$opt_H) { my ($session_params) = Centreon::SNMP::Utils::check_snmp_options($ERRORS{'UNKNOWN'}, \%OPTION);
print_usage();
exit $ERRORS{'OK'};
}
if ($opt_n && (!$opt_c || !$opt_w)) { if ($opt_n && (!$opt_c || !$opt_w)) {
print_usage(); print_usage();
exit $ERRORS{'OK'}; 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; my $process;
if(!$opt_p) { if(!$opt_p) {
print_usage(); print_usage();
exit $ERRORS{'OK'}; exit $ERRORS{'OK'};
} elsif ($opt_p !~ /([-.A-Za-z0-9]+)/){ } elsif ($opt_p !~ /([-.A-Za-z0-9]+)/){
print_usage(); print_usage();
exit $ERRORS{'OK'}; exit $ERRORS{'OK'};
} }
$process = $opt_p; $process = $opt_p;
@ -140,67 +128,38 @@ my $OID_SW_RunName = $centreon{MIB2}{SW_RUNNAME};
my $OID_SW_RunIndex =$centreon{MIB2}{SW_RUNINDEX}; my $OID_SW_RunIndex =$centreon{MIB2}{SW_RUNINDEX};
my $OID_SW_RunStatus =$centreon{MIB2}{SW_RUNSTATUS}; my $OID_SW_RunStatus =$centreon{MIB2}{SW_RUNSTATUS};
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);
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); $result = Centreon::SNMP::Utils::get_snmp_table($OID_SW_RunName, $session, $ERRORS{'UNKNOWN'}, \%OPTION);
if (!defined($result)) {
printf("UNKNOWN: %s.\n", $session->error);
$session->close;
exit $ERRORS{'UNKNOWN'};
}
$proc = 0; $proc = 0;
foreach my $key (oid_lex_sort(keys %$result)) { foreach my $key (oid_lex_sort(keys %$result)) {
my @oid_list = split (/\./,$key); my @oid_list = split (/\./,$key);
$process_list{$$result{$key}} = pop (@oid_list) ; $process_list{$$result{$key}} = pop (@oid_list) ;
if (defined($opt_p) && $opt_p ne ""){ if (defined($opt_p) && $opt_p ne ""){
$proc++ if ($$result{$key} eq $opt_p); $proc++ if ($$result{$key} eq $opt_p);
} else { } else {
$proc++; $proc++;
} }
} }
if (!($opt_n)) { if (!($opt_n)) {
if ($process_list{$process}) { if ($process_list{$process}) {
$result = $session->get_request(-varbindlist => [$OID_SW_RunStatus . "." . $process_list{$process}]); $result = Centreon::SNMP::Utils::get_snmp_leef([$OID_SW_RunStatus . "." . $process_list{$process}], $session, $ERRORS{'UNKNOWN'});
if (!defined($result)) { $proc_run = $result->{$OID_SW_RunStatus . "." . $process_list{$process} };
printf("UNKNOWN: %s.\n", $session->error);
$session->close;
exit $ERRORS{'UNKNOWN'};
}
$proc_run = $result->{$OID_SW_RunStatus . "." . $process_list{$process} };
} }
} }
# Plugin return code # Plugin return code
my $status; my $status;
if ($opt_n){ if ($opt_n){
$status = 'OK'; $status = 'OK';
if ($proc >= $opt_w){ if ($proc >= $opt_w){
$status = 'WARNING'; $status = 'WARNING';
} }
if ($proc >= $opt_c){ if ($proc >= $opt_c){
$status = 'CRITICAL'; $status = 'CRITICAL';
} }
print "Number of current processes: $proc|nbproc=$proc\n"; print "Number of current processes: $proc|nbproc=$proc\n";
exit $ERRORS{$status}; exit $ERRORS{$status};
} else { } else {
@ -217,23 +176,31 @@ sub print_usage () {
print "\nUsage:\n"; print "\nUsage:\n";
print "$PROGNAME\n"; print "$PROGNAME\n";
print " -H (--hostname) Hostname to query (required)\n"; print " -H (--hostname) Hostname to query (required)\n";
print " -C (--community) SNMP read community (defaults to public)\n";
print " 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 " -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 " -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 (--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 " -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 " -V (--version) Plugin version\n";
print " -h (--help) usage help\n"; print " -h (--help) usage help\n";
} }
sub print_help () { sub print_help () {
print "##############################################\n"; print "##############################################\n";
print "# Copyright (c) 2004-2011 Centreon #\n"; print "# Copyright (c) 2004-2013 Centreon #\n";
print "# Bugs to http://forge.centreon.com/ #\n"; print "# Bugs to http://forge.centreon.com/ #\n";
print "##############################################\n"; print "##############################################\n";
print_usage(); print_usage();

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 my $delta_of_time_to_make_average=300; # 5minutes by default
use strict; use strict;
use Net::SNMP;
use Getopt::Long; use Getopt::Long;
# Nagios specific # Nagios specific
@ -36,15 +35,16 @@ use lib "@NAGIOS_PLUGINS@";
use utils qw(%ERRORS $TIMEOUT); use utils qw(%ERRORS $TIMEOUT);
# centreon specific # centreon specific
require "@NAGIOS_PLUGINS@/Centreon/SNMP/Utils.pm";
if (eval "require centreon" ) { my %OPTION = (
use centreon qw(get_parameters); "host" => undef,
use vars qw($VERSION %centreon); "snmp-community" => "public", "snmp-version" => 1, "snmp-port" => 161,
%centreon = get_parameters(); "snmp-auth-key" => undef, "snmp-auth-user" => undef, "snmp-auth-password" => undef, "snmp-auth-protocol" => "MD5",
} else { "snmp-priv-key" => undef, "snmp-priv-password" => undef, "snmp-priv-protocol" => "DES",
print "Unable to load centreon perl module\n"; "maxrepetitions" => undef,
exit $ERRORS{'UNKNOWN'}; "64-bits" => undef,
} );
my $session_params;
# SNMP Datas # SNMP Datas
my $process_table= '1.3.6.1.2.1.25.4.2.1'; 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 $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_descr = undef; # description filter
my $o_warn = 0; # warning limit my $o_warn = 0; # warning limit
my @o_warnL= undef; # warning limits (min,max) 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_path= undef; # check path instead of name
my $o_inverse= undef; # checks max instead of min number of process my $o_inverse= undef; # checks max instead of min number of process
my $o_timeout= 5; # Default 5s Timeout 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 # Memory & CPU
my $o_mem= undef; # checks memory (max) my $o_mem= undef; # checks memory (max)
my @o_memL= undef; # warn and crit level for mem 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_cpuL= undef; # warn and crit level for cpu
my $o_delta= $delta_of_time_to_make_average; # delta time for CPU check my $o_delta= $delta_of_time_to_make_average; # delta time for CPU check
# Oreon specific # Oreon specific
my $o_step= undef;
my $o_g= undef; my $o_g= undef;
my $o_S= undef; my $o_S= undef;
my $step= undef;
my $rrd= undef;
my $start= undef; my $start= undef;
my $ServiceId= undef; my $ServiceId= undef;
my @rrd_data= undef;
# functions # functions
@ -110,55 +100,58 @@ sub isnotnum { # Return true if arg is not a number
# Get the alarm signal (just in case snmp timout screws up) # Get the alarm signal (just in case snmp timout screws up)
$SIG{'ALRM'} = sub { $SIG{'ALRM'} = sub {
print ("ERROR: Alarm signal (Nagios time-out)\n"); print ("ERROR: Alarm signal (Nagios time-out)\n");
exit $ERRORS{"UNKNOWN"}; exit $ERRORS{"UNKNOWN"};
}; };
sub read_file { sub read_file {
# Input : File, items_number # Input : File, items_number
# Returns : array of value : [line][item] # Returns : array of value : [line][item]
my ($traffic_file,$items_number)=@_; my ($traffic_file,$items_number)=@_;
my ($ligne,$n_rows)=(undef,0); my ($ligne,$n_rows)=(undef,0);
my (@last_values,@file_values,$i); my (@last_values,@file_values,$i);
open(FILE,"<".$traffic_file) || return (1,0,0); open(FILE,"<".$traffic_file) || return (1,0,0);
while($ligne = <FILE>) while($ligne = <FILE>) {
{ chomp($ligne);
chomp($ligne); @file_values = split(":",$ligne);
@file_values = split(":",$ligne); #verb("@file_values");
#verb("@file_values"); if ($#file_values >= ($items_number-1)) {
if ($#file_values >= ($items_number-1)) { # check if there is enough data, else ignore line
# check if there is enough data, else ignore line for ( $i=0 ; $i< $items_number ; $i++ ) {
for ( $i=0 ; $i< $items_number ; $i++ ) {$last_values[$n_rows][$i]=$file_values[$i]; } $last_values[$n_rows][$i]=$file_values[$i];
$n_rows++; }
$n_rows++;
}
}
close FILE;
if ($n_rows != 0) {
return (0,$n_rows,@last_values);
} else {
return (1,0,0);
} }
}
close FILE;
if ($n_rows != 0) {
return (0,$n_rows,@last_values);
} else {
return (1,0,0);
}
} }
sub write_file { sub write_file {
# Input : file , rows, items, array of value : [line][item] # Input : file , rows, items, array of value : [line][item]
# Returns : 0 / OK, 1 / error # Returns : 0 / OK, 1 / error
my ($file_out,$rows,$item,@file_values)=@_; my ($file_out,$rows,$item,@file_values)=@_;
my $start_line= ($rows > $file_history) ? $rows - $file_history : 0; my $start_line= ($rows > $file_history) ? $rows - $file_history : 0;
if ( open(FILE2,">".$file_out) ) { if ( open(FILE2,">".$file_out) ) {
for (my $i=$start_line;$i<$rows;$i++) { for (my $i=$start_line;$i<$rows;$i++) {
for (my $j=0;$j<$item;$j++) { for (my $j=0;$j<$item;$j++) {
print FILE2 $file_values[$i][$j]; print FILE2 $file_values[$i][$j];
if ($j != ($item -1)) { print FILE2 ":" }; if ($j != ($item -1)) {
} print FILE2 ":"
print FILE2 "\n"; };
}
print FILE2 "\n";
}
close FILE2;
return 0;
} else {
return 1;
} }
close FILE2;
return 0;
} else {
return 1;
}
} }
sub help { sub help {
@ -227,33 +220,41 @@ sub check_options {
my $compat_o_cpu_sum; my $compat_o_cpu_sum;
Getopt::Long::Configure ("bundling"); Getopt::Long::Configure ("bundling");
GetOptions( GetOptions(
'v' => \$o_verb, 'verbose' => \$o_verb, "H|hostname|host=s" => \$OPTION{'host'},
'h' => \$o_help, 'help' => \$o_help, "C|community=s" => \$OPTION{'snmp-community'},
'H:s' => \$o_host, 'hostname:s' => \$o_host, "snmp|snmp-version=s" => \$OPTION{'snmp-version'},
'p:i' => \$o_port, 'port:i' => \$o_port, "p|port|P|snmpport|snmp-port=i" => \$OPTION{'snmp-port'},
'C:s' => \$o_community, 'community:s' => \$o_community, "l|login|username=s" => \$OPTION{'snmp-auth-user'},
'l:s' => \$o_login, 'login:s' => \$o_login, "x|passwd|authpassword|password=s" => \$OPTION{'snmp-auth-password'},
'x:s' => \$o_passwd, 'passwd:s' => \$o_passwd, "k|authkey=s" => \$OPTION{'snmp-auth-key'},
'c:s' => \$o_crit, 'critical:s' => \$o_crit, "authprotocol=s" => \$OPTION{'snmp-auth-protocol'},
'w:s' => \$o_warn, 'warn:s' => \$o_warn, "privpassword=s" => \$OPTION{'snmp-priv-password'},
't:i' => \$o_timeout, 'timeout:i' => \$o_timeout, "privkey=s" => \$OPTION{'snmp-priv-key'},
'n:s' => \$o_descr, 'name:s' => \$o_descr, "privprotocol=s" => \$OPTION{'snmp-priv-protocol'},
'r' => \$o_noreg, 'noregexp' => \$o_noreg, "maxrepetitions=s" => \$OPTION{'maxrepetitions'},
'f' => \$o_path, 'fullpath' => \$o_path, "64-bits" => \$OPTION{'64-bits'},
'm:s' => \$o_mem, 'memory:s' => \$o_mem,
'a' => \$o_mem_avg, 'average' => \$o_mem_avg, 'v' => \$o_verb, 'verbose' => \$o_verb,
'u:s' => \$o_cpu, 'cpu' => \$o_cpu, 'h' => \$o_help, 'help' => \$o_help,
#### To be compatible with version 1.2, will be removed... #### 'c:s' => \$o_crit, 'critical:s' => \$o_crit,
's' => \$compat_o_cpu_sum, 'cpusum' => \$compat_o_cpu_sum, 'w:s' => \$o_warn, 'warn:s' => \$o_warn,
########## 't:i' => \$o_timeout, 'timeout:i' => \$o_timeout,
'V' => \$o_version, 'version' => \$o_version ); 'n:s' => \$o_descr, 'name:s' => \$o_descr,
'r' => \$o_noreg, 'noregexp' => \$o_noreg,
'f' => \$o_path, 'fullpath' => \$o_path,
'm:s' => \$o_mem, 'memory:s' => \$o_mem,
'a' => \$o_mem_avg, 'average' => \$o_mem_avg,
'u:s' => \$o_cpu, 'cpu' => \$o_cpu,
#### To be compatible with version 1.2, will be removed... ####
's' => \$compat_o_cpu_sum, 'cpusum' => \$compat_o_cpu_sum,
##########
'V' => \$o_version, 'version' => \$o_version );
if (defined ($o_help)) { help(); exit $ERRORS{"UNKNOWN"}}; if (defined ($o_help)) { help(); exit $ERRORS{"UNKNOWN"}};
if (defined($o_version)) { p_version(); exit $ERRORS{"UNKNOWN"}}; if (defined($o_version)) { p_version(); exit $ERRORS{"UNKNOWN"}};
# check snmp information ($session_params) = Centreon::SNMP::Utils::check_snmp_options($ERRORS{'UNKNOWN'}, \%OPTION);
if ( !defined($o_community) && (!defined($o_login) || !defined($o_passwd)) )
{ print "Put snmp login info!\n"; print_usage(); exit $ERRORS{"UNKNOWN"}}
# Check compulsory attributes # 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_warnL=split(/,/,$o_warn);
@o_critL=split(/,/,$o_crit); @o_critL=split(/,/,$o_crit);
#verb("$#o_warnL $#o_critL"); #verb("$#o_warnL $#o_critL");
@ -288,18 +289,13 @@ sub check_options {
#### CPU checks #### CPU checks
if (defined ($o_cpu)) { if (defined ($o_cpu)) {
@o_cpuL=split(/,/,$o_cpu); @o_cpuL=split(/,/,$o_cpu);
if ($#o_cpuL != 1) if ($#o_cpuL != 1)
{print "2 values (warning,critical) for cpu\n";print_usage(); exit $ERRORS{"UNKNOWN"}}; {print "2 values (warning,critical) for cpu\n";print_usage(); exit $ERRORS{"UNKNOWN"}};
if (isnotnum($o_cpuL[0]) || isnotnum($o_cpuL[1])) if (isnotnum($o_cpuL[0]) || isnotnum($o_cpuL[1]))
{print "Numeric values for cpu!\n";print_usage(); exit $ERRORS{"UNKNOWN"}}; {print "Numeric values for cpu!\n";print_usage(); exit $ERRORS{"UNKNOWN"}};
if ($o_cpuL[0]>$o_cpuL[1]) if ($o_cpuL[0]>$o_cpuL[1])
{print "Warning must be <= Critical for cpu!\n";print_usage(); exit $ERRORS{"UNKNOWN"}}; {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 ####### ########## MAIN #######
@ -310,58 +306,21 @@ $start=time;
# Check gobal timeout if snmp screws up # Check gobal timeout if snmp screws up
if (defined($TIMEOUT)) { if (defined($TIMEOUT)) {
#verb("Alarm at $TIMEOUT"); #verb("Alarm at $TIMEOUT");
alarm($TIMEOUT); alarm($TIMEOUT);
} else { } else {
#verb("no timeout defined : $o_timeout + 10"); #verb("no timeout defined : $o_timeout + 10");
alarm ($o_timeout+10); alarm ($o_timeout+10);
} }
# Connect to host my $session = Centreon::SNMP::Utils::connection($ERRORS{'UNKNOWN'}, $session_params);
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"};
}
# Look for process in name or path name table # Look for process in name or path name table
my $resultat=undef; my $resultat;
if ( !defined ($o_path) ) { if ( !defined ($o_path) ) {
$resultat = $session->get_table( $resultat = Centreon::SNMP::Utils::get_snmp_table($run_name_table, $session, $ERRORS{'UNKNOWN'}, \%OPTION);
Baseoid => $run_name_table
);
} else { } else {
$resultat = $session->get_table( $resultat = Centreon::SNMP::Utils::get_snmp_table($run_path_table, $session, $ERRORS{'UNKNOWN'}, \%OPTION);
Baseoid => $run_path_table
);
}
if (!defined($resultat)) {
printf("ERROR: Process name table : %s.\n", $session->error);
$session->close;
exit $ERRORS{"UNKNOWN"};
} }
my @tindex = undef; my @tindex = undef;
@ -375,81 +334,67 @@ my $count_oid = 0;
#verb("Filter : $o_descr"); #verb("Filter : $o_descr");
foreach my $key ( keys %$resultat) { foreach my $key ( keys %$resultat) {
# verb("OID : $key, Desc : $$resultat{$key}"); # verb("OID : $key, Desc : $$resultat{$key}");
# test by regexp or exact match # test by regexp or exact match
my $test = defined($o_noreg) my $test = defined($o_noreg)
? $$resultat{$key} eq $o_descr ? $$resultat{$key} eq $o_descr
: $$resultat{$key} =~ /$o_descr/i; : $$resultat{$key} =~ /$o_descr/i;
if ($test) { if ($test) {
# get the index number of the interface # get the index number of the interface
my @oid_list = split (/\./,$key); my @oid_list = split (/\./,$key);
$tindex[$num_int] = pop (@oid_list); $tindex[$num_int] = pop (@oid_list);
# get the full description # get the full description
$descr[$num_int]=$$resultat{$key}; $descr[$num_int]=$$resultat{$key};
# put the oid of running and mem (check this maybe ?) in an array. # put the oid of running and mem (check this maybe ?) in an array.
$oids[$count_oid++]=$proc_mem_table . "." . $tindex[$num_int]; $oids[$count_oid++]=$proc_mem_table . "." . $tindex[$num_int];
$oids[$count_oid++]=$proc_cpu_table . "." . $tindex[$num_int]; $oids[$count_oid++]=$proc_cpu_table . "." . $tindex[$num_int];
$oids[$count_oid++]=$proc_run_state . "." . $tindex[$num_int]; $oids[$count_oid++]=$proc_run_state . "." . $tindex[$num_int];
#verb("Name : $descr[$num_int], Index : $tindex[$num_int]"); #verb("Name : $descr[$num_int], Index : $tindex[$num_int]");
# verb($oids[$count_oid-1]); #verb($oids[$count_oid-1]);
$num_int++; $num_int++;
} }
} }
if ( $num_int == 0) { if ( $num_int == 0) {
print "No process ",(defined ($o_noreg)) ? "named " : "matching ", $o_descr, " found : CRITICAL\n"; print "No process ",(defined ($o_noreg)) ? "named " : "matching ", $o_descr, " found : CRITICAL\n";
exit $ERRORS{"CRITICAL"}; exit $ERRORS{"CRITICAL"};
} }
my $result=undef; my $result=undef;
my $num_int_ok=0; my $num_int_ok=0;
my %result_cons=(); my %result_cons=();
#$session->max_msg_size([214748]);
if ( $count_oid >= 50) { if ( $count_oid >= 50) {
my @toid=undef; my @toid=undef;
my $tmp_num=0; my $tmp_num=0;
my $tmp_index=0; my $tmp_index=0;
my $tmp_count=$count_oid; my $tmp_count=$count_oid;
my $tmp_result=undef; my $tmp_result=undef;
# verb("More than 50 oid, splitting"); # verb("More than 50 oid, splitting");
while ( $tmp_count != 0 ) { while ( $tmp_count != 0 ) {
$tmp_num = ($tmp_count >=50) ? 50 : $tmp_count; $tmp_num = ($tmp_count >=50) ? 50 : $tmp_count;
for (my $i=0; $i<$tmp_num;$i++) { for (my $i=0; $i<$tmp_num;$i++) {
$toid[$i]=$oids[$i+$tmp_index]; $toid[$i]=$oids[$i+$tmp_index];
#verb("$i : $toid[$i] : $oids[$i+$tmp_index]"); #verb("$i : $toid[$i] : $oids[$i+$tmp_index]");
}
$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;
} }
$tmp_result = $session->get_request(
Varbindlist => \@toid
);
if (!defined($tmp_result)) { printf("ERROR: running table : %s.\n", $session->error); $session->close;
exit $ERRORS{"UNKNOWN"};
}
foreach (@toid) { $result_cons{$_}=$$tmp_result{$_}; }
$tmp_count-=$tmp_num;
$tmp_index+=$tmp_num;
}
} else { } else {
$result = $session->get_request( $result = Centreon::SNMP::Utils::get_snmp_leef(\@oids, $session, $ERRORS{'UNKNOWN'});
Varbindlist => \@oids foreach (@oids) {$result_cons{$_}=$$result{$_};}
);
if (!defined($result)) { printf("ERROR: running table : %s.\n", $session->error); $session->close;
exit $ERRORS{"UNKNOWN"};
}
foreach (@oids) {$result_cons{$_}=$$result{$_};}
} }
#Check if process are in running or runnable state #Check if process are in running or runnable state
for (my $i=0; $i< $num_int; $i++) { for (my $i=0; $i< $num_int; $i++) {
my $state=$result_cons{$proc_run_state . "." . $tindex[$i]}; my $state=$result_cons{$proc_run_state . "." . $tindex[$i]};
my $tmpmem=$result_cons{$proc_mem_table . "." . $tindex[$i]}; my $tmpmem=$result_cons{$proc_mem_table . "." . $tindex[$i]};
my $tmpcpu=$result_cons{$proc_cpu_table . "." . $tindex[$i]}; my $tmpcpu=$result_cons{$proc_cpu_table . "." . $tindex[$i]};
# verb ("Process $tindex[$i] in state $state using $tmpmem, and $tmpcpu CPU"); # verb ("Process $tindex[$i] in state $state using $tmpmem, and $tmpcpu CPU");
$num_int_ok++ if (($state == 1) || ($state ==2)); $num_int_ok++ if (($state == 1) || ($state ==2));
} }
$session->close;
my $final_status=0; my $final_status=0;
my ($res_memory,$res_cpu)=(0,0); my ($res_memory,$res_cpu)=(0,0);
my $memory_print=""; my $memory_print="";
@ -458,102 +403,101 @@ my $mem_in_octet;
my $metrics = "|"; my $metrics = "|";
###### Checks memory usage ###### Checks memory usage
if (defined ($o_mem) ) { if (defined ($o_mem) ) {
if (defined ($o_mem_avg)) { if (defined ($o_mem_avg)) {
for (my $i=0; $i< $num_int; $i++) { $res_memory += $result_cons{$proc_mem_table . "." . $tindex[$i]};} for (my $i=0; $i< $num_int; $i++) { $res_memory += $result_cons{$proc_mem_table . "." . $tindex[$i]};}
$res_memory /= ($num_int_ok*1024); $res_memory /= ($num_int_ok*1024);
#verb("Memory average : $res_memory"); #verb("Memory average : $res_memory");
} else { } else {
for (my $i=0; $i< $num_int; $i++) { for (my $i=0; $i< $num_int; $i++) {
$res_memory = ($result_cons{$proc_mem_table . "." . $tindex[$i]} > $res_memory) ? $result_cons{$proc_mem_table . "." . $tindex[$i]} : $res_memory; $res_memory = ($result_cons{$proc_mem_table . "." . $tindex[$i]} > $res_memory) ? $result_cons{$proc_mem_table . "." . $tindex[$i]} : $res_memory;
} }
$res_memory /=1024; $res_memory /=1024;
#verb("Memory max : $res_memory"); #verb("Memory max : $res_memory");
} }
if ($res_memory > $o_memL[1]) { if ($res_memory > $o_memL[1]) {
$final_status=2; $final_status=2;
$memory_print=", Mem : ".sprintf("%.1f",$res_memory)."Mb > ".$o_memL[1]." CRITICAL"; $memory_print=", Mem : ".sprintf("%.1f",$res_memory)."Mb > ".$o_memL[1]." CRITICAL";
$res_memory = $res_memory * 1024 * 1024; $res_memory = $res_memory * 1024 * 1024;
$metrics .= " Ramused=".$res_memory."o"; $metrics .= " Ramused=".$res_memory."o";
} elsif ( $res_memory > $o_memL[0]) { } elsif ( $res_memory > $o_memL[0]) {
$final_status=1; $final_status=1;
$memory_print=", Mem : ".sprintf("%.1f",$res_memory)."Mb > ".$o_memL[0]." WARNING"; $memory_print=", Mem : ".sprintf("%.1f",$res_memory)."Mb > ".$o_memL[0]." WARNING";
$res_memory = $res_memory * 1024 * 1024; $res_memory = $res_memory * 1024 * 1024;
$metrics .= " Ramused=".$res_memory."o"; $metrics .= " Ramused=".$res_memory."o";
} else { } else {
$memory_print=", Mem : ".sprintf("%.1f",$res_memory)."Mb OK"; $memory_print=", Mem : ".sprintf("%.1f",$res_memory)."Mb OK";
$res_memory = $res_memory * 1024 * 1024; $res_memory = $res_memory * 1024 * 1024;
$metrics .= " Ramused=".$res_memory."o"; $metrics .= " Ramused=".$res_memory."o";
} }
} }
######## Checks CPU usage ######## Checks CPU usage
if (defined ($o_cpu) ) { if (defined ($o_cpu) ) {
my $timenow=time; my $timenow=time;
my $temp_file_name; my $temp_file_name;
my ($return,@file_values)=(undef,undef); my ($return,@file_values)=(undef,undef);
my $n_rows=0; my $n_rows=0;
my $n_items_check=2; my $n_items_check=2;
my $trigger=$timenow - ($o_delta - ($o_delta/10)); my $trigger=$timenow - ($o_delta - ($o_delta/10));
my $trigger_low=$timenow - 3*$o_delta; my $trigger_low=$timenow - 3*$o_delta;
my ($old_value,$old_time)=undef; my ($old_value,$old_time)=undef;
my $found_value=undef; my $found_value=undef;
#### Get the current values #### Get the current values
for (my $i=0; $i< $num_int; $i++) { $res_cpu += $result_cons{$proc_cpu_table . "." . $tindex[$i]};} for (my $i=0; $i< $num_int; $i++) { $res_cpu += $result_cons{$proc_cpu_table . "." . $tindex[$i]};}
# verb("Time: $timenow , cpu (centiseconds) : $res_cpu"); # verb("Time: $timenow , cpu (centiseconds) : $res_cpu");
#### Read file #### Read file
$temp_file_name=$o_descr; $temp_file_name=$o_descr;
$temp_file_name =~ s/ /_/g; $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 # First, read entire file
my @ret_array=read_file($temp_file_name,$n_items_check); my @ret_array=read_file($temp_file_name,$n_items_check);
$return = shift(@ret_array); $return = shift(@ret_array);
$n_rows = shift(@ret_array); $n_rows = shift(@ret_array);
if ($n_rows != 0) { @file_values = @ret_array }; if ($n_rows != 0) { @file_values = @ret_array };
# verb ("File read returns : $return with $n_rows rows"); # verb ("File read returns : $return with $n_rows rows");
#make the checks if the file is OK #make the checks if the file is OK
if ($return ==0) { if ($return ==0) {
my $j=$n_rows-1; my $j=$n_rows-1;
do { do {
if ($file_values[$j][0] < $trigger) { if ($file_values[$j][0] < $trigger) {
if ($file_values[$j][0] > $trigger_low) { if ($file_values[$j][0] > $trigger_low) {
# found value = centiseconds / seconds = %cpu # found value = centiseconds / seconds = %cpu
$found_value= ($res_cpu-$file_values[$j][1]) / ($timenow - $file_values[$j][0] ); $found_value= ($res_cpu-$file_values[$j][1]) / ($timenow - $file_values[$j][0] );
}
}
$j--;
} while ( ($j>=0) && (!defined($found_value)) );
}
###### Write file
$file_values[$n_rows][0]=$timenow;
$file_values[$n_rows][1]=$res_cpu;
$n_rows++;
$return=write_file($temp_file_name,$n_rows,$n_items_check,@file_values);
if ($return != 0) { $cpu_print.="! ERROR writing file $temp_file_name !";$final_status=3;}
##### Check values (if something to check...)
if (defined($found_value)) {
if ($found_value > $o_cpuL[1]) {
$final_status=2;
$cpu_print.=", Cpu : ".sprintf("%.0f",$found_value)."% > ".$o_cpuL[1]." CRITICAL";
$metrics .= " CpuUsed=$found_value";
} elsif ( $found_value > $o_cpuL[0]) {
$final_status=($final_status==2)?2:1;
$cpu_print.=", Cpu : ".sprintf("%.0f",$found_value)."% > ".$o_cpuL[0]." WARNING";
$metrics .= " CpuUsed=$found_value";
} else {
$cpu_print.=", Cpu : ".sprintf("%.0f",$found_value)."% OK";
$metrics .= " CpuUsed=$found_value";
} }
}
$j--;
} while ( ($j>=0) && (!defined($found_value)) );
}
###### Write file
$file_values[$n_rows][0]=$timenow;
$file_values[$n_rows][1]=$res_cpu;
$n_rows++;
$return=write_file($temp_file_name,$n_rows,$n_items_check,@file_values);
if ($return != 0) { $cpu_print.="! ERROR writing file $temp_file_name !";$final_status=3;}
##### Check values (if something to check...)
if (defined($found_value)) {
if ($found_value > $o_cpuL[1]) {
$final_status=2;
$cpu_print.=", Cpu : ".sprintf("%.0f",$found_value)."% > ".$o_cpuL[1]." CRITICAL";
$metrics .= " CpuUsed=$found_value";
} elsif ( $found_value > $o_cpuL[0]) {
$final_status=($final_status==2)?2:1;
$cpu_print.=", Cpu : ".sprintf("%.0f",$found_value)."% > ".$o_cpuL[0]." WARNING";
$metrics .= " CpuUsed=$found_value";
} else { } else {
$cpu_print.=", Cpu : ".sprintf("%.0f",$found_value)."% OK"; if ($final_status==0) { $final_status=3 };
$metrics .= " CpuUsed=$found_value"; $cpu_print.=", No data for CPU (".$n_rows." line(s)):UNKNOWN";
} }
} else {
if ($final_status==0) { $final_status=3 };
$cpu_print.=", No data for CPU (".$n_rows." line(s)):UNKNOWN";
}
} }
@ -561,28 +505,28 @@ print $num_int_ok, " process ", (defined ($o_noreg)) ? "named " : "matching ", $
#### Check for min and max number of process #### Check for min and max number of process
if ( $num_int_ok <= $o_critL[0] ) { if ( $num_int_ok <= $o_critL[0] ) {
print "(<= ",$o_critL[0]," : CRITICAL)"; print "(<= ",$o_critL[0]," : CRITICAL)";
$metrics .= " nbProcess=$num_int_ok"; $metrics .= " nbProcess=$num_int_ok";
$final_status=2; $final_status=2;
} elsif ( $num_int_ok <= $o_warnL[0] ) { } elsif ( $num_int_ok <= $o_warnL[0] ) {
print "(<= ",$o_warnL[0]," : WARNING)"; print "(<= ",$o_warnL[0]," : WARNING)";
$metrics .= " nbProcess=$num_int_ok"; $metrics .= " nbProcess=$num_int_ok";
$final_status=($final_status==2)?2:1; $final_status=($final_status==2)?2:1;
} else { } else {
$metrics .= " nbProcess=$num_int_ok"; $metrics .= " nbProcess=$num_int_ok";
print "(> ",$o_warnL[0],")"; print "(> ",$o_warnL[0],")";
} }
if (defined($o_critL[1]) && ($num_int_ok > $o_critL[1])) { if (defined($o_critL[1]) && ($num_int_ok > $o_critL[1])) {
print " (> ",$o_critL[1]," : CRITICAL)"; print " (> ",$o_critL[1]," : CRITICAL)";
$metrics .= " nbProcess=$num_int_ok"; $metrics .= " nbProcess=$num_int_ok";
$final_status=2; $final_status=2;
} elsif (defined($o_warnL[1]) && ($num_int_ok > $o_warnL[1])) { } elsif (defined($o_warnL[1]) && ($num_int_ok > $o_warnL[1])) {
print " (> ",$o_warnL[1]," : WARNING)"; print " (> ",$o_warnL[1]," : WARNING)";
$metrics .= " nbProcess=$num_int_ok"; $metrics .= " nbProcess=$num_int_ok";
$final_status=($final_status==2)?2:1; $final_status=($final_status==2)?2:1;
} elsif (defined($o_warnL[1])) { } elsif (defined($o_warnL[1])) {
$metrics .= " nbProcess=$num_int_ok"; $metrics .= " nbProcess=$num_int_ok";
print " (<= ",$o_warnL[1],"):OK"; print " (<= ",$o_warnL[1],"):OK";
} }
print $memory_print,$cpu_print,$metrics,"\n"; print $memory_print,$cpu_print,$metrics,"\n";
@ -591,4 +535,3 @@ if ($final_status==2) { exit $ERRORS{"CRITICAL"};}
if ($final_status==1) { exit $ERRORS{"WARNING"};} if ($final_status==1) { exit $ERRORS{"WARNING"};}
if ($final_status==3) { exit $ERRORS{"UNKNOWN"};} if ($final_status==3) { exit $ERRORS{"UNKNOWN"};}
exit $ERRORS{"OK"}; exit $ERRORS{"OK"};