From fbe1d897e9a42b6f47a29b2dcf33b396f636dbe0 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Wed, 30 Jan 2019 18:29:00 +0100 Subject: [PATCH] WIP: new version centreon-vmware 3.0.0 --- .../src/centreon/script/centreon_vmware.pm | 2 +- .../vmware/src/centreon/vmware/cmdcpuvm.pm | 2 +- .../src/centreon/vmware/cmddatastorevm.pm | 2 +- .../vmware/src/centreon/vmware/cmddevicevm.pm | 85 ++++--------------- .../src/centreon/vmware/cmddiscovery.pm | 22 +---- .../vmware/src/centreon/vmware/common.pm | 4 +- .../vmware/src/centreon/vmware/connector.pm | 4 +- 7 files changed, 26 insertions(+), 95 deletions(-) diff --git a/connectors/vmware/src/centreon/script/centreon_vmware.pm b/connectors/vmware/src/centreon/script/centreon_vmware.pm index 132d636a5..202c84ae9 100644 --- a/connectors/vmware/src/centreon/script/centreon_vmware.pm +++ b/connectors/vmware/src/centreon/script/centreon_vmware.pm @@ -54,7 +54,7 @@ BEGIN { use base qw(centreon::script); use vars qw(%centreon_vmware_config); -my $VERSION = "2.4.0"; +my $VERSION = "3.0.0"; my %handlers = (TERM => {}, HUP => {}, CHLD => {}); my @load_modules = ( diff --git a/connectors/vmware/src/centreon/vmware/cmdcpuvm.pm b/connectors/vmware/src/centreon/vmware/cmdcpuvm.pm index 90dfdf213..806dedd4a 100644 --- a/connectors/vmware/src/centreon/vmware/cmdcpuvm.pm +++ b/connectors/vmware/src/centreon/vmware/cmdcpuvm.pm @@ -91,6 +91,7 @@ sub run { name => $entity_view->{name}, connection_state => $entity_view->{'runtime.connectionState'}->val, power_state => $entity_view->{'runtime.powerState'}->val, + 'config.annotation' => defined($entity_view->{'config.annotation'}) ? $entity_view->{'config.annotation'} : undef, }; next if (centreon::vmware::common::is_connected(state => $entity_view->{'runtime.connectionState'}->val) == 0); @@ -104,7 +105,6 @@ sub run { $data->{$entity_value}->{'cpu.usagemhz.average'} = $total_cpu_mhz_average; $data->{$entity_value}->{'cpu_ready'} = $total_cpu_ready; $data->{$entity_value}->{'interval_min'} = $interval_min; - $data->{$entity_value}->{'config.annotation'} = defined($entity_view->{'config.annotation'}) ? $entity_view->{'config.annotation'} : undef; $data->{$entity_value}->{'cpu'} = {}; foreach my $id (sort { my ($cida, $cia) = split /:/, $a; diff --git a/connectors/vmware/src/centreon/vmware/cmddatastorevm.pm b/connectors/vmware/src/centreon/vmware/cmddatastorevm.pm index 599920e1b..300d0ca60 100644 --- a/connectors/vmware/src/centreon/vmware/cmddatastorevm.pm +++ b/connectors/vmware/src/centreon/vmware/cmddatastorevm.pm @@ -90,6 +90,7 @@ sub run { name => $entity_view->{name}, connection_state => $entity_view->{'runtime.connectionState'}->val, power_state => $entity_view->{'runtime.powerState'}->val, + 'config.annotation' => defined($entity_view->{'config.annotation'}) ? $entity_view->{'config.annotation'} : undef, datastore => {}, }; next if (centreon::vmware::common::is_connected(state => $entity_view->{'runtime.connectionState'}->val) == 0); @@ -148,7 +149,6 @@ sub run { next if (centreon::vmware::common::is_connected(state => $entity_view->{'runtime.connectionState'}->val) == 0 && centreon::vmware::common::is_running(power => $entity_view->{'runtime.powerState'}->val) == 0); - $data->{$entity_value}->{'config.annotation'} = defined($entity_view->{'config.annotation'}) ? $entity_view->{'config.annotation'} : undef; my %datastore_lun = (); foreach (keys %{$values->{$entity_value}}) { diff --git a/connectors/vmware/src/centreon/vmware/cmddevicevm.pm b/connectors/vmware/src/centreon/vmware/cmddevicevm.pm index f39aba832..470e382c4 100644 --- a/connectors/vmware/src/centreon/vmware/cmddevicevm.pm +++ b/connectors/vmware/src/centreon/vmware/cmddevicevm.pm @@ -30,7 +30,6 @@ sub new { bless $self, $class; $self->{commandName} = 'devicevm'; - return $self; } @@ -38,57 +37,21 @@ sub checkArgs { my ($self, %options) = @_; if (defined($options{arguments}->{vm_hostname}) && $options{arguments}->{vm_hostname} eq "") { - $options{manager}->{output}->output_add(severity => 'UNKNOWN', - short_msg => "Argument error: vm hostname cannot be null"); + centreon::vmware::common::set_response(code => 100, short_message => "Argument error: vm hostname cannot be null"); return 1; } - if (defined($options{arguments}->{disconnect_status}) && - $options{manager}->{output}->is_litteral_status(status => $options{arguments}->{disconnect_status}) == 0) { - $options{manager}->{output}->output_add(severity => 'UNKNOWN', - short_msg => "Argument error: wrong value for disconnect status '" . $options{arguments}->{disconnect_status} . "'"); - return 1; - } - if (defined($options{arguments}->{nopoweredon_status}) && - $options{manager}->{output}->is_litteral_status(status => $options{arguments}->{nopoweredon_status}) == 0) { - $options{manager}->{output}->output_add(severity => 'UNKNOWN', - short_msg => "Argument error: wrong value for nopoweredon status '" . $options{arguments}->{nopoweredon_status} . "'"); - return 1; - } - if (($options{manager}->{perfdata}->threshold_validate(label => 'warning', value => $options{arguments}->{warning})) == 0) { - $options{manager}->{output}->output_add(severity => 'UNKNOWN', - short_msg => "Argument error: wrong value for warning value '" . $options{arguments}->{warning} . "'."); - return 1; - } - if (($options{manager}->{perfdata}->threshold_validate(label => 'critical', value => $options{arguments}->{critical})) == 0) { - $options{manager}->{output}->output_add(severity => 'UNKNOWN', - short_msg => "Argument error: wrong value for critical value '" . $options{arguments}->{critical} . "'."); - return 1; - } - return 0; -} -sub initArgs { - my ($self, %options) = @_; - - foreach (keys %{$options{arguments}}) { - $self->{$_} = $options{arguments}->{$_}; - } - $self->{manager} = centreon::vmware::common::init_response(); - $self->{manager}->{output}->{plugin} = $options{arguments}->{identity}; - $self->{manager}->{perfdata}->threshold_validate(label => 'warning', value => $options{arguments}->{warning}); - $self->{manager}->{perfdata}->threshold_validate(label => 'critical', value => $options{arguments}->{critical}); + return 0; } sub run { my $self = shift; if (!($self->{connector}->{perfcounter_speriod} > 0)) { - $self->{manager}->{output}->output_add(severity => 'UNKNOWN', - short_msg => "Can't retrieve perf counters"); + centreon::vmware::common::set_response(code => -1, short_message => "Can't retrieve perf counters"); return ; } - my $multiple = 0; my $filters = $self->build_filter(label => 'name', search_option => 'vm_hostname', is_regexp => 'filter'); if (defined($self->{filter_description}) && $self->{filter_description} ne '') { $filters->{'config.annotation'} = qr/$self->{filter_description}/; @@ -102,44 +65,32 @@ sub run { my $result = centreon::vmware::common::search_entities(command => $self, view_type => 'VirtualMachine', properties => \@properties, filter => $filters); return if (!defined($result)); - if (scalar(@$result) > 1) { - $multiple = 1; - } - + my $data = {}; my $total_device_connected = 0; foreach my $entity_view (@$result) { - next if (centreon::vmware::common::vm_state(connector => $self->{connector}, - hostname => $entity_view->{name}, - state => $entity_view->{'runtime.connectionState'}->val, - power => $entity_view->{'runtime.powerState'}->val, - status => $self->{disconnect_status}, - powerstatus => $self->{nopoweredon_status}, - multiple => $multiple) == 0); + my $entity_value = $entity_view->{mo_ref}->{value}; + + $data->{$entity_value} = { + name => $entity_view->{name}, + connection_state => $entity_view->{'runtime.connectionState'}->val, + power_state => $entity_view->{'runtime.powerState'}->val, + 'config.annotation' => defined($entity_view->{'config.annotation'}) ? $entity_view->{'config.annotation'} : undef, + total_device_connected => 0, + }; + + next if (centreon::vmware::common::is_connected(state => $entity_view->{'runtime.connectionState'}->val) == 0); + next if (centreon::vmware::common::is_running(power => $entity_view->{'runtime.powerState'}->val) == 0); foreach my $dev (@{$entity_view->{'config.hardware.device'}}) { if (ref($dev) =~ /$self->{device}/) { if ($dev->connectable->connected == 1) { - my $prefix_msg = "'$entity_view->{name}'"; - if (defined($self->{display_description}) && defined($entity_view->{'config.annotation'}) && - $entity_view->{'config.annotation'} ne '') { - $prefix_msg .= ' [' . centreon::vmware::common::strip_cr(value => $entity_view->{'config.annotation'}) . ']'; - } - $self->{manager}->{output}->output_add(long_msg => sprintf("%s device connected", - $prefix_msg)); - $total_device_connected++; + $data->{$entity_value}->{total_device_connected}++; } } } } - my $exit = $self->{manager}->{perfdata}->threshold_check(value => $total_device_connected, threshold => [ { label => 'critical', exit_litteral => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); - $self->{manager}->{output}->output_add(severity => $exit, - short_msg => sprintf("%s %s device connected", $total_device_connected, $self->{device})); - $self->{manager}->{output}->perfdata_add(label => 'connected', - value => $total_device_connected, - warning => $self->{manager}->{perfdata}->get_perfdata_for_output(label => 'warning'), - critical => $self->{manager}->{perfdata}->get_perfdata_for_output(label => 'critical'), - min => 0); + centreon::vmware::common::set_response(data => $data); } 1; diff --git a/connectors/vmware/src/centreon/vmware/cmddiscovery.pm b/connectors/vmware/src/centreon/vmware/cmddiscovery.pm index a1f4ee63f..551d7c4b6 100644 --- a/connectors/vmware/src/centreon/vmware/cmddiscovery.pm +++ b/connectors/vmware/src/centreon/vmware/cmddiscovery.pm @@ -23,7 +23,6 @@ use base qw(centreon::vmware::cmdbase); use strict; use warnings; use centreon::vmware::common; -use JSON::XS; sub new { my ($class, %options) = @_; @@ -41,16 +40,6 @@ sub checkArgs { return 0; } -sub initArgs { - my ($self, %options) = @_; - - foreach (keys %{$options{arguments}}) { - $self->{$_} = $options{arguments}->{$_}; - } - $self->{manager} = centreon::vmware::common::init_response(); - $self->{manager}->{output}->{plugin} = $options{arguments}->{identity}; -} - sub run { my $self = shift; @@ -147,17 +136,8 @@ sub run { $disco_stats->{duration} = $disco_stats->{end_time} - $disco_stats->{start_time}; $disco_stats->{discovered_items} = @disco_data; $disco_stats->{results} = \@disco_data; - - my $encoded_data; - eval { - $encoded_data = encode_json($disco_stats); - }; - if ($@) { - $encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}'; - } - $self->{manager}->{output}->output_add(short_msg => $encoded_data); - $self->{manager}->{output}->display(nolabel => 1, force_ignore_perfdata => 1); + centreon::vmware::common::set_response(data => $disco_stats); } 1; diff --git a/connectors/vmware/src/centreon/vmware/common.pm b/connectors/vmware/src/centreon/vmware/common.pm index db1258ac1..976f2e3be 100644 --- a/connectors/vmware/src/centreon/vmware/common.pm +++ b/connectors/vmware/src/centreon/vmware/common.pm @@ -25,7 +25,7 @@ use VMware::VIRuntime; use VMware::VILib; use ZMQ::LibZMQ4; use ZMQ::Constants qw(:all); -use JSON; +use JSON::XS;; my $manager_display = {}; my $manager_response = {}; @@ -63,7 +63,7 @@ sub response { $response_str = $options{force_response}; } else { eval { - $response_str = JSON->new->utf8->encode($manager_response); + $response_str = JSON::XS->new->utf8->encode($manager_response); }; if ($@) { $response_str = '{ "code": -1, "short_message": "Cannot encode result" }'; diff --git a/connectors/vmware/src/centreon/vmware/connector.pm b/connectors/vmware/src/centreon/vmware/connector.pm index 31c190baf..76448ff19 100644 --- a/connectors/vmware/src/centreon/vmware/connector.pm +++ b/connectors/vmware/src/centreon/vmware/connector.pm @@ -21,7 +21,7 @@ package centreon::vmware::connector; use strict; use VMware::VIRuntime; use VMware::VILib; -use JSON; +use JSON::XS; use ZMQ::LibZMQ4; use ZMQ::Constants qw(:all); use File::Basename; @@ -167,7 +167,7 @@ sub reqclient { my $result; eval { - $result = JSON->new->utf8->decode($options{data}); + $result = JSON::XS->new->utf8->decode($options{data}); }; if ($@) { $self->{logger}->writeLogError("Cannot decode JSON: $@ (options{data}");