refacto netapp qtreeusage and add not-kbytes option (#1029)

This commit is contained in:
cgagnaire 2018-07-02 18:21:20 +02:00 committed by Simon Bomm
parent a0d08d1d42
commit c61ce641e4
1 changed files with 56 additions and 108 deletions

View File

@ -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,21 +143,9 @@ sub new {
"filter-vserver:s" => { name => 'filter_vserver' },
"filter-volume:s" => { name => 'filter_volume' },
"filter-qtree:s" => { name => 'filter_qtree' },
"not-kbytes" => { name => 'not_kbytes' },
});
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;
}
@ -152,74 +153,9 @@ 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' },
@ -231,47 +167,55 @@ my $mapping = {
sub manage_selection {
my ($self, %options) = @_;
if ($self->{snmp}->is_snmpv1()) {
my $multi = 1;
$multi = 1024 unless defined($self->{option_results}->{not_kbytes});
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