check_snmp_process.pl : update with new version from http://nagios.manubulon.com/snmp_process.html

git-svn-id: http://svn.centreon.com/trunk/plugins-2.x@8361 6bcd3966-0018-0410-8128-fd23d134de7e
This commit is contained in:
Nicolas Cordier 2009-06-09 15:56:16 +00:00
parent 166e563f55
commit dfc42daf2c
1 changed files with 311 additions and 196 deletions

View File

@ -1,62 +1,48 @@
#!/usr/bin/perl -w #!/usr/bin/perl -w
############################## check_snmp_process ############## ############################## check_snmp_process ##############
# Version : 1.2.1 my $Version='1.10';
# Date : Dec 12 2004 # Date : Oct 12 2007
# Author : Patrick Proy (patrick at proy.org) # Author : Patrick Proy (patrick at proy dot org)
# Help : http://www.manubulon.com/nagios/ # Help : http://nagios.manubulon.com
# Licence : GPL - http://www.fsf.org/licenses/gpl.txt # Licence : GPL - http://www.fsf.org/licenses/gpl.txt
# Contrib : Makina Corpus, adam At greekattic d0t com
# TODO : put $o_delta as an option # TODO : put $o_delta as an option
# If testing on localhost, selects itself....
############################################################### ###############################################################
# #
# help : ./check_snmp_process -h # help : ./check_snmp_process -h
use strict;
use Net::SNMP;
use Getopt::Long;
############### BASE DIRECTORY FOR TEMP FILE ######## ############### BASE DIRECTORY FOR TEMP FILE ########
my $o_base_dir="/tmp/tmp_Nagios_proc."; my $o_base_dir="/tmp/tmp_Nagios_proc.";
my $file_history=200; # number of data to keep in files. 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 Net::SNMP;
use Getopt::Long;
# Nagios specific # Nagios specific
use lib "@NAGIOS_PLUGINS@"; my $TIMEOUT = 15;
use utils qw(%ERRORS $TIMEOUT); my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
#my $TIMEOUT = 5;
#my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
# centreon specific
#use lib "@NAGIOS_PLUGINS@";
if (eval "require centreon" ) {
use centreon qw(get_parameters create_rrd update_rrd &is_valid_serviceid);
use vars qw($VERSION %centreon);
%centreon=get_parameters();
} else {
print "Unable to load centreon perl module\n";
exit $ERRORS{'UNKNOWN'};
}
my $pathtorrdbase = $centreon{GLOBAL}{DIR_RRDTOOL};
# 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';
my $index_table = '1.3.6.1.2.1.25.4.2.1.1'; my $index_table = '1.3.6.1.2.1.25.4.2.1.1';
my $run_name_table = '1.3.6.1.2.1.25.4.2.1.2'; my $run_name_table = '1.3.6.1.2.1.25.4.2.1.2';
my $run_path_table = '1.3.6.1.2.1.25.4.2.1.4'; my $run_path_table = '1.3.6.1.2.1.25.4.2.1.4';
my $run_param_table = '1.3.6.1.2.1.25.4.2.1.5';
my $proc_mem_table = '1.3.6.1.2.1.25.5.1.1.2'; # Kbytes my $proc_mem_table = '1.3.6.1.2.1.25.5.1.1.2'; # Kbytes
my $proc_cpu_table = '1.3.6.1.2.1.25.5.1.1.1'; # Centi sec of CPU my $proc_cpu_table = '1.3.6.1.2.1.25.5.1.1.1'; # Centi sec of CPU
my $proc_run_state = '1.3.6.1.2.1.25.4.2.1.7'; my $proc_run_state = '1.3.6.1.2.1.25.4.2.1.7';
# Globals # Globals
my $Version='1.2.1';
my $o_host = undef; # hostname my $o_host = undef; # hostname
my $o_community =undef; # community my $o_community =undef; # community
my $o_port = 161; # port my $o_port = 161; # port
my $o_version2 = undef; #use snmp v2c
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)
@ -68,10 +54,19 @@ my $o_version= undef; # print version
my $o_noreg= undef; # Do not use Regexp for name 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_get_all= undef; # get all tables at once
my $o_param= undef; # Add process parameters for selection
my $o_perf= undef; # Add performance output
my $o_timeout= 5; # Default 5s Timeout my $o_timeout= 5; # Default 5s Timeout
# SNMP V3 specific # SNMP V3 specific
my $o_login= undef; # snmp v3 login my $o_login= undef; # snmp v3 login
my $o_passwd= undef; # snmp v3 passwd my $o_passwd= undef; # snmp v3 passwd
my $v3protocols=undef; # V3 protocol list.
my $o_authproto='md5'; # Auth protocol
my $o_privproto='des'; # Priv protocol
my $o_privpass= undef; # priv password
# SNMP Message size parameter (Makina Corpus contrib)
my $o_octetlength=undef;
# 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
@ -79,27 +74,18 @@ my $o_mem_avg= undef; # cheks memory average
my $o_cpu= undef; # checks CPU usage 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
my $o_step= undef;
my $o_g= undef;
my $o_S= undef;
my $step= undef;
my $rrd= undef;
my $start= undef;
my $ServiceId= undef;
my @rrd_data= undef;
# functions # functions
sub p_version { print "check_snmp_process version : $Version\n"; } sub p_version { print "check_snmp_process version : $Version\n"; }
sub print_usage { sub print_usage {
print "Usage: $0 [-v] -H <host> -C <snmp_community> | (-l login -x passwd) [-p <port>] -n <name> [-w <min_proc>[,<max_proc>] -c <min_proc>[,max_proc] ] [-m<warn Mb>,<crit Mb> -a -u<warn %>,<crit%> ] [-t <timeout>] [-f ] [-r] [-V]\n"; print "Usage: $0 [-v] -H <host> -C <snmp_community> [-2] | (-l login -x passwd) [-p <port>] -n <name> [-w <min_proc>[,<max_proc>] -c <min_proc>[,max_proc] ] [-m<warn Mb>,<crit Mb> -a -u<warn %>,<crit%> -d<delta> ] [-t <timeout>] [-o <octet_length>] [-f -A -F ] [-r] [-V] [-g]\n";
} }
sub isnotnum { # Return true if arg is not a number sub isnotnum { # Return true if arg is not a number
my $num = shift; my $num = shift;
if ( $num =~ /^(\d+\.?\d*)|(^\.\d+)$/ ) { return 0 ;} if ( $num =~ /^-?(\d+\.?\d*)|(^\.\d+)$/ ) { return 0 ;}
return 1; return 1;
} }
@ -158,7 +144,7 @@ sub write_file {
sub help { sub help {
print "\nSNMP Process Monitor for Nagios version ",$Version,"\n"; print "\nSNMP Process Monitor for Nagios version ",$Version,"\n";
print "(c)2004 to my cat Ratoune - Author: Patrick Proy\n\n"; print "GPL licence, (c)2004-2006 Patrick Proy\n\n";
print_usage(); print_usage();
print <<EOT; print <<EOT;
-v, --verbose -v, --verbose
@ -168,11 +154,16 @@ sub help {
-H, --hostname=HOST -H, --hostname=HOST
name or IP address of host to check name or IP address of host to check
-C, --community=COMMUNITY NAME -C, --community=COMMUNITY NAME
community name for the host's SNMP agent (implies SNMP v1) community name for the host's SNMP agent (implies SNMP v1 or v2c with option)
-l, --login=LOGIN -l, --login=LOGIN ; -x, --passwd=PASSWD, -2, --v2c
Login for snmpv3 authentication (implies v3 protocol with MD5) Login and auth password for snmpv3 authentication
-x, --passwd=PASSWD If no priv password exists, implies AuthNoPriv
Password for snmpv3 authentication -2 : use snmp v2c
-X, --privpass=PASSWD
Priv password for snmpv3 (AuthPriv protocol)
-L, --protocols=<authproto>,<privproto>
<authproto> : Authentication protocol (md5|sha : default md5)
<privproto> : Priv protocole (des|aes : default des)
-p, --port=PORT -p, --port=PORT
SNMP port (Default 161) SNMP port (Default 161)
-n, --name=NAME -n, --name=NAME
@ -183,10 +174,18 @@ sub help {
-f, --fullpath -f, --fullpath
Use full path name instead of process name Use full path name instead of process name
(Windows doesn't provide full path name) (Windows doesn't provide full path name)
-A, --param
Add parameters to select processes.
ex : "named.*-t /var/named/chroot" will only select named process with this parameter
-F, --perfout
Add performance output
outputs : memory_usage, num_process, cpu_usage
-w, --warn=MIN[,MAX] -w, --warn=MIN[,MAX]
Number of process that will cause a warning Number of process that will cause a warning
-1 for no warning, MAX must be >0. Ex : -w-1,50
-c, --critical=MIN[,MAX] -c, --critical=MIN[,MAX]
number of process that will cause an error number of process that will cause an error (
-1 for no critical, MAX must be >0. Ex : -c-1,50
Notes on warning and critical : Notes on warning and critical :
with the following options : -w m1,x1 -c m2,x2 with the following options : -w m1,x1 -c m2,x2
you must have : m2 <= m1 < x1 <= x2 you must have : m2 <= m1 < x1 <= x2
@ -200,14 +199,20 @@ Notes on warning and critical :
checks cpu usage of all process checks cpu usage of all process
values are warning and critical values in % of CPU usage values are warning and critical values in % of CPU usage
if more than one CPU, value can be > 100% : 100%=1 CPU if more than one CPU, value can be > 100% : 100%=1 CPU
-d, --delta=seconds
make an average of <delta> seconds for CPU (default 300=5min)
-g, --getall
In some cases, it is necessary to get all data at once because
process die very frequently.
This option eats bandwidth an cpu (for remote host) at breakfast.
-o, --octetlength=INTEGER
max-size of the SNMP message, usefull in case of Too Long responses.
Be carefull with network filters. Range 484 - 65535, default are
usually 1472,1452,1460 or 1440.
-t, --timeout=INTEGER -t, --timeout=INTEGER
timeout for SNMP in seconds (Default: 5) timeout for SNMP in seconds (Default: 5)
-V, --version -V, --version
prints version number prints version number
-g (--rrdgraph) Create a rrd base if necessary and add datas into this one
--rrd_step Specifies the base interval in seconds with which data will be fed into the RRD (300 by default)
-S (--ServiceId) Oreon Service Id
Note : Note :
CPU usage is in % of one cpu, so maximum can be 100% * number of CPU CPU usage is in % of one cpu, so maximum can be 100% * number of CPU
example : example :
@ -222,7 +227,6 @@ EOT
sub verb { my $t=shift; print $t,"\n" if defined($o_verb) ; } sub verb { my $t=shift; print $t,"\n" if defined($o_verb) ; }
sub check_options { sub check_options {
my $compat_o_cpu_sum;
Getopt::Long::Configure ("bundling"); Getopt::Long::Configure ("bundling");
GetOptions( GetOptions(
'v' => \$o_verb, 'verbose' => \$o_verb, 'v' => \$o_verb, 'verbose' => \$o_verb,
@ -232,6 +236,8 @@ sub check_options {
'C:s' => \$o_community, 'community:s' => \$o_community, 'C:s' => \$o_community, 'community:s' => \$o_community,
'l:s' => \$o_login, 'login:s' => \$o_login, 'l:s' => \$o_login, 'login:s' => \$o_login,
'x:s' => \$o_passwd, 'passwd:s' => \$o_passwd, 'x:s' => \$o_passwd, 'passwd:s' => \$o_passwd,
'X:s' => \$o_privpass, 'privpass:s' => \$o_privpass,
'L:s' => \$v3protocols, 'protocols:s' => \$v3protocols,
'c:s' => \$o_crit, 'critical:s' => \$o_crit, 'c:s' => \$o_crit, 'critical:s' => \$o_crit,
'w:s' => \$o_warn, 'warn:s' => \$o_warn, 'w:s' => \$o_warn, 'warn:s' => \$o_warn,
't:i' => \$o_timeout, 'timeout:i' => \$o_timeout, 't:i' => \$o_timeout, 'timeout:i' => \$o_timeout,
@ -241,34 +247,44 @@ sub check_options {
'm:s' => \$o_mem, 'memory:s' => \$o_mem, 'm:s' => \$o_mem, 'memory:s' => \$o_mem,
'a' => \$o_mem_avg, 'average' => \$o_mem_avg, 'a' => \$o_mem_avg, 'average' => \$o_mem_avg,
'u:s' => \$o_cpu, 'cpu' => \$o_cpu, 'u:s' => \$o_cpu, 'cpu' => \$o_cpu,
#### To be compatible with version 1.2, will be removed... #### '2' => \$o_version2, 'v2c' => \$o_version2,
's' => \$compat_o_cpu_sum, 'cpusum' => \$compat_o_cpu_sum, 'o:i' => \$o_octetlength, 'octetlength:i' => \$o_octetlength,
########## 'g' => \$o_get_all, 'getall' => \$o_get_all,
'V' => \$o_version, 'version' => \$o_version, 'A' => \$o_param, 'param' => \$o_param,
# For Oreon rrdtool graph 'F' => \$o_perf, 'perfout' => \$o_perf,
"rrd_step:s" => \$o_step, 'd:i' => \$o_delta, 'delta:i' => \$o_delta,
"g" => \$o_g, "rrdgraph" => \$o_g, 'V' => \$o_version, 'version' => \$o_version
"S=s" => \$o_S, "ServiceId=s" => \$o_S
); );
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 # check snmp information
if ( !defined($o_community) && (!defined($o_login) || !defined($o_passwd)) ) if ( !defined($o_community) && (!defined($o_login) || !defined($o_passwd)) )
{ print "Put snmp login info!\n"; print_usage(); exit $ERRORS{"UNKNOWN"}} { print "Put snmp login info!\n"; print_usage(); exit $ERRORS{"UNKNOWN"}}
if ((defined($o_login) || defined($o_passwd)) && (defined($o_community) || defined($o_version2)) )
{ print "Can't mix snmp v1,2c,3 protocols!\n"; print_usage(); exit $ERRORS{"UNKNOWN"}}
if (defined ($v3protocols)) {
if (!defined($o_login)) { print "Put snmp V3 login info with protocols!\n"; print_usage(); exit $ERRORS{"UNKNOWN"}}
my @v3proto=split(/,/,$v3protocols);
if ((defined ($v3proto[0])) && ($v3proto[0] ne "")) {$o_authproto=$v3proto[0]; } # Auth protocol
if (defined ($v3proto[1])) {$o_privproto=$v3proto[1]; } # Priv protocol
if ((defined ($v3proto[1])) && (!defined($o_privpass))) {
print "Put snmp V3 priv login info with priv protocols!\n"; print_usage(); exit $ERRORS{"UNKNOWN"}}
}
if (defined($o_timeout) && (isnotnum($o_timeout) || ($o_timeout < 2) || ($o_timeout > 60)))
{ print "Timeout must be >1 and <60 !\n"; print_usage(); exit $ERRORS{"UNKNOWN"}}
if (!defined($o_timeout)) {$o_timeout=5;}
# Check compulsory attributes # Check compulsory attributes
if ( ! defined($o_descr) || ! defined($o_host) ) { print_usage(); exit $ERRORS{"UNKNOWN"}}; if ( ! defined($o_descr) || ! defined($o_host) ) { 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_warn $o_crit $#o_warnL $#o_critL");
if ( isnotnum($o_warnL[0]) || isnotnum($o_critL[0])) if ( isnotnum($o_warnL[0]) || isnotnum($o_critL[0]))
{ print "Numerical values for warning and critical\n";print_usage(); exit $ERRORS{"UNKNOWN"};} { print "Numerical values for warning and critical\n";print_usage(); exit $ERRORS{"UNKNOWN"};}
if ((defined($o_warnL[1]) && isnotnum($o_warnL[1])) || (defined($o_critL[1]) && isnotnum($o_critL[1]))) if ((defined($o_warnL[1]) && isnotnum($o_warnL[1])) || (defined($o_critL[1]) && isnotnum($o_critL[1])))
{ print "Numerical values for warning and critical\n";print_usage(); exit $ERRORS{"UNKNOWN"};} { print "Numerical values for warning and critical\n";print_usage(); exit $ERRORS{"UNKNOWN"};}
# Check for positive numbers # Check for positive numbers on maximum number of processes
if (($o_warnL[0] < 0) || ($o_critL[0] < 0))
{ print " warn and critical > 0 \n";print_usage(); exit $ERRORS{"UNKNOWN"}};
if ((defined($o_warnL[1]) && ($o_warnL[1] < 0)) || (defined($o_critL[1]) && ($o_critL[1] < 0))) if ((defined($o_warnL[1]) && ($o_warnL[1] < 0)) || (defined($o_critL[1]) && ($o_critL[1] < 0)))
{ print " warn and critical > 0 \n";print_usage(); exit $ERRORS{"UNKNOWN"}}; { print " Maximum process warn and critical > 0 \n";print_usage(); exit $ERRORS{"UNKNOWN"}};
# Check min_crit < min warn < max warn < crit warn # Check min_crit < min warn < max warn < crit warn
if ($o_warnL[0] < $o_critL[0]) { print " warn minimum must be >= crit minimum\n";print_usage(); exit $ERRORS{"UNKNOWN"}}; if ($o_warnL[0] < $o_critL[0]) { print " warn minimum must be >= crit minimum\n";print_usage(); exit $ERRORS{"UNKNOWN"}};
if (defined($o_warnL[1])) { if (defined($o_warnL[1])) {
@ -298,23 +314,16 @@ sub check_options {
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 ####### #### octet length checks
if (defined ($o_octetlength) && (isnotnum($o_octetlength) || $o_octetlength > 65535 || $o_octetlength < 484 )) {
if (!defined($o_S)) { $o_S="1_1" } print "octet lenght must be < 65535 and > 484\n";print_usage(); exit $ERRORS{"UNKNOWN"};
$ServiceId = is_valid_serviceid($o_S); }
if (!defined($o_step)) { $o_step="300" }
$step = $1 if ($o_step =~ /(\d+)/);
} }
########## MAIN ####### ########## MAIN #######
check_options(); check_options();
$rrd = $pathtorrdbase.$ServiceId.".rrd";
$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");
@ -328,14 +337,39 @@ if (defined($TIMEOUT)) {
my ($session,$error); my ($session,$error);
if ( defined($o_login) && defined($o_passwd)) { if ( defined($o_login) && defined($o_passwd)) {
# SNMPv3 login # SNMPv3 login
verb("SNMPv3 login"); if (!defined ($o_privpass)) {
verb("SNMPv3 AuthNoPriv login : $o_login, $o_authproto");
($session, $error) = Net::SNMP->session(
-hostname => $o_host,
-version => '3',
-port => $o_port,
-username => $o_login,
-authpassword => $o_passwd,
-authprotocol => $o_authproto,
-timeout => $o_timeout
);
} else {
verb("SNMPv3 AuthPriv login : $o_login, $o_authproto, $o_privproto");
($session, $error) = Net::SNMP->session( ($session, $error) = Net::SNMP->session(
-hostname => $o_host, -hostname => $o_host,
-version => '3', -version => '3',
-username => $o_login, -username => $o_login,
-port => $o_port,
-authpassword => $o_passwd, -authpassword => $o_passwd,
-authprotocol => 'md5', -authprotocol => $o_authproto,
-privpassword => $o_passwd, -privpassword => $o_privpass,
-privprotocol => $o_privproto,
-timeout => $o_timeout
);
}
} else {
if (defined ($o_version2)) {
# SNMPv2 Login
($session, $error) = Net::SNMP->session(
-hostname => $o_host,
-version => 2,
-community => $o_community,
-port => $o_port,
-timeout => $o_timeout -timeout => $o_timeout
); );
} else { } else {
@ -347,22 +381,39 @@ if ( defined($o_login) && defined($o_passwd)) {
-timeout => $o_timeout -timeout => $o_timeout
); );
} }
}
if (!defined($session)) { if (!defined($session)) {
printf("ERROR: %s.\n", $error); printf("ERROR: %s.\n", $error);
exit $ERRORS{"UNKNOWN"}; exit $ERRORS{"UNKNOWN"};
} }
if (defined($o_octetlength)) {
my $oct_resultat=undef;
my $oct_test= $session->max_msg_size();
verb(" actual max octets:: $oct_test");
$oct_resultat = $session->max_msg_size($o_octetlength);
if (!defined($oct_resultat)) {
printf("ERROR: Session settings : %s.\n", $session->error);
$session->close;
exit $ERRORS{"UNKNOWN"};
}
$oct_test= $session->max_msg_size();
verb(" new max octets:: $oct_test");
}
# Look for process in name or path name table # Look for process in name or path name table
my $resultat=undef; my $resultat=undef;
my %result_cons=();
my ($getall_run,$getall_cpu,$getall_mem)=(undef,undef,undef);
if ( !defined ($o_path) ) { if ( !defined ($o_path) ) {
$resultat = $session->get_table( $resultat = (Net::SNMP->VERSION < 4) ?
Baseoid => $run_name_table $session->get_table($run_name_table)
); : $session->get_table(Baseoid => $run_name_table);
} else { } else {
$resultat = $session->get_table( $resultat = (Net::SNMP->VERSION < 4) ?
Baseoid => $run_path_table $session->get_table($run_path_table)
); :$session->get_table(Baseoid => $run_path_table);
} }
if (!defined($resultat)) { if (!defined($resultat)) {
@ -371,6 +422,55 @@ if (!defined($resultat)) {
exit $ERRORS{"UNKNOWN"}; exit $ERRORS{"UNKNOWN"};
} }
my $resultat_param=undef;
if (defined($o_param)) { # Get parameter table too
$resultat_param = (Net::SNMP->VERSION < 4) ?
$session->get_table($run_param_table)
:$session->get_table(Baseoid => $run_param_table);
if (!defined($resultat_param)) {
printf("ERROR: Process param table : %s.\n", $session->error);
$session->close;
exit $ERRORS{"UNKNOWN"};
}
}
if (defined ($o_get_all)) {
$getall_run = (Net::SNMP->VERSION < 4) ?
$session->get_table($proc_run_state )
:$session->get_table(Baseoid => $proc_run_state );
if (!defined($getall_run)) {
printf("ERROR: Process run table : %s.\n", $session->error);
$session->close;
exit $ERRORS{"UNKNOWN"};
}
foreach my $key ( keys %$getall_run) {
$result_cons{$key}=$$getall_run{$key};
}
$getall_cpu = (Net::SNMP->VERSION < 4) ?
$session->get_table($proc_cpu_table)
: $session->get_table(Baseoid => $proc_cpu_table);
if (!defined($getall_cpu)) {
printf("ERROR: Process cpu table : %s.\n", $session->error);
$session->close;
exit $ERRORS{"UNKNOWN"};
}
foreach my $key ( keys %$getall_cpu) {
$result_cons{$key}=$$getall_cpu{$key};
}
$getall_mem = (Net::SNMP->VERSION < 4) ?
$session->get_table($proc_mem_table)
: $session->get_table(Baseoid => $proc_mem_table);
if (!defined($getall_mem)) {
printf("ERROR: Process memory table : %s.\n", $session->error);
$session->close;
exit $ERRORS{"UNKNOWN"};
}
foreach my $key ( keys %$getall_mem) {
$result_cons{$key}=$$getall_mem{$key};
}
}
my @tindex = undef; my @tindex = undef;
my @oids = undef; my @oids = undef;
my @descr = undef; my @descr = undef;
@ -382,8 +482,14 @@ 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}");
# test by regexp or exact match # test by regexp or exact match
# First add param if necessary
if (defined($o_param)){
my $pid = (split /\./,$key)[-1];
$pid = $run_param_table .".".$pid;
$$resultat{$key} .= " " . $$resultat_param{$pid};
}
verb("OID : $key, Desc : $$resultat{$key}");
my $test = defined($o_noreg) my $test = defined($o_noreg)
? $$resultat{$key} eq $o_descr ? $$resultat{$key} eq $o_descr
: $$resultat{$key} =~ /$o_descr/; : $$resultat{$key} =~ /$o_descr/;
@ -404,14 +510,22 @@ foreach my $key ( keys %$resultat) {
} }
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 : ";
if ($o_critL[0]>=0) {
print "CRITICAL\n";
exit $ERRORS{"CRITICAL"}; exit $ERRORS{"CRITICAL"};
} elsif ($o_warnL[0]>=0) {
print "WARNING\n";
exit $ERRORS{"WARNING"};
}
print "YOU told me it was : OK\n";
exit $ERRORS{"OK"};
} }
my $result=undef; my $result=undef;
my $num_int_ok=0; my $num_int_ok=0;
my %result_cons=(); # Splitting snmp request because can't use get_bulk_request with v1 protocol
#$session->max_msg_size([214748]); if (!defined ($o_get_all)) {
if ( $count_oid >= 50) { if ( $count_oid >= 50) {
my @toid=undef; my @toid=undef;
my $tmp_num=0; my $tmp_num=0;
@ -425,9 +539,9 @@ if ( $count_oid >= 50) {
$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 = $session->get_request( $tmp_result = (Net::SNMP->VERSION < 4) ?
Varbindlist => \@toid $session->get_request(@toid)
); : $session->get_request(Varbindlist => \@toid);
if (!defined($tmp_result)) { printf("ERROR: running table : %s.\n", $session->error); $session->close; if (!defined($tmp_result)) { printf("ERROR: running table : %s.\n", $session->error); $session->close;
exit $ERRORS{"UNKNOWN"}; exit $ERRORS{"UNKNOWN"};
} }
@ -437,14 +551,17 @@ if ( $count_oid >= 50) {
} }
} else { } else {
$result = $session->get_request( $result = (Net::SNMP->VERSION < 4) ?
Varbindlist => \@oids $session->get_request(@oids)
); : $session->get_request(Varbindlist => \@oids);
if (!defined($result)) { printf("ERROR: running table : %s.\n", $session->error); $session->close; if (!defined($result)) { printf("ERROR: running table : %s.\n", $session->error); $session->close;
exit $ERRORS{"UNKNOWN"}; exit $ERRORS{"UNKNOWN"};
} }
foreach (@oids) {$result_cons{$_}=$$result{$_};} foreach (@oids) {$result_cons{$_}=$$result{$_};}
} }
}
$session->close;
#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++) {
@ -452,18 +569,18 @@ for (my $i=0; $i< $num_int; $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");
if (!isnotnum($state)) { # check argument is numeric (can be NoSuchInstance)
$num_int_ok++ if (($state == 1) || ($state ==2)); $num_int_ok++ if (($state == 1) || ($state ==2));
} }
}
$session->close;
$rrd_data[0] = $num_int_ok;
my $final_status=0; my $final_status=0;
my $perf_output;
my ($res_memory,$res_cpu)=(0,0); my ($res_memory,$res_cpu)=(0,0);
my $memory_print=""; my $memory_print="";
my $cpu_print=""; my $cpu_print="";
###### 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]};}
@ -485,9 +602,9 @@ if (defined ($o_mem) ) {
} else { } else {
$memory_print=", Mem : ".sprintf("%.1f",$res_memory)."Mb OK"; $memory_print=", Mem : ".sprintf("%.1f",$res_memory)."Mb OK";
} }
if (defined($o_perf)) {
push @rrd_data, $res_memory; $perf_output= "'memory_usage'=".sprintf("%.1f",$res_memory) ."MB;".$o_memL[0].";".$o_memL[1];
}
} }
######## Checks CPU usage ######## Checks CPU usage
@ -526,6 +643,10 @@ if (defined ($o_cpu) ) {
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] );
if ($found_value <0) { # in case of program restart
$j=0;$found_value=undef; # don't look for more values
$n_rows=0; # reset file
}
} }
} }
$j--; $j--;
@ -548,29 +669,16 @@ if (defined ($o_cpu) ) {
} else { } else {
$cpu_print.=", Cpu : ".sprintf("%.0f",$found_value)."% OK"; $cpu_print.=", Cpu : ".sprintf("%.0f",$found_value)."% OK";
} }
push @rrd_data, $found_value; if (defined($o_perf)) {
if (!defined($perf_output)) {$perf_output="";} else {$perf_output.=" ";}
$perf_output.= "'cpu_usage'=". sprintf("%.0f",$found_value)."%;".$o_cpuL[0].";".$o_cpuL[1];
}
} else { } else {
if ($final_status==0) { $final_status=3 }; if ($final_status==0) { $final_status=3 };
$cpu_print.=", No data for CPU (".$n_rows." line(s)):UNKNOWN"; $cpu_print.=", No data for CPU (".$n_rows." line(s)):UNKNOWN";
} }
} }
##
## RRD management
##
if ($o_g) {
$start=time;
if (! -e $rrd) {
create_rrd($rrd,$#rrd_data+1,$start,$step,0,"U","GAUGE");
}
update_rrd($rrd,$start,@rrd_data);
}
print $num_int_ok, " process ", (defined ($o_noreg)) ? "named " : "matching ", $o_descr, " "; print $num_int_ok, " process ", (defined ($o_noreg)) ? "named " : "matching ", $o_descr, " ";
#### Check for min and max number of process #### Check for min and max number of process
@ -593,7 +701,14 @@ if (defined($o_critL[1]) && ($num_int_ok > $o_critL[1])) {
print " (<= ",$o_warnL[1],"):OK"; print " (<= ",$o_warnL[1],"):OK";
} }
print $memory_print,$cpu_print,"\n"; print $memory_print,$cpu_print;
if (defined($o_perf)) {
if (!defined($perf_output)) {$perf_output="";} else {$perf_output.=" ";}
$perf_output.= "'num_process'=". $num_int_ok.";".$o_warnL[0].";".$o_critL[0];
print " | ",$perf_output;
}
print "\n";
if ($final_status==2) { exit $ERRORS{"CRITICAL"};} if ($final_status==2) { exit $ERRORS{"CRITICAL"};}
if ($final_status==1) { exit $ERRORS{"WARNING"};} if ($final_status==1) { exit $ERRORS{"WARNING"};}