From 4286250506425f86a498a4fc8c1c95e4c731a13a Mon Sep 17 00:00:00 2001 From: Quentin Garnier Date: Thu, 12 Sep 2013 13:09:18 +0000 Subject: [PATCH] Fix #5811 git-svn-id: http://svn.merethis.net/centreon-esxd/trunk@81 a5eaa968-4c79-4d68-970d-af6011b5b055 --- connectors/vmware/centreon_esx_client.pl | 27 +++--- connectors/vmware/lib/cmdsnapshotvm.pm | 108 +++++++++++++++-------- 2 files changed, 82 insertions(+), 53 deletions(-) diff --git a/connectors/vmware/centreon_esx_client.pl b/connectors/vmware/centreon_esx_client.pl index 9eadbe8fa..b3ee75172 100644 --- a/connectors/vmware/centreon_esx_client.pl +++ b/connectors/vmware/centreon_esx_client.pl @@ -180,9 +180,9 @@ sub print_usage () { print "\n"; print "'snapshotvm':\n"; print " --vm VM to check (required)\n"; - print " --older If older than ms\n"; - print " --crit Critical if: there is a snasphot, or a snapshot is older than (--older)\n"; - print " --warn Warn if: there is a snasphot, or a snapshot is older than (--older)\n"; + print " --filter Use regexp for --vm option (can check multiples vm at once)\n"; + print " --warning Warning threshold in seconds (default: 3 days)\n"; + print " --critical Critical threshold in seconds (default: 5 days)\n"; print "\n"; print "'datastoresvm':\n"; print " --vm VM to check (required)\n"; @@ -593,25 +593,23 @@ sub snapshotvm_check_arg { print_usage(); exit $ERRORS{UNKNOWN}; } - if (!defined($OPTION{older})) { - $OPTION{older} = ''; - } - if (!defined($OPTION{warn})) { - $OPTION{warn} = 0; + if (defined($OPTION{filter})) { + $OPTION{filter} = 1; } else { - $OPTION{warn} = 1; + $OPTION{filter} = 0; } - if (!defined($OPTION{crit})) { - $OPTION{crit} = 0; - } else { - $OPTION{crit} = 1; + if (!defined($OPTION{warning})) { + $OPTION{warning} = 86400 * 3; + } + if (!defined($OPTION{critical})) { + $OPTION{critical} = 86400 * 5; } return 0; } sub snapshotvm_get_str { return join($separatorin, - ('snapshotvm', $OPTION{vsphere}, $OPTION{vm}, $OPTION{older}, $OPTION{warn}, $OPTION{crit})); + ('snapshotvm', $OPTION{vsphere}, $OPTION{vm}, $OPTION{filter}, $OPTION{warning}, $OPTION{critical})); } sub datastoresvm_check_arg { @@ -703,7 +701,6 @@ sub thinprovisioningvm_get_str { ('thinprovisioningvm', $OPTION{vsphere}, $OPTION{vm}, $OPTION{on}, $OPTION{warn}, $OPTION{crit})); } - sub listhost_check_arg { return 0; } diff --git a/connectors/vmware/lib/cmdsnapshotvm.pm b/connectors/vmware/lib/cmdsnapshotvm.pm index 19afe3ebb..75f3ffd04 100644 --- a/connectors/vmware/lib/cmdsnapshotvm.pm +++ b/connectors/vmware/lib/cmdsnapshotvm.pm @@ -23,14 +23,22 @@ sub getCommandName { sub checkArgs { my $self = shift; - my ($vm, $older) = @_; + my ($vm, $filter, $warn, $crit) = @_; if (!defined($vm) || $vm eq "") { $self->{logger}->writeLogError("ARGS error: need vm hostname"); return 1; } - if (defined($older) && $older ne '' && $older !~ /^-?(?:\d+\.?|\.\d)\d*\z/) { - $self->{logger}->writeLogError("ARGS error: older arg must be a positive number"); + 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; } return 0; @@ -39,66 +47,90 @@ sub checkArgs { sub initArgs { my $self = shift; $self->{lvm} = $_[0]; - $self->{older} = ((defined($_[1]) and $_[1] ne '') ? $_[1] : -1); - $self->{warn} = ((defined($_[2]) and $_[2] ne '') ? $_[2] : 0); - $self->{crit} = ((defined($_[3]) and $_[3] ne '') ? $_[3] : 0); + $self->{filter} = (defined($_[1]) && $_[1] == 1) ? 1 : 0; + $self->{warning} = ((defined($_[2]) and $_[2] ne '') ? $_[2] : 86400 * 3); + $self->{critical} = ((defined($_[3]) and $_[3] ne '') ? $_[3] : 86400 * 5); } sub run { my $self = shift; - if ($self->{older} != -1 && $self->{obj_esxd}->{module_date_parse_loaded} == 0) { + 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 CPAN Module.\n"); return ; } - my %filters = ('name' => $self->{lvm}); - my @properties = ('snapshot.rootSnapshotList'); + my %filters = (); + + if ($self->{filter} == 0) { + $filters{name} = qr/^\Q$self->{lvm}\E$/; + } else { + $filters{name} = qr/$self->{lvm}/; + } + my @properties = ('snapshot.rootSnapshotList', 'name'); my $result = centreon::esxd::common::get_entities_host($self->{obj_esxd}, 'VirtualMachine', \%filters, \@properties); if (!defined($result)) { return ; } my $status = 0; # OK - my $output = 'Snapshot(s) OK'; - - if (!defined($$result[0]->{'snapshot.rootSnapshotList'})) { - $output = 'No current snapshot.'; - $self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|$output\n"); - return ; - } + my $output = ""; + my $output_append = ''; + my $output_warning = ''; + my $output_warning_append = ''; + my $output_critical = ''; + my $output_critical_append = ''; + my $output_unknown = ''; + my $output_unknown_append = ''; + my $output_ok_unit = 'Snapshot(s) OK'; - foreach my $snapshot (@{$$result[0]->{'snapshot.rootSnapshotList'}}) { - if ($self->{older} != -1) { + foreach my $virtual (@$result) { + if (!defined($virtual->{'snapshot.rootSnapshotList'})) { + next; + } + + foreach my $snapshot (@{$virtual->{'snapshot.rootSnapshotList'}}) { # 2012-09-21T14:16:17.540469Z my $create_time = Date::Parse::str2time($snapshot->createTime); if (!defined($create_time)) { - $status = centreon::esxd::common::errors_mask(0, 'UNKNOWN'); - $self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|Can't Parse date '" . $snapshot->createTime . "'.\n"); - return ; + $status = centreon::esxd::common::errors_mask($status, 'UNKNOWN'); + centreon::esxd::common::output_add(\$output_unknown, \$output_unknown_append, ", ", + "Can't Parse date '" . $snapshot->createTime . "' for vm '" . $virtual->{'name'} . "'"); + next; } - if (time() - $create_time > $self->{older}) { - if ($self->{warn} == 1) { - $output = 'Older snapshot problem (' . $snapshot->createTime . ').'; - $status = centreon::esxd::common::errors_mask($status, 'WARNING'); - } - if ($self->{crit} == 1) { - $output = 'Older snapshot problem (' . $snapshot->createTime . ').'; - $status = centreon::esxd::common::errors_mask($status, 'CRITICAL'); - } - } - } elsif ($self->{older} == -1) { - if ($self->{warn} == 1) { - $output = 'There is at least one snapshot.'; - $status = centreon::esxd::common::errors_mask($status, 'WARNING'); - } - if ($self->{crit} == 1) { - $output = 'There is at least one snapshot.'; + if (time() - $create_time >= $self->{critical}) { + centreon::esxd::common::output_add(\$output_critical, \$output_critical_append, ", ", + "[" . $virtual->{'name'}. "]"); $status = centreon::esxd::common::errors_mask($status, 'CRITICAL'); + last; + } elsif (time() - $create_time >= $self->{warning}) { + centreon::esxd::common::output_add(\$output_warning, \$output_warning_append, ", ", + "[" . $virtual->{'name'}. "]"); + $status = centreon::esxd::common::errors_mask($status, 'WARNING'); + last; } } } + + if ($output_unknown ne "") { + $output .= $output_append . "UNKNOWN - $output_unknown"; + $output_append = ". "; + } + if ($output_critical ne "") { + $output .= $output_append . "CRITICAL - Snapshots for VM older than " . ($self->{critical} / 86400) . " days: $output_critical"; + $output_append = ". "; + } + if ($output_warning ne "") { + $output .= $output_append . "CRITICAL - Snapshots for VM older than " . ($self->{warning} / 86400) . " days: $output_warning"; + } + if ($status == 0) { + if ($self->{filter} == 1) { + $output .= $output_append . "All snapshots are ok"; + } else { + $output .= $output_append . $output_ok_unit; + } + } $self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|$output\n"); }