From 7edf8e502fb5c3691cd78933e431fe845d72fa2d Mon Sep 17 00:00:00 2001 From: Quentin Garnier Date: Wed, 11 Sep 2013 13:37:47 +0000 Subject: [PATCH] Fix #5563 git-svn-id: http://svn.merethis.net/centreon-esxd/trunk@72 a5eaa968-4c79-4d68-970d-af6011b5b055 --- connectors/vmware/centreon_esx_client.pl | 87 ++++++++----- connectors/vmware/lib/cmddatastoreusage.pm | 134 +++++++++++++++++---- connectors/vmware/lib/common.pm | 9 ++ 3 files changed, 175 insertions(+), 55 deletions(-) diff --git a/connectors/vmware/centreon_esx_client.pl b/connectors/vmware/centreon_esx_client.pl index 576ba60e2..1f3fdf59f 100644 --- a/connectors/vmware/centreon_esx_client.pl +++ b/connectors/vmware/centreon_esx_client.pl @@ -16,48 +16,53 @@ sub print_usage(); sub print_revision($$); my %OPTION = ( - "help" => undef, "version" => undef, + help => undef, version => undef, "esxd-host" => undef, "esxd-port" => 5700, - "vsphere" => '', - "usage" => undef, + vsphere => '', + usage => undef, "light-perfdata" => undef, "esx-host" => undef, - "datastore" => undef, - "nic" => undef, - "warning" => undef, - "critical" => undef, - "on" => undef, + datastore => undef, + nic => undef, + warning => undef, + critical => undef, + on => undef, + units => undef, + free => undef, + filter => undef ); Getopt::Long::Configure('bundling'); GetOptions( - "h|help" => \$OPTION{'help'}, - "V|version" => \$OPTION{'version'}, + "h|help" => \$OPTION{help}, + "V|version" => \$OPTION{version}, "H|centreon-esxd-host=s" => \$OPTION{'esxd-host'}, "P|centreon-esxd-port=i" => \$OPTION{'esxd-port'}, - "vsphere=s" => \$OPTION{'vsphere'}, + "vsphere=s" => \$OPTION{vsphere}, - "u|usage=s" => \$OPTION{'usage'}, + "u|usage=s" => \$OPTION{usage}, "e|esx-host=s" => \$OPTION{'esx-host'}, - "vm=s" => \$OPTION{'vm'}, + "vm=s" => \$OPTION{vm}, - "filter-datastores=s" => \$OPTION{'filter-datastores'}, + "filter" => \$OPTION{filter}, + "free" => \$OPTION{free}, + "units=s" => \$OPTION{units}, "light-perfdata" => \$OPTION{'light-perfdata'}, - "datastore=s" => \$OPTION{'datastore'}, - "nic=s" => \$OPTION{'nic'}, + "datastore=s" => \$OPTION{datastore}, + "nic=s" => \$OPTION{nic}, - "older=i" => \$OPTION{'older'}, - "warn" => \$OPTION{'warn'}, - "crit" => \$OPTION{'crit'}, + "older=i" => \$OPTION{older}, + "warn" => \$OPTION{warn}, + "crit" => \$OPTION{crit}, - "on" => \$OPTION{'on'}, + "on" => \$OPTION{on}, - "w|warning=i" => \$OPTION{'warning'}, - "c|critical=i" => \$OPTION{'critical'}, + "w|warning=i" => \$OPTION{warning}, + "c|critical=i" => \$OPTION{critical}, - "warning2=i" => \$OPTION{'warning2'}, - "critical2=i" => \$OPTION{'critical2'}, + "warning2=i" => \$OPTION{warning2}, + "critical2=i" => \$OPTION{critical2}, ); if (defined($OPTION{version})) { @@ -95,8 +100,11 @@ sub print_usage () { print "\n"; print "'datastore-usage':\n"; print " --datastore Datastore name 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 " -w (--warning) Warning Threshold (default 80)\n"; + print " -c (--critical) Critical Threshold (default 90)\n"; + print " --units Threshold units: %, MB (default is MB)\n"; + print " --free Threshold is for free size\n"; + print " --filter Use regexp for --datastore option (can check multiples datastores at once)\n"; print "\n"; print "'datastore-io':\n"; print " --datastore Datastore name to check (required)\n"; @@ -272,22 +280,41 @@ sub healthhost_get_str { sub datastoreusage_check_arg { if (!defined($OPTION{datastore})) { - print "Option --datastore is required\n"; + print "Option --datastore is required.\n"; print_usage(); exit $ERRORS{UNKNOWN}; } + if (defined($OPTION{filter})) { + $OPTION{filter} = 1; + } else { + $OPTION{filter} = 0; + } + if (defined($OPTION{free})) { + $OPTION{free} = 1; + } else { + $OPTION{free} = 0; + } if (!defined($OPTION{warning})) { - $OPTION{warning} = 80; + $OPTION{warning} = ($OPTION{free} == 1) ? 20 : 80; } if (!defined($OPTION{critical})) { - $OPTION{critical} = 90; + $OPTION{critical} = ($OPTION{free} == 1) ? 10 : 90; + } + if (defined($OPTION{units})) { + if ($OPTION{units} ne '%' && $OPTION{units} ne 'MB') { + print "Option --units accept '%' or 'MB'.\n"; + print_usage(); + exit $ERRORS{UNKNOWN}; + } + } else { + $OPTION{units} = ''; } return 0; } sub datastoreusage_get_str { return join($separatorin, - ('datastore-usage', $OPTION{vsphere}, $OPTION{datastore}, $OPTION{warning}, $OPTION{critical})); + ('datastore-usage', $OPTION{vsphere}, $OPTION{datastore}, $OPTION{filter}, $OPTION{warning}, $OPTION{critical}, $OPTION{free}, $OPTION{units})); } sub datastoreio_check_arg { diff --git a/connectors/vmware/lib/cmddatastoreusage.pm b/connectors/vmware/lib/cmddatastoreusage.pm index e88981db3..a0f4062d2 100644 --- a/connectors/vmware/lib/cmddatastoreusage.pm +++ b/connectors/vmware/lib/cmddatastoreusage.pm @@ -23,22 +23,30 @@ sub getCommandName { sub checkArgs { my $self = shift; - my ($ds, $warn, $crit) = @_; + my ($ds, $filter, $warn, $crit, $free, $units) = @_; if (!defined($ds) || $ds eq "") { - $self->{logger}->writeLogError("ARGS error: need datastore name"); + $self->{logger}->writeLogError("ARGS error: need datastore name."); return 1; } if (defined($warn) && $warn !~ /^-?(?:\d+\.?|\.\d)\d*\z/) { - $self->{logger}->writeLogError("ARGS error: warn threshold must be a positive number"); + $self->{logger}->writeLogError("ARGS error: warn threshold must be a positive number."); return 1; } if (defined($crit) && $crit !~ /^-?(?:\d+\.?|\.\d)\d*\z/) { - $self->{logger}->writeLogError("ARGS error: crit threshold must be a positive number"); + $self->{logger}->writeLogError("ARGS error: crit threshold must be a positive number."); return 1; } - if (defined($warn) && defined($crit) && $warn > $crit) { - $self->{logger}->writeLogError("ARGS error: warn threshold must be lower than crit threshold"); + if (defined($warn) && defined($crit) && (!defined($free) || $free != 1) && $warn > $crit) { + $self->{logger}->writeLogError("ARGS error: warn threshold must be lower than crit threshold."); + return 1; + } + if (defined($warn) && defined($crit) && defined($free) && $free == 1 && $warn < $crit) { + $self->{logger}->writeLogError("ARGS error: warn threshold must be higher than crit threshold."); + return 1; + } + if (defined($units) && ($units !~ /^(%|MB)/)) { + $self->{logger}->writeLogError("ARGS error: units should be '%' or 'MB'."); return 1; } return 0; @@ -47,14 +55,22 @@ sub checkArgs { sub initArgs { my $self = shift; $self->{ds} = $_[0]; - $self->{warn} = (defined($_[1]) ? $_[1] : 80); - $self->{crit} = (defined($_[2]) ? $_[2] : 90); + $self->{filter} = (defined($_[1]) && $_[1] == 1) ? 1 : 0; + $self->{free} = (defined($_[4]) && $_[4] == 1) ? 1 : 0; + $self->{warn} = (defined($_[2]) ? $_[2] : (($self->{free} == 1) ? 20 : 80)); + $self->{crit} = (defined($_[3]) ? $_[3] : (($self->{free} == 1) ? 10 : 90)); + $self->{units} = (defined($_[5])) ? $_[5] : '%'; } sub run { my $self = shift; + my %filters = (); - my %filters = ('name' => $self->{ds}); + if ($self->{filter} == 0) { + $filters{name} = qr/^\Q$self->{ds}\E$/; + } else { + $filters{name} = qr/$self->{ds}/; + } my @properties = ('summary'); my $result = centreon::esxd::common::get_entities_host($self->{obj_esxd}, 'Datastore', \%filters, \@properties); @@ -62,27 +78,95 @@ sub run { return ; } - return if (centreon::esxd::common::datastore_state($self->{obj_esxd}, $self->{ds}, $$result[0]->summary->accessible) == 0); - my $status = 0; # OK my $output = ""; + my $output_append = ''; + my $output_warning = ''; + my $output_warning_append = ''; + my $output_critical = ''; + my $output_critical_append = ''; + my $output_ok_unit = ''; + my $perfdata = ''; + my ($warn_threshold, $crit_threshold); + my ($pctwarn_threshold, $pctcrit_threshold) = ($self->{warn}, $self->{crit}); - my $dsName = $$result[0]->summary->name; - my $capacity = $$result[0]->summary->capacity; - my $free = $$result[0]->summary->freeSpace; - my $pct = ($capacity - $free) / $capacity * 100; - - my $usedD = ($capacity - $free) / 1024 / 1024 / 1024; - my $sizeD = $capacity / 1024 / 1024 / 1024; - - $output = "Datastore $dsName - used ".sprintf("%.2f", $usedD)." Go / ".sprintf("%.2f", $sizeD)." Go (".sprintf("%.2f", $pct)." %) |used=".($capacity - $free)."o;;;0;".$capacity." size=".$capacity."o\n"; - if ($pct >= $self->{warn}) { - $status = centreon::esxd::common::errors_mask($status, 'WARNING'); + if ($self->{units} eq '%' && $self->{free} == 1) { + $pctwarn_threshold = 100 - $self->{warn}; + $pctcrit_threshold = 100 - $self->{crit}; } - if ($pct > $self->{crit}) { - $status = centreon::esxd::common::errors_mask($status, 'CRITICAL'); + + foreach my $ds (@$result) { + if (!centreon::esxd::common::is_accessible($ds->summary->accessible)) { + centreon::esxd::common::output_add(\$output_warning, \$output_warning_append, ", ", + "'" . $ds->summary->name . "' not accessible. Can be disconnected."); + $status = centreon::esxd::common::errors_mask($status, 'WARNING'); + next; + } + + my $dsName = $ds->summary->name; + my $capacity = $ds->summary->capacity; + my $free = $ds->summary->freeSpace; + + + if ($self->{units} eq 'MB' && $self->{free} == 1) { + $warn_threshold = $capacity - ($self->{warn} * 1024 * 1024); + $crit_threshold = $capacity - ($self->{crit} * 1024 * 1024); + } elsif ($self->{units} eq 'MB' && $self->{free} == 0) { + $warn_threshold = $self->{warn} * 1024 * 1024; + $crit_threshold = $self->{crit} * 1024 * 1024; + } else { + $warn_threshold = ($capacity * $pctwarn_threshold) / 100; + $crit_threshold = ($capacity * $pctcrit_threshold) / 100; + } + + my $pct = ($capacity - $free) / $capacity * 100; + + my $usedD = ($capacity - $free) / 1024 / 1024 / 1024; + my $sizeD = $capacity / 1024 / 1024 / 1024; + + $output_ok_unit = "Datastore $dsName - used ".sprintf("%.2f", $usedD)." Go / ".sprintf("%.2f", $sizeD)." Go (".sprintf("%.2f", $pct)." %)"; + + if ($self->{units} eq '%' && $pct >= $pctcrit_threshold) { + centreon::esxd::common::output_add(\$output_critical, \$output_critical_append, ", ", + "'$dsName' used ".sprintf("%.2f", $usedD)." Go / ".sprintf("%.2f", $sizeD)." Go (".sprintf("%.2f", $pct)." %)"); + $status = centreon::esxd::common::errors_mask($status, 'CRITICAL'); + } elsif ($self->{units} eq '%' && $pct >= $pctwarn_threshold) { + centreon::esxd::common::output_add(\$output_warning, \$output_warning_append, ", ", + "'$dsName' used ".sprintf("%.2f", $usedD)." Go / ".sprintf("%.2f", $sizeD)." Go (".sprintf("%.2f", $pct)." %)"); + $status = centreon::esxd::common::errors_mask($status, 'WARNING'); + } elsif ($self->{units} eq 'MB' && ($capacity - $free) >= $crit_threshold) { + centreon::esxd::common::output_add(\$output_critical, \$output_critical_append, ", ", + "'$dsName' used ".sprintf("%.2f", $usedD)." Go / ".sprintf("%.2f", $sizeD)." Go (".sprintf("%.2f", $pct)." %)"); + $status = centreon::esxd::common::errors_mask($status, 'CRITICAL'); + } elsif ($self->{units} eq 'MB' && ($capacity - $free) >= $warn_threshold) { + centreon::esxd::common::output_add(\$output_warning, \$output_warning_append, ", ", + "'$dsName' used ".sprintf("%.2f", $usedD)." Go / ".sprintf("%.2f", $sizeD)." Go (".sprintf("%.2f", $pct)." %)"); + $status = centreon::esxd::common::errors_mask($status, 'WARNING'); + } + + if ($self->{filter} == 1) { + $perfdata .= " 'used_" . $dsName . "'=".($capacity - $free)."o;" . $warn_threshold . ";" . $crit_threshold . ";0;" . $capacity; + } else { + $perfdata .= " used=".($capacity - $free)."o;" . $warn_threshold . ";" . $crit_threshold . ";0;" . $capacity; + } } - $self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|$output\n"); + + if ($output_critical ne "") { + $output .= $output_append . "CRITICAL - Datastore(s): $output_critical"; + $output_append = ". "; + } + if ($output_warning ne "") { + $output .= $output_append . "WARNING - Datastore(s): $output_warning"; + } + if ($status == 0) { + if ($self->{filter} == 1) { + $output = "All Datastore usages are ok"; + } else { + $output = $output_ok_unit; + } + } + + $self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|$output|$perfdata\n"); } 1; diff --git a/connectors/vmware/lib/common.pm b/connectors/vmware/lib/common.pm index 4130db8fa..c7e01db9d 100644 --- a/connectors/vmware/lib/common.pm +++ b/connectors/vmware/lib/common.pm @@ -294,6 +294,15 @@ sub performance_errors { return 0; } +sub is_accessible { + my ($accessible) = @_; + + if ($accessible !~ /^true|1$/) { + return 0; + } + return 1; +} + sub datastore_state { my ($obj_esxd, $ds, $accessible) = @_;