+ Add top in snmp processcount (Fix #100)

This commit is contained in:
garnier-quentin 2015-07-21 14:25:22 +02:00
parent eb84744474
commit 356537fd6e
1 changed files with 62 additions and 14 deletions

View File

@ -59,6 +59,9 @@ sub new {
"process-status:s" => { name => 'process_status', default => 'running|runnable' }, "process-status:s" => { name => 'process_status', default => 'running|runnable' },
"memory" => { name => 'memory', }, "memory" => { name => 'memory', },
"cpu" => { name => 'cpu', }, "cpu" => { name => 'cpu', },
"top" => { name => 'top', },
"top-num:s" => { name => 'top_num', default => 5 },
"top-size:s" => { name => 'top_size', default => 52428800 }, # 50MB
}); });
$self->{statefile_cache} = centreon::plugins::statefile->new(%options); $self->{statefile_cache} = centreon::plugins::statefile->new(%options);
$self->{filter4md5} = ''; $self->{filter4md5} = '';
@ -114,20 +117,45 @@ sub check_options {
} }
} }
my $oids = {
name => '.1.3.6.1.2.1.25.4.2.1.2', # hrSWRunName
path => '.1.3.6.1.2.1.25.4.2.1.4', # hrSWRunPath
args => '.1.3.6.1.2.1.25.4.2.1.5', # hrSWRunParameters (Warning: it's truncated. (128 characters))
status => '.1.3.6.1.2.1.25.4.2.1.7', # hrSWRunStatus
};
my $oid_hrSWRunPerfMem = '.1.3.6.1.2.1.25.5.1.1.2';
my $oid_hrSWRunPerfCPU = '.1.3.6.1.2.1.25.5.1.1.1';
sub check_top {
my ($self, %options) = @_;
my %data = ();
foreach (keys %{$self->{results}->{$oids->{name}}}) {
if (/^$oids->{name}\.(.*)/ &&
defined($self->{results}->{$oid_hrSWRunPerfMem}->{$oid_hrSWRunPerfMem . '.' . $1})) {
$data{$self->{results}->{$oids->{name}}->{$_}} = 0 if (!defined($data{$self->{results}->{$oids->{name}}->{$_}}));
$data{$self->{results}->{$oids->{name}}->{$_}} += $self->{results}->{$oid_hrSWRunPerfMem}->{$oid_hrSWRunPerfMem . '.' . $1} * 1024;
}
}
my $i = 1;
foreach my $name (sort { $data{$b} <=> $data{$a} } keys %data) {
last if ($i > $self->{option_results}->{top_num});
last if ($data{$name} < $self->{option_results}->{top_size});
my ($mem_value, $amem_unit) = $self->{perfdata}->change_bytes(value => $data{$name});
$self->{output}->output_add(long_msg => sprintf("Top %d '%s' memory usage: %s %s", $i, $name, $mem_value, $amem_unit));
$self->{output}->perfdata_add(label => 'top_' . $name, unit => 'B',
value => $data{$name},
min => 0);
$i++;
}
}
sub run { sub run {
my ($self, %options) = @_; my ($self, %options) = @_;
# $options{snmp} = snmp object # $options{snmp} = snmp object
$self->{snmp} = $options{snmp}; $self->{snmp} = $options{snmp};
my $oids = {
name => '.1.3.6.1.2.1.25.4.2.1.2', # hrSWRunName
path => '.1.3.6.1.2.1.25.4.2.1.4', # hrSWRunPath
args => '.1.3.6.1.2.1.25.4.2.1.5', # hrSWRunParameters (Warning: it's truncated. (128 characters))
status => '.1.3.6.1.2.1.25.4.2.1.7', # hrSWRunStatus
};
my $oid_hrSWRunPerfMem = '.1.3.6.1.2.1.25.5.1.1.2';
my $oid_hrSWRunPerfCPU = '.1.3.6.1.2.1.25.5.1.1.1';
my $oid2check_filter; my $oid2check_filter;
# To have a better order # To have a better order
@ -153,9 +181,15 @@ sub run {
} }
} }
my $result = $self->{snmp}->get_table(oid => $oids->{$oid2check_filter}); my $oids_multiple_table = [ { oid => $oids->{$oid2check_filter} } ];
if (defined($self->{option_results}->{top})) {
push @{$oids_multiple_table}, { oid => $oids->{name} };
push @{$oids_multiple_table}, { oid => $oid_hrSWRunPerfMem };
}
$self->{results} = $self->{snmp}->get_multiple_table(oids => $oids_multiple_table);
my $result = $self->{results}->{$oids->{$oid2check_filter}};
my $instances_keep = {}; my $instances_keep = {};
foreach my $key ($self->{snmp}->oid_lex_sort(keys %$result)) { foreach my $key ($self->{snmp}->oid_lex_sort(keys %{$result})) {
my $option_val = $self->{option_results}->{'process_' . $oid2check_filter}; my $option_val = $self->{option_results}->{'process_' . $oid2check_filter};
if ($oid2check_filter eq 'status') { if ($oid2check_filter eq 'status') {
@ -229,7 +263,7 @@ sub run {
my ($total_mem_value, $total_mem_unit) = $self->{perfdata}->change_bytes(value => $total_memory); my ($total_mem_value, $total_mem_unit) = $self->{perfdata}->change_bytes(value => $total_memory);
$self->{output}->output_add(severity => $exit, $self->{output}->output_add(severity => $exit,
short_msg => sprintf("Total memory usage: %s", $total_mem_value . " " . $total_mem_unit)); short_msg => sprintf("Total memory usage: %s", $total_mem_value . " " . $total_mem_unit));
$self->{output}->perfdata_add(label => 'mem_total', $self->{output}->perfdata_add(label => 'mem_total', unit => 'B',
value => $total_memory, value => $total_memory,
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning_mem_total'), warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning_mem_total'),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical_mem_total'), critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical_mem_total'),
@ -239,7 +273,7 @@ sub run {
my ($avg_mem_value, $avg_mem_unit) = $self->{perfdata}->change_bytes(value => $total_memory / $num_processes_match); my ($avg_mem_value, $avg_mem_unit) = $self->{perfdata}->change_bytes(value => $total_memory / $num_processes_match);
$self->{output}->output_add(severity => $exit, $self->{output}->output_add(severity => $exit,
short_msg => sprintf("Average memory usage: %.2f %s", $avg_mem_value, $avg_mem_unit)); short_msg => sprintf("Average memory usage: %.2f %s", $avg_mem_value, $avg_mem_unit));
$self->{output}->perfdata_add(label => 'mem_avg', $self->{output}->perfdata_add(label => 'mem_avg', unit => 'B',
value => sprintf("%.2f", $total_memory / $num_processes_match), value => sprintf("%.2f", $total_memory / $num_processes_match),
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning_avg_total'), warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning_avg_total'),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical_avg_total'), critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical_avg_total'),
@ -292,6 +326,8 @@ sub run {
$self->{statefile_cache}->write(data => $datas); $self->{statefile_cache}->write(data => $datas);
} }
$self->check_top() if (defined($self->{option_results}->{top}));
$self->{output}->display(); $self->{output}->display();
$self->{output}->exit(); $self->{output}->exit();
} }
@ -380,6 +416,18 @@ Check memory.
Check cpu usage. Should be used with fix processes. Check cpu usage. Should be used with fix processes.
if processes pid changes too much, the plugin can compute values. if processes pid changes too much, the plugin can compute values.
=item B<--top>
Enable top memory usage display.
=item B<--top-num>
Number of processes in the top (Default: 5).
=item B<--top-size>
Minimum memory usage to be in the top (Default: 52428800 -> 50 MB).
=back =back
=cut =cut