diff --git a/connectors/vmware/centreon_esx_client.pl b/connectors/vmware/centreon_esx_client.pl index 3bfb674da..ff13233b2 100644 --- a/connectors/vmware/centreon_esx_client.pl +++ b/connectors/vmware/centreon_esx_client.pl @@ -54,6 +54,9 @@ GetOptions( "w|warning=i" => \$OPTION{'warning'}, "c|critical=i" => \$OPTION{'critical'}, + + "warning2=i" => \$OPTION{'warning2'}, + "critical2=i" => \$OPTION{'critical2'}, ); if (defined($OPTION{'version'})) { @@ -99,6 +102,13 @@ sub print_usage () { print " -w (--warning) Warning Threshold in kBps (default none)\n"; print " -c (--critical) Critical Threshold in kBps (default none)\n"; print "\n"; + print "'datastore-snapshots':\n"; + print " --datastore Datastore name to check (required)\n"; + print " -w (--warning) Warning Threshold in bytes for all snapshots (default none)\n"; + print " -c (--critical) Critical Threshold in bytes for all snapshots (default none)\n"; + print " --warning2 Warning Threshold in bytes for one snapshot (default none)\n"; + print " --critical2 Critical Threshold in bytes for one snapshot (default none)\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"; @@ -294,6 +304,32 @@ sub datastoreio_get_str { return "datastore-io|" . $OPTION{'vsphere'} . "|" . $OPTION{'datastore'} . "|" . $OPTION{'warning'} . "|" . $OPTION{'critical'}; } +sub datastoresnapshots_check_arg { + if (!defined($OPTION{'datastore'})) { + print "Option --datastore is required\n"; + print_usage(); + exit $ERRORS{'UNKNOWN'}; + } + if (!defined($OPTION{'warning'})) { + $OPTION{'warning'} = ''; + } + if (!defined($OPTION{'critical'})) { + $OPTION{'critical'} = ''; + } + if (!defined($OPTION{'warning2'})) { + $OPTION{'warning2'} = ''; + } + if (!defined($OPTION{'critical2'})) { + $OPTION{'critical2'} = ''; + } + return 0; +} + +sub datastoresnapshots_get_str { + return "datastore-snapshots|" . $OPTION{'vsphere'} . "|" . $OPTION{'datastore'} + . "|" . $OPTION{'warning'} . "|" . $OPTION{'critical'} . "|" . $OPTION{'warning2'} . "|" . $OPTION{'critical2'}; +} + sub cpuhost_check_arg { if (!defined($OPTION{'esx-host'})) { print "Option --esx-host is required\n"; @@ -335,7 +371,7 @@ sub datastoreshost_check_arg { } sub datastoreshost_get_str { - return "datastoreshost|" . $OPTION{'vsphere'} . "|" . $OPTION{'esx-host'} . "|" . $OPTION{'warning'} . "|" . $OPTION{'critical'} . "|" . $OPTION{'filter-datastores'}; + return "datastoreshost|" . $OPTION{'vsphere'} . "|" . $OPTION{'esx-host'} . "|" . $OPTION{'warning'} . "|" . $OPTION{'critical'} . "|" . $OPTION{'filter-datastores'}; } sub memhost_check_arg { @@ -644,7 +680,7 @@ if (!defined($OPTION{'usage'})) { print_usage(); exit $ERRORS{'UNKNOWN'}; } -if ($OPTION{'usage'} !~ /^(healthhost|datastore-usage|datastore-io|maintenancehost|statushost|cpuhost|datastoreshost|nethost|memhost|swaphost|countvmhost|uptimehost|cpuvm|toolsvm|snapshotvm|datastoresvm|memvm|swapvm|thinprovisioningvm|listhost|listdatastore|listnichost|getmap|stats)$/) { +if ($OPTION{'usage'} !~ /^(healthhost|datastore-usage|datastore-io|datastore-snapshots|maintenancehost|statushost|cpuhost|datastoreshost|nethost|memhost|swaphost|countvmhost|uptimehost|cpuvm|toolsvm|snapshotvm|datastoresvm|memvm|swapvm|thinprovisioningvm|listhost|listdatastore|listnichost|getmap|stats)$/) { print "Usage value is unknown\n"; print_usage(); exit $ERRORS{'UNKNOWN'}; diff --git a/connectors/vmware/centreonesxd.pm b/connectors/vmware/centreonesxd.pm index 995612415..b83ca05f3 100644 --- a/connectors/vmware/centreonesxd.pm +++ b/connectors/vmware/centreonesxd.pm @@ -26,6 +26,7 @@ my @load_modules = ('centreon::esxd::cmdcountvmhost', 'centreon::esxd::cmdcpuvm', 'centreon::esxd::cmddatastoreio', 'centreon::esxd::cmddatastoreshost', + 'centreon::esxd::cmddatastoresnapshots', 'centreon::esxd::cmddatastoresvm', 'centreon::esxd::cmddatastoreusage', 'centreon::esxd::cmdgetmap', diff --git a/connectors/vmware/lib/cmddatastoresnapshots.pm b/connectors/vmware/lib/cmddatastoresnapshots.pm new file mode 100644 index 000000000..3b51c3108 --- /dev/null +++ b/connectors/vmware/lib/cmddatastoresnapshots.pm @@ -0,0 +1,136 @@ + +package centreon::esxd::cmddatastoresnapshots; + +use strict; +use warnings; +use centreon::esxd::common; + +sub new { + my $class = shift; + my $self = {}; + $self->{logger} = shift; + $self->{obj_esxd} = shift; + $self->{commandName} = 'datastore-snapshots'; + + bless $self, $class; + return $self; +} + +sub getCommandName { + my $self = shift; + return $self->{commandName}; +} + +sub checkArgs { + my $self = shift; + my ($ds, $warn, $crit, $warn2, $crit2) = @_; + + if (!defined($ds) || $ds eq "") { + $self->{logger}->writeLogError("ARGS error: need datastore name"); + return 1; + } + if (defined($warn) && $warn ne "" && $warn !~ /^-?(?:\d+\.?|\.\d)\d*\z/) { + $self->{logger}->writeLogError("ARGS error: warn threshold must be a positive number"); + return 1; + } + if (defined($crit) && $crit ne "" && $crit !~ /^-?(?:\d+\.?|\.\d)\d*\z/) { + $self->{logger}->writeLogError("ARGS error: crit threshold must be a positive number"); + return 1; + } + if (defined($warn) && defined($crit) && $warn ne "" && $crit ne "" && $warn > $crit) { + $self->{logger}->writeLogError("ARGS error: warn threshold must be lower than crit threshold"); + return 1; + } + if (defined($warn2) && $warn2 ne "" && $warn2 !~ /^-?(?:\d+\.?|\.\d)\d*\z/) { + $self->{logger}->writeLogError("ARGS error: warn2 threshold must be a positive number"); + return 1; + } + if (defined($crit2) && $crit2 ne "" && $crit2 !~ /^-?(?:\d+\.?|\.\d)\d*\z/) { + $self->{logger}->writeLogError("ARGS error: crit2 threshold must be a positive number"); + return 1; + } + if (defined($warn2) && defined($crit2) && $warn2 ne "" && $crit2 ne "" && $warn2 > $crit2) { + $self->{logger}->writeLogError("ARGS error: warn2 threshold must be lower than crit2 threshold"); + return 1; + } + return 0; +} + +sub initArgs { + my $self = shift; + $self->{ds} = $_[0]; + $self->{warn} = (defined($_[1]) ? $_[1] : ''); + $self->{crit} = (defined($_[2]) ? $_[2] : ''); + $self->{warn2} = (defined($_[3]) ? $_[3] : ''); + $self->{crit2} = (defined($_[4]) ? $_[4] : ''); +} + +sub run { + my $self = shift; + + my %filters = ('summary.name' => $self->{ds}); + my @properties = ('summary.accessible', 'browser'); + my $result = centreon::esxd::common::get_entities_host($self->{obj_esxd}, 'Datastore', \%filters, \@properties); + if (!defined($result)) { + return ; + } + + return if (centreon::esxd::common::datastore_state($self->{obj_esxd}, $self->{ds}, $$result[0]->{'summary.accessible'}) == 0); + + @properties = (); + my $browse_ds; + return if (!($browse_ds = centreon::esxd::common::get_view($self->{obj_esxd}, $$result[0]->{'browser'}, \@properties))); + + my $snapshots; + return if (!($snapshots = centreon::esxd::common::search_in_datastore($self->{obj_esxd}, $browse_ds, '[' . $self->{ds} . ']', [VmSnapshotFileQuery->new()]))); + + my $status = 0; # OK + my $output = ''; + my $output_append = ''; + my $output_warning = ""; + my $output_warning_append = ''; + my $output_critical = ""; + my $output_critical_append = ''; + my $total_size = 0; + + foreach (@$snapshots) { + if (defined($_->file)) { + foreach my $x (@{$_->file}) { + if (defined($self->{crit2}) && $self->{crit2} ne '' && $x->fileSize >= $self->{crit2}) { + $status = centreon::esxd::common::errors_mask($status, 'CRITICAL'); + centreon::esxd::common::output_add(\$output_critical, \$output_critical_append, ", ", + "'" . $_->folderPath . ' => ' . $x->path . "'"); + } elsif (defined($self->{warn2}) && $self->{warn2} ne '' && $x->fileSize >= $self->{warn2}) { + $status = centreon::esxd::common::errors_mask($status, 'WARNING'); + centreon::esxd::common::output_add(\$output_warning, \$output_warning_append, ", ", + "'" . $_->folderPath . ' => ' . $x->path . "'"); + } + $total_size += $x->fileSize; + } + } + } + + if ($output_critical ne '') { + $output .= "CRITICAL - Snapshot size exceed limit: $output_critical."; + $output_append = " "; + } + if ($output_warning ne '') { + $output .= $output_append . "WARNING - Snapshot size exceed limit: $output_warning."; + $output_append = " "; + } + + if (defined($self->{crit}) && $self->{crit} && $total_size >= $self->{crit}) { + $output .= $output_append . "CRITICAL - Total snapshots size exceed limit: " . centreon::esxd::common::simplify_number($total_size / 1024 / 1024) . "MB."; + $status = centreon::esxd::common::errors_mask($status, 'CRITICAL'); + } elsif (defined($self->{warn}) && $self->{warn} && $total_size >= $self->{warn}) { + $output .= $output_append . "WARNING - Total snapshots size exceed limit: " . centreon::esxd::common::simplify_number($total_size / 1024 / 1024) . "MB."; + $status = centreon::esxd::common::errors_mask($status, 'WARNING'); + } else { + $output .= $output_append . "OK - Total snapshots size is ok."; + } + $output .= "|total_size=" . $total_size . "o;$self->{warn};$self->{crit};0;"; + + $self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|$output\n"); +} + +1; diff --git a/connectors/vmware/lib/common.pm b/connectors/vmware/lib/common.pm index f36f9137a..b8ac42ed3 100644 --- a/connectors/vmware/lib/common.pm +++ b/connectors/vmware/lib/common.pm @@ -95,6 +95,49 @@ sub get_views { return $results; } +sub get_view { + my $obj_esxd = shift; + my $results; + + eval { + $results = $obj_esxd->{session1}->get_view(mo_ref => $_[0], properties => $_[1]); + }; + if ($@) { + $obj_esxd->{logger}->writeLogError("'" . $obj_esxd->{whoaim} . "' $@"); + my $lerror = $@; + $lerror =~ s/\n/ /g; + $obj_esxd->print_response("-1|Error: " . $lerror . "\n"); + return undef; + } + return $results; +} + +sub search_in_datastore { + my $obj_esxd = shift; + my ($ds_browse, $ds_name, $query) = @_; + my $result; + + my $files = FileQueryFlags->new(fileSize => 1, + fileType => 1, + modification => 1, + fileOwner => 1 + ); + my $hostdb_search_spec = HostDatastoreBrowserSearchSpec->new(details => $files, + query => $query); + eval { + $result = $ds_browse->SearchDatastoreSubFolders(datastorePath=> $ds_name, + searchSpec=>$hostdb_search_spec); + }; + if ($@) { + $obj_esxd->{logger}->writeLogError("'" . $obj_esxd->{whoaim} . "' $@"); + my $lerror = $@; + $lerror =~ s/\n/ /g; + $obj_esxd->print_response("-1|Error: " . $lerror . "\n"); + return undef; + } + return $result; +} + sub get_perf_metric_ids { my $obj_esxd = shift; my $perf_names = $_[0];