From e8c24e795d3ce0391f67edb4de5d972c22da2bfe Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Mon, 4 Feb 2019 13:03:57 +0100 Subject: [PATCH] WIP centreon compat with connector 3.0.0 --- .../connector/mode/datastoresnapshot.pm | 6 +- .../vmware/connector/mode/datastoreusage.pm | 262 ++++++++++++++---- 2 files changed, 213 insertions(+), 55 deletions(-) diff --git a/centreon-plugins/apps/vmware/connector/mode/datastoresnapshot.pm b/centreon-plugins/apps/vmware/connector/mode/datastoresnapshot.pm index c9726bb12..8be9acee5 100644 --- a/centreon-plugins/apps/vmware/connector/mode/datastoresnapshot.pm +++ b/centreon-plugins/apps/vmware/connector/mode/datastoresnapshot.pm @@ -114,9 +114,9 @@ sub new { $self->{version} = '1.0'; $options{options}->add_options(arguments => { - "datastore-name:s" => { name => 'datastore_name' }, - "filter" => { name => 'filter' }, - "scope-datacenter:s" => { name => 'scope_datacenter' }, + "datastore-name:s" => { name => 'datastore_name' }, + "filter" => { name => 'filter' }, + "scope-datacenter:s" => { name => 'scope_datacenter' }, "unknown-status:s" => { name => 'unknown_status', default => '%{accessible} !~ /^true|1$/i' }, "warning-status:s" => { name => 'warning_status', default => '' }, "critical-status:s" => { name => 'critical_status', default => '' }, diff --git a/centreon-plugins/apps/vmware/connector/mode/datastoreusage.pm b/centreon-plugins/apps/vmware/connector/mode/datastoreusage.pm index 003cef3c4..e5eebf08a 100644 --- a/centreon-plugins/apps/vmware/connector/mode/datastoreusage.pm +++ b/centreon-plugins/apps/vmware/connector/mode/datastoreusage.pm @@ -20,10 +20,167 @@ package apps::vmware::connector::mode::datastoreusage; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); + +sub custom_status_output { + my ($self, %options) = @_; + + my $msg = 'accessible ' . $self->{result_values}->{accessible}; + return $msg; +} + +sub custom_status_calc { + my ($self, %options) = @_; + + $self->{result_values}->{accessible} = $options{new_datas}->{$self->{instance} . '_accessible'}; + return 0; +} + +sub custom_usage_perfdata { + my ($self, %options) = @_; + + my $label = 'used'; + my $value_perf = $self->{result_values}->{used}; + if (defined($self->{instance_mode}->{option_results}->{free})) { + $label = 'free'; + $value_perf = $self->{result_values}->{free}; + } + my $extra_label = ''; + $extra_label = '_' . $self->{result_values}->{display} if (!defined($options{extra_instance}) || $options{extra_instance} != 0); + my %total_options = (); + if ($self->{instance_mode}->{option_results}->{units} eq '%') { + $total_options{total} = $self->{result_values}->{total}; + $total_options{cast_int} = 1; + } + + $self->{output}->perfdata_add(label => $label . $extra_label, unit => 'B', + value => $value_perf, + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{label}, %total_options), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{label}, %total_options), + min => 0, max => $self->{result_values}->{total}); +} + +sub custom_usage_threshold { + my ($self, %options) = @_; + + my ($exit, $threshold_value); + $threshold_value = $self->{result_values}->{used}; + $threshold_value = $self->{result_values}->{free} if (defined($self->{instance_mode}->{option_results}->{free})); + if ($self->{instance_mode}->{option_results}->{units} eq '%') { + $threshold_value = $self->{result_values}->{prct_used}; + $threshold_value = $self->{result_values}->{prct_free} if (defined($self->{instance_mode}->{option_results}->{free})); + } + $exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-' . $self->{label}, exit_litteral => 'critical' }, { label => 'warning-'. $self->{label}, exit_litteral => 'warning' } ]); + return $exit; +} + +sub custom_usage_output { + my ($self, %options) = @_; + + my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total}); + my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}); + my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}); + my $msg = sprintf("Usage Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", + $total_size_value . " " . $total_size_unit, + $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used}, + $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}); + return $msg; +} + +sub custom_usage_calc { + my ($self, %options) = @_; + + $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; + $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'}; + + if ($self->{result_values}->{total} <= 0) { + $self->{error_msg} = 'size is 0'; + return -20; + } + + $self->{result_values}->{free} = $options{new_datas}->{$self->{instance} . '_free'}; + $self->{result_values}->{used} = $self->{result_values}->{total} - $self->{result_values}->{free}; + $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total}; + $self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used}; + + return 0; +} + +sub custom_provisioned_output { + my ($self, %options) = @_; + + my ($total_uncomitted_value, $total_uncommitted_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total_uncommitted}); + my $msg = sprintf("Provisioned: %s (%.2f%%)", + $total_uncomitted_value . " " . $total_uncommitted_unit, + $self->{result_values}->{prct_uncommitted}); + return $msg; +} + + +sub custom_provisioned_calc { + my ($self, %options) = @_; + + $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; + $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'}; + + if ($self->{result_values}->{total} <= 0) { + return -10; + } + + $self->{result_values}->{total_uncommitted} = + ($self->{result_values}->{total} - $options{new_datas}->{$self->{instance} . '_free'}) + $options{new_datas}->{$self->{instance} . '_uncommitted'}; + $self->{result_values}->{prct_uncommitted} = $self->{result_values}->{total_uncommitted} * 100 / $self->{result_values}->{total}; + + return 0; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'datastore', type => 1, cb_prefix_output => 'prefix_datastore_output', message_multiple => 'All datastores are ok', skipped_code => { -10 => 1 } } + ]; + + $self->{maps_counters}->{datastore} = [ + { label => 'status', threshold => 0, set => { + key_values => [ { name => 'accessible' }, { name => 'display' } ], + closure_custom_calc => $self->can('custom_status_calc'), + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold, + } + }, + { 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'), + } + }, + { label => 'provisioned', set => { + key_values => [ { name => 'display' }, { name => 'uncommitted' }, { name => 'total' }, { name => 'free' } ], + closure_custom_calc => $self->can('custom_provisioned_calc'), + closure_custom_output => $self->can('custom_provisioned_output'), + threshold_use => 'prct_uncommitted', + perfdatas => [ + { label => 'provisioned', value => 'total_uncommitted', template => '%s', unit => 'B', + min => 0, max => 'total', label_extra_instance => 1 }, + ], + } + }, + ]; +} + +sub prefix_datastore_output { + my ($self, %options) = @_; + + return "Datastore '" . $options{instance_value}->{display} . "' "; +} sub new { my ($class, %options) = @_; @@ -31,51 +188,47 @@ sub new { bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "datastore-name:s" => { name => 'datastore_name' }, - "filter" => { name => 'filter' }, - "scope-datacenter:s" => { name => 'scope_datacenter' }, - "disconnect-status:s" => { name => 'disconnect_status', default => 'unknown' }, - "warning:s" => { name => 'warning', }, - "critical:s" => { name => 'critical', }, - "warning-provisioned:s" => { name => 'warning_provisioned', }, - "critical-provisioned:s" => { name => 'critical_provisioned', }, - "units:s" => { name => 'units', default => '%' }, - "free" => { name => 'free' }, - }); + $options{options}->add_options(arguments => { + "datastore-name:s" => { name => 'datastore_name' }, + "filter" => { name => 'filter' }, + "scope-datacenter:s" => { name => 'scope_datacenter' }, + "units:s" => { name => 'units', default => '%' }, + "free" => { name => 'free' }, + "unknown-status:s" => { name => 'unknown_status', default => '%{accessible} !~ /^true|1$/i' }, + "warning-status:s" => { name => 'warning_status', default => '' }, + "critical-status:s" => { name => 'critical_status', default => '' }, + }); + return $self; } sub check_options { my ($self, %options) = @_; - $self->SUPER::init(%options); + $self->SUPER::check_options(%options); - foreach my $label (('warning', 'critical', 'warning_provisioned', 'critical_provisioned')) { - if (($self->{perfdata}->threshold_validate(label => $label, value => $self->{option_results}->{$label})) == 0) { - my ($label_opt) = $label; - $label_opt =~ tr/_/-/; - $self->{output}->add_option_msg(short_msg => "Wrong " . $label_opt . " threshold '" . $self->{option_results}->{$label} . "'."); - $self->{output}->option_exit(); - } - } - if ($self->{output}->is_litteral_status(status => $self->{option_results}->{disconnect_status}) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong disconnect-status status option '" . $self->{option_results}->{disconnect_status} . "'."); - $self->{output}->option_exit(); - } + $self->change_macros(macros => ['unknown_status', 'warning_status', 'critical_status']); if (!defined($self->{option_results}->{units}) || $self->{option_results}->{units} !~ /^(%|B)$/) { $self->{output}->add_option_msg(short_msg => "Wrong units option '" . $self->{option_results}->{units} . "'."); $self->{output}->option_exit(); } } -sub run { +sub manage_selection { my ($self, %options) = @_; - $self->{connector} = $options{custom}; - $self->{connector}->add_params(params => $self->{option_results}, - command => 'datastoreusage'); - $self->{connector}->run(); + $self->{datastore} = {}; + my $response = $options{custom}->execute(params => $self->{option_results}, + command => 'datastoreusage'); + foreach my $ds_id (keys %{$response->{data}}) { + my $ds_name = $response->{data}->{$ds_id}->{name}; + $self->{datastore}->{$ds_name} = { + display => $ds_name, + accessible => $response->{data}->{$ds_id}->{accessible}, + free => $response->{data}->{$ds_id}->{free}, + total => $response->{data}->{$ds_id}->{size}, + uncommitted => $response->{data}->{$ds_id}->{uncommitted}, + }; + } } 1; @@ -100,26 +253,6 @@ Datastore name is a regexp. Search in following datacenter(s) (can be a regexp). -=item B<--disconnect-status> - -Status if datastore disconnected (default: 'unknown'). - -=item B<--warning> - -Threshold warning (depends units option). - -=item B<--critical> - -Threshold critical (depends units option). - -=item B<--warning-provisioned> - -Threshold warning for provisioned storage (percent). - -=item B<--critical-provisioned> - -Threshold critical for provisioned storage (percent). - =item B<--units> Units of thresholds (Default: '%') ('%', 'B'). @@ -128,6 +261,31 @@ Units of thresholds (Default: '%') ('%', 'B'). Thresholds are on free space left. +=item B<--unknown-status> + +Set warning threshold for status (Default: '%{accessible} !~ /^true|1$/i'). +Can used special variables like: %{status} + +=item B<--warning-status> + +Set warning threshold for status (Default: ''). +Can used special variables like: %{status} + +=item B<--critical-status> + +Set critical threshold for status (Default: ''). +Can used special variables like: %{status} + +=item B<--warning-*> + +Threshold warning. +Can be: 'total', 'snapshot'. + +=item B<--critical-*> + +Threshold critical. +Can be: 'total', 'snapshot'. + =back =cut