[vm] Vérifier les snapshots

git-svn-id: http://svn.merethis.net/centreon-esxd/trunk@20 a5eaa968-4c79-4d68-970d-af6011b5b055
This commit is contained in:
Quentin Garnier 2012-09-21 15:03:07 +00:00
parent 3114bde2da
commit 2b76541fd8
3 changed files with 121 additions and 1 deletions

View File

@ -39,6 +39,10 @@ GetOptions(
"datastore=s" => \$OPTION{'datastore'},
"nic=s" => \$OPTION{'nic'},
"older=i" => \$OPTION{'older'},
"warn" => \$OPTION{'warn'},
"crit" => \$OPTION{'crit'},
"w|warning=i" => \$OPTION{'warning'},
"c|critical=i" => \$OPTION{'critical'},
);
@ -119,6 +123,12 @@ sub print_usage () {
print "'toolsvm':\n";
print " --vm VM to check (required)\n";
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 "\n";
print "'listhost':\n";
print " None\n";
print "\n";
@ -371,6 +381,32 @@ sub toolsvm_get_str {
return "toolsvm|" . $OPTION{'vm'};
}
sub snapshotvm_check_arg {
if (!defined($OPTION{'vm'})) {
print "Option --vm is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
}
if (!defined($OPTION{'older'})) {
$OPTION{'older'} = '';
}
if (!defined($OPTION{'warn'})) {
$OPTION{'warn'} = 0;
} else {
$OPTION{'warn'} = 1;
}
if (!defined($OPTION{'crit'})) {
$OPTION{'crit'} = 0;
} else {
$OPTION{'crit'} = 1;
}
return 0;
}
sub snapshotvm_get_str {
return "snapshotvm|" . $OPTION{'vm'} . "|" . $OPTION{'older'} . "|" . $OPTION{'warn'} . "|" . $OPTION{'crit'};;
}
sub listhost_check_arg {
return 0;
}
@ -425,7 +461,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|cpuvm|toolsvm|listhost|listdatastore|listnichost|getmap)$/) {
if ($OPTION{'usage'} !~ /^(healthhost|datastore-usage|datastore-io|maintenancehost|statushost|cpuhost|datastoreshost|nethost|memhost|swaphost|cpuvm|toolsvm|snapshotvm|listhost|listdatastore|listnichost|getmap)$/) {
print "Usage value is unknown\n";
print_usage();
exit $ERRORS{'UNKNOWN'};

View File

@ -22,6 +22,13 @@ use IO::Select;
use POSIX ":sys_wait_h";
use Data::Dumper;
our $module_date_parse_loaded = 0;
eval 'require DateTime::Format::ISO8601';
if (!$@) {
$module_date_parse_loaded = 1;
require DateTime::Format::ISO8601;
}
use vars qw($libpath $port $service_url $username $password $TIMEOUT_VSPHERE $TIMEOUT $TIMEOUT_KILL $REFRESH_KEEPER_SESSION);
use vars qw($LOG $log_mode $log_crit $log_facility);
use vars qw($openlog_option $syslog_err_priority $syslog_info_priority);
@ -46,6 +53,7 @@ require $libpath . '/command-listnichost.pm';
require $libpath . '/command-maintenancehost.pm';
require $libpath . '/command-memhost.pm';
require $libpath . '/command-nethost.pm';
require $libpath . '/command-snapshotvm.pm';
require $libpath . '/command-statushost.pm';
require $libpath . '/command-swaphost.pm';
require $libpath . '/command-toolsvm.pm';
@ -99,6 +107,7 @@ our %checks_descr = (
"swaphost" => {'arg' => \&swaphost_check_args, 'compute' => \&swaphost_compute_args, 'exec' => \&swaphost_do},
"cpuvm" => {'arg' => \&cpuvm_check_args, 'compute' => \&cpuvm_compute_args, 'exec' => \&cpuvm_do},
"toolsvm" => {'arg' => \&toolsvm_check_args, 'compute' => \&toolsvm_compute_args, 'exec' => \&toolsvm_do},
"snapshotvm" => {'arg' => \&snapshotvm_check_args, 'compute' => \&snapshotvm_compute_args, 'exec' => \&snapshotvm_do},
"listhost" => {'arg' => \&listhost_check_args, 'compute' => \&listhost_compute_args, 'exec' => \&listhost_do},
"listdatastore" => {'arg' => \&listdatastore_check_args, 'compute' => \&listdatastore_compute_args, 'exec' => \&listdatastore_do},
"listnichost" => {'arg' => \&listnichost_check_args, 'compute' => \&listnichost_compute_args, 'exec' => \&listnichost_do},

View File

@ -0,0 +1,75 @@
sub snapshotvm_check_args {
my ($vm, $older) = @_;
if (!defined($vm) || $vm eq "") {
writeLogFile(LOG_ESXD_ERROR, "ARGS error: need vm hostname\n");
return 1;
}
if (defined($older) && $older ne '' && $older !~ /^-?(?:\d+\.?|\.\d)\d*\z/) {
writeLogFile(LOG_ESXD_ERROR, "ARGS error: older arg must be a positive number\n");
return 1;
}
return 0;
}
sub snapshotvm_compute_args {
my $lvm = $_[0];
my $older = ((defined($_[1]) and $_[1] ne '') ? $_[1] : -1);
my $warn = ((defined($_[2]) and $_[2] ne '') ? $_[2] : 0);
my $crit = ((defined($_[3]) and $_[3] ne '') ? $_[3] : 0);
return ($lvm, $older, $warn, $crit);
}
sub snapshotvm_do {
my ($lvm, $older, $warn, $crit) = @_;
if ($module_date_parse_loaded == 0) {
my $status |= $MYERRORS_MASK{'UNKNOWN'};
print_response($ERRORS{$MYERRORS{$status}} . "|Need to install DateTime::Format::ISO8601 CPAN Module.\n");
return ;
}
my %filters = ('name' => $lvm);
my @properties = ('snapshot.rootSnapshotList');
my $result = get_entities_host('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.';
print_response($ERRORS{$MYERRORS{$status}} . "|$output\n");
return ;
}
# 2012-09-21T14:16:17.540469Z
foreach my $snapshot (@{$$result[0]->{'snapshot.rootSnapshotList'}}) {
my $create_time = DateTime::Format::ISO8601->parse_datetime($snapshot->createTime);
if ($older != -1 && time() - $create_time->epoch > $older) {
if ($warn == 1) {
$output = 'Older snapshot problem (' . $snapshot->createTime . ').';
$status |= $MYERRORS_MASK{'WARNING'};
}
if ($crit == 1) {
$output = 'Older snapshot problem (' . $snapshot->createTime . ').';
$status |= $MYERRORS_MASK{'CRITICAL'};
}
} elsif ($older == -1) {
if ($warn == 1) {
$output = 'There is at least one snapshot (' . $snapshot->createTime . ').';
$status |= $MYERRORS_MASK{'WARNING'};
}
if ($crit == 1) {
$output = 'There is at least one snapshot (' . $snapshot->createTime . ').';
$status |= $MYERRORS_MASK{'CRITICAL'};
}
}
}
print_response($ERRORS{$MYERRORS{$status}} . "|$output\n");
}
1;