Fix errors and better management vsphere4/5

git-svn-id: http://svn.merethis.net/centreon-esxd/trunk@83 a5eaa968-4c79-4d68-970d-af6011b5b055
This commit is contained in:
Quentin Garnier 2013-10-04 22:06:50 +00:00
parent 840697991b
commit 452e7cdbbd
6 changed files with 993 additions and 926 deletions

View File

@ -32,6 +32,9 @@ my %OPTION = (
skip_errors => undef,
filter => undef,
consolidation => undef,
check_disk_limit => undef,
# For Autodisco
xml => undef,
show_attributes => undef,
@ -64,12 +67,14 @@ GetOptions(
"crit" => \$OPTION{crit},
"on" => \$OPTION{on},
"check-consolidation" => \$OPTION{consolidation},
"check-disk" => \$OPTION{check_disk_limit},
"w|warning=f" => \$OPTION{warning},
"c|critical=f" => \$OPTION{critical},
"w|warning:f" => \$OPTION{warning},
"c|critical:f" => \$OPTION{critical},
"warning2=f" => \$OPTION{warning2},
"critical2=f" => \$OPTION{critical2},
"warning2:f" => \$OPTION{warning2},
"critical2:f" => \$OPTION{critical2},
"xml" => \$OPTION{xml},
"show-attributes" => \$OPTION{show_attributes},
@ -128,6 +133,8 @@ sub print_usage () {
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 " --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";
@ -183,12 +190,14 @@ sub print_usage () {
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 " --check-consolidation Check if VM needs consolidation (since vsphere 5.0)\n";
print "\n";
print "'limitvm':\n";
print " --vm VM to check (required)\n";
print " --filter Use regexp for --vm option (can check multiples vm at once)\n";
print " --warn Warning threshold if set (default)\n";
print " --crit Critical threshold if set\n";
print " --check-disk Check Disk limits (since vsphere 5.0)\n";
print "\n";
print "'datastoresvm':\n";
print " --vm VM to check (required)\n";
@ -309,21 +318,9 @@ sub datastoreusage_check_arg {
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{skip_errors})) {
$OPTION{skip_errors} = 1;
} else {
$OPTION{skip_errors} = 0;
}
$OPTION{filter} = (defined($OPTION{filter}) ? 1 : 0);
$OPTION{free} = (defined($OPTION{free}) ? 1 : 0);
$OPTION{skip_errors} = (defined($OPTION{skip_errors}) ? 1 : 0);
if (!defined($OPTION{warning})) {
$OPTION{warning} = ($OPTION{free} == 1) ? 20 : 80;
}
@ -385,12 +382,14 @@ sub datastoresnapshots_check_arg {
if (!defined($OPTION{critical2})) {
$OPTION{critical2} = '';
}
$OPTION{filter} = (defined($OPTION{filter}) ? 1 : 0);
$OPTION{skip_errors} = (defined($OPTION{skip_errors}) ? 1 : 0);
return 0;
}
sub datastoresnapshots_get_str {
return join($separatorin,
('datastore-snapshots', $OPTION{vsphere}, $OPTION{datastore}, $OPTION{warning}, $OPTION{critical}, $OPTION{warning2}, $OPTION{critical2}));
('datastore-snapshots', $OPTION{vsphere}, $OPTION{datastore}, $OPTION{filter}, $OPTION{warning}, $OPTION{critical}, $OPTION{warning2}, $OPTION{critical2}, $OPTION{skip_errors}));
}
sub cpuhost_check_arg {
@ -431,11 +430,7 @@ sub datastoreshost_check_arg {
if (!defined($OPTION{datastore})) {
$OPTION{datastore} = '';
}
if (defined($OPTION{filter})) {
$OPTION{filter} = 1;
} else {
$OPTION{filter} = 0;
}
$OPTION{filter} = (defined($OPTION{filter}) ? 1 : 0);
return 0;
}
@ -501,16 +496,8 @@ sub nethost_check_arg {
if (!defined($OPTION{critical})) {
$OPTION{critical} = 90;
}
if (defined($OPTION{filter})) {
$OPTION{filter} = 1;
} else {
$OPTION{filter} = 0;
}
if (defined($OPTION{skip_errors})) {
$OPTION{skip_errors} = 1;
} else {
$OPTION{skip_errors} = 0;
}
$OPTION{filter} = (defined($OPTION{filter}) ? 1 : 0);
$OPTION{skip_errors} = (defined($OPTION{skip_errors}) ? 1 : 0);
return 0;
}
@ -599,11 +586,8 @@ sub snapshotvm_check_arg {
print_usage();
exit $ERRORS{UNKNOWN};
}
if (defined($OPTION{filter})) {
$OPTION{filter} = 1;
} else {
$OPTION{filter} = 0;
}
$OPTION{filter} = (defined($OPTION{filter}) ? 1 : 0);
$OPTION{consolidation} = (defined($OPTION{consolidation}) ? 1 : 0);
if (!defined($OPTION{warning})) {
$OPTION{warning} = 86400 * 3;
}
@ -615,7 +599,7 @@ sub snapshotvm_check_arg {
sub snapshotvm_get_str {
return join($separatorin,
('snapshotvm', $OPTION{vsphere}, $OPTION{vm}, $OPTION{filter}, $OPTION{warning}, $OPTION{critical}));
('snapshotvm', $OPTION{vsphere}, $OPTION{vm}, $OPTION{filter}, $OPTION{warning}, $OPTION{critical}, $OPTION{consolidation}));
}
sub limitvm_check_arg {
@ -624,27 +608,20 @@ sub limitvm_check_arg {
print_usage();
exit $ERRORS{UNKNOWN};
}
if (defined($OPTION{filter})) {
$OPTION{filter} = 1;
} else {
$OPTION{filter} = 0;
}
$OPTION{filter} = (defined($OPTION{filter}) ? 1 : 0);
if ((!defined($OPTION{warn}) && !defined($OPTION{crit})) || defined($OPTION{warn})) {
$OPTION{warn} = 1;
} else {
$OPTION{warn} = 0;
}
if (!defined($OPTION{crit})) {
$OPTION{crit} = 0;
} else {
$OPTION{crit} = 1;
}
$OPTION{crit} = (defined($OPTION{crit}) ? 1 : 0);
$OPTION{check_disk_limit} = (defined($OPTION{check_disk_limit}) ? 1 : 0);
return 0;
}
sub limitvm_get_str {
return join($separatorin,
('limitvm', $OPTION{vsphere}, $OPTION{vm}, $OPTION{filter}, $OPTION{warn}, $OPTION{crit}));
('limitvm', $OPTION{vsphere}, $OPTION{vm}, $OPTION{filter}, $OPTION{warn}, $OPTION{crit}, $OPTION{check_disk_limit}));
}
sub datastoresvm_check_arg {
@ -713,21 +690,9 @@ sub thinprovisioningvm_check_arg {
print_usage();
exit $ERRORS{UNKNOWN};
}
if (!defined($OPTION{on})) {
$OPTION{on} = 0;
} else {
$OPTION{on} = 1;
}
if (!defined($OPTION{warn})) {
$OPTION{warn} = 0;
} else {
$OPTION{warn} = 1;
}
if (!defined($OPTION{crit})) {
$OPTION{crit} = 0;
} else {
$OPTION{crit} = 1;
}
$OPTION{on} = (defined($OPTION{on}) ? 1 : 0);
$OPTION{warn} = (defined($OPTION{warn}) ? 1 : 0);
$OPTION{crit} = (defined($OPTION{crit}) ? 1 : 0);
return 0;
}
@ -746,16 +711,8 @@ sub listhost_get_str {
}
sub listdatastore_check_arg {
if (!defined($OPTION{xml})) {
$OPTION{xml} = 0;
} else {
$OPTION{xml} = 1;
}
if (!defined($OPTION{show_attributes})) {
$OPTION{show_attributes} = 0;
} else {
$OPTION{show_attributes} = 1;
}
$OPTION{xml} = (defined($OPTION{xml}) ? 1 : 0);
$OPTION{show_attributes} = (defined($OPTION{show_attributes}) ? 1 : 0);
return 0;
}
@ -770,16 +727,8 @@ sub listnichost_check_arg {
print_usage();
exit $ERRORS{UNKNOWN};
}
if (!defined($OPTION{xml})) {
$OPTION{xml} = 0;
} else {
$OPTION{xml} = 1;
}
if (!defined($OPTION{show_attributes})) {
$OPTION{show_attributes} = 0;
} else {
$OPTION{show_attributes} = 1;
}
$OPTION{xml} = (defined($OPTION{xml}) ? 1 : 0);
$OPTION{show_attributes} = (defined($OPTION{show_attributes}) ? 1 : 0);
return 0;
}

View File

@ -59,30 +59,45 @@ sub checkArgs {
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] : '');
$self->{filter} = (defined($_[1]) && $_[1] == 1) ? 1 : 0;
$self->{warn} = (defined($_[2]) ? $_[2] : '');
$self->{crit} = (defined($_[3]) ? $_[3] : '');
$self->{warn2} = (defined($_[4]) ? $_[4] : '');
$self->{crit2} = (defined($_[5]) ? $_[5] : '');
$self->{skip_errors} = (defined($_[6]) && $_[6] == 1) ? 1 : 0;
}
sub run {
my $self = shift;
my %filters = ();
my %filters = ('summary.name' => $self->{ds});
my @properties = ('summary.accessible', 'browser');
if ($self->{filter} == 0) {
$filters{name} = qr/^\Q$self->{ds}\E$/;
} else {
$filters{name} = qr/$self->{ds}/;
}
my @properties = ('summary.accessible', 'summary.name', '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);
my @ds_array = ();
my %ds_names = ();
foreach my $entity_view (@$result) {
next if (!centreon::esxd::common::is_accessible($entity_view->{'summary.accessible'}));
if (defined($entity_view->browser)) {
push @ds_array, $entity_view->browser;
if ($self->{filter} == 1) {
$ds_names{$entity_view->{mo_ref}->{value}} = $entity_view->{'summary.name'};
}
}
}
@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 $result2;
return if (!($result2 = centreon::esxd::common::get_views($self->{obj_esxd}, \@ds_array, \@properties)));
my $status = 0; # OK
my $output = '';
@ -91,46 +106,119 @@ sub run {
my $output_warning_append = '';
my $output_critical = "";
my $output_critical_append = '';
my $total_size = 0;
my $output_warning_total = "";
my $output_warning_total_append = '';
my $output_critical_total = "";
my $output_critical_total_append = '';
my $output_unknown = '';
my $output_unknown_append = '';
my $output_ok_unit = '';
my $perfdata = '';
foreach my $browse_ds (@$result2) {
my $dsName;
if ($self->{filter} == 1) {
my $tmp_name = $browse_ds->{mo_ref}->{value};
$tmp_name =~ s/^datastoreBrowser-//i;
$dsName = $ds_names{$tmp_name};
} else {
$dsName = $self->{ds};
}
my ($snapshots, $msg) = centreon::esxd::common::search_in_datastore($self->{obj_esxd}, $browse_ds, '[' . $dsName . ']', [VmSnapshotFileQuery->new()], 1);
if (!defined($snapshots)) {
$msg =~ s/\n/ /g;
if ($msg =~ /NoPermissionFault/i) {
$msg = "Not enough permissions";
}
if ($self->{skip_errors} == 0 || $self->{filter} == 0) {
$status = centreon::esxd::common::errors_mask($status, 'UNKNOWN');
centreon::esxd::common::output_add(\$output_unknown, \$output_unknown_append, ", ",
"'" . $dsName . "' $msg");
}
next;
}
my $total_size = 0;
my $lwarn = '';
my $lcrit = '';
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 . "'");
$lwarn .= " [" . $_->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 . "'");
$lcrit .= " [" . $_->folderPath . ']=>[' . $x->path . "]";
}
$total_size += $x->fileSize;
}
}
}
if ($output_critical ne '') {
$output .= "CRITICAL - Snapshot size exceed limit: $output_critical.";
$output_append = " ";
if ($lcrit ne '') {
centreon::esxd::common::output_add(\$output_critical, \$output_critical_append, ", ",
"'$dsName'" . $lcrit);
}
if ($output_warning ne '') {
$output .= $output_append . "WARNING - Snapshot size exceed limit: $output_warning.";
$output_append = " ";
if ($lwarn ne '') {
centreon::esxd::common::output_add(\$output_warning, \$output_warning_append, ", ",
"'$dsName'" . $lwarn);
}
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.";
centreon::esxd::common::output_add(\$output_critical_total, \$output_critical_total_append, ", ",
"'$dsName' Total snapshots used " . 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.";
centreon::esxd::common::output_add(\$output_warning_total, \$output_warning_total_append, ", ",
"'$dsName' Total snapshots used " . 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_ok_unit .= "'$dsName' 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");
if ($self->{filter} == 1) {
$perfdata .= " 'total_size_" . $dsName . "'=" . $total_size . "o;$self->{warn};$self->{crit};0;";
} else {
$perfdata .= " 'total_size=" . $total_size . "o;$self->{warn};$self->{crit};0;";
}
}
if ($output_unknown ne "") {
$output .= $output_append . "UNKNOWN - $output_unknown";
$output_append = ". ";
}
if ($output_critical_total ne '' || $output_critical ne '') {
$output .= $output_append . "CRITICAL -";
if ($output_critical_total ne '') {
$output .= " " . $output_critical_total;
$output_append = ' -';
}
if ($output_critical ne '') {
$output .= $output_append . " Snapshots size exceed limit: " . $output_critical;
}
$output_append = '. ';
}
if ($output_warning_total ne '' || $output_warning ne '') {
$output .= $output_append . "WARNING -";
if ($output_warning_total ne '') {
$output .= " " . $output_warning_total;
$output_append = ' -';
}
if ($output_warning ne '') {
$output .= $output_append . " Snapshots size exceed limit: " . $output_warning;
}
}
if ($status == 0) {
if ($self->{filter} == 1) {
$output .= $output_append . "All Total snapshots size is ok";
} else {
$output .= $output_append . $output_ok_unit;
}
}
$self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|$output|$perfdata\n");
}
1;

View File

@ -102,12 +102,15 @@ sub run {
if (!centreon::esxd::common::is_accessible($ds->summary->accessible)) {
if ($self->{skip_errors} == 0 || $self->{filter} == 0) {
$status = centreon::esxd::common::errors_mask($status, 'UNKNOWN');
}
centreon::esxd::common::output_add(\$output_unknown, \$output_unknown_append, ", ",
"'" . $ds->summary->name . "' not accessible. Can be disconnected");
}
next;
}
# capacity 0...
next if ($ds->summary->capacity <= 0);
my $dsName = $ds->summary->name;
my $capacity = $ds->summary->capacity;
my $free = $ds->summary->freeSpace;

View File

@ -38,6 +38,7 @@ sub initArgs {
$self->{filter} = (defined($_[1]) && $_[1] == 1) ? 1 : 0;
$self->{warn} = (defined($_[2]) && $_[2] == 1) ? 1 : 0;
$self->{crit} = (defined($_[3]) && $_[3] == 1) ? 1 : 0;
$self->{disk} = (defined($_[4]) && $_[4] == 1) ? 1 : 0;
if ($self->{warn} == 0 && $self->{crit} == 0) {
$self->{warn} = 1;
}
@ -53,7 +54,12 @@ sub run {
} else {
$filters{name} = qr/$self->{lvm}/;
}
my @properties = ('name', 'config.hardware.device', 'config.cpuAllocation.limit', 'config.memoryAllocation.limit');
my @properties;
push @properties, 'name', 'config.cpuAllocation.limit', 'config.memoryAllocation.limit';
if ($self->{disk} == 1) {
push @properties, 'config.hardware.device';
}
my $result = centreon::esxd::common::get_entities_host($self->{obj_esxd}, 'VirtualMachine', \%filters, \@properties);
if (!defined($result)) {
return ;
@ -92,6 +98,7 @@ sub run {
}
# Disk
if ($self->{disk} == 1) {
foreach my $device (@{$virtual->{'config.hardware.device'}}) {
if ($device->isa('VirtualDisk')) {
if ($self->{crit} == 1 && defined($device->storageIOAllocation->limit) && $device->storageIOAllocation->limit != -1) {
@ -103,6 +110,7 @@ sub run {
}
}
}
}
# Set
if ($limit_set_crit ne '') {

View File

@ -50,6 +50,7 @@ sub initArgs {
$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);
$self->{consolidate} = (defined($_[4]) && $_[4] == 1) ? 1 : 0;
}
sub run {
@ -68,7 +69,12 @@ sub run {
} else {
$filters{name} = qr/$self->{lvm}/;
}
my @properties = ('snapshot.rootSnapshotList', 'name');
my @properties;
push @properties, 'snapshot.rootSnapshotList', 'name';
if ($self->{consolidate} == 1) {
push @properties, 'runtime.consolidationNeeded';
}
my $result = centreon::esxd::common::get_entities_host($self->{obj_esxd}, 'VirtualMachine', \%filters, \@properties);
if (!defined($result)) {
return ;
@ -84,8 +90,16 @@ sub run {
my $output_unknown = '';
my $output_unknown_append = '';
my $output_ok_unit = 'Snapshot(s) OK';
my $consolidate_vms = '';
my $consolidate_vms_append = '';
foreach my $virtual (@$result) {
if ($self->{consolidate} == 1 && defined($virtual->{'runtime.consolidationNeeded'}) && ($virtual->{'runtime.consolidationNeeded'} == 1 || $virtual->{'runtime.consolidationNeeded'} =~ /^true$/i)) {
$status = centreon::esxd::common::errors_mask($status, 'CRITICAL');
$consolidate_vms .= $consolidate_vms_append . '[' . $virtual->{'name'} . ']';
$consolidate_vms_append = ', ';
}
if (!defined($virtual->{'snapshot.rootSnapshotList'})) {
next;
}
@ -96,7 +110,7 @@ sub run {
if (!defined($create_time)) {
$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'} . "'");
"Can't Parse date '" . $snapshot->createTime . "' for vm [" . $virtual->{'name'} . "]");
next;
}
if (time() - $create_time >= $self->{critical}) {
@ -117,6 +131,10 @@ sub run {
$output .= $output_append . "UNKNOWN - $output_unknown";
$output_append = ". ";
}
if ($consolidate_vms ne "") {
$output .= $output_append . "CRITICAL - VMs need consolidation : " . $consolidate_vms;
$output_append = ". ";
}
if ($output_critical ne "") {
$output .= $output_append . "CRITICAL - Snapshots for VM older than " . ($self->{critical} / 86400) . " days: $output_critical";
$output_append = ". ";

View File

@ -121,7 +121,7 @@ sub get_view {
sub search_in_datastore {
my $obj_esxd = shift;
my ($ds_browse, $ds_name, $query) = @_;
my ($ds_browse, $ds_name, $query, $return) = @_;
my $result;
my $files = FileQueryFlags->new(fileSize => 1,
@ -136,6 +136,7 @@ sub search_in_datastore {
searchSpec=>$hostdb_search_spec);
};
if ($@) {
return (undef, $@) if (defined($return) && $return == 1);
vmware_error($obj_esxd, $@);
return undef;
}