WIP centreon compat vmware 3.0.0 connector

This commit is contained in:
garnier-quentin 2019-02-01 15:11:12 +01:00
parent 208e93d3fb
commit 72b2ccbcc7
4 changed files with 325 additions and 125 deletions

View File

@ -20,10 +20,109 @@
package apps::vmware::connector::mode::countvmhost;
use base qw(centreon::plugins::mode);
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 {
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 => 'global', type => 0, skipped_code => { -10 => 1 } },
{ name => 'host', type => 1, cb_prefix_output => 'prefix_host_output', message_multiple => 'All ESX Hosts are ok' },
];
$self->{maps_counters}->{global} = [
{ label => 'total-on', set => {
key_values => [ { name => 'poweredon' }, { name => 'total' } ],
output_template => '%s VM(s) poweredon',
perfdatas => [
{ label => 'poweredon', value => 'poweredon_absolute', template => '%s',
min => 0, max => 'total_absolute' },
],
}
},
{ label => 'total-off', set => {
key_values => [ { name => 'poweredoff' }, { name => 'total' } ],
output_template => '%s VM(s) poweredoff',
perfdatas => [
{ label => 'poweredoff', value => 'poweredoff_absolute', template => '%s',
min => 0, max => 'total_absolute' },
],
}
},
{ label => 'total-suspended', set => {
key_values => [ { name => 'suspended' }, { name => 'total' } ],
output_template => '%s VM(s) suspended',
perfdatas => [
{ label => 'suspended', value => 'suspended_absolute', template => '%s',
min => 0, max => 'total_absolute' },
],
}
},
];
$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 => 'on', set => {
key_values => [ { name => 'poweredon' }, { name => 'total' } ],
output_template => '%s VM(s) poweredon',
perfdatas => [
{ label => 'poweredon', value => 'poweredon_absolute', template => '%s',
min => 0, max => 'total_absolute', label_extra_instance => 1 },
],
}
},
{ label => 'off', set => {
key_values => [ { name => 'poweredoff' }, { name => 'total' } ],
output_template => '%s VM(s) poweredoff',
perfdatas => [
{ label => 'poweredoff', value => 'poweredoff_absolute', template => '%s',
min => 0, max => 'total_absolute', label_extra_instance => 1 },
],
}
},
{ label => 'suspended', set => {
key_values => [ { name => 'suspended' }, { name => 'total' } ],
output_template => '%s VM(s) suspended',
perfdatas => [
{ label => 'suspended', value => 'suspended_absolute', template => '%s',
min => 0, max => 'total_absolute', label_extra_instance => 1 },
],
}
},
];
}
sub prefix_host_output {
my ($self, %options) = @_;
return "Host '" . $options{instance_value}->{display} . "' : ";
}
sub new {
my ($class, %options) = @_;
@ -31,49 +130,48 @@ sub new {
bless $self, $class;
$self->{version} = '1.0';
$options{options}->add_options(arguments =>
{
$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-on:s" => { name => 'warning_on' },
"critical-on:s" => { name => 'critical_on' },
"warning-off:s" => { name => 'warning_off' },
"critical-off:s" => { name => 'critical_off' },
"warning-suspended:s" => { name => 'warning_suspended' },
"critical-suspended:s" => { name => 'critical_suspended' },
"unknown-status:s" => { name => 'unknown_status', default => '%{status} !~ /^connected$/' },
"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_on', 'critical_on', 'warning_off', 'critical_off', 'warning_suspended', 'critical_suspended')) {
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},
$self->{global} = { poweredon => 0, poweredoff => 0, suspended => 0 };
$self->{host} = {};
my $response = $options{custom}->execute(params => $self->{option_results},
command => 'countvmhost');
$self->{connector}->run();
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},
poweredon => $response->{data}->{$host_id}->{poweredon},
poweredoff => $response->{data}->{$host_id}->{poweredoff},
suspended => $response->{data}->{$host_id}->{suspended},
total => $response->{data}->{$host_id}->{poweredon} + $response->{data}->{$host_id}->{poweredoff} + $response->{data}->{$host_id}->{suspended},
};
$self->{global}->{poweredon} += $response->{data}->{$host_id}->{poweredon} if (defined($response->{data}->{$host_id}->{poweredon}));
$self->{global}->{poweredoff} += $response->{data}->{$host_id}->{poweredoff} if (defined($response->{data}->{$host_id}->{poweredoff}));
$self->{global}->{suspended} += $response->{data}->{$host_id}->{suspended} if (defined($response->{data}->{$host_id}->{suspended}));
}
}
1;
@ -103,33 +201,32 @@ 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$/').
Can used special variables like: %{status}
=item B<--warning-on>
=item B<--warning-status>
Threshold warning for 'poweredOn' vms.
Set warning threshold for status (Default: '').
Can used special variables like: %{status}
=item B<--critical-on>
=item B<--critical-status>
Threshold critical for 'poweredOn' vms.
Set critical threshold for status (Default: '').
Can used special variables like: %{status}
=item B<--warning-off>
=item B<--warning-*>
Threshold warning for 'poweredOff' vms.
Threshold warning.
Can be: 'total-on', 'total-off', 'total-suspended',
'on', 'off', 'suspended'.
=item B<--critical-off>
=item B<--critical-*>
Threshold critical for 'poweredOff' vms.
=item B<--warning-suspended>
Threshold warning for 'suspended' vms.
=item B<--critical-suspended>
Threshold critical for 'suspended' vms.
Threshold critical.
Can be: 'total-on', 'total-off', 'total-suspended',
'on', 'off', 'suspended'.
=back

