This commit is contained in:
garnier-quentin 2019-07-10 10:17:17 +02:00
parent 5beb104106
commit 9ba5232416
1 changed files with 52 additions and 24 deletions

View File

@ -32,25 +32,28 @@ sub new {
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => { $options{options}->add_options(arguments => {
"vm-hostname:s" => { name => 'vm_hostname' }, 'vm-hostname:s' => { name => 'vm_hostname' },
"filter" => { name => 'filter' }, 'filter' => { name => 'filter' },
"scope-datacenter:s" => { name => 'scope_datacenter' }, 'scope-datacenter:s' => { name => 'scope_datacenter' },
"scope-cluster:s" => { name => 'scope_cluster' }, 'scope-cluster:s' => { name => 'scope_cluster' },
"scope-host:s" => { name => 'scope_host' }, 'scope-host:s' => { name => 'scope_host' },
"filter-description:s" => { name => 'filter_description' }, 'filter-description:s' => { name => 'filter_description' },
"filter-os:s" => { name => 'filter_os' }, 'filter-os:s' => { name => 'filter_os' },
"filter-uuid:s" => { name => 'filter_uuid' }, 'filter-uuid:s' => { name => 'filter_uuid' },
"display-description" => { name => 'display_description' }, 'display-description' => { name => 'display_description' },
"check-consolidation" => { name => 'check_consolidation' }, 'check-consolidation' => { name => 'check_consolidation' },
"nopoweredon-skip" => { name => 'nopoweredon_skip' }, 'nopoweredon-skip' => { name => 'nopoweredon_skip' },
"warning:s" => { name => 'warning' }, 'warning:s' => { name => 'warning' },
"critical:s" => { name => 'critical' }, 'critical:s' => { name => 'critical' },
"disconnect-status:s" => { name => 'disconnect_status', default => 'unknown' }, 'disconnect-status:s' => { name => 'disconnect_status', default => 'unknown' },
'unit:s' => { name => 'unit', default => 's' },
}); });
return $self; return $self;
} }
my $unitdiv = { s => ['seconds', 1], w => ['weeks', 604800], d => ['days', 86400], h => ['hours', 3600], m => ['minutes', 60] };
sub check_options { sub check_options {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->SUPER::init(%options); $self->SUPER::init(%options);
@ -68,6 +71,10 @@ sub check_options {
$self->{output}->add_option_msg(short_msg => "Wrong disconnect-status option '" . $self->{option_results}->{disconnect_status} . "'."); $self->{output}->add_option_msg(short_msg => "Wrong disconnect-status option '" . $self->{option_results}->{disconnect_status} . "'.");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
if ($self->{option_results}->{unit} eq '' || !defined($unitdiv->{$self->{option_results}->{unit}})) {
$self->{option_results}->{unit} = 's';
}
} }
sub run { sub run {
@ -119,7 +126,7 @@ sub run {
my $diff_time = time() - $create_time; my $diff_time = time() - $create_time;
my $days = int($diff_time / 60 / 60 / 24); my $days = int($diff_time / 60 / 60 / 24);
my $exit = $self->{perfdata}->threshold_check(value => $diff_time, threshold => [ { label => 'critical', exit_litteral => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); my $exit = $self->{perfdata}->threshold_check(value => ($diff_time / $unitdiv->{$self->{option_results}->{unit}}->[1]), threshold => [ { label => 'critical', exit_litteral => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]);
my $prefix_msg = "'$vm_name'"; my $prefix_msg = "'$vm_name'";
if (defined($self->{display_description}) && defined($response->{data}->{$vm_id}->{'config.annotation'}) && if (defined($self->{display_description}) && defined($response->{data}->{$vm_id}->{'config.annotation'}) &&
@ -150,19 +157,35 @@ sub run {
min => 0 min => 0
); );
if (scalar(keys %{$vm_errors{warning}}) > 0) { if (scalar(keys %{$vm_errors{warning}}) > 0) {
$self->{output}->output_add(severity => 'WARNING', $self->{output}->output_add(
short_msg => sprintf('Snapshots for VM older than %d days: [%s]', ($self->{option_results}->{warning} / 86400), severity => 'WARNING',
join('] [', sort keys %{$vm_errors{warning}}))); short_msg => sprintf(
'Snapshots for VM older than %d %s: [%s]',
$self->{option_results}->{warning},
$unitdiv->{$self->{option_results}->{unit}}->[0],
join('] [', sort keys %{$vm_errors{warning}})
)
);
} }
if (scalar(keys %{$vm_errors{critical}}) > 0) { if (scalar(keys %{$vm_errors{critical}}) > 0) {
$self->{output}->output_add(severity => 'CRITICAL', $self->{output}->output_add(
short_msg => sprintf('Snapshots for VM older than %d days: [%s]', ($self->{option_results}->{critical} / 86400), severity => 'CRITICAL',
join('] [', sort keys %{$vm_errors{critical}}))); short_msg => sprintf(
'Snapshots for VM older than %d %s: [%s]',
$self->{option_results}->{critical},
$unitdiv->{$self->{option_results}->{unit}}->[0],
join('] [', sort keys %{$vm_errors{critical}})
)
);
} }
if (scalar(keys %vm_consolidate) > 0) { if (scalar(keys %vm_consolidate) > 0) {
$self->{output}->output_add(severity => 'CRITICAL', $self->{output}->output_add(
short_msg => sprintf('VMs need consolidation: [%s]', severity => 'CRITICAL',
join('] [', sort keys %vm_consolidate))); short_msg => sprintf(
'VMs need consolidation: [%s]',
join('] [', sort keys %vm_consolidate)
)
);
} }
$self->{output}->display(); $self->{output}->display();
@ -224,6 +247,11 @@ Status if VM disconnected (default: 'unknown').
Skip check if VM is not poweredOn. Skip check if VM is not poweredOn.
=item B<--unit>
Select the unit for performance data and thresholds. May be 's' for seconds, 'm' for minutes,
'h' for hours, 'd' for days, 'w' for weeks. Default is seconds
=item B<--warning> =item B<--warning>
Threshold warning in seconds. Threshold warning in seconds.