diff --git a/centreon/common/airespace/snmp/mode/apchannelinterference.pm b/centreon/common/airespace/snmp/mode/apchannelinterference.pm index 5905154ae..ee3b1d882 100644 --- a/centreon/common/airespace/snmp/mode/apchannelinterference.pm +++ b/centreon/common/airespace/snmp/mode/apchannelinterference.pm @@ -135,7 +135,7 @@ Check AP Channel Interference. =item B<--filter-counters> Only display some counters (regexp can be used). -Example to check SSL connections only : --filter-counters='interference-util' +Example: --filter-counters='interference-util' =item B<--warning-*> diff --git a/centreon/common/airespace/snmp/mode/apstatus.pm b/centreon/common/airespace/snmp/mode/apstatus.pm index 0e213e440..fabc1ca98 100644 --- a/centreon/common/airespace/snmp/mode/apstatus.pm +++ b/centreon/common/airespace/snmp/mode/apstatus.pm @@ -238,7 +238,7 @@ Check AP status. =item B<--filter-counters> Only display some counters (regexp can be used). -Example to check SSL connections only : --filter-counters='^total-disassociating|total-associated$' +Example: --filter-counters='^total-disassociating|total-associated$' =item B<--filter-name> diff --git a/centreon/common/airespace/snmp/mode/apusers.pm b/centreon/common/airespace/snmp/mode/apusers.pm index 67931e44c..301becd8b 100644 --- a/centreon/common/airespace/snmp/mode/apusers.pm +++ b/centreon/common/airespace/snmp/mode/apusers.pm @@ -199,7 +199,7 @@ Check total users connected and status on AP. =item B<--filter-counters> Only display some counters (regexp can be used). -Example to check SSL connections only : --filter-counters='^total|total-idle$' +Example: --filter-counters='^total|total-idle$' =item B<--warning-*> diff --git a/network/3com/snmp/mode/cpu.pm b/network/3com/snmp/mode/cpu.pm index 71e7df6fe..af8b72602 100644 --- a/network/3com/snmp/mode/cpu.pm +++ b/network/3com/snmp/mode/cpu.pm @@ -20,48 +20,56 @@ package network::3com::snmp::mode::cpu; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; -use centreon::plugins::values; -my $instance_mode; - -my $maps_counters = { - cpu => { - '000_5s' => { - set => { +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'cpu', type => 1, cb_prefix_output => 'prefix_cpu_output', message_multiple => 'All CPU usages are ok' } + ]; + $self->{maps_counters}->{cpu} = [ + { label => '5s', set => { key_values => [ { name => 'usage_5s' }, { name => 'display' } ], output_template => '%s %% (5sec)', output_error_template => "%s (5sec)", perfdatas => [ { label => 'cpu_5s', value => 'usage_5s_absolute', template => '%d', - unit => '%', min => 0, max => 100 }, + unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display_absolute' }, ], - }, + } }, - '001_1m' => { - set => { + { label => '1m', set => { key_values => [ { name => 'usage_1m' }, { name => 'display' } ], output_template => '%s %% (1m)', output_error_template => "%s (1min)", perfdatas => [ { label => 'cpu_1m', value => 'usage_1m_absolute', template => '%d', - unit => '%', min => 0, max => 100 }, + unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display_absolute' }, ], - }, + } }, - '002_5m' => { - set => { + { label => '5m', set => { key_values => [ { name => 'usage_5m' }, { name => 'display' } ], output_template => '%s %% (5min)', output_error_template => "%s (5min)", perfdatas => [ { label => 'cpu_5m', value => 'usage_5m_absolute', template => '%d', - unit => '%', min => 0, max => 100 }, + unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display_absolute' }, ], - }, + } }, + ]; +} + +sub prefix_cpu_output { + my ($self, %options) = @_; + + if ($self->{multiple} == 1) { + return "CPU '" . $options{instance_value}->{display} . "' Usage "; } -}; + return "CPU Usage "; +} sub new { my ($class, %options) = @_; @@ -72,107 +80,10 @@ sub new { $options{options}->add_options(arguments => { }); - - foreach my $key (('cpu')) { - foreach (keys %{$maps_counters->{$key}}) { - my ($id, $name) = split /_/; - if (!defined($maps_counters->{$key}->{$_}->{threshold}) || $maps_counters->{$key}->{$_}->{threshold} != 0) { - $options{options}->add_options(arguments => { - 'warning-' . $name . ':s' => { name => 'warning-' . $name }, - 'critical-' . $name . ':s' => { name => 'critical-' . $name }, - }); - } - $maps_counters->{$key}->{$_}->{obj} = centreon::plugins::values->new(output => $self->{output}, perfdata => $self->{perfdata}, - label => $name); - $maps_counters->{$key}->{$_}->{obj}->set(%{$maps_counters->{$key}->{$_}->{set}}); - } - } return $self; } -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - - foreach my $key (('cpu')) { - foreach (keys %{$maps_counters->{$key}}) { - $maps_counters->{$key}->{$_}->{obj}->init(option_results => $self->{option_results}); - } - } - $instance_mode = $self; -} - -sub run_instances { - my ($self, %options) = @_; - - my $multiple = 1; - if (scalar(keys %{$self->{cpu}}) == 1) { - $multiple = 0; - } - - if ($multiple == 1) { - $self->{output}->output_add(severity => 'OK', - short_msg => 'All CPU usages are ok'); - } - - foreach my $id (sort keys %{$self->{cpu}}) { - my ($short_msg, $short_msg_append, $long_msg, $long_msg_append) = ('', '', '', ''); - my @exits = (); - foreach (sort keys %{$maps_counters->{cpu}}) { - my $obj = $maps_counters->{cpu}->{$_}->{obj}; - $obj->set(instance => $id); - - my ($value_check) = $obj->execute(values => $self->{cpu}->{$id}); - - if ($value_check != 0) { - $long_msg .= $long_msg_append . $obj->output_error(); - $long_msg_append = ', '; - next; - } - my $exit2 = $obj->threshold_check(); - push @exits, $exit2; - - my $output = $obj->output(); - $long_msg .= $long_msg_append . $output; - $long_msg_append = ', '; - - if (!$self->{output}->is_status(litteral => 1, value => $exit2, compare => 'ok')) { - $short_msg .= $short_msg_append . $output; - $short_msg_append = ', '; - } - - $obj->perfdata(extra_instance => $multiple); - } - - my $prefix = "CPU Usage "; - if ($multiple == 1) { - $prefix = sprintf("CPU '%s' Usage ", $self->{cpu}->{$id}->{display}); - } - $self->{output}->output_add(long_msg => "${prefix}$long_msg"); - my $exit = $self->{output}->get_most_critical(status => [ @exits ]); - if (!$self->{output}->is_status(litteral => 1, value => $exit, compare => 'ok')) { - $self->{output}->output_add(severity => $exit, - short_msg => "${prefix}$short_msg" - ); - } - - if ($multiple == 0) { - $self->{output}->output_add(short_msg => "${prefix}$long_msg"); - } - } -} - -sub run { - my ($self, %options) = @_; - - $self->manage_selection(%options); - $self->run_instances(); - - $self->{output}->display(); - $self->{output}->exit(); -} - my $mapping = { hwCpuCostRate => { oid => '.1.3.6.1.4.1.43.45.1.6.1.1.1.2' }, hwCpuCostRatePer1Min => { oid => '.1.3.6.1.4.1.43.45.1.6.1.1.1.3' }, @@ -214,6 +125,11 @@ Check cpu usages. =over 8 +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='5m' + =item B<--warning-*> Threshold warning. diff --git a/network/3com/snmp/mode/memory.pm b/network/3com/snmp/mode/memory.pm index d0e92264b..c54fdd0d5 100644 --- a/network/3com/snmp/mode/memory.pm +++ b/network/3com/snmp/mode/memory.pm @@ -20,27 +20,10 @@ package network::3com::snmp::mode::memory; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; -use centreon::plugins::values; - -my $instance_mode; - -my $maps_counters = { - mem => { - '000_usage' => { - set => { - key_values => [ { name => 'display' }, { name => 'total' }, { name => 'used' } ], - closure_custom_calc => \&custom_usage_calc, - closure_custom_output => \&custom_usage_output, - closure_custom_perfdata => \&custom_usage_perfdata, - closure_custom_threshold_check => \&custom_usage_threshold, - }, - }, - } -}; sub custom_usage_perfdata { my ($self, %options) = @_; @@ -89,6 +72,34 @@ sub custom_usage_calc { return 0; } +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' } + ]; + + $self->{maps_counters}->{memory} = [ + { label => 'usage', set => { + key_values => [ { name => 'display' }, { name => 'total' }, { name => 'used' } ], + closure_custom_calc => $self->can('custom_usage_calc'), + closure_custom_output => $self->can('custom_usage_output'), + closure_custom_perfdata => $self->can('custom_usage_perfdata'), + closure_custom_threshold_check => $self->can('custom_usage_threshold'), + } + }, + ]; +} + +sub prefix_memory_output { + my ($self, %options) = @_; + + if ($self->{multiple} == 1) { + return "Memory '" . $options{instance_value}->{display} . "' "; + } + return ''; +} + sub new { my ($class, %options) = @_; my $self = $class->SUPER::new(package => __PACKAGE__, %options); @@ -98,107 +109,10 @@ sub new { $options{options}->add_options(arguments => { }); - - foreach my $key (('mem')) { - foreach (keys %{$maps_counters->{$key}}) { - my ($id, $name) = split /_/; - if (!defined($maps_counters->{$key}->{$_}->{threshold}) || $maps_counters->{$key}->{$_}->{threshold} != 0) { - $options{options}->add_options(arguments => { - 'warning-' . $name . ':s' => { name => 'warning-' . $name }, - 'critical-' . $name . ':s' => { name => 'critical-' . $name }, - }); - } - $maps_counters->{$key}->{$_}->{obj} = centreon::plugins::values->new(output => $self->{output}, perfdata => $self->{perfdata}, - label => $name); - $maps_counters->{$key}->{$_}->{obj}->set(%{$maps_counters->{$key}->{$_}->{set}}); - } - } return $self; } -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - - foreach my $key (('mem')) { - foreach (keys %{$maps_counters->{$key}}) { - $maps_counters->{$key}->{$_}->{obj}->init(option_results => $self->{option_results}); - } - } - $instance_mode = $self; -} - -sub run_instances { - my ($self, %options) = @_; - - my $multiple = 1; - if (scalar(keys %{$self->{mem}}) == 1) { - $multiple = 0; - } - - if ($multiple == 1) { - $self->{output}->output_add(severity => 'OK', - short_msg => 'All memory usages are ok'); - } - - foreach my $id (sort keys %{$self->{mem}}) { - my ($short_msg, $short_msg_append, $long_msg, $long_msg_append) = ('', '', '', ''); - my @exits = (); - foreach (sort keys %{$maps_counters->{mem}}) { - my $obj = $maps_counters->{mem}->{$_}->{obj}; - $obj->set(instance => $id); - - my ($value_check) = $obj->execute(values => $self->{mem}->{$id}); - - if ($value_check != 0) { - $long_msg .= $long_msg_append . $obj->output_error(); - $long_msg_append = ', '; - next; - } - my $exit2 = $obj->threshold_check(); - push @exits, $exit2; - - my $output = $obj->output(); - $long_msg .= $long_msg_append . $output; - $long_msg_append = ', '; - - if (!$self->{output}->is_status(litteral => 1, value => $exit2, compare => 'ok')) { - $short_msg .= $short_msg_append . $output; - $short_msg_append = ', '; - } - - $obj->perfdata(extra_instance => $multiple); - } - - my $prefix = ''; - if ($multiple == 1) { - $prefix = sprintf("Memory '%s' ", $self->{mem}->{$id}->{display}); - } - $self->{output}->output_add(long_msg => "${prefix}$long_msg"); - my $exit = $self->{output}->get_most_critical(status => [ @exits ]); - if (!$self->{output}->is_status(litteral => 1, value => $exit, compare => 'ok')) { - $self->{output}->output_add(severity => $exit, - short_msg => "${prefix}$short_msg" - ); - } - - if ($multiple == 0) { - $self->{output}->output_add(short_msg => "${prefix}$long_msg"); - } - } -} - -sub run { - my ($self, %options) = @_; - - $self->manage_selection(%options); - $self->run_instances(); - - $self->{output}->display(); - $self->{output}->exit(); -} - my $mapping = { hwMemSize => { oid => '.1.3.6.1.4.1.43.45.1.6.1.2.1.1.2' }, hwMemFree => { oid => '.1.3.6.1.4.1.43.45.1.6.1.2.1.1.3' }, @@ -210,7 +124,7 @@ sub manage_selection { # a3com-huawei-splat-devm.mib my $oid_hwMemEntry = '.1.3.6.1.4.1.43.45.1.6.1.2.1.1'; my $results = $options{snmp}->get_table(oid => $oid_hwMemEntry, nothing_quit => 1); - $self->{mem} = {}; + $self->{memory} = {}; foreach my $oid (keys %{$results}) { next if ($oid !~ /^$mapping->{hwMemSize}->{oid}\.(.*)/); my $instance = $1; @@ -218,10 +132,10 @@ sub manage_selection { my $total = $result->{hwMemSize}; my $used = $result->{hwMemSize} - $result->{hwMemFree}; - $self->{mem}->{$instance} = { display => $instance, used => $used, total => $total }; + $self->{memory}->{$instance} = { display => $instance, used => $used, total => $total }; } - if (scalar(keys %{$self->{mem}}) <= 0) { + if (scalar(keys %{$self->{memory}}) <= 0) { $self->{output}->add_option_msg(short_msg => "No entry found."); $self->{output}->option_exit(); } diff --git a/network/digi/portserverts/snmp/mode/memory.pm b/network/digi/portserverts/snmp/mode/memory.pm index b04a2172c..d5933ff4a 100644 --- a/network/digi/portserverts/snmp/mode/memory.pm +++ b/network/digi/portserverts/snmp/mode/memory.pm @@ -20,24 +20,10 @@ package network::digi::portserverts::snmp::mode::memory; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; -use centreon::plugins::values; - -my $maps_counters = { - global => { - '000_usage' => { set => { - key_values => [ { name => 'free' }, { name => 'total' } ], - closure_custom_calc => \&custom_usage_calc, - closure_custom_output => \&custom_usage_output, - closure_custom_perfdata => \&custom_usage_perfdata, - closure_custom_threshold_check => \&custom_usage_threshold, - } - }, - }, -}; sub custom_usage_perfdata { my ($self, %options) = @_; @@ -81,6 +67,25 @@ sub custom_usage_calc { return 0; } +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'memory', type => 0 } + ]; + + $self->{maps_counters}->{memory} = [ + { label => 'usage', set => { + key_values => [ { name => 'free' }, { name => 'total' } ], + closure_custom_calc => $self->can('custom_usage_calc'), + closure_custom_output => $self->can('custom_usage_output'), + closure_custom_perfdata => $self->can('custom_usage_perfdata'), + closure_custom_threshold_check => $self->can('custom_usage_threshold'), + } + }, + ]; +} + sub new { my ($class, %options) = @_; my $self = $class->SUPER::new(package => __PACKAGE__, %options); @@ -90,95 +95,18 @@ sub new { $options{options}->add_options(arguments => { }); - - foreach my $key (('global')) { - foreach (keys %{$maps_counters->{$key}}) { - my ($id, $name) = split /_/; - if (!defined($maps_counters->{$key}->{$_}->{threshold}) || $maps_counters->{$key}->{$_}->{threshold} != 0) { - $options{options}->add_options(arguments => { - 'warning-' . $name . ':s' => { name => 'warning-' . $name }, - 'critical-' . $name . ':s' => { name => 'critical-' . $name }, - }); - } - $maps_counters->{$key}->{$_}->{obj} = centreon::plugins::values->new(output => $self->{output}, perfdata => $self->{perfdata}, - label => $name); - $maps_counters->{$key}->{$_}->{obj}->set(%{$maps_counters->{$key}->{$_}->{set}}); - } - } - + return $self; } -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - - foreach my $key (('global')) { - foreach (keys %{$maps_counters->{$key}}) { - $maps_counters->{$key}->{$_}->{obj}->init(option_results => $self->{option_results}); - } - } -} - -sub run { - my ($self, %options) = @_; - $self->{snmp} = $options{snmp}; - - $self->manage_selection(); - - my ($short_msg, $short_msg_append, $long_msg, $long_msg_append) = ('', '', '', ''); - my @exits; - - foreach (sort keys %{$maps_counters->{global}}) { - my $obj = $maps_counters->{global}->{$_}->{obj}; - - use Data::Dumper; - Data::Dumper::Dumper($obj); - $obj->set(instance => 'global'); - - my ($value_check) = $obj->execute(values => $self->{global}); - - if ($value_check != 0) { - $long_msg .= $long_msg_append . $obj->output_error(); - $long_msg_append = ', '; - next; - } - my $exit2 = $obj->threshold_check(); - push @exits, $exit2; - - my $output = $obj->output(); - $long_msg .= $long_msg_append . $output; - $long_msg_append = ', '; - - if (!$self->{output}->is_status(litteral => 1, value => $exit2, compare => 'ok')) { - $short_msg .= $short_msg_append . $output; - $short_msg_append = ', '; - } - - $obj->perfdata(); - } - - my $exit = $self->{output}->get_most_critical(status => [ @exits ]); - if (!$self->{output}->is_status(litteral => 1, value => $exit, compare => 'ok')) { - $self->{output}->output_add(severity => $exit, - short_msg => "$short_msg" - ); - } else { - $self->{output}->output_add(short_msg => "$long_msg"); - } - - $self->{output}->display(); - $self->{output}->exit(); -} - sub manage_selection { my ($self, %options) = @_; my $oid_memoryTotalMemory = '.1.3.6.1.4.1.332.11.5.3.3.22.11.0'; my $oid_memoryAvailable = '.1.3.6.1.4.1.332.11.5.3.3.22.12.0'; - my $result = $self->{snmp}->get_leef(oids => [$oid_memoryTotalMemory, $oid_memoryAvailable], - nothing_quit => 1); - $self->{global} = { free => $result->{$oid_memoryAvailable}, total => $result->{$oid_memoryTotalMemory} }; + my $result = $options{snmp}->get_leef(oids => [$oid_memoryTotalMemory, $oid_memoryAvailable], + nothing_quit => 1); + $self->{memory} = { free => $result->{$oid_memoryAvailable}, total => $result->{$oid_memoryTotalMemory} }; } 1; diff --git a/network/extreme/snmp/mode/cpu.pm b/network/extreme/snmp/mode/cpu.pm index 1ead97521..cd045a7c3 100644 --- a/network/extreme/snmp/mode/cpu.pm +++ b/network/extreme/snmp/mode/cpu.pm @@ -20,71 +20,89 @@ package network::extreme::snmp::mode::cpu; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; -use centreon::plugins::values; -my $maps_counters = { - cpu => { - '000_5secs' => { set => { - key_values => [ { name => 'extremeCpuMonitorSystemUtilization5secs' }, { name => 'num' }, ], - output_template => '5 seconds : %.2f %%', - perfdatas => [ - { label => 'cpu_5secs', value => 'extremeCpuMonitorSystemUtilization5secs_absolute', template => '%.2f', - min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'num_absolute' }, - ], - } - }, - '001_10secs' => { set => { - key_values => [ { name => 'extremeCpuMonitorSystemUtilization10secs' }, { name => 'num' }, ], - output_template => '10 seconds : %.2f %%', - perfdatas => [ - { label => 'cpu_10secs', value => 'extremeCpuMonitorSystemUtilization10secs_absolute', template => '%.2f', - min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'num_absolute' }, - ], - } - }, - '002_30secs' => { set => { - key_values => [ { name => 'extremeCpuMonitorSystemUtilization30secs' }, { name => 'num' }, ], - output_template => '30 seconds : %.2f %%', - perfdatas => [ - { label => 'cpu_30secs', value => 'extremeCpuMonitorSystemUtilization30secs_absolute', template => '%.2f', - min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'num_absolute' }, - ], - } - }, - '003_1min' => { set => { - key_values => [ { name => 'extremeCpuMonitorSystemUtilization1min' }, { name => 'num' }, ], - output_template => '1 minute : %.2f %%', - perfdatas => [ - { label => 'cpu_1min', value => 'extremeCpuMonitorSystemUtilization1min_absolute', template => '%.2f', - min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'num_absolute' }, - ], - } - }, - '004_5min' => { set => { - key_values => [ { name => 'extremeCpuMonitorSystemUtilization5mins' }, { name => 'num' }, ], - output_template => '5 minutes : %.2f %%', - perfdatas => [ - { label => 'cpu_5min', value => 'extremeCpuMonitorSystemUtilization5mins_absolute', template => '%.2f', - min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'num_absolute' }, - ], - } - }, +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0, cb_init => 'skip_global', }, + { name => 'cpu', type => 1, cb_prefix_output => 'prefix_cpu_output', message_multiple => 'All CPU usages are ok' } + ]; + $self->{maps_counters}->{global} = [ + { label => 'total', set => { + key_values => [ { name => 'total' } ], + output_template => 'Total CPU Usage : %.2f %%', + perfdatas => [ + { label => 'cpu_total', value => 'total_absolute', template => '%.2f', min => 0, max => 100, unit => '%' }, + ], + } }, - total => { - '000_total' => { set => { - key_values => [ { name => 'total' } ], - output_template => 'CPU Usage : %.2f %%', - perfdatas => [ - { label => 'cpu_total', value => 'total_absolute', template => '%.2f', min => 0, max => 100, unit => '%' }, - ], - } - }, - } -}; + ]; + + $self->{maps_counters}->{cpu} = [ + { label => '5secs', set => { + key_values => [ { name => 'extremeCpuMonitorSystemUtilization5secs' }, { name => 'num' }, ], + output_template => '5 seconds : %.2f %%', + perfdatas => [ + { label => 'cpu_5secs', value => 'extremeCpuMonitorSystemUtilization5secs_absolute', template => '%.2f', + min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'num_absolute' }, + ], + } + }, + { label => '10secs', set => { + key_values => [ { name => 'extremeCpuMonitorSystemUtilization10secs' }, { name => 'num' }, ], + output_template => '10 seconds : %.2f %%', + perfdatas => [ + { label => 'cpu_10secs', value => 'extremeCpuMonitorSystemUtilization10secs_absolute', template => '%.2f', + min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'num_absolute' }, + ], + } + }, + { label => '30secs', set => { + key_values => [ { name => 'extremeCpuMonitorSystemUtilization30secs' }, { name => 'num' }, ], + output_template => '30 seconds : %.2f %%', + perfdatas => [ + { label => 'cpu_30secs', value => 'extremeCpuMonitorSystemUtilization30secs_absolute', template => '%.2f', + min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'num_absolute' }, + ], + } + }, + { label => '1min', set => { + key_values => [ { name => 'extremeCpuMonitorSystemUtilization1min' }, { name => 'num' }, ], + output_template => '1 minute : %.2f %%', + perfdatas => [ + { label => 'cpu_1min', value => 'extremeCpuMonitorSystemUtilization1min_absolute', template => '%.2f', + min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'num_absolute' }, + ], + } + }, + { label => '5min', set => { + key_values => [ { name => 'extremeCpuMonitorSystemUtilization5mins' }, { name => 'num' }, ], + output_template => '5 minutes : %.2f %%', + perfdatas => [ + { label => 'cpu_5min', value => 'extremeCpuMonitorSystemUtilization5mins_absolute', template => '%.2f', + min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'num_absolute' }, + ], + } + }, + ]; +} + +sub skip_global { + my ($self, %options) = @_; + + scalar(keys %{$self->{cpu}}) > 1 ? return(0) : return(1); +} + +sub prefix_cpu_output { + my ($self, %options) = @_; + + return "CPU '" . $options{instance_value}->{num} . "' "; +} sub new { my ($class, %options) = @_; @@ -94,147 +112,11 @@ sub new { $self->{version} = '1.0'; $options{options}->add_options(arguments => { - }); - - foreach my $key (('cpu', 'total')) { - foreach (keys %{$maps_counters->{$key}}) { - my ($id, $name) = split /_/; - if (!defined($maps_counters->{$key}->{$_}->{threshold}) || $maps_counters->{$key}->{$_}->{threshold} != 0) { - $options{options}->add_options(arguments => { - 'warning-' . $name . ':s' => { name => 'warning-' . $name }, - 'critical-' . $name . ':s' => { name => 'critical-' . $name }, - }); - } - $maps_counters->{$key}->{$_}->{obj} = centreon::plugins::values->new(output => $self->{output}, perfdata => $self->{perfdata}, - label => $name); - $maps_counters->{$key}->{$_}->{obj}->set(%{$maps_counters->{$key}->{$_}->{set}}); - } - } + }); return $self; } -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - - foreach my $key (('cpu', 'total')) { - foreach (keys %{$maps_counters->{$key}}) { - $maps_counters->{$key}->{$_}->{obj}->init(option_results => $self->{option_results}); - } - } -} - -sub check_total { - my ($self, %options) = @_; - - my ($short_msg, $short_msg_append, $long_msg, $long_msg_append) = ('', '', '', ''); - my @exits = (); - foreach (sort keys %{$maps_counters->{total}}) { - my $obj = $maps_counters->{total}->{$_}->{obj}; - $obj->set(instance => 'global'); - - my ($value_check) = $obj->execute(values => $self->{global}); - - if ($value_check != 0) { - $long_msg .= $long_msg_append . $obj->output_error(); - $long_msg_append = ', '; - next; - } - my $exit2 = $obj->threshold_check(); - push @exits, $exit2; - - my $output = $obj->output(); - $long_msg .= $long_msg_append . $output; - $long_msg_append = ', '; - - if (!$self->{output}->is_status(litteral => 1, value => $exit2, compare => 'ok')) { - $short_msg .= $short_msg_append . $output; - $short_msg_append = ', '; - } - - $obj->perfdata(); - } - - my $exit = $self->{output}->get_most_critical(status => [ @exits ]); - if (!$self->{output}->is_status(litteral => 1, value => $exit, compare => 'ok')) { - $self->{output}->output_add(severity => $exit, - short_msg => "Total $short_msg" - ); - } else { - $self->{output}->output_add(short_msg => "Total $long_msg"); - } -} - -sub run { - my ($self, %options) = @_; - # $options{snmp} = snmp object - $self->{snmp} = $options{snmp}; - - $self->manage_selection(); - - my $multiple = 1; - if (scalar(keys %{$self->{cpu}}) == 1) { - $multiple = 0; - } - - if ($multiple == 1) { - $self->check_total(); - } - - #### - # By CPU - if ($multiple == 1) { - $self->{output}->output_add(severity => 'OK', - short_msg => 'All CPU usages are ok'); - } - - foreach my $id (sort keys %{$self->{cpu}}) { - my ($short_msg, $short_msg_append, $long_msg, $long_msg_append) = ('', '', '', ''); - my @exits = (); - foreach (sort keys %{$maps_counters->{cpu}}) { - my $obj = $maps_counters->{cpu}->{$_}->{obj}; - $obj->set(instance => $id); - - my ($value_check) = $obj->execute(values => $self->{cpu}->{$id}); - - if ($value_check != 0) { - $long_msg .= $long_msg_append . $obj->output_error(); - $long_msg_append = ', '; - next; - } - my $exit2 = $obj->threshold_check(); - push @exits, $exit2; - - my $output = $obj->output(); - $long_msg .= $long_msg_append . $output; - $long_msg_append = ', '; - - if (!$self->{output}->is_status(litteral => 1, value => $exit2, compare => 'ok')) { - $short_msg .= $short_msg_append . $output; - $short_msg_append = ', '; - } - - $maps_counters->{cpu}->{$_}->{obj}->perfdata(extra_instance => $multiple); - } - - $self->{output}->output_add(long_msg => "CPU '" . $self->{cpu}->{$id}->{num} . "' Usage $long_msg"); - my $exit = $self->{output}->get_most_critical(status => [ @exits ]); - if (!$self->{output}->is_status(litteral => 1, value => $exit, compare => 'ok')) { - $self->{output}->output_add(severity => $exit, - short_msg => "CPU '" . $self->{cpu}->{$id}->{num} . "' Usage $short_msg" - ); - } - - if ($multiple == 0) { - $self->{output}->output_add(short_msg => "CPU '" . $self->{cpu}->{$id}->{num} . "' Usage $long_msg"); - } - } - - $self->{output}->display(); - $self->{output}->exit(); -} - my $mapping = { extremeCpuMonitorSystemUtilization5secs => { oid => '.1.3.6.1.4.1.1916.1.32.1.4.1.5' }, extremeCpuMonitorSystemUtilization10secs => { oid => '.1.3.6.1.4.1.1916.1.32.1.4.1.6' }, @@ -248,23 +130,22 @@ sub manage_selection { my $oid_extremeCpuMonitorSystemEntry = '.1.3.6.1.4.1.1916.1.32.1.4.1'; my $oid_extremeCpuMonitorTotalUtilization = '.1.3.6.1.4.1.1916.1.32.1.2'; # without .0 - $self->{results} = $self->{snmp}->get_multiple_table(oids => [ + $self->{results} = $options{snmp}->get_multiple_table(oids => [ { oid => $oid_extremeCpuMonitorTotalUtilization }, { oid => $oid_extremeCpuMonitorSystemEntry }, - ], - , nothing_quit => 1); + ], nothing_quit => 1); $self->{cpu} = {}; foreach my $oid (keys %{$self->{results}->{$oid_extremeCpuMonitorSystemEntry}}) { next if ($oid !~ /^$mapping->{extremeCpuMonitorSystemUtilization1min}->{oid}\.(.*)$/); my $instance = $1; - my $result = $self->{snmp}->map_instance(mapping => $mapping, results => $self->{results}->{$oid_extremeCpuMonitorSystemEntry}, instance => $instance); + my $result = $options{snmp}->map_instance(mapping => $mapping, results => $self->{results}->{$oid_extremeCpuMonitorSystemEntry}, instance => $instance); foreach (keys %{$mapping}) { $result->{$_} = undef if (defined($result->{$_}) && $result->{$_} =~ /n\/a/i); } - $self->{cpu}->{$instance} = {num => $instance, %$result}; + $self->{cpu}->{$instance} = { num => $instance, %$result }; } $self->{global} = { total => $self->{results}->{$oid_extremeCpuMonitorTotalUtilization}->{$oid_extremeCpuMonitorTotalUtilization . '.0'} }; @@ -280,6 +161,11 @@ Check CPU usages. =over 8 +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='^(1min|5min)$' + =item B<--warning-*> Threshold warning. diff --git a/network/extreme/snmp/mode/memory.pm b/network/extreme/snmp/mode/memory.pm index 2d7b224f6..5444bb1ef 100644 --- a/network/extreme/snmp/mode/memory.pm +++ b/network/extreme/snmp/mode/memory.pm @@ -20,24 +20,10 @@ package network::extreme::snmp::mode::memory; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; -use centreon::plugins::values; - -my $maps_counters = { - '000_usage' => { set => { - key_values => [ - { name => 'display' }, { name => 'free' }, { name => 'total' }, - ], - closure_custom_calc => \&custom_usage_calc, - closure_custom_output => \&custom_usage_output, - closure_custom_perfdata => \&custom_usage_perfdata, - closure_custom_threshold_check => \&custom_usage_threshold, - } - }, -}; sub custom_usage_perfdata { my ($self, %options) = @_; @@ -86,6 +72,31 @@ sub custom_usage_calc { return 0; } +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' } + ]; + + $self->{maps_counters}->{memory} = [ + { label => 'usage', set => { + key_values => [ { name => 'display' }, { name => 'free' }, { name => 'total' } ], + closure_custom_calc => $self->can('custom_usage_calc'), + closure_custom_output => $self->can('custom_usage_output'), + closure_custom_perfdata => $self->can('custom_usage_perfdata'), + closure_custom_threshold_check => $self->can('custom_usage_threshold'), + } + }, + ]; +} + +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); @@ -94,95 +105,11 @@ sub new { $self->{version} = '1.0'; $options{options}->add_options(arguments => { - }); - - foreach (keys %{$maps_counters}) { - my ($id, $name) = split /_/; - if (!defined($maps_counters->{$_}->{threshold}) || $maps_counters->{$_}->{threshold} != 0) { - $options{options}->add_options(arguments => { - 'warning-' . $name . ':s' => { name => 'warning-' . $name }, - 'critical-' . $name . ':s' => { name => 'critical-' . $name }, - }); - } - $maps_counters->{$_}->{obj} = centreon::plugins::values->new(output => $self->{output}, perfdata => $self->{perfdata}, - label => $name); - $maps_counters->{$_}->{obj}->set(%{$maps_counters->{$_}->{set}}); - } + }); return $self; } -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - - foreach (keys %{$maps_counters}) { - $maps_counters->{$_}->{obj}->init(option_results => $self->{option_results}); - } -} - -sub run { - my ($self, %options) = @_; - # $options{snmp} = snmp object - $self->{snmp} = $options{snmp}; - - $self->manage_selection(); - - my $multiple = 1; - if (scalar(keys %{$self->{memory_selected}}) == 1) { - $multiple = 0; - } - - if ($multiple == 1) { - $self->{output}->output_add(severity => 'OK', - short_msg => 'All memory usages are ok'); - } - - foreach my $id (sort keys %{$self->{memory_selected}}) { - my ($short_msg, $short_msg_append, $long_msg, $long_msg_append) = ('', '', '', ''); - my @exits; - foreach (sort keys %{$maps_counters}) { - $maps_counters->{$_}->{obj}->set(instance => $id); - - my ($value_check) = $maps_counters->{$_}->{obj}->execute(values => $self->{memory_selected}->{$id}); - - if ($value_check != 0) { - $long_msg .= $long_msg_append . $maps_counters->{$_}->{obj}->output_error(); - $long_msg_append = ', '; - next; - } - my $exit2 = $maps_counters->{$_}->{obj}->threshold_check(); - push @exits, $exit2; - - my $output = $maps_counters->{$_}->{obj}->output(); - $long_msg .= $long_msg_append . $output; - $long_msg_append = ', '; - - if (!$self->{output}->is_status(litteral => 1, value => $exit2, compare => 'ok')) { - $short_msg .= $short_msg_append . $output; - $short_msg_append = ', '; - } - - $maps_counters->{$_}->{obj}->perfdata(extra_instance => $multiple); - } - - $self->{output}->output_add(long_msg => "Memory '" . $self->{memory_selected}->{$id}->{display} . "' $long_msg"); - my $exit = $self->{output}->get_most_critical(status => [ @exits ]); - if (!$self->{output}->is_status(litteral => 1, value => $exit, compare => 'ok')) { - $self->{output}->output_add(severity => $exit, - short_msg => "Memory '" . $self->{memory_selected}->{$id}->{display} . "' $short_msg" - ); - } - - if ($multiple == 0) { - $self->{output}->output_add(short_msg => "Memory '" . $self->{memory_selected}->{$id}->{display} . "' $long_msg"); - } - } - - $self->{output}->display(); - $self->{output}->exit(); -} - my $mapping = { extremeMemoryMonitorSystemFree => { oid => '.1.3.6.1.4.1.1916.1.32.2.2.1.3' }, extremeMemoryMonitorSystemTotal => { oid => '.1.3.6.1.4.1.1916.1.32.2.2.1.2' }, @@ -191,17 +118,17 @@ my $mapping = { sub manage_selection { my ($self, %options) = @_; - $self->{memory_selected} = {}; + $self->{memory} = {}; my $oid_extremeMemoryMonitorSystemEntry = '.1.3.6.1.4.1.1916.1.32.2.2.1'; - $self->{results} = $self->{snmp}->get_table(oid => $oid_extremeMemoryMonitorSystemEntry, - nothing_quit => 1); + $self->{results} = $options{snmp}->get_table(oid => $oid_extremeMemoryMonitorSystemEntry, + nothing_quit => 1); foreach my $oid (keys %{$self->{results}}) { next if ($oid !~ /^$mapping->{extremeMemoryMonitorSystemFree}->{oid}\.(\d+)/); my $instance = $1; - my $result = $self->{snmp}->map_instance(mapping => $mapping, results => $self->{results}, instance => $instance); + my $result = $options{snmp}->map_instance(mapping => $mapping, results => $self->{results}, instance => $instance); - $self->{memory_selected}->{$instance} = { display => $instance, - free => $result->{extremeMemoryMonitorSystemFree} * 1024, total => $result->{extremeMemoryMonitorSystemTotal} * 1024}; + $self->{memory}->{$instance} = { display => $instance, + free => $result->{extremeMemoryMonitorSystemFree} * 1024, total => $result->{extremeMemoryMonitorSystemTotal} * 1024}; } } diff --git a/network/redback/snmp/mode/disk.pm b/network/redback/snmp/mode/disk.pm index 0b5ac4982..f1432ad4e 100644 --- a/network/redback/snmp/mode/disk.pm +++ b/network/redback/snmp/mode/disk.pm @@ -20,25 +20,10 @@ package network::redback::snmp::mode::disk; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; -use centreon::plugins::values; - -my $maps_counters = { - '0_usage' => { class => 'centreon::plugins::values', obj => undef, - set => { - key_values => [ - { name => 'display' }, { name => 'total' }, { name => 'used' }, - ], - closure_custom_calc => \&custom_usage_calc, - closure_custom_output => \&custom_usage_output, - closure_custom_perfdata => \&custom_usage_perfdata, - closure_custom_threshold_check => \&custom_usage_threshold, - } - }, -}; sub custom_usage_perfdata { my ($self, %options) = @_; @@ -87,6 +72,31 @@ sub custom_usage_calc { return 0; } +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'disk', type => 1, cb_prefix_output => 'prefix_disk_output', message_multiple => 'All disk usages are ok' } + ]; + + $self->{maps_counters}->{disk} = [ + { label => 'usage', set => { + key_values => [ { name => 'display' }, { name => 'total' }, { name => 'used' } ], + closure_custom_calc => $self->can('custom_usage_calc'), + closure_custom_output => $self->can('custom_usage_output'), + closure_custom_perfdata => $self->can('custom_usage_perfdata'), + closure_custom_threshold_check => $self->can('custom_usage_threshold'), + } + }, + ]; +} + +sub prefix_disk_output { + my ($self, %options) = @_; + + return "Disk '" . $options{instance_value}->{display} . "' "; +} + sub new { my ($class, %options) = @_; my $self = $class->SUPER::new(package => __PACKAGE__, %options); @@ -97,95 +107,10 @@ sub new { { "filter-name:s" => { name => 'filter_name' }, }); - - foreach (keys %{$maps_counters}) { - my ($id, $name) = split /_/; - if (!defined($maps_counters->{$_}->{threshold}) || $maps_counters->{$_}->{threshold} != 0) { - $options{options}->add_options(arguments => { - 'warning-' . $name . ':s' => { name => 'warning-' . $name }, - 'critical-' . $name . ':s' => { name => 'critical-' . $name }, - }); - } - my $class = $maps_counters->{$_}->{class}; - $maps_counters->{$_}->{obj} = $class->new(output => $self->{output}, perfdata => $self->{perfdata}, - label => $name); - $maps_counters->{$_}->{obj}->set(%{$maps_counters->{$_}->{set}}); - } - + return $self; } -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - - foreach (keys %{$maps_counters}) { - $maps_counters->{$_}->{obj}->init(option_results => $self->{option_results}); - } -} - -sub run { - my ($self, %options) = @_; - # $options{snmp} = snmp object - $self->{snmp} = $options{snmp}; - - $self->manage_selection(); - - my $multiple = 1; - if (scalar(keys %{$self->{disk_selected}}) == 1) { - $multiple = 0; - } - - if ($multiple == 1) { - $self->{output}->output_add(severity => 'OK', - short_msg => 'All disk usages are ok'); - } - - foreach my $id (sort keys %{$self->{disk_selected}}) { - my ($short_msg, $short_msg_append, $long_msg, $long_msg_append) = ('', '', '', ''); - my @exits; - foreach (sort keys %{$maps_counters}) { - $maps_counters->{$_}->{obj}->set(instance => $id); - - my ($value_check) = $maps_counters->{$_}->{obj}->execute(values => $self->{disk_selected}->{$id}); - - if ($value_check != 0) { - $long_msg .= $long_msg_append . $maps_counters->{$_}->{obj}->output_error(); - $long_msg_append = ', '; - next; - } - my $exit2 = $maps_counters->{$_}->{obj}->threshold_check(); - push @exits, $exit2; - - my $output = $maps_counters->{$_}->{obj}->output(); - $long_msg .= $long_msg_append . $output; - $long_msg_append = ', '; - - if (!$self->{output}->is_status(litteral => 1, value => $exit2, compare => 'ok')) { - $short_msg .= $short_msg_append . $output; - $short_msg_append = ', '; - } - - $maps_counters->{$_}->{obj}->perfdata(extra_instance => $multiple); - } - - $self->{output}->output_add(long_msg => "Disk '" . $self->{disk_selected}->{$id}->{display} . "' $long_msg"); - my $exit = $self->{output}->get_most_critical(status => [ @exits ]); - if (!$self->{output}->is_status(litteral => 1, value => $exit, compare => 'ok')) { - $self->{output}->output_add(severity => $exit, - short_msg => "Disk '" . $self->{disk_selected}->{$id}->{display} . "' $short_msg" - ); - } - - if ($multiple == 0) { - $self->{output}->output_add(short_msg => "Disk '" . $self->{disk_selected}->{$id}->{display} . "' $long_msg"); - } - } - - $self->{output}->display(); - $self->{output}->exit(); -} - my $mapping = { rbnSRStorageDescr => { oid => '.1.3.6.1.4.1.2352.2.24.1.2.1.1.2' }, rbnSRStorageSize => { oid => '.1.3.6.1.4.1.2352.2.24.1.2.1.1.5' }, # KB @@ -195,14 +120,14 @@ my $mapping = { sub manage_selection { my ($self, %options) = @_; - $self->{disk_selected} = {}; + $self->{disk} = {}; my $oid_rbnSRStorageEntry = '.1.3.6.1.4.1.2352.2.24.1.2.1.1'; - $self->{results} = $self->{snmp}->get_table(oid => $oid_rbnSRStorageEntry, + $self->{results} = $options{snmp}->get_table(oid => $oid_rbnSRStorageEntry, nothing_quit => 1); foreach my $oid (keys %{$self->{results}}) { next if ($oid !~ /^$mapping->{rbnSRStorageSize}->{oid}\.(\d+)/); my $instance = $1; - my $result = $self->{snmp}->map_instance(mapping => $mapping, results => $self->{results}, instance => $instance); + my $result = $options{snmp}->map_instance(mapping => $mapping, results => $self->{results}, instance => $instance); if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && $result->{rbnSRStorageDescr} !~ /$self->{option_results}->{filter_name}/) { $self->{output}->output_add(long_msg => "Skipping '" . $result->{rbnSRStorageDescr} . "': no matching filter."); @@ -213,11 +138,11 @@ sub manage_selection { next; } - $self->{disk_selected}->{$instance} = { display => $result->{rbnSRStorageDescr}, - used => $result->{rbnSRStorageUtilization}, total => $result->{rbnSRStorageSize} * 1024}; + $self->{disk}->{$instance} = { display => $result->{rbnSRStorageDescr}, + used => $result->{rbnSRStorageUtilization}, total => $result->{rbnSRStorageSize} * 1024}; } - if (scalar(keys %{$self->{disk_selected}}) <= 0) { + if (scalar(keys %{$self->{disk}}) <= 0) { $self->{output}->add_option_msg(short_msg => "No entry found."); $self->{output}->option_exit(); } diff --git a/network/redback/snmp/mode/memory.pm b/network/redback/snmp/mode/memory.pm index 700145de6..ed9dd24b3 100644 --- a/network/redback/snmp/mode/memory.pm +++ b/network/redback/snmp/mode/memory.pm @@ -20,25 +20,10 @@ package network::redback::snmp::mode::memory; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; -use centreon::plugins::values; - -my $maps_counters = { - '0_usage' => { class => 'centreon::plugins::values', obj => undef, - set => { - key_values => [ - { name => 'display' }, { name => 'free' }, { name => 'used' }, - ], - closure_custom_calc => \&custom_usage_calc, - closure_custom_output => \&custom_usage_output, - closure_custom_perfdata => \&custom_usage_perfdata, - closure_custom_threshold_check => \&custom_usage_threshold, - } - }, -}; sub custom_usage_perfdata { my ($self, %options) = @_; @@ -87,6 +72,31 @@ sub custom_usage_calc { return 0; } +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' } + ]; + + $self->{maps_counters}->{memory} = [ + { label => 'usage', set => { + key_values => [ { name => 'display' }, { name => 'free' }, { name => 'used' } ], + closure_custom_calc => $self->can('custom_usage_calc'), + closure_custom_output => $self->can('custom_usage_output'), + closure_custom_perfdata => $self->can('custom_usage_perfdata'), + closure_custom_threshold_check => $self->can('custom_usage_threshold'), + } + }, + ]; +} + +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); @@ -96,96 +106,11 @@ sub new { $options{options}->add_options(arguments => { "filter-name:s" => { name => 'filter_name' }, - }); - - foreach (keys %{$maps_counters}) { - my ($id, $name) = split /_/; - if (!defined($maps_counters->{$_}->{threshold}) || $maps_counters->{$_}->{threshold} != 0) { - $options{options}->add_options(arguments => { - 'warning-' . $name . ':s' => { name => 'warning-' . $name }, - 'critical-' . $name . ':s' => { name => 'critical-' . $name }, - }); - } - my $class = $maps_counters->{$_}->{class}; - $maps_counters->{$_}->{obj} = $class->new(output => $self->{output}, perfdata => $self->{perfdata}, - label => $name); - $maps_counters->{$_}->{obj}->set(%{$maps_counters->{$_}->{set}}); - } + }); return $self; } -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - - foreach (keys %{$maps_counters}) { - $maps_counters->{$_}->{obj}->init(option_results => $self->{option_results}); - } -} - -sub run { - my ($self, %options) = @_; - # $options{snmp} = snmp object - $self->{snmp} = $options{snmp}; - - $self->manage_selection(); - - my $multiple = 1; - if (scalar(keys %{$self->{memory_selected}}) == 1) { - $multiple = 0; - } - - if ($multiple == 1) { - $self->{output}->output_add(severity => 'OK', - short_msg => 'All memory usages are ok'); - } - - foreach my $id (sort keys %{$self->{memory_selected}}) { - my ($short_msg, $short_msg_append, $long_msg, $long_msg_append) = ('', '', '', ''); - my @exits; - foreach (sort keys %{$maps_counters}) { - $maps_counters->{$_}->{obj}->set(instance => $id); - - my ($value_check) = $maps_counters->{$_}->{obj}->execute(values => $self->{memory_selected}->{$id}); - - if ($value_check != 0) { - $long_msg .= $long_msg_append . $maps_counters->{$_}->{obj}->output_error(); - $long_msg_append = ', '; - next; - } - my $exit2 = $maps_counters->{$_}->{obj}->threshold_check(); - push @exits, $exit2; - - my $output = $maps_counters->{$_}->{obj}->output(); - $long_msg .= $long_msg_append . $output; - $long_msg_append = ', '; - - if (!$self->{output}->is_status(litteral => 1, value => $exit2, compare => 'ok')) { - $short_msg .= $short_msg_append . $output; - $short_msg_append = ', '; - } - - $maps_counters->{$_}->{obj}->perfdata(extra_instance => $multiple); - } - - $self->{output}->output_add(long_msg => "Memory '" . $self->{memory_selected}->{$id}->{display} . "' $long_msg"); - my $exit = $self->{output}->get_most_critical(status => [ @exits ]); - if (!$self->{output}->is_status(litteral => 1, value => $exit, compare => 'ok')) { - $self->{output}->output_add(severity => $exit, - short_msg => "Memory '" . $self->{memory_selected}->{$id}->{display} . "' $short_msg" - ); - } - - if ($multiple == 0) { - $self->{output}->output_add(short_msg => "Memory '" . $self->{memory_selected}->{$id}->{display} . "' $long_msg"); - } - } - - $self->{output}->display(); - $self->{output}->exit(); -} - my $mapping = { rbnMemoryModule => { oid => '.1.3.6.1.4.1.2352.2.16.1.2.1.2' }, rbnMemoryFreeKBytes => { oid => '.1.3.6.1.4.1.2352.2.16.1.2.1.3' }, @@ -195,25 +120,25 @@ my $mapping = { sub manage_selection { my ($self, %options) = @_; - $self->{memory_selected} = {}; + $self->{memory} = {}; my $oid_rbnMemoryEntry = '.1.3.6.1.4.1.2352.2.16.1.2.1'; - $self->{results} = $self->{snmp}->get_table(oid => $oid_rbnMemoryEntry, - nothing_quit => 1); + $self->{results} = $options{snmp}->get_table(oid => $oid_rbnMemoryEntry, + nothing_quit => 1); foreach my $oid (keys %{$self->{results}}) { next if ($oid !~ /^$mapping->{rbnMemoryModule}->{oid}\.(\d+)/); my $instance = $1; - my $result = $self->{snmp}->map_instance(mapping => $mapping, results => $self->{results}, instance => $instance); + my $result = $options{snmp}->map_instance(mapping => $mapping, results => $self->{results}, instance => $instance); if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && $result->{rbnMemoryModule} !~ /$self->{option_results}->{filter_name}/) { $self->{output}->output_add(long_msg => "Skipping '" . $result->{rbnMemoryModule} . "': no matching filter."); next; } - $self->{memory_selected}->{$instance} = { display => $result->{rbnMemoryModule}, - free => $result->{rbnMemoryFreeKBytes} * 1024, used => $result->{rbnMemoryKBytesInUse} * 1024}; + $self->{memory}->{$instance} = { display => $result->{rbnMemoryModule}, + free => $result->{rbnMemoryFreeKBytes} * 1024, used => $result->{rbnMemoryKBytesInUse} * 1024}; } - if (scalar(keys %{$self->{memory_selected}}) <= 0) { + if (scalar(keys %{$self->{memory}}) <= 0) { $self->{output}->add_option_msg(short_msg => "No entry found."); $self->{output}->option_exit(); }