This commit is contained in:
Quentin Garnier 2014-10-29 10:28:20 +01:00
parent 1b5de2d360
commit 68c7a186b9
3 changed files with 164 additions and 225 deletions

View File

@ -0,0 +1,163 @@
package centreon::esxd::cmddatastoresnapshot;
use strict;
use warnings;
use centreon::esxd::common;
sub new {
my $class = shift;
my $self = {};
$self->{logger} = shift;
$self->{commandName} = 'datastoresnapshot';
bless $self, $class;
return $self;
}
sub getCommandName {
my $self = shift;
return $self->{commandName};
}
sub checkArgs {
my ($self, %options) = @_;
if (defined($options{arguments}->{datastore_name}) && $options{arguments}->{datastore_name} eq "") {
$options{manager}->{output}->output_add(severity => 'UNKNOWN',
short_msg => "Argument error: datastore name cannot be null");
return 1;
}
if (defined($options{arguments}->{disconnect_status}) &&
$options{manager}->{output}->is_litteral_status(status => $options{arguments}->{disconnect_status}) == 0) {
$options{manager}->{output}->output_add(severity => 'UNKNOWN',
short_msg => "Argument error: wrong value for disconnect status '" . $options{arguments}->{disconnect_status} . "'");
return 1;
}
foreach my $label (('warning_total', 'critical_total', 'warning_snapshot', 'critical_snapshot')) {
if (($options{manager}->{perfdata}->threshold_validate(label => $label, value => $options{arguments}->{$label})) == 0) {
$options{manager}->{output}->output_add(severity => 'UNKNOWN',
short_msg => "Argument error: wrong value for $label value '" . $options{arguments}->{$label} . "'.");
return 1;
}
}
return 0;
}
sub initArgs {
my ($self, %options) = @_;
foreach (keys %{$options{arguments}}) {
$self->{$_} = $options{arguments}->{$_};
}
$self->{manager} = centreon::esxd::common::init_response();
$self->{manager}->{output}->{plugin} = $options{arguments}->{identity};
foreach my $label (('warning_total', 'critical_total', 'warning_snapshot', 'critical_snapshot')) {
$self->{manager}->{perfdata}->threshold_validate(label => $label, value => $options{arguments}->{$label});
}
}
sub set_connector {
my ($self, %options) = @_;
$self->{obj_esxd} = $options{connector};
}
sub run {
my $self = shift;
my %filters = ();
my $multiple = 0;
if (defined($self->{datastore_name}) && !defined($self->{filter})) {
$filters{name} = qr/^\Q$self->{datastore_name}\E$/;
} elsif (!defined($self->{datastore_name})) {
$filters{name} = qr/.*/;
} else {
$filters{name} = qr/$self->{datastore_name}/;
}
my @properties = ('summary.accessible', 'summary.name', 'browser');
my $result = centreon::esxd::common::get_entities_host($self->{obj_esxd}, 'Datastore', \%filters, \@properties);
return if (!defined($result));
if (scalar(@$result) > 1) {
$multiple = 1;
}
my @ds_array = ();
my %ds_names = ();
foreach my $entity_view (@$result) {
next if (centreon::esxd::common::datastore_state(connector => $self->{obj_esxd},
name => $entity_view->{'summary.name'},
state => $entity_view->{'summary.accessible'},
status => $self->{disconnect_status},
multiple => $multiple) == 0);
if (defined($entity_view->browser)) {
push @ds_array, $entity_view->browser;
$ds_names{$entity_view->{mo_ref}->{value}} = $entity_view->{'summary.name'};
}
}
@properties = ();
my $result2;
return if (!($result2 = centreon::esxd::common::get_views($self->{obj_esxd}, \@ds_array, \@properties)));
$self->{manager}->{output}->output_add(severity => 'OK',
short_msg => sprintf("All snapshot sizes are ok"));
foreach my $browse_ds (@$result2) {
my $dsName;
my $tmp_name = $browse_ds->{mo_ref}->{value};
$tmp_name =~ s/^datastoreBrowser-//i;
$dsName = $ds_names{$tmp_name};
$self->{manager}->{output}->output_add(long_msg => "Checking datastore '$dsName':");
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 ($multiple == 0) {
$self->{manager}->{output}->output_add(severity => 'UNKNOWN',
short_msg => sprintf("Datastore '%s' %s", $dsName, $msg));
}
next;
}
my $total_size = 0;
my $lwarn = '';
my $lcrit = '';
foreach (@$snapshots) {
if (defined($_->file)) {
foreach my $x (@{$_->file}) {
my $exit = $self->{manager}->{perfdata}->threshold_check(value => $x->fileSize, threshold => [ { label => 'critical_snapshot', exit_litteral => 'critical' }, { label => 'warning_snapshot', exit_litteral => 'warning' } ]);
$self->{manager}->{output}->set_status(exit_litteral => $exit);
my ($size_value, $size_unit) = $self->{manager}->{perfdata}->change_bytes(value => $x->fileSize);
$self->{manager}->{output}->output_add(long_msg => sprintf(" %s: snapshot [%s]=>[%s] [size = %s]",
$exit, $_->folderPath, $x->path, $size_value . ' ' . $size_unit));
$total_size += $x->fileSize;
}
}
}
my $exit = $self->{manager}->{perfdata}->threshold_check(value => $total_size, threshold => [ { label => 'critical_total', exit_litteral => 'critical' }, { label => 'warning_total', exit_litteral => 'warning' } ]);
my ($size_value, $size_unit) = $self->{manager}->{perfdata}->change_bytes(value => $total_size);
$self->{manager}->{output}->set_status(exit_litteral => $exit);
$self->{manager}->{output}->output_add(long_msg => sprintf(" %s: total snapshots [size = %s]",
$exit, $size_value . ' ' . $size_unit));
my $extra_label = '';
$extra_label = '_' . $dsName if ($multiple == 1);
$self->{manager}->{output}->perfdata_add(label => 'total_size' . $extra_label, unit => 'B',
value => $total_size,
warning => $self->{manager}->{perfdata}->get_perfdata_for_output(label => 'warning_total'),
critical => $self->{manager}->{perfdata}->get_perfdata_for_output(label => 'critical_total'),
min => 0);
}
if (!$self->{manager}->{output}->is_status(compare => 'ok', litteral => 1)) {
$self->{manager}->{output}->output_add(severity => $self->{manager}->{output}->get_litteral_status(),
short_msg => sprintf("Snapshot sizes exceed limits"));
}
}
1;

