From 71e3a9fe7ea6e61242499eac1cbbc8a134d477a7 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Wed, 14 Oct 2020 15:53:39 +0200 Subject: [PATCH] wip dlink snmp plugin --- network/dlink/standard/snmp/mode/cpu.pm | 295 +++++++++++++++------ network/dlink/standard/snmp/mode/memory.pm | 167 ++++++++++++ network/dlink/standard/snmp/plugin.pm | 1 + 3 files changed, 382 insertions(+), 81 deletions(-) create mode 100644 network/dlink/standard/snmp/mode/memory.pm diff --git a/network/dlink/standard/snmp/mode/cpu.pm b/network/dlink/standard/snmp/mode/cpu.pm index 66a9c168c..b42a1178b 100644 --- a/network/dlink/standard/snmp/mode/cpu.pm +++ b/network/dlink/standard/snmp/mode/cpu.pm @@ -20,103 +20,234 @@ package network::dlink::standard::snmp::mode::cpu; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'cpu_avg', type => 0, cb_prefix_output => 'prefix_cpu_avg_output', message_separator => ' ', skipped_code => { -10 => 1 } }, + { name => 'cpu_core', type => 1, cb_prefix_output => 'prefix_cpu_core_output', message_separator => ' ', message_multiple => 'All core cpu are ok', skipped_code => { -10 => 1 } } + ]; + + $self->{maps_counters}->{cpu_avg} = [ + { label => 'average-5s', nlabel => 'cpu.utilization.5s.percentage', set => { + key_values => [ { name => 'average_5s' } ], + output_template => '%.2f %% (5s)', + perfdatas => [ + { template => '%.2f', min => 0, max => 100, unit => '%' } + ] + } + }, + { label => 'average-1m', nlabel => 'cpu.utilization.1m.percentage', set => { + key_values => [ { name => 'average_1m' } ], + output_template => '%.2f %% (1m)', + perfdatas => [ + { template => '%.2f', min => 0, max => 100, unit => '%' } + ] + } + }, + { label => 'average-5m', nlabel => 'cpu.utilization.5m.percentage', set => { + key_values => [ { name => 'average_5m' } ], + output_template => '%.2f %% (5m)', + perfdatas => [ + { template => '%.2f', min => 0, max => 100, unit => '%' } + ] + } + } + ]; + + $self->{maps_counters}->{cpu_core} = [ + { label => 'core-5s', nlabel => 'core.cpu.utilization.5s.percentage', set => { + key_values => [ { name => 'cpu_5s' }, { name => 'display' } ], + output_template => '%.2f %% (5s)', + perfdatas => [ + { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display' } + ] + } + }, + { label => 'core-1m', nlabel => 'core.cpu.utilization.1m.percentage', set => { + key_values => [ { name => 'cpu_1m' }, { name => 'display' } ], + output_template => '%.2f %% (1m)', + perfdatas => [ + { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display' } + ] + } + }, + { label => 'core-5m', nlabel => 'core.cpu.utilization.5m.percentage', set => { + key_values => [ { name => 'cpu_5m' }, { name => 'display' } ], + output_template => '%.2f %% (5m)', + perfdatas => [ + { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display' } + ] + } + } + ]; +} + +sub prefix_cpu_avg_output { + my ($self, %options) = @_; + + return $self->{cpu_avg}->{count} . " CPU(s) average usage is "; +} + +sub prefix_cpu_core_output { + my ($self, %options) = @_; + + return "CPU '" . $options{instance_value}->{display} . "' usage "; +} + sub new { my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); bless $self, $class; - $options{options}->add_options(arguments => - { - "warning:s" => { name => 'warning', default => '' }, - "critical:s" => { name => 'critical', default => '' }, - }); + $options{options}->add_options(arguments => { + 'check-order:s' => { name => 'check_order', default => 'common,industrial,agent' }, + }); return $self; } -sub check_options { +sub check_table_cpu { my ($self, %options) = @_; - $self->SUPER::init(%options); - - ($self->{warn5s}, $self->{warn1m}, $self->{warn5m}) = split /,/, $self->{option_results}->{warning}; - ($self->{crit5s}, $self->{crit1m}, $self->{crit5m}) = split /,/, $self->{option_results}->{critical}; - - if (($self->{perfdata}->threshold_validate(label => 'warn5s', value => $self->{warn5s})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong warning (5sec) threshold '" . $self->{warn5s} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'warn1m', value => $self->{warn1m})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong warning (1min) threshold '" . $self->{warn1m} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'warn5m', value => $self->{warn5m})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong warning (5min) threshold '" . $self->{warn5m} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'crit5s', value => $self->{crit5s})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong critical (crit5s) threshold '" . $self->{crit5s} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'crit1m', value => $self->{crit1m})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong critical (1min) threshold '" . $self->{crit1m} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'crit5m', value => $self->{crit5})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong critical (5min) threshold '" . $self->{crit5m} . "'."); - $self->{output}->option_exit(); - } + + my $instances = {}; + foreach my $oid (keys %{$options{snmp_result}}) { + $oid =~ /\.(\d+)\.(\d+)$/; + my $display = 'unit' . $1 . $self->{output}->get_instance_perfdata_separator() . $2; + my $instance = $1 . '.' . $2; + next if (defined($instances->{$instance})); + $instances->{$instance} = 1; + + my $cpu5sec = defined($options{snmp_result}->{$options{sec5} . '.' . $instance}) ? $options{snmp_result}->{$options{sec5} . '.' . $instance} : undef; + my $cpu1min = defined($options{snmp_result}->{$options{min1} . '.' . $instance}) ? $options{snmp_result}->{$options{min1} . '.' . $instance} : undef; + my $cpu5min = defined($options{snmp_result}->{$options{min5} . '.' . $instance}) ? $options{snmp_result}->{$options{min5} . '.' . $instance} : undef; + + # Case that it's maybe other CPU oid in table for datas. + next if (!defined($cpu5sec) && !defined($cpu1min) && !defined($cpu5min)); + + $self->{checked_cpu} = 1; + $self->{cpu_core}->{$instance} = { + display => $display, + cpu_5s => $cpu5sec, + cpu_1m => $cpu1min, + cpu_5m => $cpu5min + }; + } } -sub run { +sub check_cpu_average { my ($self, %options) = @_; - $self->{snmp} = $options{snmp}; + + my $count = scalar(keys %{$self->{cpu_core}}); + my ($avg_5s, $avg_1m, $avg_5m); + foreach (values %{$self->{cpu_core}}) { + $avg_5s = defined($avg_5s) ? $avg_5s + $_->{cpu_5s} : $_->{cpu_5s} + if (defined($_->{cpu_5s})); + $avg_1m = defined($avg_1m) ? $avg_1m + $_->{cpu_1m} : $_->{cpu_1m} + if (defined($_->{cpu_1m})); + $avg_5m = defined($avg_5m) ? $avg_5m + $_->{cpu_5m} : $_->{cpu_5m} + if (defined($_->{cpu_5m})); + } + + $self->{cpu_avg} = { + average_5s => defined($avg_5s) ? $avg_5s / $count : undef, + average_1m => defined($avg_1m) ? $avg_1m / $count : undef, + average_5m => defined($avg_5m) ? $avg_5m / $count : undef, + count => $count, + }; +} + +sub check_cpu_industrial { + my ($self, %options) = @_; + + return if ($self->{checked_cpu} == 1); + + my $oid_dEntityExtCpuUtilEntry = '.1.3.6.1.4.1.171.14.5.1.7.1'; + my $oid_dEntityExtCpuUtilFiveSeconds = '.1.3.6.1.4.1.171.14.5.1.7.1.3'; + my $oid_dEntityExtCpuUtilOneMinute = '.1.3.6.1.4.1.171.14.5.1.7.1.4'; + my $oid_dEntityExtCpuUtilFiveMinutes = '.1.3.6.1.4.1.171.14.5.1.7.1.5'; + + my $snmp_result = $options{snmp}->get_table( + oid => $oid_dEntityExtCpuUtilEntry, start => $oid_dEntityExtCpuUtilFiveSeconds, end => $oid_dEntityExtCpuUtilFiveMinutes + ); + $self->check_table_cpu( + snmp_result => $snmp_result, + sec5 => $oid_dEntityExtCpuUtilFiveSeconds, + min1 => $oid_dEntityExtCpuUtilOneMinute, + min5 => $oid_dEntityExtCpuUtilFiveMinutes + ); +} + +sub check_cpu_agent { + my ($self, %options) = @_; + + return if ($self->{checked_cpu} == 1); my $oid_agentCPUutilizationIn5sec = '.1.3.6.1.4.1.171.12.1.1.6.1.0'; my $oid_agentCPUutilizationIn1min = '.1.3.6.1.4.1.171.12.1.1.6.2.0'; my $oid_agentCPUutilizationIn5min = '.1.3.6.1.4.1.171.12.1.1.6.3.0'; - - $self->{result} = $self->{snmp}->get_leef(oids => [ $oid_agentCPUutilizationIn5sec, $oid_agentCPUutilizationIn1min, $oid_agentCPUutilizationIn5min], - nothing_quit => 1); - my $cpu5sec = $self->{result}->{$oid_agentCPUutilizationIn5sec}; - my $cpu1min = $self->{result}->{$oid_agentCPUutilizationIn1min}; - my $cpu5min = $self->{result}->{$oid_agentCPUutilizationIn5min}; + my $snmp_result = $options{snmp}->get_leef( + oids => [$oid_agentCPUutilizationIn5sec, $oid_agentCPUutilizationIn1min, $oid_agentCPUutilizationIn5min] + ); + + if (defined($snmp_result->{$oid_agentCPUutilizationIn5min})) { + $self->{checked_cpu} = 1; + $self->{cpu_core}->{0} = { + display => 0, + cpu_5s => $snmp_result->{$oid_agentCPUutilizationIn5sec}, + cpu_1m => $snmp_result->{$oid_agentCPUutilizationIn1min}, + cpu_5m => $snmp_result->{$oid_agentCPUutilizationIn5min} + }; + } +} + +sub check_cpu_common { + my ($self, %options) = @_; + + return if ($self->{checked_cpu} == 1); + + my $oid_esEntityExtCpuUtilEntry = '.1.3.6.1.4.1.171.17.5.1.7.1'; + my $oid_esEntityExtCpuUtilFiveSeconds = '.1.3.6.1.4.1.171.17.5.1.7.1.3'; + my $oid_esEntityExtCpuUtilOneMinute = '.1.3.6.1.4.1.171.17.5.1.7.1.4'; + my $oid_esEntityExtCpuUtilFiveMinutes = '.1.3.6.1.4.1.171.17.5.1.7.1.5'; + + my $snmp_result = $options{snmp}->get_table( + oid => $oid_esEntityExtCpuUtilEntry, start => $oid_esEntityExtCpuUtilFiveSeconds, end => $oid_esEntityExtCpuUtilFiveMinutes + ); + $self->check_table_cpu( + snmp_result => $snmp_result, + sec5 => $oid_esEntityExtCpuUtilFiveSeconds, + min1 => $oid_esEntityExtCpuUtilOneMinute, + min5 => $oid_esEntityExtCpuUtilFiveMinutes + ); +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{cpu_avg} = {}; + $self->{cpu_core} = {}; + $self->{checked_cpu} = 0; - my $exit1 = $self->{perfdata}->threshold_check(value => $cpu5sec, - threshold => [ { label => 'crit5s', exit_litteral => 'critical' }, { label => 'warn5s', exit_litteral => 'warning' } ]); - my $exit2 = $self->{perfdata}->threshold_check(value => $cpu1min, - threshold => [ { label => 'crit1m', exit_litteral => 'critical' }, { label => 'warn1m', exit_litteral => 'warning' } ]); - my $exit3 = $self->{perfdata}->threshold_check(value => $cpu5min, - threshold => [ { label => 'crit5m', exit_litteral => 'critical' }, { label => 'warn5m', exit_litteral => 'warning' } ]); - my $exit = $self->{output}->get_most_critical(status => [ $exit1, $exit2, $exit3 ]); - - $self->{output}->output_add(severity => $exit, - short_msg => sprintf("CPU Usage: %.2f%% (5sec), %.2f%% (1min), %.2f%% (5min)", - $cpu5sec, $cpu1min, $cpu5min)); - - $self->{output}->perfdata_add(label => "cpu_5s", unit => '%', - value => $cpu5sec, - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warn5s'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'crit5s'), - min => 0, max => 100); - $self->{output}->perfdata_add(label => "cpu_1m", unit => '%', - value => $cpu1min, - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warn1m'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'crit1m'), - min => 0, max => 100); - $self->{output}->perfdata_add(label => "cpu_5m", unit => '%', - value => $cpu5min, - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warn5m'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'crit5m'), - min => 0, max => 100); - - $self->{output}->display(); - $self->{output}->exit(); + foreach (split /,/, $self->{option_results}->{check_order}) { + my $method = $self->can('check_cpu_' . $_); + if ($method) { + $self->$method(snmp => $options{snmp}); + } + } + + if ($self->{checked_cpu} == 0) { + $self->{output}->add_option_msg(short_msg => "Cannot find CPU informations"); + $self->{output}->option_exit(); + } + + $self->check_cpu_average(); } 1; @@ -125,18 +256,20 @@ __END__ =head1 MODE -Check cpu usage (genmgmt.mib). +Check cpu usage. =over 8 -=item B<--warning> +=item B<--check-order> -Threshold warning in percent (5s,1min,5min). +Check cpu in standard dlink mib. If you have some issue (wrong cpu information in a specific mib), you can change the order +(Default: 'common,industrial,agent'). -=item B<--critical> +=item B<--warning-*> B<--critical-*> -Threshold critical in percent (5s,1min,5min). +Thresholds. +Can be: 'core-5s', 'core-1m', 'core-5m', 'average-5s', 'average-1m', 'average-5m'. =back -=cut \ No newline at end of file +=cut diff --git a/network/dlink/standard/snmp/mode/memory.pm b/network/dlink/standard/snmp/mode/memory.pm new file mode 100644 index 000000000..3d58f8328 --- /dev/null +++ b/network/dlink/standard/snmp/mode/memory.pm @@ -0,0 +1,167 @@ +# +# Copyright 2020 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package network::dlink::standard::snmp::mode::memory; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; + +sub custom_usage_output { + my ($self, %options) = @_; + + return sprintf( + 'total: %s %s used: %s %s (%.2f%%) free: %s %s (%.2f%%)', + $self->{perfdata}->change_bytes(value => $self->{result_values}->{total}), + $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}), + $self->{result_values}->{prct_used}, + $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}), + $self->{result_values}->{prct_free} + ); +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'memory', type => 1, cb_prefix_output => 'prefix_memory_output', message_multiple => 'All memory usages are ok', skipped_code => { -10 => 1 } } + ]; + + $self->{maps_counters}->{memory} = [ + { label => 'usage', display_ok => 0, nlabel => 'memory.usage.bytes', set => { + key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, { name => 'display' } ], + closure_custom_output => $self->can('custom_usage_output'), + perfdatas => [ + { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1 } + ] + } + }, + { label => 'usage-free', display_ok => 0, nlabel => 'memory.free.bytes', set => { + key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, { name => 'display' } ], + closure_custom_output => $self->can('custom_usage_output'), + perfdatas => [ + { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1 } + ] + } + }, + { label => 'usage-prct', nlabel => 'memory.usage.percentage', set => { + key_values => [ { name => 'prct_used' }, { name => 'used' }, { name => 'free' }, { name => 'prct_free' }, { name => 'total' }, { name => 'display' } ], + closure_custom_output => $self->can('custom_usage_output'), + perfdatas => [ + { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1 } + ] + } + } + ]; +} + +sub prefix_memory_output { + my ($self, %options) = @_; + + return "Memory '" . $options{instance_value}->{display} . "' "; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); + bless $self, $class; + + $options{options}->add_options(arguments => { + }); + + return $self; +} + +my $map_memory_type = { + 1 => 'dram', 2 => 'flash', 3 => 'nvram' +}; + +my $mapping_industrial = { + total => { oid => '.1.3.6.1.4.1.171.14.5.1.4.1.3' }, # dEntityExtMemUtilTotal (KB) + used => { oid => '.1.3.6.1.4.1.171.14.5.1.4.1.4' }, # dEntityExtMemUtilUsed (KB) + free => { oid => '.1.3.6.1.4.1.171.14.5.1.4.1.5' } # dEntityExtMemUtilUsed (KB) +}; +my $oid_dEntityExtMemoryUtilEntry = '.1.3.6.1.4.1.171.14.5.1.4.1'; + +my $mapping_common = { + total => { oid => '.1.3.6.1.4.1.171.17.5.1.4.1.3' }, # esEntityExtMemUtilTotal (KB) + used => { oid => '.1.3.6.1.4.1.171.17.5.1.4.1.4' }, # esEntityExtMemUtilUsed (KB) + free => { oid => '.1.3.6.1.4.1.171.17.5.1.4.1.5' } # esEntityExtMemUtilFree (KB) +}; +my $oid_esEntityExtMemoryUtilEntry = '.1.3.6.1.4.1.171.17.5.1.4.1'; + +sub check_memory { + my ($self, %options) = @_; + + return if ($self->{checked_memory} == 1); + + foreach (keys %{$options{snmp_result}}) { + next if (! /^$options{mapping}->{total}->{oid}\.(\d+)\.(\d+)$/); + my $instance = $1 . '.' . $2; + my $display = 'unit' . $1 . $self->{output}->get_instance_perfdata_separator() . $map_memory_type->{$2}; + my $result = $options{snmp}->map_instance(mapping => $options{mapping}, results => $options{snmp_result}, instance => $instance); + + $self->{checked_memory} = 1; + $self->{memory}->{$display} = { + display => $display, + used => $result->{used} * 1024, + free => $result->{free} * 1024, + prct_used => ($result->{used} * 1024 * 100) / ($result->{total} * 1024), + prct_free => ($result->{free} * 1024 * 100) / ($result->{total} * 1024), + total => $result->{total} * 1024 + }; + } +} + +sub manage_selection { + my ($self, %options) = @_; + + my $snmp_result = $options{snmp}->get_multiple_table( + oids => [ + { oid => $oid_dEntityExtMemoryUtilEntry, start => $mapping_industrial->{total}->{oid} }, + { oid => $oid_esEntityExtMemoryUtilEntry, start => $mapping_common->{total}->{oid} } + ], + nothing_quit => 1 + ); + + $self->{checked_memory} = 0; + $self->check_memory(snmp => $options{snmp}, snmp_result => $snmp_result->{$oid_dEntityExtMemoryUtilEntry}, mapping => $mapping_industrial); + $self->check_memory(snmp => $options{snmp}, snmp_result => $snmp_result->{$oid_esEntityExtMemoryUtilEntry}, mapping => $mapping_common); +} + +1; + +__END__ + +=head1 MODE + +Check memory usages. + +=over 8 + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'usage' (B), 'usage-free' (B), 'usage-prct' (%). + +=back + +=cut diff --git a/network/dlink/standard/snmp/plugin.pm b/network/dlink/standard/snmp/plugin.pm index 26e739ad1..a5554e8f3 100644 --- a/network/dlink/standard/snmp/plugin.pm +++ b/network/dlink/standard/snmp/plugin.pm @@ -35,6 +35,7 @@ sub new { 'hardware' => 'network::dlink::standard::snmp::mode::hardware', 'interfaces' => 'snmp_standard::mode::interfaces', 'list-interfaces' => 'snmp_standard::mode::listinterfaces', + 'memory' => 'network::dlink::standard::snmp::mode::memory' ); return $self;