Compatibility SNMPv3

This commit is contained in:
Quentin Garnier 2013-01-14 17:23:56 +01:00
parent 7361eeae43
commit 20222757dc
1 changed files with 251 additions and 397 deletions

View File

@ -13,8 +13,9 @@ my $Version='1.10';
# help : ./check_snmp_process -h # help : ./check_snmp_process -h
use strict; use strict;
use Net::SNMP;
use Getopt::Long; use Getopt::Long;
require "@NAGIOS_PLUGINS@/Centreon/SNMP/Utils.pm";
############### 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.";
@ -26,6 +27,16 @@ my $delta_of_time_to_make_average=300; # 5minutes by default
my $TIMEOUT = 15; my $TIMEOUT = 15;
my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4); my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
my %OPTION = (
"host" => undef,
"snmp-community" => "public", "snmp-version" => 1, "snmp-port" => 161,
"snmp-auth-key" => undef, "snmp-auth-user" => undef, "snmp-auth-password" => undef, "snmp-auth-protocol" => "MD5",
"snmp-priv-key" => undef, "snmp-priv-password" => undef, "snmp-priv-protocol" => "DES",
"maxrepetitions" => undef,
"64-bits" => undef,
);
my $session_params;
# SNMP Datas # 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';
@ -37,12 +48,6 @@ 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 $o_host = undef; # hostname
my $o_community =undef; # community
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)
@ -58,15 +63,6 @@ my $o_get_all= undef; # get all tables at once
my $o_param= undef; # Add process parameters for selection my $o_param= undef; # Add process parameters for selection
my $o_perf= undef; # Add performance output my $o_perf= undef; # Add performance output
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
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
@ -96,50 +92,49 @@ $SIG{'ALRM'} = sub {
}; };
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++ ) {$last_values[$n_rows][$i]=$file_values[$i]; }
for ( $i=0 ; $i< $items_number ; $i++ ) {$last_values[$n_rows][$i]=$file_values[$i]; } $n_rows++;
$n_rows++; }
} }
} close FILE;
close FILE; if ($n_rows != 0) {
if ($n_rows != 0) { return (0,$n_rows,@last_values);
return (0,$n_rows,@last_values); } else {
} else { return (1,0,0);
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 {
@ -159,13 +154,6 @@ sub help {
Login and auth password for snmpv3 authentication Login and auth password for snmpv3 authentication
If no priv password exists, implies AuthNoPriv If no priv password exists, implies AuthNoPriv
-2 : use snmp v2c -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
SNMP port (Default 161)
-n, --name=NAME -n, --name=NAME
Name of the process (regexp) Name of the process (regexp)
No trailing slash ! No trailing slash !
@ -205,10 +193,6 @@ Notes on warning and critical :
In some cases, it is necessary to get all data at once because In some cases, it is necessary to get all data at once because
process die very frequently. process die very frequently.
This option eats bandwidth an cpu (for remote host) at breakfast. 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
@ -229,15 +213,22 @@ sub verb { my $t=shift; print $t,"\n" if defined($o_verb) ; }
sub check_options { sub check_options {
Getopt::Long::Configure ("bundling"); Getopt::Long::Configure ("bundling");
GetOptions( GetOptions(
"H|hostname|host=s" => \$OPTION{'host'},
"C|community=s" => \$OPTION{'snmp-community'},
"snmp|snmp-version=s" => \$OPTION{'snmp-version'},
"p|port|P|snmpport|snmp-port=i" => \$OPTION{'snmp-port'},
"l|login|username=s" => \$OPTION{'snmp-auth-user'},
"x|passwd|authpassword|password=s" => \$OPTION{'snmp-auth-password'},
"k|authkey=s" => \$OPTION{'snmp-auth-key'},
"authprotocol=s" => \$OPTION{'snmp-auth-protocol'},
"privpassword=s" => \$OPTION{'snmp-priv-password'},
"privkey=s" => \$OPTION{'snmp-priv-key'},
"privprotocol=s" => \$OPTION{'snmp-priv-protocol'},
"maxrepetitions=s" => \$OPTION{'maxrepetitions'},
"64-bits" => \$OPTION{'64-bits'},
'v' => \$o_verb, 'verbose' => \$o_verb, 'v' => \$o_verb, 'verbose' => \$o_verb,
'h' => \$o_help, 'help' => \$o_help, 'h' => \$o_help, 'help' => \$o_help,
'H:s' => \$o_host, 'hostname:s' => \$o_host,
'p:i' => \$o_port, 'port:i' => \$o_port,
'C:s' => \$o_community, 'community:s' => \$o_community,
'l:s' => \$o_login, 'login:s' => \$o_login,
'x:s' => \$o_passwd, 'passwd:s' => \$o_passwd,
'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,
@ -247,8 +238,6 @@ 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,
'2' => \$o_version2, 'v2c' => \$o_version2,
'o:i' => \$o_octetlength, 'octetlength:i' => \$o_octetlength,
'g' => \$o_get_all, 'getall' => \$o_get_all, 'g' => \$o_get_all, 'getall' => \$o_get_all,
'A' => \$o_param, 'param' => \$o_param, 'A' => \$o_param, 'param' => \$o_param,
'F' => \$o_perf, 'perfout' => \$o_perf, 'F' => \$o_perf, 'perfout' => \$o_perf,
@ -258,23 +247,12 @@ sub check_options {
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)) ) ($session_params) = Centreon::SNMP::Utils::check_snmp_options($ERRORS{'UNKNOWN'}, \%OPTION);
{ 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))) 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"}} { print "Timeout must be >1 and <60 !\n"; print_usage(); exit $ERRORS{"UNKNOWN"}}
if (!defined($o_timeout)) {$o_timeout=5;} 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) ) { 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_warn $o_crit $#o_warnL $#o_critL"); verb("$o_warn $o_crit $#o_warnL $#o_critL");
@ -314,10 +292,6 @@ 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"}};
} }
#### octet length checks
if (defined ($o_octetlength) && (isnotnum($o_octetlength) || $o_octetlength > 65535 || $o_octetlength < 484 )) {
print "octet lenght must be < 65535 and > 484\n";print_usage(); exit $ERRORS{"UNKNOWN"};
}
} }
########## MAIN ####### ########## MAIN #######
@ -326,149 +300,43 @@ check_options();
# 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
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(
-hostname => $o_host,
-version => '3',
-username => $o_login,
-port => $o_port,
-authpassword => $o_passwd,
-authprotocol => $o_authproto,
-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
);
} 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"};
}
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 %result_cons=();
my ($getall_run,$getall_cpu,$getall_mem)=(undef,undef,undef); my ($getall_run,$getall_cpu,$getall_mem)=(undef,undef,undef);
if ( !defined ($o_path) ) { if ( !defined ($o_path) ) {
$resultat = (Net::SNMP->VERSION < 4) ? $resultat = Centreon::SNMP::Utils::get_snmp_table($run_name_table, $session, $ERRORS{'UNKNOWN'}, \%OPTION);
$session->get_table($run_name_table)
: $session->get_table(Baseoid => $run_name_table);
} else { } else {
$resultat = (Net::SNMP->VERSION < 4) ? $resultat = Centreon::SNMP::Utils::get_snmp_table($run_path_table, $session, $ERRORS{'UNKNOWN'}, \%OPTION);
$session->get_table($run_path_table)
:$session->get_table(Baseoid => $run_path_table);
}
if (!defined($resultat)) {
printf("ERROR: Process name table : %s.\n", $session->error);
$session->close;
exit $ERRORS{"UNKNOWN"};
} }
my $resultat_param=undef; my $resultat_param=undef;
if (defined($o_param)) { # Get parameter table too if (defined($o_param)) { # Get parameter table too
$resultat_param = (Net::SNMP->VERSION < 4) ? $resultat_param = Centreon::SNMP::Utils::get_snmp_table($run_param_table, $session, $ERRORS{'UNKNOWN'}, \%OPTION);
$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)) { if (defined ($o_get_all)) {
$getall_run = (Net::SNMP->VERSION < 4) ? $getall_run = Centreon::SNMP::Utils::get_snmp_table($proc_run_state, $session, $ERRORS{'UNKNOWN'}, \%OPTION);
$session->get_table($proc_run_state ) foreach my $key ( keys %$getall_run) {
:$session->get_table(Baseoid => $proc_run_state ); $result_cons{$key}=$$getall_run{$key};
if (!defined($getall_run)) { }
printf("ERROR: Process run table : %s.\n", $session->error); $getall_cpu = Centreon::SNMP::Utils::get_snmp_table($proc_cpu_table, $session, $ERRORS{'UNKNOWN'}, \%OPTION);
$session->close; foreach my $key ( keys %$getall_cpu) {
exit $ERRORS{"UNKNOWN"}; $result_cons{$key}=$$getall_cpu{$key};
} }
foreach my $key ( keys %$getall_run) { $getall_mem = Centreon::SNMP::Utils::get_snmp_table($proc_mem_table, $session, $ERRORS{'UNKNOWN'}, \%OPTION);
$result_cons{$key}=$$getall_run{$key}; foreach my $key ( keys %$getall_mem) {
} $result_cons{$key}=$$getall_mem{$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;
@ -482,96 +350,83 @@ my $count_oid = 0;
verb("Filter : $o_descr"); verb("Filter : $o_descr");
foreach my $key ( keys %$resultat) { foreach my $key ( keys %$resultat) {
# test by regexp or exact match # test by regexp or exact match
# First add param if necessary # First add param if necessary
if (defined($o_param)){ if (defined($o_param)){
my $pid = (split /\./,$key)[-1]; my $pid = (split /\./,$key)[-1];
$pid = $run_param_table .".".$pid; $pid = $run_param_table .".".$pid;
$$resultat{$key} .= " " . $$resultat_param{$pid}; $$resultat{$key} .= " " . $$resultat_param{$pid};
} }
verb("OID : $key, Desc : $$resultat{$key}"); 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/;
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 : "; print "No process ",(defined ($o_noreg)) ? "named " : "matching ", $o_descr, " found : ";
if ($o_critL[0]>=0) { if ($o_critL[0]>=0) {
print "CRITICAL\n"; print "CRITICAL\n";
exit $ERRORS{"CRITICAL"}; exit $ERRORS{"CRITICAL"};
} elsif ($o_warnL[0]>=0) { } elsif ($o_warnL[0]>=0) {
print "WARNING\n"; print "WARNING\n";
exit $ERRORS{"WARNING"}; exit $ERRORS{"WARNING"};
} }
print "YOU told me it was : OK\n"; print "YOU told me it was : OK\n";
exit $ERRORS{"OK"}; exit $ERRORS{"OK"};
} }
my $result=undef; my $result=undef;
my $num_int_ok=0; my $num_int_ok=0;
# Splitting snmp request because can't use get_bulk_request with v1 protocol # Splitting snmp request because can't use get_bulk_request with v1 protocol
if (!defined ($o_get_all)) { 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;
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 = (Net::SNMP->VERSION < 4) ? $tmp_result = Centreon::SNMP::Utils::get_snmp_leef(\@toid, $session, $ERRORS{'UNKNOWN'});
$session->get_request(@toid) foreach (@toid) { $result_cons{$_}=$$tmp_result{$_}; }
: $session->get_request(Varbindlist => \@toid); $tmp_count-=$tmp_num;
if (!defined($tmp_result)) { printf("ERROR: running table : %s.\n", $session->error); $session->close; $tmp_index+=$tmp_num;
exit $ERRORS{"UNKNOWN"}; }
} } else {
foreach (@toid) { $result_cons{$_}=$$tmp_result{$_}; } my $tmp_result = Centreon::SNMP::Utils::get_snmp_leef(\@oids, $session, $ERRORS{'UNKNOWN'});
$tmp_count-=$tmp_num; foreach (@oids) {$result_cons{$_}=$$tmp_result{$_};}
$tmp_index+=$tmp_num;
}
} else {
$result = (Net::SNMP->VERSION < 4) ?
$session->get_request(@oids)
: $session->get_request(Varbindlist => \@oids);
if (!defined($result)) { printf("ERROR: running table : %s.\n", $session->error); $session->close;
exit $ERRORS{"UNKNOWN"};
} }
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++) {
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");
if (!isnotnum($state)) { # check argument is numeric (can be NoSuchInstance) 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));
} }
} }
my $final_status=0; my $final_status=0;
@ -582,136 +437,135 @@ 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]};}
$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";
} 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";
} else { } else {
$memory_print=", Mem : ".sprintf("%.1f",$res_memory)."Mb OK"; $memory_print=", Mem : ".sprintf("%.1f",$res_memory)."Mb OK";
} }
if (defined($o_perf)) { if (defined($o_perf)) {
$perf_output= "'memory_usage'=".sprintf("%.1f",$res_memory) ."MB;".$o_memL[0].";".$o_memL[1]; $perf_output= "'memory_usage'=".sprintf("%.1f",$res_memory) ."MB;".$o_memL[0].";".$o_memL[1];
} }
} }
######## 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
for (my $i=0; $i< $num_int; $i++) { $res_cpu += $result_cons{$proc_cpu_table . "." . $tindex[$i]};}
verb("Time: $timenow , cpu (centiseconds) : $res_cpu");
#### Read file #### Get the current values
$temp_file_name=$o_descr; for (my $i=0; $i< $num_int; $i++) { $res_cpu += $result_cons{$proc_cpu_table . "." . $tindex[$i]};}
$temp_file_name =~ s/ /_/g;
$temp_file_name = $o_base_dir . $o_host ."." . $temp_file_name; verb("Time: $timenow , cpu (centiseconds) : $res_cpu");
# First, read entire file
my @ret_array=read_file($temp_file_name,$n_items_check); #### Read file
$return = shift(@ret_array); $temp_file_name=$o_descr;
$n_rows = shift(@ret_array); $temp_file_name =~ s/ /_/g;
if ($n_rows != 0) { @file_values = @ret_array }; $temp_file_name = $o_base_dir . $OPTION{'host'} ."." . $temp_file_name;
verb ("File read returns : $return with $n_rows rows"); # First, read entire file
#make the checks if the file is OK my @ret_array=read_file($temp_file_name,$n_items_check);
if ($return ==0) { $return = shift(@ret_array);
my $j=$n_rows-1; $n_rows = shift(@ret_array);
do { if ($n_rows != 0) { @file_values = @ret_array };
if ($file_values[$j][0] < $trigger) { verb ("File read returns : $return with $n_rows rows");
if ($file_values[$j][0] > $trigger_low) { #make the checks if the file is OK
# found value = centiseconds / seconds = %cpu if ($return ==0) {
$found_value= ($res_cpu-$file_values[$j][1]) / ($timenow - $file_values[$j][0] ); my $j=$n_rows-1;
if ($found_value <0) { # in case of program restart do {
$j=0;$found_value=undef; # don't look for more values if ($file_values[$j][0] < $trigger) {
$n_rows=0; # reset file if ($file_values[$j][0] > $trigger_low) {
} # found value = centiseconds / seconds = %cpu
} $found_value= ($res_cpu-$file_values[$j][1]) / ($timenow - $file_values[$j][0] );
} if ($found_value <0) { # in case of program restart
$j--; $j=0;$found_value=undef; # don't look for more values
} while ( ($j>=0) && (!defined($found_value)) ); $n_rows=0; # reset file
} }
###### Write file }
$file_values[$n_rows][0]=$timenow; }
$file_values[$n_rows][1]=$res_cpu; $j--;
$n_rows++; } while ( ($j>=0) && (!defined($found_value)) );
$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;} ###### Write file
##### Check values (if something to check...) $file_values[$n_rows][0]=$timenow;
if (defined($found_value)) { $file_values[$n_rows][1]=$res_cpu;
if ($found_value > $o_cpuL[1]) { $n_rows++;
$final_status=2; $return=write_file($temp_file_name,$n_rows,$n_items_check,@file_values);
$cpu_print.=", Cpu : ".sprintf("%.0f",$found_value)."% > ".$o_cpuL[1]." CRITICAL"; if ($return != 0) { $cpu_print.="! ERROR writing file $temp_file_name !";$final_status=3;}
} elsif ( $found_value > $o_cpuL[0]) { ##### Check values (if something to check...)
$final_status=($final_status==2)?2:1; if (defined($found_value)) {
$cpu_print.=", Cpu : ".sprintf("%.0f",$found_value)."% > ".$o_cpuL[0]." WARNING"; if ($found_value > $o_cpuL[1]) {
} else { $final_status=2;
$cpu_print.=", Cpu : ".sprintf("%.0f",$found_value)."% OK"; $cpu_print.=", Cpu : ".sprintf("%.0f",$found_value)."% > ".$o_cpuL[1]." CRITICAL";
} elsif ( $found_value > $o_cpuL[0]) {
$final_status=($final_status==2)?2:1;
$cpu_print.=", Cpu : ".sprintf("%.0f",$found_value)."% > ".$o_cpuL[0]." WARNING";
} else {
$cpu_print.=", Cpu : ".sprintf("%.0f",$found_value)."% OK";
}
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 {
if ($final_status==0) { $final_status=3 };
$cpu_print.=", No data for CPU (".$n_rows." line(s)):UNKNOWN";
} }
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 {
if ($final_status==0) { $final_status=3 };
$cpu_print.=", No data for CPU (".$n_rows." line(s)):UNKNOWN";
}
} }
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
if ( $num_int_ok <= $o_critL[0] ) { if ( $num_int_ok <= $o_critL[0] ) {
print "(<= ",$o_critL[0]," : CRITICAL)"; print "(<= ",$o_critL[0]," : CRITICAL)";
$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)";
$final_status=($final_status==2)?2:1; $final_status=($final_status==2)?2:1;
} else { } else {
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)";
$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)";
$final_status=($final_status==2)?2:1; $final_status=($final_status==2)?2:1;
} elsif (defined($o_warnL[1])) { } elsif (defined($o_warnL[1])) {
print " (<= ",$o_warnL[1],"):OK"; print " (<= ",$o_warnL[1],"):OK";
} }
print $memory_print,$cpu_print; print $memory_print,$cpu_print;
if (defined($o_perf)) { if (defined($o_perf)) {
if (!defined($perf_output)) {$perf_output="";} else {$perf_output.=" ";} if (!defined($perf_output)) {$perf_output="";} else {$perf_output.=" ";}
$perf_output.= "'num_process'=". $num_int_ok.";".$o_warnL[0].";".$o_critL[0]; $perf_output.= "'num_process'=". $num_int_ok.";".$o_warnL[0].";".$o_critL[0];
print " | ",$perf_output; print " | ",$perf_output;
} }
print "\n"; 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"};}
if ($final_status==3) { exit $ERRORS{"UNKNOWN"};} if ($final_status==3) { exit $ERRORS{"UNKNOWN"};}
exit $ERRORS{"OK"}; exit $ERRORS{"OK"};