From ed1e4171c078f16e97c281f656610a834c23aed9 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Wed, 6 Feb 2019 10:16:58 +0100 Subject: [PATCH] WIP: centreon connector 3.0.0 vmware compat --- apps/vmware/connector/mode/cpuvm.pm | 6 +- .../vmware/connector/mode/datastorecountvm.pm | 6 +- apps/vmware/connector/mode/datastoreio.pm | 7 +- apps/vmware/connector/mode/datastoreiops.pm | 6 +- .../connector/mode/datastoresnapshot.pm | 6 +- apps/vmware/connector/mode/datastoreusage.pm | 6 +- apps/vmware/connector/mode/datastorevm.pm | 6 +- apps/vmware/connector/mode/devicevm.pm | 8 +- apps/vmware/connector/mode/memoryvm.pm | 6 +- apps/vmware/connector/mode/statusvm.pm | 159 +++++++++++++--- apps/vmware/connector/mode/swaphost.pm | 141 ++++++++++---- apps/vmware/connector/mode/swapvm.pm | 174 +++++++++++++----- apps/vmware/connector/mode/timehost.pm | 136 ++++++++++---- apps/vmware/connector/mode/uptimehost.pm | 143 ++++++++++---- .../connector/mode/vmoperationcluster.pm | 103 +++++++---- 15 files changed, 676 insertions(+), 237 deletions(-) diff --git a/apps/vmware/connector/mode/cpuvm.pm b/apps/vmware/connector/mode/cpuvm.pm index 36a8c1912..04cde98a2 100644 --- a/apps/vmware/connector/mode/cpuvm.pm +++ b/apps/vmware/connector/mode/cpuvm.pm @@ -243,17 +243,17 @@ Search in following host(s) (can be a regexp). =item B<--unknown-status> Set warning threshold for status (Default: '%{connection_state} !~ /^connected$/i or %{power_state} !~ /^poweredOn$/i'). -Can used special variables like: %{status} +Can used special variables like: %{connection_state}, %{power_state} =item B<--warning-status> Set warning threshold for status (Default: ''). -Can used special variables like: %{status} +Can used special variables like: %{connection_state}, %{power_state} =item B<--critical-status> Set critical threshold for status (Default: ''). -Can used special variables like: %{status} +Can used special variables like: %{connection_state}, %{power_state} =item B<--warning-*> diff --git a/apps/vmware/connector/mode/datastorecountvm.pm b/apps/vmware/connector/mode/datastorecountvm.pm index bf3860b60..f1a650087 100644 --- a/apps/vmware/connector/mode/datastorecountvm.pm +++ b/apps/vmware/connector/mode/datastorecountvm.pm @@ -200,17 +200,17 @@ Search in following datacenter(s) (can be a regexp). =item B<--unknown-status> Set warning threshold for status (Default: '%{accessible} !~ /^true|1$/i'). -Can used special variables like: %{status} +Can used special variables like: %{accessible} =item B<--warning-status> Set warning threshold for status (Default: ''). -Can used special variables like: %{status} +Can used special variables like: %{accessible} =item B<--critical-status> Set critical threshold for status (Default: ''). -Can used special variables like: %{status} +Can used special variables like: %{accessible} =item B<--warning-*> diff --git a/apps/vmware/connector/mode/datastoreio.pm b/apps/vmware/connector/mode/datastoreio.pm index 6e1c78972..4d98d2dd2 100644 --- a/apps/vmware/connector/mode/datastoreio.pm +++ b/apps/vmware/connector/mode/datastoreio.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 { @@ -185,17 +184,17 @@ Status if datastore disconnected (default: 'unknown'). =item B<--unknown-status> Set warning threshold for status (Default: '%{accessible} !~ /^true|1$/i'). -Can used special variables like: %{status} +Can used special variables like: %{accessible} =item B<--warning-status> Set warning threshold for status (Default: ''). -Can used special variables like: %{status} +Can used special variables like: %{accessible} =item B<--critical-status> Set critical threshold for status (Default: ''). -Can used special variables like: %{status} +Can used special variables like: %{accessible} =item B<--warning-*> diff --git a/apps/vmware/connector/mode/datastoreiops.pm b/apps/vmware/connector/mode/datastoreiops.pm index a5bf75215..8b8088540 100644 --- a/apps/vmware/connector/mode/datastoreiops.pm +++ b/apps/vmware/connector/mode/datastoreiops.pm @@ -218,17 +218,17 @@ Only display VMs with iops higher value (default: 50). =item B<--unknown-status> Set warning threshold for status (Default: '%{accessible} !~ /^true|1$/i'). -Can used special variables like: %{status} +Can used special variables like: %{accessible} =item B<--warning-status> Set warning threshold for status (Default: ''). -Can used special variables like: %{status} +Can used special variables like: %{accessible} =item B<--critical-status> Set critical threshold for status (Default: ''). -Can used special variables like: %{status} +Can used special variables like: %{accessible} =item B<--warning-*> diff --git a/apps/vmware/connector/mode/datastoresnapshot.pm b/apps/vmware/connector/mode/datastoresnapshot.pm index 8be9acee5..d9d2bb076 100644 --- a/apps/vmware/connector/mode/datastoresnapshot.pm +++ b/apps/vmware/connector/mode/datastoresnapshot.pm @@ -189,17 +189,17 @@ Search in following datacenter(s) (can be a regexp). =item B<--unknown-status> Set warning threshold for status (Default: '%{accessible} !~ /^true|1$/i'). -Can used special variables like: %{status} +Can used special variables like: %{accessible} =item B<--warning-status> Set warning threshold for status (Default: ''). -Can used special variables like: %{status} +Can used special variables like: %{accessible} =item B<--critical-status> Set critical threshold for status (Default: ''). -Can used special variables like: %{status} +Can used special variables like: %{accessible} =item B<--warning-*> diff --git a/apps/vmware/connector/mode/datastoreusage.pm b/apps/vmware/connector/mode/datastoreusage.pm index 8fa539502..ba9d3b02b 100644 --- a/apps/vmware/connector/mode/datastoreusage.pm +++ b/apps/vmware/connector/mode/datastoreusage.pm @@ -263,17 +263,17 @@ 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} +Can used special variables like: %{accessible} =item B<--warning-status> Set warning threshold for status (Default: ''). -Can used special variables like: %{status} +Can used special variables like: %{accessible} =item B<--critical-status> Set critical threshold for status (Default: ''). -Can used special variables like: %{status} +Can used special variables like: %{accessible} =item B<--warning-*> diff --git a/apps/vmware/connector/mode/datastorevm.pm b/apps/vmware/connector/mode/datastorevm.pm index f179eb8ab..17b3fb4f9 100644 --- a/apps/vmware/connector/mode/datastorevm.pm +++ b/apps/vmware/connector/mode/datastorevm.pm @@ -245,17 +245,17 @@ Display virtual machine description. =item B<--unknown-status> Set warning threshold for status (Default: '%{connection_state} !~ /^connected$/i or %{power_state} !~ /^poweredOn$/i'). -Can used special variables like: %{status} +Can used special variables like: %{connection_state}, %{power_state} =item B<--warning-status> Set warning threshold for status (Default: ''). -Can used special variables like: %{status} +Can used special variables like: %{connection_state}, %{power_state} =item B<--critical-status> Set critical threshold for status (Default: ''). -Can used special variables like: %{status} +Can used special variables like: %{connection_state}, %{power_state} =item B<--warning-*> diff --git a/apps/vmware/connector/mode/devicevm.pm b/apps/vmware/connector/mode/devicevm.pm index d46a12971..45e5a369e 100644 --- a/apps/vmware/connector/mode/devicevm.pm +++ b/apps/vmware/connector/mode/devicevm.pm @@ -157,7 +157,7 @@ sub manage_selection { } $self->{global}->{device_connected} += $self->{vm}->{$vm_name}->{device_connected}; - } + } } 1; @@ -206,17 +206,17 @@ Device to check (Required) (Example: --device='VirtualCdrom'). =item B<--unknown-status> Set warning threshold for status (Default: '%{connection_state} !~ /^connected$/i'). -Can used special variables like: %{status} +Can used special variables like: %{connection_state} =item B<--warning-status> Set warning threshold for status (Default: ''). -Can used special variables like: %{status} +Can used special variables like: %{connection_state} =item B<--critical-status> Set critical threshold for status (Default: ''). -Can used special variables like: %{status} +Can used special variables like: %{connection_state} =item B<--warning-*> diff --git a/apps/vmware/connector/mode/memoryvm.pm b/apps/vmware/connector/mode/memoryvm.pm index de2deac84..c8119d8ce 100644 --- a/apps/vmware/connector/mode/memoryvm.pm +++ b/apps/vmware/connector/mode/memoryvm.pm @@ -346,17 +346,17 @@ Display virtual machine description. =item B<--unknown-status> Set warning threshold for status (Default: '%{connection_state} !~ /^connected$/i or %{power_state} !~ /^poweredOn$/i'). -Can used special variables like: %{status} +Can used special variables like: %{connection_state}, %{power_state} =item B<--warning-status> Set warning threshold for status (Default: ''). -Can used special variables like: %{status} +Can used special variables like: %{connection_state}, %{power_state} =item B<--critical-status> Set critical threshold for status (Default: ''). -Can used special variables like: %{status} +Can used special variables like: %{connection_state}, %{power_state} =item B<--units> diff --git a/apps/vmware/connector/mode/statusvm.pm b/apps/vmware/connector/mode/statusvm.pm index 2586106ed..1c60c652b 100644 --- a/apps/vmware/connector/mode/statusvm.pm +++ b/apps/vmware/connector/mode/statusvm.pm @@ -20,10 +20,78 @@ package apps::vmware::connector::mode::statusvm; -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}->{connection_state}; + return $msg; +} + +sub custom_status_calc { + my ($self, %options) = @_; + + $self->{result_values}->{connection_state} = $options{new_datas}->{$self->{instance} . '_connection_state'}; + return 0; +} + +sub custom_overall_output { + my ($self, %options) = @_; + + my $msg = 'overall status is ' . $self->{result_values}->{overall_status}; + return $msg; +} + +sub custom_overall_calc { + my ($self, %options) = @_; + + $self->{result_values}->{overall_status} = $options{new_datas}->{$self->{instance} . '_overall_status'}; + return 0; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'vm', type => 1, cb_prefix_output => 'prefix_vm_output', message_multiple => 'All virtual machines are ok' }, + ]; + + $self->{maps_counters}->{vm} = [ + { label => 'status', threshold => 0, set => { + key_values => [ { name => 'connection_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 => 'overall-status', threshold => 0, set => { + key_values => [ { name => 'overall_status' } ], + closure_custom_calc => $self->can('custom_overall_calc'), + closure_custom_output => $self->can('custom_overall_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold, + } + }, + ]; +} + +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 new { my ($class, %options) = @_; @@ -31,37 +99,52 @@ 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' }, - "display-description" => { name => 'display_description' }, - }); + $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' }, + "unknown-status:s" => { name => 'unknown_status', default => '%{connection_state} !~ /^connected$/i' }, + "warning-status:s" => { name => 'warning_status', default => '' }, + "critical-status:s" => { name => 'critical_status', default => '' }, + "unknown-overall-status:s" => { name => 'unknown_overall_status', default => '%{overall_status} =~ /gray/i' }, + "warning-overall-status:s" => { name => 'warning_overall_status', default => '%{overall_status} =~ /yellow/i' }, + "critical-overall-status:s" => { name => 'critical_overall_status', default => '%{overall_status} =~ /red/i' }, + }); + return $self; } sub check_options { my ($self, %options) = @_; - $self->SUPER::init(%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(); - } + $self->SUPER::check_options(%options); + + $self->change_macros(macros => ['unknown_status', 'warning_status', 'critical_status', + 'unknown_overall_status', 'warning_overall_status', 'critical_overall_status']); } -sub run { +sub manage_selection { my ($self, %options) = @_; - $self->{connector} = $options{custom}; - $self->{connector}->add_params(params => $self->{option_results}, - command => 'statusvm'); - $self->{connector}->run(); + $self->{vm} = {}; + my $response = $options{custom}->execute(params => $self->{option_results}, + command => 'statusvm'); + + foreach my $vm_id (keys %{$response->{data}}) { + my $vm_name = $response->{data}->{$vm_id}->{name}; + $self->{vm}->{$vm_name} = { + display => $vm_name, + connection_state => $response->{data}->{$vm_id}->{connection_state}, + overall_status => $response->{data}->{$vm_id}->{overall_status}, + }; + + 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; @@ -99,9 +182,35 @@ Search in following cluster(s) (can be a regexp). Search in following host(s) (can be a regexp). -=item B<--disconnect-status> +=item B<--unknown-status> -Status if VM disconnected (default: 'unknown'). +Set warning threshold for status (Default: '%{connection_state} !~ /^connected$/i'). +Can used special variables like: %{connection_state} + +=item B<--warning-status> + +Set warning threshold for status (Default: ''). +Can used special variables like: %{connection_state} + +=item B<--critical-status> + +Set critical threshold for status (Default: ''). +Can used special variables like: %{connection_state} + +=item B<--unknown-overall-status> + +Set warning threshold for status (Default: '%{overall_status} =~ /gray/i'). +Can used special variables like: %{overall_status} + +=item B<--warning-overall-status> + +Set warning threshold for status (Default: '%{overall_status} =~ /yellow/i'). +Can used special variables like: %{overall_status} + +=item B<--critical-overall-status> + +Set critical threshold for status (Default: '%{overall_status} =~ /red/i'). +Can used special variables like: %{overall_status} =back diff --git a/apps/vmware/connector/mode/swaphost.pm b/apps/vmware/connector/mode/swaphost.pm index 6b4af84a5..e636ffe28 100644 --- a/apps/vmware/connector/mode/swaphost.pm +++ b/apps/vmware/connector/mode/swaphost.pm @@ -20,10 +20,70 @@ package apps::vmware::connector::mode::swaphost; -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 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 => 'swap-in', set => { + key_values => [ { name => 'swap_in' }, { name => 'display' } ], + output_template => 'Swap In: %s %s/s', + output_change_bytes => 2, + perfdatas => [ + { label => 'swap_in', value => 'swap_in_absolute', template => '%s', + unit => 'B/s', min => 0, label_extra_instance => 1 }, + ], + } + }, + { label => 'swap-out', set => { + key_values => [ { name => 'swap_out' }, { name => 'display' } ], + output_template => 'Swap Out: %s %s/s', + output_change_bytes => 2, + perfdatas => [ + { label => 'swap_out', value => 'swap_out_absolute', template => '%s', + unit => 'B/s', min => 0, label_extra_instance => 1 }, + ], + } + }, + ]; +} + +sub prefix_host_output { + my ($self, %options) = @_; + + return "Host '" . $options{instance_value}->{display} . "' : "; +} sub new { my ($class, %options) = @_; @@ -31,44 +91,42 @@ 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', }, - }); + $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 => '' }, + }); + return $self; } sub check_options { my ($self, %options) = @_; - $self->SUPER::init(%options); + $self->SUPER::check_options(%options); - if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'."); - $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 => 'swaphost'); - $self->{connector}->run(); + $self->{host} = {}; + my $response = $options{custom}->execute(params => $self->{option_results}, + command => 'swaphost'); + + 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}, + swap_in => $response->{data}->{$host_id}->{'mem.swapinRate.average'}, + swap_out => $response->{data}->{$host_id}->{'mem.swapoutRate.average'}, + }; + } } 1; @@ -98,17 +156,30 @@ 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<--warning> +=item B<--warning-status> -Threshold warning in bytes per seconds. +Set warning threshold for status (Default: ''). +Can used special variables like: %{status} -=item B<--critical> +=item B<--critical-status> -Threshold critical in bytes per seconds. +Set critical threshold for status (Default: ''). +Can used special variables like: %{status} + +=item B<--warning-*> + +Threshold warning. +Can be: 'swap-in', 'swap-out'. + +=item B<--critical-*> + +Threshold critical. +Can be: 'swap-in', 'swap-out'. =back diff --git a/apps/vmware/connector/mode/swapvm.pm b/apps/vmware/connector/mode/swapvm.pm index e41441bbe..caa08469c 100644 --- a/apps/vmware/connector/mode/swapvm.pm +++ b/apps/vmware/connector/mode/swapvm.pm @@ -20,10 +20,77 @@ package apps::vmware::connector::mode::swapvm; -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 set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'vm', type => 1, cb_prefix_output => 'prefix_vm_output', message_multiple => 'All virtual machines are ok', skipped_code => { -10 => 1 } } + ]; + + $self->{maps_counters}->{vm} = [ + { 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, + } + }, + { label => 'swap-in', set => { + key_values => [ { name => 'swap_in' }, { name => 'display' } ], + output_template => 'Swap In: %s %s/s', + output_change_bytes => 2, + perfdatas => [ + { label => 'swap_in', value => 'swap_in_absolute', template => '%s', + unit => 'B/s', min => 0, label_extra_instance => 1 }, + ], + } + }, + { label => 'swap-out', set => { + key_values => [ { name => 'swap_out' }, { name => 'display' } ], + output_template => 'Swap Out: %s %s/s', + output_change_bytes => 2, + perfdatas => [ + { label => 'swap_out', value => 'swap_out_absolute', template => '%s', + unit => 'B/s', 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 new { my ($class, %options) = @_; @@ -31,53 +98,51 @@ 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' }, + "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 => 'swapvm'); - $self->{connector}->run(); + $self->{vm} = {}; + my $response = $options{custom}->execute(params => $self->{option_results}, + command => 'swapvm'); + + foreach my $vm_id (keys %{$response->{data}}) { + my $vm_name = $response->{data}->{$vm_id}->{name}; + + $self->{vm}->{$vm_name} = { + display => $vm_name, + connection_state => $response->{data}->{$vm_id}->{connection_state}, + power_state => $response->{data}->{$vm_id}->{power_state}, + swap_in => $response->{data}->{$vm_id}->{'mem.swapinRate.average'}, + swap_out => $response->{data}->{$vm_id}->{'mem.swapoutRate.average'}, + }; + + 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 +180,34 @@ 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 in bytes per seconds. +Set warning threshold for status (Default: '%{connection_state} !~ /^connected$/i or %{power_state} !~ /^poweredOn$/i'). +Can used special variables like: %{connection_state}, %{power_state} -=item B<--critical> +=item B<--warning-status> -Threshold critical in bytes per seconds. +Set warning threshold for status (Default: ''). +Can used special variables like: %{connection_state}, %{power_state} + +=item B<--critical-status> + +Set critical threshold for status (Default: ''). +Can used special variables like: %{connection_state}, %{power_state} + +=item B<--warning-*> + +Threshold warning. +Can be: 'swap-in', 'swap-out'. + +=item B<--critical-*> + +Threshold critical. +Can be: 'swap-in', 'swap-out'. =back diff --git a/apps/vmware/connector/mode/timehost.pm b/apps/vmware/connector/mode/timehost.pm index af2ce131b..03337558b 100644 --- a/apps/vmware/connector/mode/timehost.pm +++ b/apps/vmware/connector/mode/timehost.pm @@ -20,10 +20,68 @@ package apps::vmware::connector::mode::timehost; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; +use Date::Parse; +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_time_output { + my ($self, %options) = @_; + + my $msg = sprintf("time offset %d second(s) : %s", + $self->{result_values}->{offset_absolute}, $self->{result_values}->{date_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 => 'time', set => { + key_values => [ { name => 'offset' }, { name => 'date' }, { name => 'display' } ], + closure_custom_output => $self->can('custom_time_output'), + perfdatas => [ + { label => 'offset', value => 'offset_absolute', template => '%.2f', + unit => 's', label_extra_instance => 1 }, + ], + } + }, + ]; +} + +sub prefix_host_output { + my ($self, %options) = @_; + + return "Host '" . $options{instance_value}->{display} . "' : "; +} sub new { my ($class, %options) = @_; @@ -31,44 +89,49 @@ sub new { bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "esx-hostname:s" => { name => 'esx_hostname' }, - "filter" => { name => 'filter' }, - "disconnect-status:s" => { name => 'disconnect_status', default => 'unknown' }, - "scope-datacenter:s" => { name => 'scope_datacenter' }, - "scope-cluster:s" => { name => 'scope_cluster' }, - "warning-time:s" => { name => 'warning_time' }, - "critical-time:s" => { name => 'critical_time' }, - }); + $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 => '' }, + }); + return $self; } sub check_options { my ($self, %options) = @_; - $self->SUPER::init(%options); + $self->SUPER::check_options(%options); - foreach my $label (('warning_time', 'critical_time')) { - 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(); - } + $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 => 'timehost'); - $self->{connector}->run(); + $self->{host} = {}; + my $response = $options{custom}->execute(params => $self->{option_results}, + command => 'timehost'); + + foreach my $host_id (keys %{$response->{data}}) { + my $host_name = $response->{data}->{$host_id}->{name}; + + my $offset; + if (defined($response->{data}->{$host_id}->{current_time})) { + my $timestamp = Date::Parse::str2time($response->{data}->{$host_id}->{current_time}); + $offset = time() - $timestamp; + } + + $self->{host}->{$host_name} = { + display => $host_name, + state => $response->{data}->{$host_id}->{state}, + offset => $offset, + date => $response->{data}->{$host_id}->{current_time}, + }; + } } 1; @@ -98,9 +161,20 @@ 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 VM disconnected (default: 'unknown'). +Set warning threshold for status (Default: '%{status} !~ /^connected$/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-time> diff --git a/apps/vmware/connector/mode/uptimehost.pm b/apps/vmware/connector/mode/uptimehost.pm index 6a112b4ac..e0d38d82b 100644 --- a/apps/vmware/connector/mode/uptimehost.pm +++ b/apps/vmware/connector/mode/uptimehost.pm @@ -20,54 +20,118 @@ package apps::vmware::connector::mode::uptimehost; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; +use Date::Parse; +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_time_output { + my ($self, %options) = @_; + + my $msg = sprintf("Uptime: %s day(s)", + int($self->{result_values}->{offset_absolute} / 60 / 60 / 24)); + 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 => 'time', set => { + key_values => [ { name => 'offset' }, { name => 'date' }, { name => 'display' } ], + closure_custom_output => $self->can('custom_time_output'), + perfdatas => [ + { label => 'uptime', value => 'offset_absolute', template => '%.2f', + unit => 's', label_extra_instance => 1 }, + ], + } + }, + ]; +} + +sub prefix_host_output { + my ($self, %options) = @_; + + return "Host '" . $options{instance_value}->{display} . "' : "; +} + sub new { my ($class, %options) = @_; my $self = $class->SUPER::new(package => __PACKAGE__, %options); 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', }, - }); + $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 => '' }, + }); + return $self; } sub check_options { my ($self, %options) = @_; - $self->SUPER::init(%options); + $self->SUPER::check_options(%options); - if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'."); - $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 => 'uptimehost'); - $self->{connector}->run(); + $self->{host} = {}; + my $response = $options{custom}->execute(params => $self->{option_results}, + command => 'uptimehost'); + + foreach my $host_id (keys %{$response->{data}}) { + my $host_name = $response->{data}->{$host_id}->{name}; + + my $offset; + if (defined($response->{data}->{$host_id}->{boot_time})) { + my $timestamp = Date::Parse::str2time($response->{data}->{$host_id}->{boot_time}); + $offset = time() - $timestamp; + } + + $self->{host}->{$host_name} = { + display => $host_name, + state => $response->{data}->{$host_id}->{state}, + offset => $offset, + date => $response->{data}->{$host_id}->{boot_time}, + }; + } } 1; @@ -76,7 +140,7 @@ __END__ =head1 MODE -Check ESX swap rate usage. +Check ESX uptime. =over 8 @@ -97,15 +161,26 @@ 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<--warning> +=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-time> Threshold warning in seconds. -=item B<--critical> +=item B<--critical-time> Threshold critical in seconds. diff --git a/apps/vmware/connector/mode/vmoperationcluster.pm b/apps/vmware/connector/mode/vmoperationcluster.pm index 62fc44e51..6e1c5c149 100644 --- a/apps/vmware/connector/mode/vmoperationcluster.pm +++ b/apps/vmware/connector/mode/vmoperationcluster.pm @@ -20,54 +20,91 @@ package apps::vmware::connector::mode::vmoperationcluster; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; +use Digest::MD5 qw(md5_hex); + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'cluster', type => 1, cb_prefix_output => 'prefix_cluster_output', message_multiple => 'All virtual machine operations are ok', skipped_code => { -10 => 1 } } + ]; + + $self->{maps_counters}->{cluster} = [ + { label => 'svmotion', set => { + key_values => [ { name => 'numSVMotion', diff => 1 }, { name => 'display' } ], + output_template => 'SVMotion %s', + perfdatas => [ + { label => 'svmotion', value => 'numSVMotion_absolute', template => '%s', + label_extra_instance => 1 }, + ], + } + }, + { label => 'vmotion', set => { + key_values => [ { name => 'numVMotion', diff => 1 }, { name => 'display' } ], + output_template => 'VMotion %s', + perfdatas => [ + { label => 'vmotion', value => 'numVMotion_absolute', template => '%s', + label_extra_instance => 1 }, + ], + } + }, + { label => 'clone', set => { + key_values => [ { name => 'numClone', diff => 1 }, { name => 'display' } ], + output_template => 'Clone %s', + perfdatas => [ + { label => 'clone', value => 'numClone_absolute', template => '%s', + label_extra_instance => 1 }, + ], + } + }, + ]; +} + +sub prefix_cluster_output { + my ($self, %options) = @_; + + return "Cluster '" . $options{instance_value}->{display} . "' vm operations: "; +} sub new { my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); + my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1); bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "cluster:s" => { name => 'cluster' }, - "filter" => { name => 'filter' }, - "scope-datacenter:s" => { name => 'scope_datacenter' }, - "warning-svmotion:s" => { name => 'warning_svmotion' }, - "critical-svmotion:s" => { name => 'critical_svmotion' }, - "warning-vmotion:s" => { name => 'warning_vmotion' }, - "critical-vmotion:s" => { name => 'critical_vmotion' }, - "warning-clone:s" => { name => 'warning_clone' }, - "critical-clone:s" => { name => 'critical_clone' }, - }); + $options{options}->add_options(arguments => { + "cluster:s" => { name => 'cluster' }, + "filter" => { name => 'filter' }, + "scope-datacenter:s" => { name => 'scope_datacenter' }, + }); + return $self; } -sub check_options { +sub manage_selection { my ($self, %options) = @_; - $self->SUPER::init(%options); + + $self->{cluster} = {}; + my $response = $options{custom}->execute(params => $self->{option_results}, + command => 'vmoperationcluster'); - foreach my $label (('warning_svmotion', 'critical_svmotion', 'warning_vmotion', 'critical_vmotion', - 'warning_clone', 'critical_clone')) { - 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(); - } + foreach my $cluster_id (keys %{$response->{data}}) { + my $cluster_name = $response->{data}->{$cluster_id}->{name}; + $self->{cluster}->{$cluster_name} = { + display => $cluster_name, + numVMotion => $response->{data}->{$cluster_id}->{'vmop.numVMotion.latest'}, + numClone => $response->{data}->{$cluster_id}->{'vmop.numClone.latest'}, + numSVMotion => $response->{data}->{$cluster_id}->{'vmop.numSVMotion.latest'}, + }; } -} - -sub run { - my ($self, %options) = @_; - $self->{connector} = $options{custom}; - - $self->{connector}->add_params(params => $self->{option_results}, - command => 'vmoperationcluster'); - $self->{connector}->run(); + + $self->{cache_name} = "cache_vmware_" . $options{custom}->get_id() . '_' . $self->{mode} . '_' . + (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' . + (defined($self->{option_results}->{cluster}) ? md5_hex($self->{option_results}->{cluster}) : md5_hex('all')); } 1;