View File

@ -1,224 +0,0 @@
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, $filter, $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->{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 = ();
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 ;
}
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 $result2;
return if (!($result2 = centreon::esxd::common::get_views($self->{obj_esxd}, \@ds_array, \@properties)));
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_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');
$lwarn .= " [" . $_->folderPath . ']=>[' . $x->path . "]";
} elsif (defined($self->{warn2}) && $self->{warn2} ne '' && $x->fileSize >= $self->{warn2}) {
$status = centreon::esxd::common::errors_mask($status, 'WARNING');
$lcrit .= " [" . $_->folderPath . ']=>[' . $x->path . "]";
}
$total_size += $x->fileSize;
}
}
}
if ($lcrit ne '') {
centreon::esxd::common::output_add(\$output_critical, \$output_critical_append, ", ",
"'$dsName'" . $lcrit);
}
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}) {
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}) {
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_ok_unit .= "'$dsName' Total snapshots size is ok.";
}
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

@ -35,7 +35,7 @@ my @load_modules = (
'centreon::esxd::cmddatastoreio',
'centreon::esxd::cmddatastoreiops',
'centreon::esxd::cmddatastorehost',
'centreon::esxd::cmddatastoresnapshots',
'centreon::esxd::cmddatastoresnapshot',
'centreon::esxd::cmddatastorevm',
'centreon::esxd::cmddatastoreusage',
'centreon::esxd::cmdgetmap',