From 5057741870f43a816c313b73c2dc4b1118dd26b6 Mon Sep 17 00:00:00 2001 From: qgarnier Date: Mon, 13 Sep 2021 14:42:17 +0200 Subject: [PATCH] enh(snmp_standard): mode storage - simple access thresolds (#3101) --- snmp_standard/mode/storage.pm | 127 +++++++++++++++++++++------------- 1 file changed, 80 insertions(+), 47 deletions(-) diff --git a/snmp_standard/mode/storage.pm b/snmp_standard/mode/storage.pm index fabc8dc1c..07441af29 100644 --- a/snmp_standard/mode/storage.pm +++ b/snmp_standard/mode/storage.pm @@ -155,55 +155,38 @@ sub custom_usage_calc { return 0; } +sub custom_access_perfdata { + my ($self, %options) = @_; + + $self->{output}->perfdata_add( + nlabel => $self->{nlabel}, + instances => $self->{result_values}->{display}, + value => $self->{result_values}->{access}, + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-access'), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-access'), + min => 1, max => 2 + ); +} + +sub custom_access_threshold { + my ($self, %options) = @_; + + return $self->{perfdata}->threshold_check( + value => $self->{result_values}->{access}, + threshold => [ + { label => 'critical-access', exit_litteral => 'critical' }, + { label => 'warning-access', exit_litteral => 'warning' } + ] + ); +} + sub custom_access_output { my ($self, %options) = @_; - my $msg = sprintf( - 'Access : %s', + return sprintf( + 'Access: %s', $self->{result_values}->{access} == 1 ? 'readWrite' : 'readOnly' ); - - return $msg; -} - -sub set_counters { - my ($self, %options) = @_; - - $self->{maps_counters_type} = [ - { name => 'global', type => 0 }, - { name => 'storage', type => 1, cb_prefix_output => 'prefix_storage_output', message_multiple => 'All storages are ok', skipped_code => { -10 => 1 } }, - ]; - - $self->{maps_counters}->{global} = [ - { label => 'count', nlabel => 'storage.partitions.count', display_ok => 0, set => { - key_values => [ { name => 'count' } ], - output_template => 'Partitions count : %d', - perfdatas => [ - { label => 'count', value => 'count', template => '%d', min => 0 } - ] - } - }, - ]; - - $self->{maps_counters}->{storage} = [ - { label => 'usage', nlabel => 'storage.space.usage.bytes', set => { - key_values => [ { name => 'display' }, { name => 'used' }, { name => 'size' }, { name => 'allocation_units' } ], - 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') - } - }, - { label => 'access', nlabel => 'storage.access', set => { - key_values => [ { name => 'access' }, { name => 'display' } ], - closure_custom_output => $self->can('custom_access_output'), - perfdatas => [ - { label => 'access', value => 'access', template => '%d', min => 1, max => 2, - label_extra_instance => 1, instance_use => 'display' } - ] - } - } - ]; } sub prefix_storage_output { @@ -218,6 +201,44 @@ sub default_storage_type { return '^(hrStorageFixedDisk|hrStorageNetworkDisk|hrFSBerkeleyFFS)$'; } +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0 }, + { name => 'storage', type => 1, cb_prefix_output => 'prefix_storage_output', message_multiple => 'All storages are ok', skipped_code => { -10 => 1 } } + ]; + + $self->{maps_counters}->{global} = [ + { label => 'count', nlabel => 'storage.partitions.count', display_ok => 0, set => { + key_values => [ { name => 'count' } ], + output_template => 'Partitions count: %d', + perfdatas => [ + { label => 'count', template => '%d', min => 0 } + ] + } + } + ]; + + $self->{maps_counters}->{storage} = [ + { label => 'usage', nlabel => 'storage.space.usage.bytes', set => { + key_values => [ { name => 'display' }, { name => 'used' }, { name => 'size' }, { name => 'allocation_units' } ], + 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') + } + }, + { label => 'access', nlabel => 'storage.access.count', threshold => 0, set => { + key_values => [ { name => 'access' }, { name => 'display' } ], + closure_custom_output => $self->can('custom_access_output'), + closure_custom_threshold_check => $self->can('custom_access_threshold'), + closure_custom_perfdata => $self->can('custom_access_perfdata') + } + } + ]; +} + sub new { my ($class, %options) = @_; my $self = $class->SUPER::new(package => __PACKAGE__, %options); @@ -240,7 +261,9 @@ sub new { 'filter-duplicate' => { name => 'filter_duplicate' }, 'filter-storage-type:s' => { name => 'filter_storage_type', default => $self->default_storage_type() }, 'add-access' => { name => 'add_access' }, - 'counters-overflow' => { name => 'counters_overflow' } + 'counters-overflow' => { name => 'counters_overflow' }, + 'warning-access:s' => { name => 'warning_access' }, + 'critical-access:s' => { name => 'critical_access' } }); $self->{storage_id_selected} = []; @@ -253,6 +276,16 @@ sub check_options { my ($self, %options) = @_; $self->SUPER::check_options(%options); + foreach (('warning', 'critical')) { + next if (!defined($self->{option_results}->{$_ . '_access'})); + $self->{option_results}->{$_ . '_access'} = '@2:2' if ($self->{option_results}->{$_ . '_access'} =~ /readOnly/i); + $self->{option_results}->{$_ . '_access'} = '@1:1' if ($self->{option_results}->{$_ . '_access'} =~ /readWrite/i); + if (($self->{perfdata}->threshold_validate(label => $_ . '-access', value => $self->{option_results}->{$_ . '_access'})) == 0) { + $self->{output}->add_option_msg(short_msg => "Wrong $_-access threshold '" . $self->{option_results}->{$_ . '_access'} . "'."); + $self->{output}->option_exit(); + } + } + if (defined($self->{option_results}->{space_reservation}) && ($self->{option_results}->{space_reservation} < 0 || $self->{option_results}->{space_reservation} > 100)) { $self->{output}->add_option_msg(short_msg => "Space reservation argument must be between 0 and 100 percent."); @@ -513,7 +546,7 @@ sub get_selection { sub get_display_value { my ($self, %options) = @_; my $value = $self->{statefile_cache}->get(name => $self->{option_results}->{oid_display} . "_" . $options{id}); - + if (defined($self->{option_results}->{display_transform_src})) { $self->{option_results}->{display_transform_dst} = '' if (!defined($self->{option_results}->{display_transform_dst})); eval "\$value =~ s{$self->{option_results}->{display_transform_src}}{$self->{option_results}->{display_transform_dst}}"; @@ -544,7 +577,7 @@ Threshold warning. =item B<--critical-access> Threshold critical. -Check if storage is readOnly: --critical-access=@2:2 +Check if storage is readOnly: --critical-access=readOnly =item B<--add-access>