diff --git a/apps/vmware/connector/mode/cpuvm.pm b/apps/vmware/connector/mode/cpuvm.pm index bd2e620f8..36a8c1912 100644 --- a/apps/vmware/connector/mode/cpuvm.pm +++ b/apps/vmware/connector/mode/cpuvm.pm @@ -24,7 +24,6 @@ use base qw(centreon::plugins::templates::counter); use strict; use warnings; -use centreon::plugins::misc; use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); sub custom_status_output { diff --git a/apps/vmware/connector/mode/datastoreusage.pm b/apps/vmware/connector/mode/datastoreusage.pm index e5eebf08a..8fa539502 100644 --- a/apps/vmware/connector/mode/datastoreusage.pm +++ b/apps/vmware/connector/mode/datastoreusage.pm @@ -120,7 +120,6 @@ sub custom_provisioned_output { return $msg; } - sub custom_provisioned_calc { my ($self, %options) = @_; diff --git a/apps/vmware/connector/mode/maintenancehost.pm b/apps/vmware/connector/mode/maintenancehost.pm index f0bfe963a..cd0754868 100644 --- a/apps/vmware/connector/mode/maintenancehost.pm +++ b/apps/vmware/connector/mode/maintenancehost.pm @@ -20,10 +20,72 @@ package apps::vmware::connector::mode::maintenancehost; -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 = 'status ' . $self->{result_values}->{status}; + return $msg; +} + +sub custom_status_calc { + my ($self, %options) = @_; + + $self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_state'}; + return 0; +} + +sub custom_maintenance_output { + my ($self, %options) = @_; + + my $msg = 'maintenance mode is ' . $self->{result_values}->{maintenance}; + return $msg; +} + +sub custom_maintenance_calc { + my ($self, %options) = @_; + + $self->{result_values}->{maintenance} = $options{new_datas}->{$self->{instance} . '_maintenance'}; + return 0; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'host', type => 1, cb_prefix_output => 'prefix_host_output', message_multiple => 'All ESX Hosts are ok' }, + ]; + + $self->{maps_counters}->{host} = [ + { label => 'status', threshold => 0, set => { + key_values => [ { name => 'state' } ], + 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 => 'maintenance-status', threshold => 0, set => { + key_values => [ { name => 'maintenance' } ], + closure_custom_calc => $self->can('custom_maintenance_calc'), + closure_custom_output => $self->can('custom_maintenance_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold, + } + }, + ]; +} + +sub prefix_host_output { + my ($self, %options) = @_; + + return "Host '" . $options{instance_value}->{display} . "' : "; +} sub new { my ($class, %options) = @_; @@ -31,40 +93,44 @@ sub new { bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "esx-hostname:s" => { name => 'esx_hostname' }, - "filter" => { name => 'filter' }, - "scope-datacenter:s" => { name => 'scope_datacenter' }, - "scope-cluster:s" => { name => 'scope_cluster' }, - "disconnect-status:s" => { name => 'disconnect_status', default => 'unknown' }, - "maintenance-alert:s" => { name => 'maintenance_alert', default => '^(?!(false))' }, - "maintenance-status:s" => { name => 'maintenance_status', default => 'critical' }, - }); + $options{options}->add_options(arguments => { + "esx-hostname:s" => { name => 'esx_hostname' }, + "filter" => { name => 'filter' }, + "scope-datacenter:s" => { name => 'scope_datacenter' }, + "scope-cluster:s" => { name => 'scope_cluster' }, + "unknown-status:s" => { name => 'unknown_status', default => '%{status} !~ /^connected$/i' }, + "warning-status:s" => { name => 'warning_status', default => '' }, + "critical-status:s" => { name => 'critical_status', default => '' }, + "warning-maintenance-status:s" => { name => 'warning_maintenance_status', default => '' }, + "critical-maintenance-status:s" => { name => 'critical_maintenance_status', default => '%{maintenance} !~ /false/' }, + }); + return $self; } sub check_options { my ($self, %options) = @_; - $self->SUPER::init(%options); + $self->SUPER::check_options(%options); - 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(); - } - if ($self->{output}->is_litteral_status(status => $self->{option_results}->{maintenance_status}) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong maintenance-status status option '" . $self->{option_results}->{maintenance_status} . "'."); - $self->{output}->option_exit(); - } + $self->change_macros(macros => ['unknown_status', 'warning_status', 'critical_status', + 'warning_maintenance_status', 'critical_maintenance_status']); } -sub run { +sub manage_selection { my ($self, %options) = @_; - $self->{connector} = $options{custom}; - $self->{connector}->add_params(params => $self->{option_results}, - command => 'maintenancehost'); - $self->{connector}->run(); + $self->{host} = {}; + my $response = $options{custom}->execute(params => $self->{option_results}, + command => 'maintenancehost'); + + foreach my $host_id (keys %{$response->{data}}) { + my $host_name = $response->{data}->{$host_id}->{name}; + $self->{host}->{$host_name} = { + display => $host_name, + state => $response->{data}->{$host_id}->{state}, + maintenance => $response->{data}->{$host_id}->{inMaintenanceMode}, + }; + } } 1; @@ -94,17 +160,31 @@ Search in following datacenter(s) (can be a regexp). Search in following cluster(s) (can be a regexp). -=item B<--disconnect-status> +=item B<--unknown-status> -Status if ESX host disconnected (default: 'unknown'). +Set warning threshold for status (Default: '%{status} !~ /^connected$/i'). +Can used special variables like: %{status} -=item B<--maintenance-alert> +=item B<--warning-status> -Alert if maintenance mode value matches (default: '^(?!(false))'). +Set warning threshold for status (Default: ''). +Can used special variables like: %{status} -=item B<--maintenance-status> +=item B<--critical-status> + +Set critical threshold for status (Default: ''). +Can used special variables like: %{status} + +=item B<--warning-maintenance-status> + +Set warning threshold for status (Default: ''). +Can used special variables like: %{maintenance} + +=item B<--critical-maintenance-status> + +Set critical threshold for status (Default: '%{maintenance} !~ /false/'). +Can used special variables like: %{maintenance} -Maintenance alert status (default: 'critical'). =back diff --git a/apps/vmware/connector/mode/memoryhost.pm b/apps/vmware/connector/mode/memoryhost.pm index 14360eace..cdde3c709 100644 --- a/apps/vmware/connector/mode/memoryhost.pm +++ b/apps/vmware/connector/mode/memoryhost.pm @@ -20,10 +20,162 @@ package apps::vmware::connector::mode::memoryhost; -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 = 'status : ' . $self->{result_values}->{status}; + return $msg; +} + +sub custom_status_calc { + my ($self, %options) = @_; + + $self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_state'}; + 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("Memory 'consumed' 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}->{used} = $options{new_datas}->{$self->{instance} . '_consumed'}; + $self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used}; + $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_overhead_output { + my ($self, %options) = @_; + + my ($overhead_value, $overhead_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{overhead_absolute}); + my $msg = sprintf("Memory Overhead: %s", + $overhead_value . " " . $overhead_unit); + return $msg; +} + +sub custom_memstate_output { + my ($self, %options) = @_; + + my $msg = 'Memory state is ' . $self->{result_values}->{mem_state_str_absolute}; + return $msg; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'host', type => 1, cb_prefix_output => 'prefix_host_output', message_multiple => 'All hosts are ok', skipped_code => { -10 => 1 } } + ]; + + $self->{maps_counters}->{host} = [ + { label => 'status', threshold => 0, set => { + key_values => [ { name => 'state' }, { 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 => 'consumed-memory', set => { + key_values => [ { name => 'display' }, { name => 'consumed' }, { 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 => 'overhead-memory', set => { + key_values => [ { name => 'overhead' }, { name => 'display' } ], + closure_custom_output => $self->can('custom_overhead_output'), + perfdatas => [ + { label => 'overhead', value => 'overhead_absolute', template => '%s', unit => 'B', + min => 0, label_extra_instance => 1 }, + ], + } + }, + { label => 'state-memory', set => { + key_values => [ { name => 'mem_state' }, { name => 'mem_state_str' }, { name => 'display' } ], + closure_custom_output => $self->can('custom_memstate_output'), + perfdatas => [ + { label => 'state', value => 'mem_state_absolute', template => '%s', + min => 0, max => 3, label_extra_instance => 1 }, + ], + } + }, + ]; +} + +sub prefix_host_output { + my ($self, %options) = @_; + + return "Host '" . $options{instance_value}->{display} . "' : "; +} sub new { my ($class, %options) = @_; @@ -31,47 +183,47 @@ sub new { bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "esx-hostname:s" => { name => 'esx_hostname' }, - "filter" => { name => 'filter' }, - "scope-datacenter:s" => { name => 'scope_datacenter' }, - "scope-cluster:s" => { name => 'scope_cluster' }, - "disconnect-status:s" => { name => 'disconnect_status', default => 'unknown' }, - "warning:s" => { name => 'warning' }, - "critical:s" => { name => 'critical' }, - "warning-state:s" => { name => 'warning_state' }, - "critical-state:s" => { name => 'critical_state' }, - "no-memory-state" => { name => 'no_memory_state' }, - }); + $options{options}->add_options(arguments => { + "esx-hostname:s" => { name => 'esx_hostname' }, + "filter" => { name => 'filter' }, + "scope-datacenter:s" => { name => 'scope_datacenter' }, + "scope-cluster:s" => { name => 'scope_cluster' }, + "units:s" => { name => 'units', default => '%' }, + "free" => { name => 'free' }, + "unknown-status:s" => { name => 'unknown_status', default => '%{status} !~ /^connected$/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_state', 'critical_state')) { - 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']); } -sub run { +sub manage_selection { my ($self, %options) = @_; - $self->{connector} = $options{custom}; - $self->{connector}->add_params(params => $self->{option_results}, - command => 'memhost'); - $self->{connector}->run(); + $self->{host} = {}; + my $response = $options{custom}->execute(params => $self->{option_results}, + command => 'memhost'); + + foreach my $host_id (keys %{$response->{data}}) { + my $host_name = $response->{data}->{$host_id}->{name}; + $self->{host}->{$host_name} = { + display => $host_name, + state => $response->{data}->{$host_id}->{state}, + consumed => $response->{data}->{$host_id}->{'mem.consumed.average'}, + overhead => $response->{data}->{$host_id}->{'mem.overhead.average'}, + mem_state => $response->{data}->{$host_id}->{mem_state}, + mem_state_str => $response->{data}->{$host_id}->{mem_state_str}, + total => $response->{data}->{$host_id}->{mem_size}, + }; + } } 1; @@ -101,23 +253,50 @@ Search in following datacenter(s) (can be a regexp). Search in following cluster(s) (can be a regexp). -=item B<--disconnect-status> +=item B<--units> -Status if ESX host disconnected (default: 'unknown'). +Units of thresholds (Default: '%') ('%', 'B'). -=item B<--warning> +=item B<--free> -Threshold warning in percent. +Thresholds are on free space left. -=item B<--critical> +=item B<--unknown-status> -Threshold critical in percent. +Set warning threshold for status (Default: '%{status} !~ /^connected$/i'). +Can used special variables like: %{status} -=item B<--warning-state> +=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-consumed-memory> + +Threshold warning (can use unit option). + +=item B<--critical-consumed-memory> + +Threshold critical (can use unit option). + +=item B<--warning-overhead-memory> + +Threshold overhead. + +=item B<--critical-overhead-memory> + +Threshold critical. + +=item B<--warning-state-memory> Threshold warning. For state != 'high': --warning-state=0 -=item B<--critical-state> +=item B<--critical-state-memory> Threshold critical. For state != 'high': --warning-state=0 diff --git a/apps/vmware/connector/mode/memoryvm.pm b/apps/vmware/connector/mode/memoryvm.pm index 995c2e5f9..de2deac84 100644 --- a/apps/vmware/connector/mode/memoryvm.pm +++ b/apps/vmware/connector/mode/memoryvm.pm @@ -20,10 +20,227 @@ package apps::vmware::connector::mode::memoryvm; -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 = '[connection state ' . $self->{result_values}->{connection_state} . '][power state ' . $self->{result_values}->{power_state} . ']'; + return $msg; +} + +sub custom_status_calc { + my ($self, %options) = @_; + + $self->{result_values}->{connection_state} = $options{new_datas}->{$self->{instance} . '_connection_state'}; + $self->{result_values}->{power_state} = $options{new_datas}->{$self->{instance} . '_power_state'}; + return 0; +} + +sub custom_usage_perfdata { + my ($self, %options) = @_; + + my $label = $self->{label} . '_used'; + my $value_perf = $self->{result_values}->{used}; + if (defined($self->{instance_mode}->{option_results}->{free})) { + $label = $self->{label} . '_free'; + $value_perf = $self->{result_values}->{free}; + } + my $extra_label = ''; + $extra_label = '_' . $self->{instance} 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("Memory %s Usage Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", + $self->{result_values}->{label_ref}, + $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'}; + $self->{result_values}->{label_ref} = $options{extra_options}->{label_ref}; + + if ($self->{result_values}->{total} <= 0) { + $self->{error_msg} = 'size is 0'; + return -20; + } + + $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_' . $self->{result_values}->{label_ref}}; + $self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used}; + $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_overhead_output { + my ($self, %options) = @_; + + my ($value, $unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{overhead_absolute}); + my $msg = sprintf("Memory overhead: %s %s", $value, $unit); + return $msg; +} + +sub custom_ballooning_output { + my ($self, %options) = @_; + + my ($value, $unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{vmmemctl_absolute}); + my $msg = sprintf("Memory ballooning: %s %s", $value, $unit); + return $msg; +} + +sub custom_shared_output { + my ($self, %options) = @_; + + my ($value, $unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{shared_absolute}); + my $msg = sprintf("Memory shared: %s %s", $value, $unit); + return $msg; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'vm', type => 3, cb_prefix_output => 'prefix_vm_output', cb_long_output => 'vm_long_output', indent_long_output => ' ', message_multiple => 'All virtual machines are ok', + group => [ + { name => 'global', type => 0, skipped_code => { -10 => 1 } }, + { name => 'global_consumed', type => 0, skipped_code => { -10 => 1 } }, + { name => 'global_active', type => 0, skipped_code => { -10 => 1 } }, + { name => 'global_overhead', type => 0, skipped_code => { -10 => 1 } }, + { name => 'global_vmmemctl', type => 0, skipped_code => { -10 => 1 } }, + { name => 'global_shared', type => 0, skipped_code => { -10 => 1 } }, + ] + } + ]; + + $self->{maps_counters}->{global} = [ + { label => 'status', threshold => 0, set => { + key_values => [ { name => 'connection_state' }, { name => 'power_state' } ], + 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, + } + }, + ]; + + $self->{maps_counters}->{global_consumed} = [ + { label => 'consumed', set => { + key_values => [ { name => 'consumed' }, { name => 'total' } ], + closure_custom_calc => $self->can('custom_usage_calc'), closure_custom_calc_extra_options => { label_ref => 'consumed' }, + 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'), + } + }, + ]; + $self->{maps_counters}->{global_active} = [ + { label => 'active', set => { + key_values => [ { name => 'active' }, { name => 'total' } ], + closure_custom_calc => $self->can('custom_usage_calc'), closure_custom_calc_extra_options => { label_ref => 'active' }, + 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'), + } + }, + ]; + $self->{maps_counters}->{global_overhead} = [ + { label => 'overhead', set => { + key_values => [ { name => 'overhead' } ], + closure_custom_output => $self->can('custom_overhead_output'), + perfdatas => [ + { label => 'overhead', value => 'overhead_absolute', template => '%s', unit => 'B', + min => 0, label_extra_instance => 1 }, + ], + } + }, + ]; + $self->{maps_counters}->{global_vmmemctl} = [ + { label => 'ballooning', set => { + key_values => [ { name => 'vmmemctl' } ], + closure_custom_output => $self->can('custom_ballooning_output'), + perfdatas => [ + { label => 'ballooning', value => 'vmmemctl_absolute', template => '%s', unit => 'B', + min => 0, label_extra_instance => 1 }, + ], + } + }, + ]; + $self->{maps_counters}->{global_shared} = [ + { label => 'shared', set => { + key_values => [ { name => 'shared' } ], + closure_custom_output => $self->can('custom_shared_output'), + perfdatas => [ + { label => 'shared', value => 'shared_absolute', template => '%s', unit => 'B', + min => 0, label_extra_instance => 1 }, + ], + } + }, + ]; +} + +sub prefix_vm_output { + my ($self, %options) = @_; + + my $msg = "Virtual machine '" . $options{instance_value}->{display} . "'"; + if (defined($options{instance_value}->{config_annotation})) { + $msg .= ' [annotation: ' . $options{instance_value}->{config_annotation} . ']'; + } + $msg .= ' : '; + + return $msg; +} + +sub vm_long_output { + my ($self, %options) = @_; + + my $msg = "checking virtual machine '" . $options{instance_value}->{display} . "'"; + if (defined($options{instance_value}->{config_annotation})) { + $msg .= ' [annotation: ' . $options{instance_value}->{config_annotation} . ']'; + } + + return $msg; +} sub new { my ($class, %options) = @_; @@ -31,53 +248,60 @@ sub new { bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "vm-hostname:s" => { name => 'vm_hostname' }, - "filter" => { name => 'filter' }, - "scope-datacenter:s" => { name => 'scope_datacenter' }, - "scope-cluster:s" => { name => 'scope_cluster' }, - "scope-host:s" => { name => 'scope_host' }, - "filter-description:s" => { name => 'filter_description' }, - "disconnect-status:s" => { name => 'disconnect_status', default => 'unknown' }, - "nopoweredon-status:s" => { name => 'nopoweredon_status', default => 'unknown' }, - "display-description" => { name => 'display_description' }, - "warning:s" => { name => 'warning' }, - "critical:s" => { name => 'critical' }, - }); + $options{options}->add_options(arguments => { + "vm-hostname:s" => { name => 'vm_hostname' }, + "filter" => { name => 'filter' }, + "scope-datacenter:s" => { name => 'scope_datacenter' }, + "scope-cluster:s" => { name => 'scope_cluster' }, + "scope-host:s" => { name => 'scope_host' }, + "filter-description:s" => { name => 'filter_description' }, + "display-description" => { name => 'display_description' }, + "units:s" => { name => 'units', default => '%' }, + "free" => { name => 'free' }, + "unknown-status:s" => { name => 'unknown_status', default => '%{connection_state} !~ /^connected$/i or %{power_state} !~ /^poweredOn$/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')) { - 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 option '" . $self->{option_results}->{disconnect_status} . "'."); - $self->{output}->option_exit(); - } - if ($self->{output}->is_litteral_status(status => $self->{option_results}->{nopoweredon_status}) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong nopoweredon-status option '" . $self->{option_results}->{nopoweredon_status} . "'."); - $self->{output}->option_exit(); - } + $self->change_macros(macros => ['unknown_status', 'warning_status', 'critical_status']); } -sub run { +sub manage_selection { my ($self, %options) = @_; - $self->{connector} = $options{custom}; - $self->{connector}->add_params(params => $self->{option_results}, - command => 'memvm'); - $self->{connector}->run(); + $self->{vm} = {}; + my $response = $options{custom}->execute(params => $self->{option_results}, + command => 'memvm'); + + foreach my $vm_id (keys %{$response->{data}}) { + my $vm_name = $response->{data}->{$vm_id}->{name}; + + $self->{vm}->{$vm_name} = { display => $vm_name, + global => { + connection_state => $response->{data}->{$vm_id}->{connection_state}, + power_state => $response->{data}->{$vm_id}->{power_state}, + }, + }; + + foreach (('consumed', 'active', 'overhead', 'vmmemctl', 'shared')) { + next if (!defined($response->{data}->{$vm_id}->{'mem.' . $_ . '.average'})); + $self->{vm}->{$vm_name}->{'global_' . $_} = { + $_ => $response->{data}->{$vm_id}->{'mem.' . $_ . '.average'}, + total => $response->{data}->{$vm_id}->{memory_size} + }; + } + + if (defined($self->{option_results}->{display_description})) { + $self->{vm}->{$vm_name}->{config_annotation} = $options{custom}->strip_cr(value => $response->{data}->{$vm_id}->{'config.annotation'}); + } + } } 1; @@ -115,25 +339,42 @@ Search in following cluster(s) (can be a regexp). Search in following host(s) (can be a regexp). -=item B<--disconnect-status> - -Status if VM disconnected (default: 'unknown'). - -=item B<--nopoweredon-status> - -Status if VM is not poweredOn (default: 'unknown'). - =item B<--display-description> Display virtual machine description. -=item B<--warning> +=item B<--unknown-status> -Threshold warning for consumed memory (in percent). +Set warning threshold for status (Default: '%{connection_state} !~ /^connected$/i or %{power_state} !~ /^poweredOn$/i'). +Can used special variables like: %{status} -=item B<--critical> +=item B<--warning-status> -Threshold critical for consumed memory (in percent). +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<--units> + +Units of thresholds (Default: '%') ('%', 'B'). + +=item B<--free> + +Thresholds are on free space left. + +=item B<--warning-*> + +Threshold warning. +Can be: 'consumed', 'active', 'overhead', 'ballooning', 'shared'. + +=item B<--critical-*> + +Threshold critical. +Can be: 'consumed', 'active', 'overhead', 'ballooning', 'shared'. =back diff --git a/centreon/plugins/templates/counter.pm b/centreon/plugins/templates/counter.pm index 57d58e556..461af6170 100644 --- a/centreon/plugins/templates/counter.pm +++ b/centreon/plugins/templates/counter.pm @@ -494,6 +494,7 @@ sub run_multiple { if (defined($options{config}->{indent_long_output})); foreach my $group (@{$options{config}->{group}}) { + next if (!defined($self->{$options{config}->{name}}->{$instance}->{$group->{name}})); $self->{$group->{name}} = $self->{$options{config}->{name}}->{$instance}->{$group->{name}}; if ($group->{type} == 1) { @@ -502,7 +503,7 @@ sub run_multiple { $self->run_global(config => $group, multiple_parent => $multiple, called_multiple => 1, force_instance => $instance, indent_long_output => $indent_long_output); } } - } + } } sub run {