From ab4985ed690f0fc7f04edddb85358e87a89f4b52 Mon Sep 17 00:00:00 2001 From: Quentin Garnier Date: Wed, 22 Oct 2014 16:07:03 +0200 Subject: [PATCH] + Refs #7246 WIP: need commands (memhost, swaphost, uptimehost) --- connectors/vmware/centreon_esx_client.pl | 24 --- .../vmware/src/centreon/esxd/cmdmemhost.pm | 142 ++++++++++++------ .../vmware/src/centreon/esxd/cmdswaphost.pm | 134 ++++++++++++----- .../vmware/src/centreon/esxd/cmduptimehost.pm | 119 +++++++++++---- 4 files changed, 281 insertions(+), 138 deletions(-) diff --git a/connectors/vmware/centreon_esx_client.pl b/connectors/vmware/centreon_esx_client.pl index ef1646a95..a7b0d812f 100644 --- a/connectors/vmware/centreon_esx_client.pl +++ b/connectors/vmware/centreon_esx_client.pl @@ -113,7 +113,6 @@ sub print_usage () { print " --vsphere vsphere name (default: none)\n"; print " -u (--usage) What to check. The list and args (required)\n"; print "\n"; - print "'datastore-usage':\n"; print " --datastore Datastore name to check (required)\n"; print " -w (--warning) Warning Threshold (default 80)\n"; @@ -145,12 +144,6 @@ sub print_usage () { print " --filter Use regexp for --datastore option (can check multiples datastores at once)\n"; print " --skip-errors Status OK if not enough permissions or others errors (when you checks multiples)\n"; print "\n"; - print "'cpuhost':\n"; - print " -e (--esx-host) Esx Host to check (required)\n"; - print " -w (--warning) Warning Threshold in percent (default 80)\n"; - print " -c (--critical) Critical Threshold in percent (default 90)\n"; - print " --light-perfdata Display only total average cpu perfdata\n"; - print "\n"; print "'nethost':\n"; print " -e (--esx-host) Esx Host to check (required)\n"; print " --nic Physical nic name to check (required)\n"; @@ -159,16 +152,6 @@ sub print_usage () { print " --filter Use regexp for --nic option (can check multiple nics at once)\n"; print " --skip-errors Status OK if some nic are down (when you checks multiples)\n"; print "\n"; - print "'memhost':\n"; - print " -e (--esx-host) Esx Host to check (required)\n"; - print " -w (--warning) Warning Threshold in percent (default 80)\n"; - print " -c (--critical) Critical Threshold in percent (default 90)\n"; - print "\n"; - print "'swaphost':\n"; - print " -e (--esx-host) Esx Host to check (required)\n"; - print " -w (--warning) Warning Threshold in MB/s (default 0.8)\n"; - print " -c (--critical) Critical Threshold in MB/s (default 1)\n"; - print "\n"; print "'datastoreshost':\n"; print " -e (--esx-host) Esx Host to check (required)\n"; print " -w (--warning) Warning Threshold in ms (latency) (default none)\n"; @@ -183,9 +166,6 @@ sub print_usage () { print " --warning2 Warning Threshold if more VMs not on (default none)\n"; print " --critical2 Critical Threshold if more VMs not on (default none)\n"; print "\n"; - print "'uptimehost':\n"; - print " -e (--esx-host) Esx Host to check (required)\n"; - print "\n"; print "'cpuvm':\n"; print " --vm VM to check (required)\n"; print " -w (--warning) Warning Threshold in percent for cpu average (default 80)\n"; @@ -239,10 +219,6 @@ sub print_usage () { print " --warn Warn\n"; print " --skip-errors Status OK if vms are disconnected (when you checks multiples)\n"; print "\n"; - - print "'stats':\n"; - print " -w (--warning) Warning Threshold in total client connections (default none)\n"; - print " -c (--critical) Critical Threshold in total client connections (default none)\n"; } sub print_help () { diff --git a/connectors/vmware/src/centreon/esxd/cmdmemhost.pm b/connectors/vmware/src/centreon/esxd/cmdmemhost.pm index 88f88fadd..95e95db13 100644 --- a/connectors/vmware/src/centreon/esxd/cmdmemhost.pm +++ b/connectors/vmware/src/centreon/esxd/cmdmemhost.pm @@ -9,7 +9,6 @@ sub new { my $class = shift; my $self = {}; $self->{logger} = shift; - $self->{obj_esxd} = shift; $self->{commandName} = 'memhost'; bless $self, $class; @@ -22,79 +21,134 @@ sub getCommandName { } sub checkArgs { - my $self = shift; - my ($host, $warn, $crit) = @_; + 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 cannot be null"); return 1; } - if (defined($warn) && $warn !~ /^-?(?:\d+\.?|\.\d)\d*\z/) { - $self->{logger}->writeLogError("ARGS error: warn threshold must be a positive number"); + 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($crit) && $crit !~ /^-?(?:\d+\.?|\.\d)\d*\z/) { - $self->{logger}->writeLogError("ARGS error: crit threshold must be a positive number"); - 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 (defined($warn) && defined($crit) && $warn > $crit) { - $self->{logger}->writeLogError("ARGS error: warn threshold must be lower than crit threshold"); - 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 = shift; - $self->{lhost} = $_[0]; - $self->{warn} = (defined($_[1]) ? $_[1] : 80); - $self->{crit} = (defined($_[2]) ? $_[2] : 90); + my ($self, %options) = @_; + + foreach (keys %{$options{arguments}}) { + $self->{$_} = $options{arguments}->{$_}; + } + $self->{manager} = centreon::esxd::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}); +} + +sub set_connector { + my ($self, %options) = @_; + + $self->{obj_esxd} = $options{connector}; } sub run { my $self = shift; if (!($self->{obj_esxd}->{perfcounter_speriod} > 0)) { - my $status = centreon::esxd::common::errors_mask(0, 'UNKNOWN'); - $self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|Can't retrieve perf counters.\n"); + $self->{manager}->{output}->output_add(severity => 'UNKNOWN', + short_msg => "Can't retrieve perf counters"); return ; } - my %filters = ('name' => $self->{lhost}); - my @properties = ('summary.hardware.memorySize', 'runtime.connectionState'); + 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 @properties = ('name', 'summary.hardware.memorySize', 'runtime.connectionState'); my $result = centreon::esxd::common::get_entities_host($self->{obj_esxd}, 'HostSystem', \%filters, \@properties); - if (!defined($result)) { - return ; - } + return if (!defined($result)); - return if (centreon::esxd::common::host_state($self->{obj_esxd}, $self->{lhost}, - $$result[0]->{'runtime.connectionState'}->val) == 0); - - my $memory_size = $$result[0]->{'summary.hardware.memorySize'}; - + if (scalar(@$result) > 1) { + $multiple = 1; + } my $values = centreon::esxd::common::generic_performance_values_historic($self->{obj_esxd}, $result, [{'label' => 'mem.consumed.average', 'instances' => ['']}, {'label' => 'mem.overhead.average', 'instances' => ['']}], - $self->{obj_esxd}->{perfcounter_speriod}); + $self->{obj_esxd}->{perfcounter_speriod}, + skip_undef_counter => 1, multiples => 1, multiples_result_by_entity => 1); return if (centreon::esxd::common::performance_errors($self->{obj_esxd}, $values) == 1); - - my $mem_used = centreon::esxd::common::simplify_number(centreon::esxd::common::convert_number($values->{$self->{obj_esxd}->{perfcounter_cache}->{'mem.consumed.average'}->{'key'} . ":"}[0])); - my $mem_overhead = centreon::esxd::common::simplify_number(centreon::esxd::common::convert_number($values->{$self->{obj_esxd}->{perfcounter_cache}->{'mem.overhead.average'}->{'key'} . ":"}[0])); - my $status = 0; # OK - my $output = ''; - if ($mem_used * 100 / ($memory_size / 1024) >= $self->{warn}) { - $status = centreon::esxd::common::errors_mask($status, 'WARNING'); + if ($multiple == 1) { + $self->{manager}->{output}->output_add(severity => 'OK', + short_msg => sprintf("All memory usages are ok")); } - if ($mem_used * 100 / ($memory_size / 1024) >= $self->{crit}) { - $status = centreon::esxd::common::errors_mask($status, 'CRITICAL'); + foreach my $entity_view (@$result) { + 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); + my $entity_value = $entity_view->{mo_ref}->{value}; + my $memory_size = $entity_view->{'summary.hardware.memorySize'}; # in B + + # in KB + my $mem_used = centreon::esxd::common::simplify_number(centreon::esxd::common::convert_number($values->{$entity_value}->{$self->{obj_esxd}->{perfcounter_cache}->{'mem.consumed.average'}->{'key'} . ":"}[0])) * 1024; + my $mem_overhead = centreon::esxd::common::simplify_number(centreon::esxd::common::convert_number($values->{$entity_value}->{$self->{obj_esxd}->{perfcounter_cache}->{'mem.overhead.average'}->{'key'} . ":"}[0])) * 1024; + my $mem_free = $memory_size - $mem_used; + my $prct_used = $mem_used * 100 / $memory_size; + my $prct_free = 100 - $prct_used; + + my $exit = $self->{manager}->{perfdata}->threshold_check(value => $prct_used, threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); + my ($total_value, $total_unit) = $self->{manager}->{perfdata}->change_bytes(value => $memory_size); + my ($used_value, $used_unit) = $self->{manager}->{perfdata}->change_bytes(value => $mem_used); + my ($free_value, $free_unit) = $self->{manager}->{perfdata}->change_bytes(value => $mem_free); + + $self->{manager}->{output}->output_add(long_msg => sprintf("'%s' Memory Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", + $entity_view->{name}, + $total_value . " " . $total_unit, + $used_value . " " . $used_unit, $prct_used, + $free_value . " " . $free_unit, $prct_free)); + if ($multiple == 0 || + !$self->{manager}->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { + $self->{manager}->{output}->output_add(severity => $exit, + short_msg => sprintf("'%s' Memory Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", + $entity_view->{name}, + $total_value . " " . $total_unit, + $used_value . " " . $used_unit, $prct_used, + $free_value . " " . $free_unit, $prct_free)); + } + + my $extra_label = ''; + $extra_label = '_' . $entity_view->{name} if ($multiple == 1); + $self->{manager}->{output}->perfdata_add(label => 'used' . $extra_label, unit => 'B', + value => $mem_used, + warning => $self->{manager}->{perfdata}->get_perfdata_for_output(label => 'warning', total => $memory_size, cast_int => 1), + critical => $self->{manager}->{perfdata}->get_perfdata_for_output(label => 'critical', total => $memory_size, cast_int => 1), + min => 0, max => $memory_size); + $self->{manager}->{output}->perfdata_add(label => 'overhead' . $extra_label, unit => 'B', + value => $mem_overhead, + min => 0); } - - $output = "Memory used : " . centreon::esxd::common::simplify_number($mem_used / 1024 / 1024) . " Go - size : " . centreon::esxd::common::simplify_number($memory_size / 1024 / 1024 / 1024) . " Go - percent : " . centreon::esxd::common::simplify_number($mem_used * 100 / ($memory_size / 1024)) . " %"; - $output .= "|used=" . ($mem_used * 1024) . "o;" . centreon::esxd::common::simplify_number($memory_size * $self->{warn} / 100, 0) . ";" . centreon::esxd::common::simplify_number($memory_size * $self->{crit} / 100, 0) . ";0;" . ($memory_size) . " size=" . $memory_size . "o" . " overhead=" . ($mem_overhead * 1024) . "o"; - - $self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|$output\n"); } 1; diff --git a/connectors/vmware/src/centreon/esxd/cmdswaphost.pm b/connectors/vmware/src/centreon/esxd/cmdswaphost.pm index fbd52611f..f196e1345 100644 --- a/connectors/vmware/src/centreon/esxd/cmdswaphost.pm +++ b/connectors/vmware/src/centreon/esxd/cmdswaphost.pm @@ -9,7 +9,6 @@ sub new { my $class = shift; my $self = {}; $self->{logger} = shift; - $self->{obj_esxd} = shift; $self->{commandName} = 'swaphost'; bless $self, $class; @@ -22,77 +21,130 @@ sub getCommandName { } sub checkArgs { - my $self = shift; - my ($host, $warn, $crit) = @_; + 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 cannot be null"); return 1; } - if (defined($warn) && $warn !~ /^-?(?:\d+\.?|\.\d)\d*\z/) { - $self->{logger}->writeLogError("ARGS error: warn threshold must be a positive number"); + 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($crit) && $crit !~ /^-?(?:\d+\.?|\.\d)\d*\z/) { - $self->{logger}->writeLogError("ARGS error: crit threshold must be a positive number"); - 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 (defined($warn) && defined($crit) && $warn > $crit) { - $self->{logger}->writeLogError("ARGS error: warn threshold must be lower than crit threshold"); - 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 = shift; - $self->{lhost} = $_[0]; - $self->{warn} = (defined($_[1]) ? $_[1] : 0.8); - $self->{crit} = (defined($_[2]) ? $_[2] : 1); + my ($self, %options) = @_; + + foreach (keys %{$options{arguments}}) { + $self->{$_} = $options{arguments}->{$_}; + } + $self->{manager} = centreon::esxd::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}); +} + +sub set_connector { + my ($self, %options) = @_; + + $self->{obj_esxd} = $options{connector}; } sub run { my $self = shift; if (!($self->{obj_esxd}->{perfcounter_speriod} > 0)) { - my $status = centreon::esxd::common::errors_mask(0, 'UNKNOWN'); - $self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|Can't retrieve perf counters.\n"); + $self->{manager}->{output}->output_add(severity => 'UNKNOWN', + short_msg => "Can't retrieve perf counters"); return ; } - my %filters = ('name' => $self->{lhost}); + 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 @properties = ('name', 'runtime.connectionState'); my $result = centreon::esxd::common::get_entities_host($self->{obj_esxd}, 'HostSystem', \%filters, \@properties); - if (!defined($result)) { - return ; - } + return if (!defined($result)); - return if (centreon::esxd::common::host_state($self->{obj_esxd}, $self->{lhost}, - $$result[0]->{'runtime.connectionState'}->val) == 0); - + if (scalar(@$result) > 1) { + $multiple = 1; + } my $values = centreon::esxd::common::generic_performance_values_historic($self->{obj_esxd}, $result, [{'label' => 'mem.swapinRate.average', 'instances' => ['']}, {'label' => 'mem.swapoutRate.average', 'instances' => ['']}], $self->{obj_esxd}->{perfcounter_speriod}); return if (centreon::esxd::common::performance_errors($self->{obj_esxd}, $values) == 1); - - my $swap_in = centreon::esxd::common::simplify_number(centreon::esxd::common::convert_number($values->{$self->{obj_esxd}->{perfcounter_cache}->{'mem.swapinRate.average'}->{'key'} . ":"}[0])); - my $swap_out = centreon::esxd::common::simplify_number(centreon::esxd::common::convert_number($values->{$self->{obj_esxd}->{perfcounter_cache}->{'mem.swapoutRate.average'}->{'key'} . ":"}[0])); - my $status = 0; # OK - my $output = ''; - - if (($swap_in / 1024) >= $self->{warn} || ($swap_out / 1024) >= $self->{warn}) { - $status = centreon::esxd::common::errors_mask($status, 'WARNING'); + + if ($multiple == 1) { + $self->{manager}->{output}->output_add(severity => 'OK', + short_msg => sprintf("All swap rate usages are ok")); } - if (($swap_in / 1024) >= $self->{crit} || ($swap_out / 1024) >= $self->{crit}) { - $status = centreon::esxd::common::errors_mask($status, 'CRITICAL'); + foreach my $entity_view (@$result) { + 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); + my $entity_value = $entity_view->{mo_ref}->{value}; + + # KBps + my $swap_in = centreon::esxd::common::simplify_number(centreon::esxd::common::convert_number($values->{$self->{obj_esxd}->{perfcounter_cache}->{'mem.swapinRate.average'}->{'key'} . ":"}[0])) * 1024; + my $swap_out = centreon::esxd::common::simplify_number(centreon::esxd::common::convert_number($values->{$self->{obj_esxd}->{perfcounter_cache}->{'mem.swapoutRate.average'}->{'key'} . ":"}[0])) * 1024; + + my $exit1 = $self->{manager}->{perfdata}->threshold_check(value => $swap_in, threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); + my $exit2 = $self->{manager}->{perfdata}->threshold_check(value => $swap_out, threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); + my $exit = $self->{manager}->{output}->get_most_critical(status => [ $exit1, $exit2 ]); + my ($swap_in_value, $swap_in_unit) = $self->{manager}->{perfdata}->change_bytes(value => $swap_in); + my ($swap_out_value, $swap_out_unit) = $self->{manager}->{perfdata}->change_bytes(value => $swap_out); + + $self->{manager}->{output}->output_add(long_msg => sprintf("'%s' Swap In: %s Swap Out: %s", + $entity_view->{name}, + $swap_in_value . " " . $swap_in_unit . "/s", + $swap_out_value . " " . $swap_out_unit . "/s")); + if ($multiple == 0 || + !$self->{manager}->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { + $self->{manager}->{output}->output_add(severity => $exit, + short_msg => sprintf("'%s' Swap In: %s Swap Out: %s", + $entity_view->{name}, + $swap_in_value . " " . $swap_in_unit . "/s", + $swap_out_value . " " . $swap_out_unit . "/s")); + } + + my $extra_label = ''; + $extra_label = '_' . $entity_view->{name} if ($multiple == 1); + $self->{manager}->{output}->perfdata_add(label => 'swap_in' . $extra_label, unit => 'B/s', + value => $swap_in, + warning => $self->{manager}->{perfdata}->get_perfdata_for_output(label => 'warning'), + critical => $self->{manager}->{perfdata}->get_perfdata_for_output(label => 'critical'), + min => 0); + $self->{manager}->{output}->perfdata_add(label => 'swap_out' . $extra_label, unit => 'B/s', + value => $swap_out, + warning => $self->{manager}->{perfdata}->get_perfdata_for_output(label => 'warning'), + critical => $self->{manager}->{perfdata}->get_perfdata_for_output(label => 'critical'), + min => 0); } - - $output = "Swap In : " . centreon::esxd::common::simplify_number($swap_in / 1024 * 8) . " Mb/s , Swap Out : " . centreon::esxd::common::simplify_number($swap_out / 1024 * 8) . " Mb/s "; - $output .= "|swap_in=" . ($swap_in * 1024 * 8) . "b/s swap_out=" . (($swap_out * 1024 * 8)) . "b/s"; - - $self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|$output\n"); } 1; diff --git a/connectors/vmware/src/centreon/esxd/cmduptimehost.pm b/connectors/vmware/src/centreon/esxd/cmduptimehost.pm index b1457a400..0550aec9e 100644 --- a/connectors/vmware/src/centreon/esxd/cmduptimehost.pm +++ b/connectors/vmware/src/centreon/esxd/cmduptimehost.pm @@ -9,7 +9,6 @@ sub new { my $class = shift; my $self = {}; $self->{logger} = shift; - $self->{obj_esxd} = shift; $self->{commandName} = 'uptimehost'; bless $self, $class; @@ -22,55 +21,117 @@ sub getCommandName { } sub checkArgs { - my $self = shift; - my ($lhost) = @_; + my ($self, %options) = @_; - if (!defined($lhost) || $lhost eq "") { - $self->{logger}->writeLogError("ARGS error: need host name"); + 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 (($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 = 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}; + $self->{manager}->{perfdata}->threshold_validate(label => 'warning', value => $options{arguments}->{warning}); + $self->{manager}->{perfdata}->threshold_validate(label => 'critical', value => $options{arguments}->{critical}); +} + +sub set_connector { + my ($self, %options) = @_; + + $self->{obj_esxd} = $options{connector}; } sub run { my $self = shift; if ($self->{obj_esxd}->{module_date_parse_loaded} == 0) { - my $status = centreon::esxd::common::errors_mask(0, 'UNKNOWN'); - $self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|Need to install Date::Parse Perl Module.\n"); + $self->{manager}->{output}->output_add(severity => 'UNKNOWN', + short_msg => "Need to install Date::Parse Perl Module."); return ; } - my %filters = ('name' => $self->{lhost}); - my @properties = ('runtime.bootTime', 'runtime.connectionState'); + 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 @properties = ('name', 'runtime.bootTime', '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; + } + if ($multiple == 1) { + $self->{manager}->{output}->output_add(severity => 'OK', + short_msg => sprintf("All uptimes are ok")); } - return if (centreon::esxd::common::host_state($self->{obj_esxd}, $self->{lhost}, - $$result[0]->{'runtime.connectionState'}->val) == 0); + foreach my $entity_view (@$result) { + 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); - my $create_time = Date::Parse::str2time($$result[0]->{'runtime.bootTime'}); - if (!defined($create_time)) { - my $status = centreon::esxd::common::errors_mask(0, 'UNKNOWN'); - $self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|Can't Parse date '" . $$result[0]->{'runtime.bootTime'} . "'.\n"); - return ; + my $create_time = Date::Parse::str2time($entity_view->{'runtime.bootTime'}); + if (!defined($create_time)) { + $self->{manager}->{output}->output_add(severity => 'UNKNOWN', + short_msg => "Can't Parse date '" . $entity_view->{'runtime.bootTime'} . "'"); + return ; + } + + my $diff_time = time() - $create_time; + my $days = int($diff_time / 60 / 60 / 24); + + my $exit = $self->{manager}->{perfdata}->threshold_check(value => $diff_time, threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); + $self->{manager}->{output}->output_add(long_msg => sprintf("'%s' Uptime: %s day(s)", + $entity_view->{name}, + $days)); + if ($multiple == 0 || + !$self->{manager}->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { + $self->{manager}->{output}->output_add(severity => $exit, + short_msg => sprintf("'%s' Uptime: %s day(s)", + $entity_view->{name}, + $days)); + } + + my $extra_label = ''; + $extra_label = '_' . $entity_view->{name} if ($multiple == 1); + $self->{manager}->{output}->perfdata_add(label => 'uptime' . $extra_label, unit => 's', + value => $diff_time, + warning => $self->{manager}->{perfdata}->get_perfdata_for_output(label => 'warning'), + critical => $self->{manager}->{perfdata}->get_perfdata_for_output(label => 'critical'), + min => 0); } - my $diff_time = time() - $create_time; - my $days = int($diff_time / 60 / 60 / 24); - - my $output = ''; - my $status = 0; # OK - - $output = "Uptime (in day): $days|uptime=" . $days . "day(s)\n"; - - $self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|$output\n"); } 1;