refactor cpudetailed with counters class

This commit is contained in:
garnier-quentin 2019-04-15 17:04:21 +02:00
parent 590388ad26
commit 9b36cd1aaf
1 changed files with 205 additions and 152 deletions

View File

@ -20,175 +20,228 @@
package snmp_standard::mode::cpudetailed;
use base qw(centreon::plugins::mode);
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use centreon::plugins::statefile;
use Digest::MD5 qw(md5_hex);
my $oids = {
'.1.3.6.1.4.1.2021.11.50' => { counter => 'user', output => 'User %.2f %%' }, # ssCpuRawUser
'.1.3.6.1.4.1.2021.11.51' => { counter => 'nice', output => 'Nice %.2f %%' }, # ssCpuRawNice
'.1.3.6.1.4.1.2021.11.52' => { counter => 'system', output => 'System %.2f %%' }, # ssCpuRawSystem
'.1.3.6.1.4.1.2021.11.53' => { counter => 'idle', output => 'Idle %.2f %%' }, # ssCpuRawIdle
'.1.3.6.1.4.1.2021.11.54' => { counter => 'wait', output => 'Wait %.2f %%' }, # ssCpuRawWait
'.1.3.6.1.4.1.2021.11.55' => { counter => 'kernel', output => 'Kernel %.2f %%' }, # ssCpuRawKernel
'.1.3.6.1.4.1.2021.11.56' => { counter => 'interrupt', output => 'Interrupt %.2f %%' }, # ssCpuRawInterrupt
'.1.3.6.1.4.1.2021.11.61' => { counter => 'softirq', output => 'Soft Irq %.2f %%' }, # ssCpuRawSoftIRQ
'.1.3.6.1.4.1.2021.11.64' => { counter => 'steal', output => 'Steal %.2f %%' }, # ssCpuRawSteal
'.1.3.6.1.4.1.2021.11.65' => { counter => 'guest', output => 'Guest %.2f %%' }, # ssCpuRawGuest
'.1.3.6.1.4.1.2021.11.66' => { counter => 'guestnice', output => 'Guest Nice %.2f %%' }, # ssCpuRawGuestNice
};
sub custom_cpu_calc {
my ($self, %options) = @_;
if (!defined($self->{instance_mode}->{total_cpu})) {
$self->{instance_mode}->{total_cpu} = 0;
foreach (keys %{$options{new_datas}}) {
if (/$self->{instance}_/) {
my $new_total = $options{new_datas}->{$_};
next if (!defined($options{old_datas}->{$_}));
my $old_total = $options{old_datas}->{$_};
my $diff_total = $new_total - $old_total;
if ($diff_total < 0) {
$self->{instance_mode}->{total_cpu} += $old_total;
} else {
$self->{instance_mode}->{total_cpu} += $diff_total;
}
}
}
}
if ($self->{instance_mode}->{total_cpu} <= 0) {
$self->{error_msg} = "counter not moved";
return -12;
}
if ($options{old_datas}->{$self->{instance} . '_' . $options{extra_options}->{label_ref}} > $options{new_datas}->{$self->{instance} . '_' . $options{extra_options}->{label_ref}}) {
$options{old_datas}->{$self->{instance} . '_' . $options{extra_options}->{label_ref}} = 0;
}
$self->{result_values}->{prct_used} =
($options{new_datas}->{$self->{instance} . '_' . $options{extra_options}->{label_ref}} -
$options{old_datas}->{$self->{instance} . '_' . $options{extra_options}->{label_ref}}) * 100 /
$self->{instance_mode}->{total_cpu};
return 0;
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0, cb_prefix_output => 'prefix_cpu_output', skipped_code => { -10 => 1 } },
];
$self->{maps_counters}->{global} = [
{ label => 'user', set => {
key_values => [],
closure_custom_calc => $self->can('custom_cpu_calc'), closure_custom_calc_extra_options => { label_ref => 'ssCpuRawUser' },
manual_keys => 1,
threshold_use => 'prct_used', output_use => 'prct_used',
output_template => 'User %.2f %%',
perfdatas => [
{ value => 'prct_used', template => '%.2f', min => 0 , max => 100, unit => '%' },
],
}
},
{ label => 'nice', set => {
key_values => [],
closure_custom_calc => $self->can('custom_cpu_calc'), closure_custom_calc_extra_options => { label_ref => 'ssCpuRawNice' },
manual_keys => 1,
threshold_use => 'prct_used', output_use => 'prct_used',
output_template => 'Nice %.2f %%',
perfdatas => [
{ value => 'prct_used', template => '%.2f', min => 0 , max => 100, unit => '%' },
],
}
},
{ label => 'system', set => {
key_values => [],
closure_custom_calc => $self->can('custom_cpu_calc'), closure_custom_calc_extra_options => { label_ref => 'ssCpuRawSystem' },
manual_keys => 1,
threshold_use => 'prct_used', output_use => 'prct_used',
output_template => 'System %.2f %%',
perfdatas => [
{ value => 'prct_used', template => '%.2f', min => 0 , max => 100, unit => '%' },
],
}
},
{ label => 'idle', set => {
key_values => [],
closure_custom_calc => $self->can('custom_cpu_calc'), closure_custom_calc_extra_options => { label_ref => 'ssCpuRawIdle' },
manual_keys => 1,
threshold_use => 'prct_used', output_use => 'prct_used',
output_template => 'Idle %.2f %%',
perfdatas => [
{ value => 'prct_used', template => '%.2f', min => 0 , max => 100, unit => '%' },
],
}
},
{ label => 'wait', set => {
key_values => [],
closure_custom_calc => $self->can('custom_cpu_calc'), closure_custom_calc_extra_options => { label_ref => 'ssCpuRawWait' },
manual_keys => 1,
threshold_use => 'prct_used', output_use => 'prct_used',
output_template => 'Wait %.2f %%',
perfdatas => [
{ value => 'prct_used', template => '%.2f', min => 0 , max => 100, unit => '%' },
],
}
},
{ label => 'kernel', set => {
key_values => [],
closure_custom_calc => $self->can('custom_cpu_calc'), closure_custom_calc_extra_options => { label_ref => 'ssCpuRawKernel' },
manual_keys => 1,
threshold_use => 'prct_used', output_use => 'prct_used',
output_template => 'Kernel %.2f %%',
perfdatas => [
{ value => 'prct_used', template => '%.2f', min => 0 , max => 100, unit => '%' },
],
}
},
{ label => 'interrupt', set => {
key_values => [],
closure_custom_calc => $self->can('custom_cpu_calc'), closure_custom_calc_extra_options => { label_ref => 'ssCpuRawInterrupt' },
manual_keys => 1,
threshold_use => 'prct_used', output_use => 'prct_used',
output_template => 'Interrupt %.2f %%',
perfdatas => [
{ value => 'prct_used', template => '%.2f', min => 0 , max => 100, unit => '%' },
],
}
},
{ label => 'softirq', set => {
key_values => [],
closure_custom_calc => $self->can('custom_cpu_calc'), closure_custom_calc_extra_options => { label_ref => 'ssCpuRawSoftIRQ' },
manual_keys => 1,
threshold_use => 'prct_used', output_use => 'prct_used',
output_template => 'Soft Irq %.2f %%',
perfdatas => [
{ value => 'prct_used', template => '%.2f', min => 0 , max => 100, unit => '%' },
],
}
},
{ label => 'steal', set => {
key_values => [],
closure_custom_calc => $self->can('custom_cpu_calc'), closure_custom_calc_extra_options => { label_ref => 'ssCpuRawSteal' },
manual_keys => 1,
threshold_use => 'prct_used', output_use => 'prct_used',
output_template => 'Steal %.2f %%',
perfdatas => [
{ value => 'prct_used', template => '%.2f', min => 0 , max => 100, unit => '%' },
],
}
},
{ label => 'guest', set => {
key_values => [],
closure_custom_calc => $self->can('custom_cpu_calc'), closure_custom_calc_extra_options => { label_ref => 'ssCpuRawGuest' },
manual_keys => 1,
threshold_use => 'prct_used', output_use => 'prct_used',
output_template => 'Guest %.2f %%',
perfdatas => [
{ value => 'prct_used', template => '%.2f', min => 0 , max => 100, unit => '%' },
],
}
},
{ label => 'guestnice', set => {
key_values => [],
closure_custom_calc => $self->can('custom_cpu_calc'), closure_custom_calc_extra_options => { label_ref => 'ssCpuRawGuestNice' },
manual_keys => 1,
threshold_use => 'prct_used', output_use => 'prct_used',
output_template => 'Guest Nice %.2f %%',
perfdatas => [
{ value => 'prct_used', template => '%.2f', min => 0 , max => 100, unit => '%' },
],
}
},
];
}
sub prefix_cpu_output {
my ($self, %options) = @_;
return 'CPU Usage: ';
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1);
bless $self, $class;
$self->{version} = '1.0';
$options{options}->add_options(arguments =>
{
});
foreach (keys %{$oids}) {
$options{options}->add_options(arguments => {
'warning-' . $oids->{$_}->{counter} . ':s' => { name => 'warning_' . $oids->{$_}->{counter} },
'critical-' . $oids->{$_}->{counter} . ':s' => { name => 'critical_' . $oids->{$_}->{counter} },
});
}
$self->{statefile_value} = centreon::plugins::statefile->new(%options);
$options{options}->add_options(arguments => {
});
return $self;
}
sub check_options {
my $mapping = {
ssCpuRawUser => { oid => '.1.3.6.1.4.1.2021.11.50' },
ssCpuRawNice => { oid => '.1.3.6.1.4.1.2021.11.51' },
ssCpuRawSystem => { oid => '.1.3.6.1.4.1.2021.11.52' },
ssCpuRawIdle => { oid => '.1.3.6.1.4.1.2021.11.53' },
ssCpuRawWait => { oid => '.1.3.6.1.4.1.2021.11.54' },
ssCpuRawKernel => { oid => '.1.3.6.1.4.1.2021.11.55' },
ssCpuRawInterrupt => { oid => '.1.3.6.1.4.1.2021.11.56' },
ssCpuRawSoftIRQ => { oid => '.1.3.6.1.4.1.2021.11.61' },
ssCpuRawSteal => { oid => '.1.3.6.1.4.1.2021.11.64' },
ssCpuRawGuest => { oid => '.1.3.6.1.4.1.2021.11.65' },
ssCpuRawGuestNice => { oid => '.1.3.6.1.4.1.2021.11.66' },
};
sub manage_selection {
my ($self, %options) = @_;
$self->SUPER::init(%options);
foreach (keys %{$oids}) {
if (($self->{perfdata}->threshold_validate(label => 'warning-' . $oids->{$_}->{counter}, value => $self->{option_results}->{'warning_' . $oids->{$_}->{counter}})) == 0) {
$self->{output}->add_option_msg(short_msg => "Wrong warning-" . $oids->{$_}->{counter} . " threshold '" . $self->{option_results}->{'warning_' . $oids->{$_}->{counter}} . "'.");
$self->{output}->option_exit();
}
if (($self->{perfdata}->threshold_validate(label => 'critical-' . $oids->{$_}->{counter}, value => $self->{option_results}->{'critical_' . $oids->{$_}->{counter}})) == 0) {
$self->{output}->add_option_msg(short_msg => "Wrong critical-" . $oids->{$_}->{counter} . " threshold '" . $self->{option_results}->{'critical_' . $oids->{$_}->{counter}} . "'.");
$self->{output}->option_exit();
}
}
$self->{statefile_value}->check_options(%options);
}
my $oid_systemStats = '.1.3.6.1.4.1.2021.11';
my $snmp_result = $options{snmp}->get_table(
oid => $oid_systemStats,
start => $mapping->{ssCpuRawUser}->{oid},
nothing_quit => 1
);
sub run {
my ($self, %options) = @_;
$self->{snmp} = $options{snmp};
$self->{hostname} = $self->{snmp}->get_hostname();
$self->{snmp_port} = $self->{snmp}->get_port();
my $result = $self->{snmp}->get_table(oid => '.1.3.6.1.4.1.2021.11',
start => '.1.3.6.1.4.1.2021.11.50',
nothing_quit => 1);
# construct values
my $info_indexes = {};
my $new_datas = {};
my $old_datas = {};
$new_datas->{last_timestamp} = time();
$self->{statefile_value}->read(statefile => "snmpstandard_" . $self->{hostname} . '_' . $self->{snmp_port} . '_' . $self->{mode});
foreach my $oid (keys %{$result}) {
$oid =~ /(.*)\.(\d+)$/;
my ($oid_base, $index) = ($1, $2);
# If not, we skip oid
next if (!defined($oids->{$oid_base}));
$new_datas->{$oids->{$oid_base}->{counter} . '_' . $index} = $result->{$oid};
$old_datas->{$oids->{$oid_base}->{counter} . '_' . $index} = $self->{statefile_value}->get(name => $oids->{$oid_base}->{counter} . '_' . $index);
$info_indexes->{$index} = { total => 0, old_total => 0, buffer_creation => 0 } if (!defined($info_indexes->{$index}));
if (!defined($old_datas->{$oids->{$oid_base}->{counter} . '_' . $index})) {
$info_indexes->{$index}->{buffer_creation} = 1;
next;
}
if ($new_datas->{$oids->{$oid_base}->{counter} . '_' . $index} < $old_datas->{$oids->{$oid_base}->{counter} . '_' . $index}) {
$info_indexes->{$index}->{buffer_creation} = 1;
next;
}
$info_indexes->{$index}->{total} += $new_datas->{$oids->{$oid_base}->{counter} . '_' . $index};
$info_indexes->{$index}->{old_total} += $old_datas->{$oids->{$oid_base}->{counter} . '_' . $index};
}
$self->{statefile_value}->write(data => $new_datas);
my $multiple = 0;
if (scalar(keys %$info_indexes) > 1) {
$multiple = 1;
}
if ($multiple == 1) {
$self->{output}->output_add(severity => 'OK',
short_msg => "All CPU usage are ok.");
}
# Manage output
foreach my $index (keys %$info_indexes) {
if ($info_indexes->{$index}->{buffer_creation} == 1) {
if ($multiple == 0) {
$self->{output}->output_add(severity => 'OK',
short_msg => "Buffer creation...");
} else {
$self->{output}->output_add(long_msg => "CPU '$index': Buffer creation...");
}
next;
}
if ($info_indexes->{$index}->{total} - $info_indexes->{$index}->{old_total} == 0) {
if ($multiple == 0) {
$self->{output}->output_add(severity => 'OK',
short_msg => "Counter not moved. Have to wait.");
} else {
$self->{output}->output_add(long_msg => "CPU '$index': Counter not moved. Have to wait.");
}
next;
}
my @exits = ();
foreach my $oid (keys %{$result}) {
$oid =~ /(.*)\.(\d+)$/;
my ($oid_base, $index2) = ($1, $2);
# If not, we skip oid
next if ($index2 != $index || !defined($oids->{$oid_base}));
my $value = (($new_datas->{$oids->{$oid_base}->{counter} . '_' . $index} - $old_datas->{$oids->{$oid_base}->{counter} . '_' . $index}) * 100) / ($info_indexes->{$index}->{total} - $info_indexes->{$index}->{old_total});
push @exits, $self->{perfdata}->threshold_check(value => $value, threshold => [ { label => 'critical-' . $oids->{$oid_base}->{counter}, 'exit_litteral' => 'critical' }, { label => 'warning-' . $oids->{$oid_base}->{counter}, 'exit_litteral' => 'warning' }]);
}
my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => '0');
my $exit = $self->{output}->get_most_critical(status => [ @exits ]);
my $str_output = "CPU Usage: ";
$str_output = "CPU '$index' Usage: " if ($multiple == 1);
my $str_append = '';
foreach my $oid (keys %{$result}) {
$oid =~ /(.*)\.(\d+)$/;
my ($oid_base, $index2) = ($1, $2);
# If not, we skip oid
next if ($index2 != $index || !defined($oids->{$oid_base}));
my $value = (($new_datas->{$oids->{$oid_base}->{counter} . '_' . $index} - $old_datas->{$oids->{$oid_base}->{counter} . '_' . $index}) * 100) / ($info_indexes->{$index}->{total} - $info_indexes->{$index}->{old_total});
$str_output .= $str_append . sprintf($oids->{$oid_base}->{output}, $value);
$str_append = ', ';
my $warning = $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $oids->{$oid_base}->{counter});
my $critical = $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $oids->{$oid_base}->{counter});
$self->{cache_name} = "snmpstandard_" . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' . $self->{mode} . '_' .
(defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all'));
my $extra_perf_label = '';
$extra_perf_label = $index if ($multiple == 1);
$self->{output}->perfdata_add(label => $oids->{$oid_base}->{counter} . $extra_perf_label, unit => '%',
value => sprintf("%.2f", $value),
warning => $warning,
critical => $critical,
min => 0, max => 100);
}
if ($multiple == 0) {
$self->{output}->output_add(severity => $exit,
short_msg => $str_output);
}
$self->{output}->output_add(long_msg => $str_output);
}
$self->{output}->display();
$self->{output}->exit();
$self->{global} = { %$result };
}
1;