Refactoring of 3 commands
This commit is contained in:
Quentin Garnier 2014-10-22 11:23:43 +02:00
parent 301cdc07ae
commit 03ac8daff8
8 changed files with 274 additions and 210 deletions

View File

@ -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 ;
}
return if (!defined($result));
my $status = 0; # OK
my $output = '';
my $output_append = "";
$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 $output_append2 = '';
my %vms = ();
foreach my $vm (@$result2) {
if ($vm->{'summary.runtime.powerState'}->val eq "poweredOn") {
$output .= $output_append2 . "[" . $vm->name . "]";
$output_append2 = ', ';
}
$vms{$vm->name} = $vm->{'summary.runtime.powerState'}->val;
}
$output_append = ". ";
}
$self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|$output\n");
foreach (sort keys %vms) {
$self->{manager}->{output}->output_add(long_msg => sprintf(" %s [%s]",
$_, $vms{$_}));
}
}
}
1;

View File

@ -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;

View File

@ -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 = '<data>';
foreach my $datastore (@$result) {
if ($datastore->summary->accessible) {
$output .= $output_append . "'" . $datastore->summary->name . "'";
$output_append = ', ';
}
$xml_output .= '<element name="' . $datastore->summary->name . '" />';
}
$xml_output .= '</data>';
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;

View File

@ -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;

View File

@ -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;

View File

@ -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 = '<data>';
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 .= '<element name="' . $_->device . '" />';
$nics{$_->device}{up} = 1;
} else {
if (defined($nic_in_vswitch{$_->key})) {
$output_down .= $output_down_append . "'" . $_->device . "'";
$output_down_append = ', ';
$xml_output .= '<element name="' . $_->device . '" />';
} else {
$output_down_no_vswitch .= $output_down_no_vswitch_append . "'" . $_->device . "'";
$output_down_no_vswitch_append = ', ';
}
$nics{$_->device}{down} = 1;
}
}
$xml_output .= '</data>';
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);
}
}
}

View File

@ -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;
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 $status = 0; # OK
my $output = '';
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;

View File

@ -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',