View File

@ -47,9 +47,9 @@ sub set_counters {
$self->{maps_counters_type} = [
{ name => 'host', type => 3, cb_prefix_output => 'prefix_host_output', cb_long_output => 'host_long_output', message_multiple => 'All hosts are ok',
group => [
{ name => 'global', type => 0, skipped_code => { -11 => 1 } },
{ name => 'global_cpu', cb_prefix_output => 'prefix_global_cpu_output', type => 0, skipped_code => { -11 => 1 } },
{ name => 'cpu', display_long => 0, cb_prefix_output => 'prefix_cpu_output', message_multiple => 'All CPUs are ok', type => 1, skipped_code => { -11 => 1 } },
{ name => 'global', type => 0, skipped_code => { -10 => 1 } },
{ name => 'global_cpu', cb_prefix_output => 'prefix_global_cpu_output', type => 0, skipped_code => { -10 => 1 } },
{ name => 'cpu', display_long => 0, cb_prefix_output => 'prefix_cpu_output', message_multiple => 'All CPUs are ok', type => 1, skipped_code => { -10 => 1 } },
]
}
];
@ -148,7 +148,6 @@ sub check_options {
$self->change_macros(macros => ['unknown_status', 'warning_status', 'critical_status']);
}
sub manage_selection {
my ($self, %options) = @_;

View File

@ -20,10 +20,122 @@
package apps::vmware::connector::mode::cpuvm;
use base qw(centreon::plugins::mode);
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 {
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 => 3, cb_prefix_output => 'prefix_vm_output', cb_long_output => 'vm_long_output', message_multiple => 'All virtual machines are ok',
group => [
{ name => 'global', type => 0, skipped_code => { -10 => 1 } },
{ name => 'global_cpu', cb_prefix_output => 'prefix_global_cpu_output', type => 0, skipped_code => { -10 => 1 } },
{ name => 'cpu', display_long => 0, cb_prefix_output => 'prefix_cpu_output', message_multiple => 'All CPUs are ok', type => 1, 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_cpu} = [
{ label => 'total-cpu', set => {
key_values => [ { name => 'cpu_average' } ],
output_template => '%s %%',
perfdatas => [
{ label => 'cpu_total', value => 'cpu_average_absolute', template => '%s', unit => '%',
min => 0, max => 100, label_extra_instance => 1 },
],
}
},
{ label => 'total-cpu-mhz', set => {
key_values => [ { name => 'cpu_average_mhz' } ],
output_template => '%s MHz',
perfdatas => [
{ label => 'cpu_total_MHz', value => 'cpu_average_mhz_absolute', template => '%s', unit => 'MHz',
min => 0, label_extra_instance => 1 },
],
}
},
{ label => 'cpu-ready', set => {
key_values => [ { name => 'cpu_ready' } ],
output_template => 'ready %s %%',
perfdatas => [
{ label => 'cpu_ready', value => 'cpu_ready_absolute', template => '%s', unit => '%',
min => 0, max => 100, label_extra_instance => 1 },
],
}
},
];
$self->{maps_counters}->{cpu} = [
{ label => 'cpu', set => {
key_values => [ { name => 'cpu_usage' }, { name => 'display' } ],
output_template => 'usage : %s',
perfdatas => [
{ label => 'cpu', value => 'cpu_usage_absolute', template => '%s', unit => '%',
min => 0, max => 100, label_extra_instance => 1 },
],
}
},
];
}
sub prefix_vm_output {
my ($self, %options) = @_;
return "Virtual machine '" . $options{instance_value}->{display} . "' : ";
}
sub vm_long_output {
my ($self, %options) = @_;
if (!defined($options{instance_value}->{display})) {
use Data::Dumper;
print Data::Dumper::Dumper($options{instance_value});
}
return "checking virtual machine '" . $options{instance_value}->{display} . "'";
}
sub prefix_global_cpu_output {
my ($self, %options) = @_;
return "cpu total average : ";
}
sub prefix_cpu_output {
my ($self, %options) = @_;
return "cpu '" . $options{instance_value}->{display} . "' ";
}
sub new {
my ($class, %options) = @_;
@ -31,57 +143,60 @@ sub new {
bless $self, $class;
$self->{version} = '1.0';
$options{options}->add_options(arguments =>
{
$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-usagemhz:s" => { name => 'warning_usagemhz' },
"critical-usagemhz:s" => { name => 'critical_usagemhz' },
"warning-usage:s" => { name => 'warning_usage' },
"critical-usage:s" => { name => 'critical_usage' },
"warning-ready:s" => { name => 'warning_ready' },
"critical-ready:s" => { name => 'critical_ready' },
"filter-description:s" => { name => 'filter_description' },
"unknown-status:s" => { name => 'unknown_status', default => '%{connection_state} !~ /^connected$/ and %{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_usagemhz', 'critical_usagemhz', 'warning_usage', 'critical_usage', 'warning_ready', 'critical_ready')) {
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},
$self->{vm} = {};
my $response = $options{custom}->execute(params => $self->{option_results},
command => 'cpuvm');
$self->{connector}->run();
foreach my $vm_id (keys %{$response->{data}}) {
my $vm_name = $response->{data}->{$vm_id}->{name};
$self->{vm}->{$vm_name} = { display => $vm_name,
cpu => {},
global => {
connection_state => $response->{data}->{$vm_id}->{connection_state},
power_state => $response->{data}->{$vm_id}->{power_state},
},
global_cpu => {
cpu_average => $response->{data}->{$vm_id}->{'cpu.usage.average'},
cpu_average_mhz => $response->{data}->{$vm_id}->{'cpu.usagemhz.average'},
cpu_ready => $response->{data}->{$vm_id}->{'cpu_ready'},
},
};
if (defined($self->{options_results}->{display_description})) {
$self->{vm}->{$vm_name}->{config_annotation} = $response->{data}->{$vm_id}->{'config.annotation'};
}
foreach my $cpu_id (sort keys %{$response->{data}->{$vm_id}->{cpu}}) {
$self->{vm}->{$vm_name}->{cpu}->{$cpu_id} = { display => $cpu_id, cpu_usage => $response->{data}->{$vm_id}->{cpu}->{$cpu_id} };
}
}
}
1;
@ -119,41 +234,30 @@ 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$/ and %{power_state} !~ /^poweredOn$/i').
Can used special variables like: %{status}
=item B<--nopoweredon-status>
=item B<--warning-status>
Status if VM is not poweredOn (default: 'unknown').
Set warning threshold for status (Default: '').
Can used special variables like: %{status}
=item B<--display-description>
=item B<--critical-status>
Display virtual machine description.
Set critical threshold for status (Default: '').
Can used special variables like: %{status}
=item B<--warning-usagemhz>
=item B<--warning-*>
Threshold warning in mhz.
Threshold warning.
Can be: 'total-cpu', 'total-cpu-mhz', 'cpu'.
=item B<--critical-usagemhz>
=item B<--critical-*>
Threshold critical in mhz.
=item B<--warning-usage>
Threshold warning in percent.
=item B<--critical-usage>
Threshold critical in percent.
=item B<--warning-ready>
Threshold warning in percent.
=item B<--critical-ready>
Threshold critical in percent.
Threshold critical.
Can be: 'total-cpu', 'total-cpu-mhz', 'cpu'.
=back

View File

@ -437,7 +437,7 @@ sub run_multiple_instances {
);
}
if ($self->{multiple} == 0) {
if ($self->{multiple} == 0 && $multiple_parent == 0) {
$self->run_multiple_prefix_output(severity => 'ok', short_msg => "${prefix_output}${long_msg}${suffix_output}");
}
}