From c61ce641e4bdca65155d4885d1cd23ccb802da1d Mon Sep 17 00:00:00 2001 From: cgagnaire Date: Mon, 2 Jul 2018 18:21:20 +0200 Subject: [PATCH] refacto netapp qtreeusage and add not-kbytes option (#1029) --- .../storage/netapp/snmp/mode/qtreeusage.pm | 164 ++++++------------ 1 file changed, 56 insertions(+), 108 deletions(-) diff --git a/centreon-plugins/storage/netapp/snmp/mode/qtreeusage.pm b/centreon-plugins/storage/netapp/snmp/mode/qtreeusage.pm index 30b9badd4..079c4d2b6 100644 --- a/centreon-plugins/storage/netapp/snmp/mode/qtreeusage.pm +++ b/centreon-plugins/storage/netapp/snmp/mode/qtreeusage.pm @@ -20,25 +20,32 @@ package storage::netapp::snmp::mode::qtreeusage; -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 => 'name' }, { name => 'used' }, { 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, - } - }, -}; my $instance_mode; +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'qtree', type => 1, cb_prefix_output => 'prefix_qtree_output', message_multiple => 'All qtree usages are ok.' }, + ]; + + $self->{maps_counters}->{qtree} = [ + { label => 'usage', set => { + key_values => [ { name => 'name' }, { name => 'used' }, { 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 custom_usage_perfdata { my ($self, %options) = @_; @@ -117,6 +124,12 @@ sub custom_usage_calc { return 0; } +sub prefix_qtree_output { + my ($self, %options) = @_; + + return "Qtree '" . $options{instance_value}->{name} . "' "; +} + sub new { my ($class, %options) = @_; my $self = $class->SUPER::new(package => __PACKAGE__, %options); @@ -130,96 +143,19 @@ sub new { "filter-vserver:s" => { name => 'filter_vserver' }, "filter-volume:s" => { name => 'filter_volume' }, "filter-qtree:s" => { name => 'filter_qtree' }, - }); - - 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}}); - } - + "not-kbytes" => { name => 'not_kbytes' }, + }); + 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}); - } - + $instance_mode = $self; } -sub run { - my ($self, %options) = @_; - $self->{snmp} = $options{snmp}; - - $self->manage_selection(); - - my $multiple = 1; - if (scalar(keys %{$self->{qtree_selected}}) == 1) { - $multiple = 0; - } - - if ($multiple == 1) { - $self->{output}->output_add(severity => 'OK', - short_msg => 'All qtree usages are ok'); - } - - foreach my $id (sort keys %{$self->{qtree_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->{qtree_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 => "Qtree '" . $self->{qtree_selected}->{$id}->{name} . "' $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 => "Qtree '" . $self->{qtree_selected}->{$id}->{name} . "' $short_msg" - ); - } - - if ($multiple == 0) { - $self->{output}->output_add(short_msg => "Qtree '" . $self->{qtree_selected}->{$id}->{name} . "' $long_msg"); - } - } - - $self->{output}->display(); - $self->{output}->exit(); -} - my $mapping = { qrV2Tree => { oid => '.1.3.6.1.4.1.789.1.4.6.1.14' }, qrV264KBytesUsed => { oid => '.1.3.6.1.4.1.789.1.4.6.1.25' }, @@ -230,48 +166,56 @@ my $mapping = { sub manage_selection { my ($self, %options) = @_; + + my $multi = 1; + $multi = 1024 unless defined($self->{option_results}->{not_kbytes}); - if ($self->{snmp}->is_snmpv1()) { + if ($options{snmp}->is_snmpv1()) { $self->{output}->add_option_msg(short_msg => "Need to use SNMP v2c or v3."); $self->{output}->option_exit(); } - my $results = $self->{snmp}->get_multiple_table(oids => [ + my $results = $options{snmp}->get_multiple_table(oids => [ { oid => $mapping->{qrV2Tree}->{oid} }, { oid => $mapping->{qrV264KBytesUsed}->{oid} }, { oid => $mapping->{qrV264KBytesLimit}->{oid} }, { oid => $mapping->{qrV2VolumeName}->{oid} }, { oid => $mapping->{qrV2Vserver}->{oid} }, ], return_type => 1, nothing_quit => 1); - $self->{qtree_selected} = {}; + $self->{qtree} = {}; foreach my $oid (keys %{$results}) { next if ($oid !~ /^$mapping->{qrV2Tree}->{oid}\.(.*)/); my $instance = $1; - my $result = $self->{snmp}->map_instance(mapping => $mapping, results => $results, instance => $instance); - + my $result = $options{snmp}->map_instance(mapping => $mapping, results => $results, instance => $instance); + if (defined($self->{option_results}->{filter_vserver}) && $self->{option_results}->{filter_vserver} ne '' && - $result->{qrV2Vserver} !~ /$self->{option_results}->{filter_vserver}/) { - $self->{output}->output_add(long_msg => "Skipping '" . $result->{qrV2Vserver} . "': no matching vserver name."); + defined($result->{qrV2Vserver}) && $result->{qrV2Vserver} ne '' && $result->{qrV2Vserver} !~ /$self->{option_results}->{filter_vserver}/) { + $self->{output}->output_add(long_msg => "Skipping '" . $result->{qrV2Vserver} . "': no matching vserver name.", debug => 1); next; } if (defined($self->{option_results}->{filter_volume}) && $self->{option_results}->{filter_volume} ne '' && - $result->{qrV2VolumeName} !~ /$self->{option_results}->{filter_volume}/) { - $self->{output}->output_add(long_msg => "Skipping '" . $result->{qrV2VolumeName} . "': no matching volume name."); + defined($result->{qrV2VolumeName}) && $result->{qrV2VolumeName} ne '' && $result->{qrV2VolumeName} !~ /$self->{option_results}->{filter_volume}/) { + $self->{output}->output_add(long_msg => "Skipping '" . $result->{qrV2VolumeName} . "': no matching volume name.", debug => 1); next; } if (defined($self->{option_results}->{filter_qtree}) && $self->{option_results}->{filter_qtree} ne '' && $result->{qrV2Tree} !~ /$self->{option_results}->{filter_qtree}/) { - $self->{output}->output_add(long_msg => "Skipping '" . $result->{qrV2Tree} . "': no matching qtree name."); + $self->{output}->output_add(long_msg => "Skipping '" . $result->{qrV2Tree} . "': no matching qtree name.", debug => 1); + next; + } + if (!defined($result->{qrV264KBytesUsed}) && !defined($result->{qrV264KBytesLimit})) { + $self->{output}->output_add(long_msg => "Skipping qtree '" . $result->{qrV2Tree} . "': no used or total values.", debug => 1); next; } - + my $name = ''; $name = $result->{qrV2Vserver} . '/' if (defined($result->{qrV2Vserver}) && $result->{qrV2Vserver} ne ''); - $name .= $result->{qrV2VolumeName} . '/' . $result->{qrV2Tree}; + $name .= $result->{qrV2VolumeName} . '/' if (defined($result->{qrV2VolumeName}) && $result->{qrV2VolumeName} ne ''); + $name .= $result->{qrV2Tree}; - $self->{qtree_selected}->{$instance} = { name => $name, used => $result->{qrV264KBytesUsed} * 1024, total => $result->{qrV264KBytesLimit} * 1024 }; + $self->{qtree}->{$instance} = { name => $name, used => $result->{qrV264KBytesUsed} * $multi, total => $result->{qrV264KBytesLimit} * $multi }; } - if (scalar(keys %{$self->{qtree_selected}}) <= 0) { + if (scalar(keys %{$self->{qtree}}) <= 0) { $self->{output}->add_option_msg(short_msg => "No entry found."); $self->{output}->option_exit(); } @@ -315,6 +259,10 @@ Filter by volume name (can be a regexp). Filter by qtree name (can be a regexp). +=item B<--not-kbytes> + +If qrV264KBytesUsed and qrV264KBytesLimit OIDs are not really KBytes. + =back =cut