From 03ac8daff832027b4b842d4a6b0fc52b486dbe96 Mon Sep 17 00:00:00 2001 From: Quentin Garnier Date: Wed, 22 Oct 2014 11:23:43 +0200 Subject: [PATCH] Refs #7246 Refactoring of 3 commands --- .../vmware/src/centreon/esxd/cmdgetmap.pm | 70 ++++++++----- .../vmware/src/centreon/esxd/cmdhealthhost.pm | 12 ++- .../src/centreon/esxd/cmdlistdatastore.pm | 65 ------------- .../src/centreon/esxd/cmdlistdatastores.pm | 97 +++++++++++++++++++ .../vmware/src/centreon/esxd/cmdlisthost.pm | 54 ----------- .../src/centreon/esxd/cmdlistnichost.pm | 93 +++++++++++------- .../src/centreon/esxd/cmdmaintenancehost.pm | 90 ++++++++++++----- .../src/centreon/script/centreonesxd.pm | 3 +- 8 files changed, 274 insertions(+), 210 deletions(-) delete mode 100644 connectors/vmware/src/centreon/esxd/cmdlistdatastore.pm create mode 100644 connectors/vmware/src/centreon/esxd/cmdlistdatastores.pm delete mode 100644 connectors/vmware/src/centreon/esxd/cmdlisthost.pm diff --git a/connectors/vmware/src/centreon/esxd/cmdgetmap.pm b/connectors/vmware/src/centreon/esxd/cmdgetmap.pm index ee83bdcfb..1c06df0b9 100644 --- a/connectors/vmware/src/centreon/esxd/cmdgetmap.pm +++ b/connectors/vmware/src/centreon/esxd/cmdgetmap.pm @@ -9,7 +9,6 @@ sub new { my $class = shift; my $self = {}; $self->{logger} = shift; - $self->{obj_esxd} = shift; $self->{commandName} = 'getmap'; bless $self, $class; @@ -22,56 +21,75 @@ sub getCommandName { } sub checkArgs { - my $self = shift; + my ($self, %options) = @_; + + if (defined($options{arguments}->{esx_hostname}) && $options{arguments}->{esx_hostname} eq "") { + $options{manager}->{output}->output_add(severity => 'UNKNOWN', + short_msg => "Argument error: esx hostname cannot be null"); + return 1; + } return 0; } sub initArgs { - my $self = shift; - $self->{lhost} = $_[0]; + my ($self, %options) = @_; + + foreach (keys %{$options{arguments}}) { + $self->{$_} = $options{arguments}->{$_}; + } + $self->{manager} = centreon::esxd::common::init_response(); + $self->{manager}->{output}->{plugin} = $options{arguments}->{identity}; +} + +sub set_connector { + my ($self, %options) = @_; + + $self->{obj_esxd} = $options{connector}; } sub run { my $self = shift; my %filters = (); - if (defined($self->{lhost}) and $self->{lhost} ne "") { - %filters = ('name' => $self->{lhost}); + + if (defined($self->{esx_hostname}) && !defined($self->{filter})) { + $filters{name} = qr/^\Q$self->{esx_hostname}\E$/; + } elsif (!defined($self->{esx_hostname})) { + $filters{name} = qr/.*/; + } else { + $filters{name} = qr/$self->{esx_hostname}/; } my @properties = ('name', 'vm'); my $result = centreon::esxd::common::get_entities_host($self->{obj_esxd}, 'HostSystem', \%filters, \@properties); - if (!defined($result)) { - return ; - } - - my $status = 0; # OK - my $output = ''; - my $output_append = ""; + return if (!defined($result)); + $self->{manager}->{output}->output_add(severity => 'OK', + short_msg => sprintf("List ESX host(s):")); + foreach my $entity_view (@$result) { - $output .= $output_append . "ESX Host '" . $entity_view->name . "': "; + $self->{manager}->{output}->output_add(long_msg => sprintf(" %s%s", $entity_view->name, + defined($self->{vm_no}) ? '' : ':')); + next if (defined($self->{vm_no})); + my @vm_array = (); if (defined $entity_view->vm) { - @vm_array = (@vm_array, @{$entity_view->vm}); + @vm_array = (@vm_array, @{$entity_view->vm}); } @properties = ('name', 'summary.runtime.powerState'); my $result2 = centreon::esxd::common::get_views($self->{obj_esxd}, \@vm_array, \@properties); - if (!defined($result)) { - return ; + return if (!defined($result2)); + + my %vms = (); + foreach my $vm (@$result2) { + $vms{$vm->name} = $vm->{'summary.runtime.powerState'}->val; } - my $output_append2 = ''; - foreach my $vm (@$result2) { - if ($vm->{'summary.runtime.powerState'}->val eq "poweredOn") { - $output .= $output_append2 . "[" . $vm->name . "]"; - $output_append2 = ', '; - } + foreach (sort keys %vms) { + $self->{manager}->{output}->output_add(long_msg => sprintf(" %s [%s]", + $_, $vms{$_})); } - $output_append = ". "; } - - $self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|$output\n"); } 1; diff --git a/connectors/vmware/src/centreon/esxd/cmdhealthhost.pm b/connectors/vmware/src/centreon/esxd/cmdhealthhost.pm index b0d48f725..8d4d8344d 100644 --- a/connectors/vmware/src/centreon/esxd/cmdhealthhost.pm +++ b/connectors/vmware/src/centreon/esxd/cmdhealthhost.pm @@ -28,6 +28,12 @@ sub checkArgs { short_msg => "Argument error: esx 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; + } return 0; } @@ -56,7 +62,7 @@ sub run { my $multiple = 0; if (defined($self->{esx_hostname}) && !defined($self->{filter})) { - $filters{name} = qr/^\Q$self->{esx_hostname}\E$/; + $filters{name} = qr/^\Q$self->{esx_hostname}\E$/; } elsif (!defined($self->{esx_hostname})) { $filters{name} = qr/.*/; } else { @@ -66,9 +72,7 @@ sub run { my @properties = ('name', 'runtime.healthSystemRuntime.hardwareStatusInfo', 'runtime.healthSystemRuntime.systemHealthInfo.numericSensorInfo', 'runtime.connectionState'); my $result = centreon::esxd::common::get_entities_host($self->{obj_esxd}, 'HostSystem', \%filters, \@properties); - if (!defined($result)) { - return ; - } + return if (!defined($result)); if (scalar(@$result) > 1) { $multiple = 1; diff --git a/connectors/vmware/src/centreon/esxd/cmdlistdatastore.pm b/connectors/vmware/src/centreon/esxd/cmdlistdatastore.pm deleted file mode 100644 index 959e9d013..000000000 --- a/connectors/vmware/src/centreon/esxd/cmdlistdatastore.pm +++ /dev/null @@ -1,65 +0,0 @@ - -package centreon::esxd::cmdlistdatastore; - -use strict; -use warnings; -use centreon::esxd::common; - -sub new { - my $class = shift; - my $self = {}; - $self->{logger} = shift; - $self->{obj_esxd} = shift; - $self->{commandName} = 'listdatastore'; - - bless $self, $class; - return $self; -} - -sub getCommandName { - my $self = shift; - return $self->{commandName}; -} - -sub checkArgs { - my $self = shift; - return 0; -} - -sub initArgs { - my $self = shift; - $self->{xml} = (defined($_[0]) && $_[0] == 1) ? 1 : 0; -} - -sub run { - my $self = shift; - - my %filters = (); - my @properties = ('summary'); - - my $result = centreon::esxd::common::get_entities_host($self->{obj_esxd}, 'Datastore', \%filters, \@properties); - if (!defined($result)) { - return ; - } - - my $status = 0; # OK - my $output = 'Datastore List: '; - my $output_append = ""; - my $xml_output = ''; - foreach my $datastore (@$result) { - if ($datastore->summary->accessible) { - $output .= $output_append . "'" . $datastore->summary->name . "'"; - $output_append = ', '; - } - $xml_output .= ''; - } - $xml_output .= ''; - - if ($self->{xml} == 1) { - $self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|$xml_output\n"); - } else { - $self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|$output\n"); - } -} - -1; diff --git a/connectors/vmware/src/centreon/esxd/cmdlistdatastores.pm b/connectors/vmware/src/centreon/esxd/cmdlistdatastores.pm new file mode 100644 index 000000000..451199aa7 --- /dev/null +++ b/connectors/vmware/src/centreon/esxd/cmdlistdatastores.pm @@ -0,0 +1,97 @@ + +package centreon::esxd::cmdlistdatastores; + +use strict; +use warnings; +use centreon::esxd::common; + +sub new { + my $class = shift; + my $self = {}; + $self->{logger} = shift; + $self->{commandName} = 'listdatastores'; + + bless $self, $class; + return $self; +} + +sub getCommandName { + my $self = shift; + return $self->{commandName}; +} + +sub checkArgs { + my ($self, %options) = @_; + + if (defined($options{arguments}->{esx_hostname}) && $options{arguments}->{esx_hostname} eq "") { + $options{manager}->{output}->output_add(severity => 'UNKNOWN', + short_msg => "Argument error: esx hostname cannot be null"); + return 1; + } + return 0; +} + +sub initArgs { + my ($self, %options) = @_; + + foreach (keys %{$options{arguments}}) { + $self->{$_} = $options{arguments}->{$_}; + } + $self->{manager} = centreon::esxd::common::init_response(); + $self->{manager}->{output}->{plugin} = $options{arguments}->{identity}; +} + +sub set_connector { + my ($self, %options) = @_; + + $self->{obj_esxd} = $options{connector}; +} + +sub run { + my $self = shift; + + my %filters = (); + my $multiple = 0; + + if (defined($self->{datastore_name}) && !defined($self->{filter})) { + $filters{name} = qr/^\Q$self->{datastore_name}\E$/; + } elsif (!defined($self->{datastore_name})) { + $filters{name} = qr/.*/; + } else { + $filters{name} = qr/$self->{datastore_name}/; + } + my @properties = ('summary'); + + my $result = centreon::esxd::common::get_entities_host($self->{obj_esxd}, 'Datastore', \%filters, \@properties); + return if (!defined($result)); + + if (!defined($self->{disco_show})) { + $self->{manager}->{output}->output_add(severity => 'OK', + short_msg => 'List datastore(s):'); + } + foreach my $datastore (@$result) { + if (defined($self->{disco_show})) { + $self->{manager}->{output}->add_disco_entry(name => $datastore->summary->name, + accessible => $datastore->summary->accessible); + } else { + $self->{manager}->{output}->output_add(long_msg => sprintf(" %s [%s]", + $datastore->summary->name, + $datastore->summary->accessible)); + } + } + + if (defined($self->{disco_show})) { + my $stdout; + { + local *STDOUT; + $self->{manager}->{output}->{option_results}->{output_xml} = 1; + open STDOUT, '>', \$stdout; + $self->{manager}->{output}->display_disco_show(); + delete $self->{manager}->{output}->{option_results}->{output_xml}; + $self->{manager}->{output}->output_add(severity => 'OK', + short_msg => $stdout); + } + } +} + +1; diff --git a/connectors/vmware/src/centreon/esxd/cmdlisthost.pm b/connectors/vmware/src/centreon/esxd/cmdlisthost.pm deleted file mode 100644 index 4d44ea733..000000000 --- a/connectors/vmware/src/centreon/esxd/cmdlisthost.pm +++ /dev/null @@ -1,54 +0,0 @@ - -package centreon::esxd::cmdlisthost; - -use strict; -use warnings; -use centreon::esxd::common; - -sub new { - my $class = shift; - my $self = {}; - $self->{logger} = shift; - $self->{obj_esxd} = shift; - $self->{commandName} = 'listhost'; - - bless $self, $class; - return $self; -} - -sub getCommandName { - my $self = shift; - return $self->{commandName}; -} - -sub checkArgs { - my $self = shift; - return 0; -} - -sub initArgs { - my $self = shift; -} - -sub run { - my $self = shift; - my %filters = (); - my @properties = ('name'); - my $result = centreon::esxd::common::get_entities_host($self->{obj_esxd}, 'HostSystem', \%filters, \@properties); - if (!defined($result)) { - return ; - } - - my $status = 0; # OK - my $output = 'Host List: '; - my $output_append = ""; - - foreach my $entity_view (@$result) { - $output .= $output_append . $entity_view->{name}; - $output_append = ', '; - } - - $self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|$output\n"); -} - -1; diff --git a/connectors/vmware/src/centreon/esxd/cmdlistnichost.pm b/connectors/vmware/src/centreon/esxd/cmdlistnichost.pm index f41269b80..72d413065 100644 --- a/connectors/vmware/src/centreon/esxd/cmdlistnichost.pm +++ b/connectors/vmware/src/centreon/esxd/cmdlistnichost.pm @@ -9,7 +9,6 @@ sub new { my $class = shift; my $self = {}; $self->{logger} = shift; - $self->{obj_esxd} = shift; $self->{commandName} = 'listnichost'; bless $self, $class; @@ -22,32 +21,40 @@ sub getCommandName { } sub checkArgs { - my $self = shift; - my ($host) = @_; + my ($self, %options) = @_; - if (!defined($host) || $host eq "") { - $self->{logger}->writeLogError("ARGS error: need hostname"); + if (!defined($options{arguments}->{esx_hostname}) || $options{arguments}->{esx_hostname} eq "") { + $options{manager}->{output}->output_add(severity => 'UNKNOWN', + short_msg => "Argument error: esx hostname need to be set"); return 1; } return 0; } sub initArgs { - my $self = shift; - $self->{lhost} = $_[0]; - $self->{xml} = (defined($_[1]) && $_[1] == 1) ? 1 : 0; + my ($self, %options) = @_; + + foreach (keys %{$options{arguments}}) { + $self->{$_} = $options{arguments}->{$_}; + } + $self->{manager} = centreon::esxd::common::init_response(); + $self->{manager}->{output}->{plugin} = $options{arguments}->{identity}; +} + +sub set_connector { + my ($self, %options) = @_; + + $self->{obj_esxd} = $options{connector}; } sub run { my $self = shift; my %nic_in_vswitch = (); - my %filters = ('name' => $self->{lhost}); + my %filters = (name => $self->{esx_hostname}); my @properties = ('config.network.pnic', 'config.network.vswitch', 'config.network.proxySwitch'); my $result = centreon::esxd::common::get_entities_host($self->{obj_esxd}, 'HostSystem', \%filters, \@properties); - if (!defined($result)) { - return ; - } + return if (!defined($result)); # Get Name from vswitch if (defined($$result[0]->{'config.network.vswitch'})) { @@ -68,36 +75,48 @@ sub run { } } - my $status = 0; # OK - my $output_up = 'Nic Up List: '; - my $output_down = 'Nic Down List: '; - my $output_down_no_vswitch = 'Nic Down List (not in vswitch, dvswitch): '; - my $output_up_append = ""; - my $output_down_append = ""; - my $output_down_no_vswitch_append = ""; - my $xml_output = ''; + if (!defined($self->{disco_show})) { + $self->{manager}->{output}->output_add(severity => 'OK', + short_msg => 'List nic host:'); + } + + my %nics = (); foreach (@{$$result[0]->{'config.network.pnic'}}) { + if (defined($nic_in_vswitch{$_->key})) { + $nics{$_->device}{vswitch} = 1; + } if (defined($_->linkSpeed)) { - $output_up .= $output_up_append . "'" . $_->device . "'"; - $output_up_append = ', '; - $xml_output .= ''; + $nics{$_->device}{up} = 1; } else { - if (defined($nic_in_vswitch{$_->key})) { - $output_down .= $output_down_append . "'" . $_->device . "'"; - $output_down_append = ', '; - $xml_output .= ''; - } else { - $output_down_no_vswitch .= $output_down_no_vswitch_append . "'" . $_->device . "'"; - $output_down_no_vswitch_append = ', '; - } + $nics{$_->device}{down} = 1; } } - $xml_output .= ''; - - if ($self->{xml} == 1) { - $self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|$xml_output\n"); - } else { - $self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|$output_up. $output_down. $output_down_no_vswitch.\n"); + + foreach my $nic_name (sort keys %nics) { + my $status = defined($nics{$nic_name}{up}) ? 'up' : 'down'; + my $vswitch = defined($nics{$nic_name}{vswitch}) ? 1 : 0; + + if (defined($self->{disco_show})) { + $self->{manager}->{output}->add_disco_entry(name => $nic_name, + status => $status, + vswitch => $vswitch); + } else { + $self->{manager}->{output}->output_add(long_msg => sprintf('%s [status: %s] [vswitch: %s]', + $nic_name, $status, $vswitch)); + } + } + + if (defined($self->{disco_show})) { + my $stdout; + { + local *STDOUT; + $self->{manager}->{output}->{option_results}->{output_xml} = 1; + open STDOUT, '>', \$stdout; + $self->{manager}->{output}->display_disco_show(); + delete $self->{manager}->{output}->{option_results}->{output_xml}; + $self->{manager}->{output}->output_add(severity => 'OK', + short_msg => $stdout); + } } } diff --git a/connectors/vmware/src/centreon/esxd/cmdmaintenancehost.pm b/connectors/vmware/src/centreon/esxd/cmdmaintenancehost.pm index 8abb4bdcb..808b727c4 100644 --- a/connectors/vmware/src/centreon/esxd/cmdmaintenancehost.pm +++ b/connectors/vmware/src/centreon/esxd/cmdmaintenancehost.pm @@ -9,7 +9,6 @@ sub new { my $class = shift; my $self = {}; $self->{logger} = shift; - $self->{obj_esxd} = shift; $self->{commandName} = 'maintenancehost'; bless $self, $class; @@ -22,44 +21,91 @@ sub getCommandName { } sub checkArgs { - my $self = shift; - my ($lhost) = @_; + my ($self, %options) = @_; - if (!defined($lhost) || $lhost eq "") { - $self->{logger}->writeLogError("ARGS error: need hostname"); + if (defined($options{arguments}->{esx_hostname}) && $options{arguments}->{esx_hostname} eq "") { + $options{manager}->{output}->output_add(severity => 'UNKNOWN', + short_msg => "Argument error: esx 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}->{maintenance_status}) && + $options{manager}->{output}->is_litteral_status(status => $options{arguments}->{maintenance_status}) == 0) { + $options{manager}->{output}->output_add(severity => 'UNKNOWN', + short_msg => "Argument error: wrong value for maintenance status '" . $options{arguments}->{maintenance_status} . "'"); return 1; } return 0; } sub initArgs { - my $self = shift; - $self->{lhost} = $_[0]; + my ($self, %options) = @_; + + foreach (keys %{$options{arguments}}) { + $self->{$_} = $options{arguments}->{$_}; + } + $self->{manager} = centreon::esxd::common::init_response(); + $self->{manager}->{output}->{plugin} = $options{arguments}->{identity}; +} + +sub set_connector { + my ($self, %options) = @_; + + $self->{obj_esxd} = $options{connector}; } sub run { my $self = shift; - my %filters = ('name' => $self->{lhost}); - my @properties = ('runtime.inMaintenanceMode'); - my $result = centreon::esxd::common::get_entities_host($self->{obj_esxd}, 'HostSystem', \%filters, \@properties); - if (!defined($result)) { - return ; - } + my %filters = (); + my $multiple = 0; - my $status = 0; # OK - my $output = ''; + if (defined($self->{esx_hostname}) && !defined($self->{filter})) { + $filters{name} = qr/^\Q$self->{esx_hostname}\E$/; + } elsif (!defined($self->{esx_hostname})) { + $filters{name} = qr/.*/; + } else { + $filters{name} = qr/$self->{esx_hostname}/; + } + + my @properties = ('name', 'runtime.inMaintenanceMode', 'runtime.connectionState'); + my $result = centreon::esxd::common::get_entities_host($self->{obj_esxd}, 'HostSystem', \%filters, \@properties); + return if (!defined($result)); + + if (scalar(@$result) > 1) { + $multiple = 1; + } + if ($multiple == 1) { + $self->{manager}->{output}->output_add(severity => 'OK', + short_msg => sprintf("All ESX maintenance mode are ok")); + } foreach my $entity_view (@$result) { - if ($entity_view->{'runtime.inMaintenanceMode'} ne "false") { - $status = centreon::esxd::common::errors_mask($status, 'CRITICAL'); - $output = "Server " . $self->{lhost} . " is on maintenance mode."; - } else { - $output = "Server " . $self->{lhost} . " is not on maintenance mode."; + next if (centreon::esxd::common::host_state(connector => $self->{obj_esxd}, + hostname => $entity_view->{name}, + state => $entity_view->{'runtime.connectionState'}->val, + status => $self->{disconnect_status}, + multiple => $multiple) == 0); + + $self->{manager}->{output}->output_add(long_msg => sprintf("'%s' maintenance mode is %s", + $entity_view->{name}, $entity_view->{'runtime.inMaintenanceMode'})); + + if ($entity_view->{'runtime.inMaintenanceMode'} =~ /$self->{maintenance_alert}/ && + !$self->{manager}->{output}->is_status(value => $self->{maintenance_status}, compare => 'ok', litteral => 1)) { + $self->{manager}->{output}->output_add(severity => $self->{maintenance_status}, + short_msg => sprintf("'%s' maintenance mode is %s", + $entity_view->{name}, $entity_view->{'runtime.inMaintenanceMode'})) + } elsif ($multiple == 0) { + $self->{manager}->{output}->output_add(severity => 'OK', + short_msg => sprintf("'%s' maintenance mode is %s", + $entity_view->{name}, $entity_view->{'runtime.inMaintenanceMode'})) } } - - $self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|$output\n"); } 1; diff --git a/connectors/vmware/src/centreon/script/centreonesxd.pm b/connectors/vmware/src/centreon/script/centreonesxd.pm index d173e8dcc..329e5a85e 100644 --- a/connectors/vmware/src/centreon/script/centreonesxd.pm +++ b/connectors/vmware/src/centreon/script/centreonesxd.pm @@ -40,8 +40,7 @@ my @load_modules = ('centreon::esxd::cmdcountvmhost', 'centreon::esxd::cmdgetmap', 'centreon::esxd::cmdhealthhost', 'centreon::esxd::cmdlimitvm', - 'centreon::esxd::cmdlistdatastore', - 'centreon::esxd::cmdlisthost', + 'centreon::esxd::cmdlistdatastores', 'centreon::esxd::cmdlistnichost', 'centreon::esxd::cmdmemhost', 'centreon::esxd::cmdmaintenancehost',