enh(mode): snmp numeric value output (#2680)

This commit is contained in:
Thibault S 2021-03-26 15:54:53 +01:00 committed by GitHub
parent 3a63f3641d
commit 3869787121
1 changed files with 44 additions and 45 deletions

View File

@ -31,23 +31,23 @@ sub new {
my ($class, %options) = @_; my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options); my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => { $options{options}->add_options(arguments => {
'oid:s' => { name => 'oid' }, 'oid:s' => { name => 'oid' },
'oid-type:s' => { name => 'oid_type' }, 'oid-type:s' => { name => 'oid_type' },
'counter-per-seconds' => { name => 'counter_per_seconds' }, 'counter-per-seconds' => { name => 'counter_per_seconds' },
'warning:s' => { name => 'warning' }, 'warning:s' => { name => 'warning' },
'critical:s' => { name => 'critical' }, 'critical:s' => { name => 'critical' },
'extracted-pattern:s' => { name => 'extracted_pattern' }, 'extracted-pattern:s' => { name => 'extracted_pattern' },
'format:s' => { name => 'format' }, 'format:s' => { name => 'format' },
'format-custom:s' => { name => 'format_custom' }, 'format-custom:s' => { name => 'format_custom' },
'format-scale' => { name => 'format_scale' }, 'format-scale' => { name => 'format_scale' },
'format-scale-type:s' => { name => 'format_scale_type' }, 'format-scale-type:s' => { name => 'format_scale_type' },
'perfdata-unit:s' => { name => 'perfdata_unit' }, 'perfdata-unit:s' => { name => 'perfdata_unit' },
'perfdata-name:s' => { name => 'perfdata_name' }, 'perfdata-name:s' => { name => 'perfdata_name' },
'perfdata-min:s' => { name => 'perfdata_min' }, 'perfdata-min:s' => { name => 'perfdata_min' },
'perfdata-max:s' => { name => 'perfdata_max' }, 'perfdata-max:s' => { name => 'perfdata_max' },
'config-json:s' => { name => 'config_json' }, 'config-json:s' => { name => 'config_json' }
}); });
$self->{statefile_cache} = centreon::plugins::statefile->new(%options); $self->{statefile_cache} = centreon::plugins::statefile->new(%options);
@ -57,24 +57,24 @@ sub new {
sub add_data { sub add_data {
my ($self, %options) = @_; my ($self, %options) = @_;
my $entry = {}; my $entry = {};
return if (!defined($options{data}->{oid}) || $options{data}->{oid} eq ''); return if (!defined($options{data}->{oid}) || $options{data}->{oid} eq '');
$entry->{oid} = $options{data}->{oid}; $entry->{oid} = $options{data}->{oid};
$entry->{oid} = '.' . $entry->{oid} if ($options{data}->{oid} !~ /^\./); $entry->{oid} = '.' . $entry->{oid} if ($options{data}->{oid} !~ /^\./);
$entry->{oid_type} = defined($options{data}->{oid_type}) && $options{data}->{oid_type} ne '' ? $options{data}->{oid_type} : 'gauge'; $entry->{oid_type} = defined($options{data}->{oid_type}) && $options{data}->{oid_type} ne '' ? $options{data}->{oid_type} : 'gauge';
if ($entry->{oid_type} !~ /^gauge|counter$/i) { if ($entry->{oid_type} !~ /^gauge|counter$/i) {
$self->{output}->add_option_msg(short_msg => "Wrong oid-type argument '" . $entry->{oid_type} . "' ('gauge' or 'counter')."); $self->{output}->add_option_msg(short_msg => "Wrong oid-type argument '" . $entry->{oid_type} . "' ('gauge' or 'counter').");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
$entry->{format_scale_type} = defined($options{data}->{format_scale_type}) && $options{data}->{format_scale_type} ne '' ? $options{data}->{format_scale_type} : 'other'; $entry->{format_scale_type} = defined($options{data}->{format_scale_type}) && $options{data}->{format_scale_type} ne '' ? $options{data}->{format_scale_type} : 'other';
if ($entry->{format_scale_type} !~ /^other|network$/i) { if ($entry->{format_scale_type} !~ /^other|network$/i) {
$self->{output}->add_option_msg(short_msg => "Wrong format-scale-type argument '" . $entry->{format_scale_type} . "' ('other' or 'network')."); $self->{output}->add_option_msg(short_msg => "Wrong format-scale-type argument '" . $entry->{format_scale_type} . "' ('other' or 'network').");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
if (($self->{perfdata}->threshold_validate(label => 'warning-' . $options{num}, value => $options{data}->{warning})) == 0) { if (($self->{perfdata}->threshold_validate(label => 'warning-' . $options{num}, value => $options{data}->{warning})) == 0) {
$self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $options{data}->{warning} . "'."); $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $options{data}->{warning} . "'.");
$self->{output}->option_exit(); $self->{output}->option_exit();
@ -83,7 +83,7 @@ sub add_data {
$self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $options{data}->{critical} . "'."); $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $options{data}->{critical} . "'.");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
foreach (['oid_type', 'gauge'], ['counter_per_seconds'], ['format', 'current value is %s'], foreach (['oid_type', 'gauge'], ['counter_per_seconds'], ['format', 'current value is %s'],
['format_custom', ''], ['format_scale'], ['format_custom', ''], ['format_scale'],
['perfdata_unit', ''], ['perfdata_name', 'value'], ['perfdata_unit', ''], ['perfdata_name', 'value'],
@ -94,10 +94,10 @@ sub add_data {
$entry->{$_->[0]} = $_->[1]; $entry->{$_->[0]} = $_->[1];
} }
} }
push @{$self->{entries}}, $entry; push @{$self->{entries}}, $entry;
push @{$self->{request_oids}}, $entry->{oid}; push @{$self->{request_oids}}, $entry->{oid};
if (defined($options{data}->{oid_type}) && $options{data}->{oid_type} =~ /^counter$/i) { if (defined($options{data}->{oid_type}) && $options{data}->{oid_type} =~ /^counter$/i) {
$self->{use_statefile} = 1; $self->{use_statefile} = 1;
} }
@ -122,7 +122,7 @@ sub check_options {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response"); $self->{output}->add_option_msg(short_msg => "Cannot decode json response");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
my $i = 0; my $i = 0;
foreach (@$content) { foreach (@$content) {
$self->add_data(data => $_, num => $i); $self->add_data(data => $_, num => $i);
@ -131,12 +131,12 @@ sub check_options {
} else { } else {
$self->add_data(data => $self->{option_results}, num => 0); $self->add_data(data => $self->{option_results}, num => 0);
} }
if (scalar(@{$self->{entries}}) == 0) { if (scalar(@{$self->{entries}}) == 0) {
$self->{output}->add_option_msg(short_msg => "Need to specify an OID."); $self->{output}->add_option_msg(short_msg => "Need to specify an OID.");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
if ($self->{use_statefile} == 1) { if ($self->{use_statefile} == 1) {
$self->{statefile_cache}->check_options(%options); $self->{statefile_cache}->check_options(%options);
} }
@ -144,7 +144,7 @@ sub check_options {
sub check_data { sub check_data {
my ($self, %options) = @_; my ($self, %options) = @_;
if (!defined($self->{results}->{$options{entry}->{oid}})) { if (!defined($self->{results}->{$options{entry}->{oid}})) {
$self->{output}->output_add( $self->{output}->output_add(
severity => 'UNKNOWN', severity => 'UNKNOWN',
@ -166,20 +166,20 @@ sub check_data {
); );
return ; return ;
} }
if ($options{entry}->{oid_type} =~ /^counter$/i) { if ($options{entry}->{oid_type} =~ /^counter$/i) {
my $old_timestamp = $self->{statefile_cache}->get(name => 'timestamp'); my $old_timestamp = $self->{statefile_cache}->get(name => 'timestamp');
my $old_value = $self->{statefile_cache}->get(name => 'value-' . $options{num}); my $old_value = $self->{statefile_cache}->get(name => 'value-' . $options{num});
$self->{cache_datas}->{timestamp} = time(); $self->{cache_datas}->{timestamp} = time();
$self->{cache_datas}->{'value-' . $options{num}} = $value; $self->{cache_datas}->{'value-' . $options{num}} = $value;
if (!defined($old_timestamp)) { if (!defined($old_timestamp)) {
$self->{output}->output_add(severity => 'OK', $self->{output}->output_add(severity => 'OK',
short_msg => "Buffer creation..."); short_msg => "Buffer creation...");
return ; return ;
} }
# Reboot or counter goes back # Reboot or counter goes back
if ($old_value > $value) { if ($old_value > $value) {
$old_value = 0; $old_value = 0;
@ -191,18 +191,17 @@ sub check_data {
$value = $value / $delta_time; $value = $value / $delta_time;
} }
} }
if ($options{entry}->{format_custom} ne '') { if ($options{entry}->{format_custom} ne '') {
$value = eval "$value $options{entry}->{format_custom}"; $value = eval "$value $options{entry}->{format_custom}";
} }
my $exit = $self->{perfdata}->threshold_check(value => $value, my $exit = $self->{perfdata}->threshold_check(value => $value,
threshold => [ { label => 'critical-' . $options{num}, exit_litteral => 'critical' }, { label => 'warning-' . $options{num}, exit_litteral => 'warning' } ]); threshold => [ { label => 'critical-' . $options{num}, exit_litteral => 'critical' }, { label => 'warning-' . $options{num}, exit_litteral => 'warning' } ]);
if (defined($options{entry}->{format_scale})) { if (defined($options{entry}->{format_scale})) {
my ($value_mod, $value_unit) = $self->{perfdata}->change_bytes(value => $value); my $network = $options{entry}->{format_scale_type} =~ /^network$/i ? { network => 1 } : {};
if ($options{entry}->{format_scale_type} =~ /^network$/i) { my ($value_mod, $value_unit) = $self->{perfdata}->change_bytes(value => $value, %{$network});
($value_mod, $value_unit) = $self->{perfdata}->change_bytes(value => $value, network => 1); $value_unit .= '/s' if (defined($options{entry}->{counter_per_seconds}));
}
$self->{output}->output_add(severity => $exit, $self->{output}->output_add(severity => $exit,
short_msg => sprintf($options{entry}->{format}, $value_mod . $value_unit)); short_msg => sprintf($options{entry}->{format}, $value_mod . $value_unit));
} else { } else {
@ -219,7 +218,7 @@ sub check_data {
sub run { sub run {
my ($self, %options) = @_; my ($self, %options) = @_;
if ($self->{use_statefile} == 1) { if ($self->{use_statefile} == 1) {
$self->{cache_datas} = {}; $self->{cache_datas} = {};
$self->{statefile_cache}->read(statefile => 'snmpstandard_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' . $self->{mode} . '_' . md5_hex(join('-', @{$self->{request_oids}}))); $self->{statefile_cache}->read(statefile => 'snmpstandard_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' . $self->{mode} . '_' . md5_hex(join('-', @{$self->{request_oids}})));
@ -231,11 +230,11 @@ sub run {
$self->check_data(entry => $_, num => $num); $self->check_data(entry => $_, num => $num);
$num++; $num++;
} }
if ($self->{use_statefile} == 1) { if ($self->{use_statefile} == 1) {
$self->{statefile_cache}->write(data => $self->{cache_datas}); $self->{statefile_cache}->write(data => $self->{cache_datas});
} }
$self->{output}->display(); $self->{output}->display();
$self->{output}->exit(); $self->{output}->exit();
} }