update datastore usage

This commit is contained in:
garnier-quentin 2019-10-04 13:08:31 +02:00
parent 489a2ed95b
commit fcfa76947d
1 changed files with 97 additions and 108 deletions

View File

@ -40,86 +40,30 @@ sub custom_status_calc {
return 0;
}
sub custom_usage_perfdata {
my ($self, %options) = @_;
my ($label, $nlabel) = ('used', $self->{nlabel});
my $value_perf = $self->{result_values}->{used};
if (defined($self->{instance_mode}->{option_results}->{free})) {
($label, $nlabel) = ('free', 'datastore.space.free.bytes');
$value_perf = $self->{result_values}->{free};
}
my %total_options = ();
if ($self->{instance_mode}->{option_results}->{units} eq '%') {
$total_options{total} = $self->{result_values}->{total};
$total_options{cast_int} = 1;
}
$self->{output}->perfdata_add(
label => $label, unit => 'B',
instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
nlabel => $nlabel,
value => $value_perf,
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options),
min => 0, max => $self->{result_values}->{total}
);
}
sub custom_usage_threshold {
my ($self, %options) = @_;
my ($exit, $threshold_value);
$threshold_value = $self->{result_values}->{used};
$threshold_value = $self->{result_values}->{free} if (defined($self->{instance_mode}->{option_results}->{free}));
if ($self->{instance_mode}->{option_results}->{units} eq '%') {
$threshold_value = $self->{result_values}->{prct_used};
$threshold_value = $self->{result_values}->{prct_free} if (defined($self->{instance_mode}->{option_results}->{free}));
}
$exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' } ]);
return $exit;
}
sub custom_usage_output {
my ($self, %options) = @_;
my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
my $msg = sprintf("Usage Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)",
$total_size_value . " " . $total_size_unit,
$total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
$total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free});
my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total_space_absolute});
my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used_space_absolute});
my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used_space_absolute});
my $msg = sprintf(
'Usage Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)',
$total_size_value . " " . $total_size_unit,
$total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used_space_absolute},
$total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free_space_absolute}
);
return $msg;
}
sub custom_usage_calc {
my ($self, %options) = @_;
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
$self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'};
if ($self->{result_values}->{total} <= 0) {
$self->{error_msg} = 'size is 0';
return -20;
}
$self->{result_values}->{free} = $options{new_datas}->{$self->{instance} . '_free'};
$self->{result_values}->{used} = $self->{result_values}->{total} - $self->{result_values}->{free};
$self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
$self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used};
return 0;
}
sub custom_provisioned_output {
my ($self, %options) = @_;
my ($total_uncomitted_value, $total_uncommitted_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total_uncommitted});
my $msg = sprintf("Provisioned: %s (%.2f%%)",
$total_uncomitted_value . " " . $total_uncommitted_unit,
$self->{result_values}->{prct_uncommitted});
my $msg = sprintf(
'Provisioned: %s (%.2f%%)',
$total_uncomitted_value . " " . $total_uncommitted_unit,
$self->{result_values}->{prct_uncommitted}
);
return $msg;
}
@ -127,15 +71,15 @@ sub custom_provisioned_calc {
my ($self, %options) = @_;
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
$self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'};
$self->{result_values}->{total_space} = $options{new_datas}->{$self->{instance} . '_total_space'};
if ($self->{result_values}->{total} <= 0) {
if ($self->{result_values}->{total_space} <= 0) {
return -10;
}
$self->{result_values}->{total_uncommitted} =
($self->{result_values}->{total} - $options{new_datas}->{$self->{instance} . '_free'}) + $options{new_datas}->{$self->{instance} . '_uncommitted'};
$self->{result_values}->{prct_uncommitted} = $self->{result_values}->{total_uncommitted} * 100 / $self->{result_values}->{total};
($self->{result_values}->{total_space} - $options{new_datas}->{$self->{instance} . '_free_space'}) + $options{new_datas}->{$self->{instance} . '_uncommitted'};
$self->{result_values}->{prct_uncommitted} = $self->{result_values}->{total_uncommitted} * 100 / $self->{result_values}->{total_space};
return 0;
}
@ -157,21 +101,40 @@ sub set_counters {
}
},
{ label => 'usage', nlabel => 'datastore.space.usage.bytes', set => {
key_values => [ { name => 'display' }, { name => 'free' }, { name => 'total' } ],
closure_custom_calc => $self->can('custom_usage_calc'),
key_values => [ { name => 'used_space' }, { name => 'free_space' }, { name => 'prct_used_space' }, { name => 'prct_free_space' }, { name => 'total_space' }, { name => 'display' }, ],
closure_custom_output => $self->can('custom_usage_output'),
closure_custom_perfdata => $self->can('custom_usage_perfdata'),
closure_custom_threshold_check => $self->can('custom_usage_threshold'),
perfdatas => [
{ label => 'used', value => 'used_space_absolute', template => '%d', min => 0, max => 'total_space_absolute',
unit => 'B', cast_int => 1, label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'usage-free', nlabel => 'datastore.space.free.bytes', display_ok => 0, set => {
key_values => [ { name => 'free_space' }, { name => 'used_space' }, { name => 'prct_used_space' }, { name => 'prct_free_space' }, { name => 'total_space' }, { name => 'display' }, ],
closure_custom_output => $self->can('custom_usage_output'),
perfdatas => [
{ label => 'free', value => 'free_space_absolute', template => '%d', min => 0, max => 'total_space_absolute',
unit => 'B', cast_int => 1, label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'usage-prct', nlabel => 'datastore.space.usage.percentage', display_ok => 0, set => {
key_values => [ { name => 'prct_used_space' }, { name => 'display' } ],
output_template => 'used : %.2f %%',
perfdatas => [
{ label => 'used_prct', value => 'prct_used_space_absolute', template => '%.2f', min => 0, max => 100,
unit => '%', label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'provisioned', nlabel => 'datastore.space.provisioned.bytes', set => {
key_values => [ { name => 'display' }, { name => 'uncommitted' }, { name => 'total' }, { name => 'free' } ],
key_values => [ { name => 'display' }, { name => 'uncommitted' }, { name => 'total_space' }, { name => 'free_space' } ],
closure_custom_calc => $self->can('custom_provisioned_calc'),
closure_custom_output => $self->can('custom_provisioned_output'),
threshold_use => 'prct_uncommitted',
perfdatas => [
{ label => 'provisioned', value => 'total_uncommitted', template => '%s', unit => 'B',
min => 0, max => 'total', label_extra_instance => 1 },
min => 0, max => 'total_space', label_extra_instance => 1 },
],
}
},
@ -190,14 +153,14 @@ sub new {
bless $self, $class;
$options{options}->add_options(arguments => {
"datastore-name:s" => { name => 'datastore_name' },
"filter" => { name => 'filter' },
"scope-datacenter:s" => { name => 'scope_datacenter' },
"units:s" => { name => 'units', default => '%' },
"free" => { name => 'free' },
"unknown-status:s" => { name => 'unknown_status', default => '%{accessible} !~ /^true|1$/i' },
"warning-status:s" => { name => 'warning_status', default => '' },
"critical-status:s" => { name => 'critical_status', default => '' },
'datastore-name:s' => { name => 'datastore_name' },
'filter' => { name => 'filter' },
'scope-datacenter:s' => { name => 'scope_datacenter' },
'units:s' => { name => 'units', default => '%' },
'free' => { name => 'free' },
'unknown-status:s' => { name => 'unknown_status', default => '%{accessible} !~ /^true|1$/i' },
'warning-status:s' => { name => 'warning_status', default => '' },
'critical-status:s' => { name => 'critical_status', default => '' },
});
return $self;
@ -205,6 +168,15 @@ sub new {
sub check_options {
my ($self, %options) = @_;
# Compatibility
$self->compat_threshold_counter(%options,
compat => {
th => [ ['usage', { free => 'usage-free', prct => 'usage-prct'} ], [ 'datastore.space.usage.bytes', { free => 'datastore.space.free.bytes', prct => 'datastore.space.usage.percentage' } ] ],
units => $options{option_results}->{units}, free => $options{option_results}->{free}
}
);
$self->SUPER::check_options(%options);
$self->change_macros(macros => ['unknown_status', 'warning_status', 'critical_status']);
@ -214,19 +186,48 @@ sub check_options {
}
}
sub custom_usage_calc {
my ($self, %options) = @_;
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
$self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'};
if ($self->{result_values}->{total} <= 0) {
$self->{error_msg} = 'size is 0';
return -20;
}
$self->{result_values}->{free} = $options{new_datas}->{$self->{instance} . '_free'};
$self->{result_values}->{used} = $self->{result_values}->{total} - $self->{result_values}->{free};
$self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
$self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used};
return 0;
}
sub manage_selection {
my ($self, %options) = @_;
$self->{datastore} = {};
my $response = $options{custom}->execute(params => $self->{option_results},
command => 'datastoreusage');
my $response = $options{custom}->execute(
params => $self->{option_results},
command => 'datastoreusage'
);
foreach my $ds_id (keys %{$response->{data}}) {
my $ds_name = $response->{data}->{$ds_id}->{name};
if ($response->{data}->{$ds_id}->{size} <= 0) {
$self->{output}->output_add(long_msg => "skipping datastore '" . $ds_name . "': no total size");
next;
}
$self->{datastore}->{$ds_name} = {
display => $ds_name,
accessible => $response->{data}->{$ds_id}->{accessible},
free => $response->{data}->{$ds_id}->{free},
total => $response->{data}->{$ds_id}->{size},
used_space => $response->{data}->{$ds_id}->{size} - $response->{data}->{$ds_id}->{free},
free_space => $response->{data}->{$ds_id}->{free},
total_space => $response->{data}->{$ds_id}->{size},
prct_used_space => ($response->{data}->{$ds_id}->{size} - $response->{data}->{$ds_id}->{free}) * 100 / $response->{data}->{$ds_id}->{size},
prct_free_space => $response->{data}->{$ds_id}->{free} * 100 / $response->{data}->{$ds_id}->{size},
uncommitted => $response->{data}->{$ds_id}->{uncommitted},
};
}
@ -254,14 +255,6 @@ Datastore name is a regexp.
Search in following datacenter(s) (can be a regexp).
=item B<--units>
Units of thresholds (Default: '%') ('%', 'B').
=item B<--free>
Thresholds are on free space left.
=item B<--unknown-status>
Set warning threshold for status (Default: '%{accessible} !~ /^true|1$/i').
@ -277,15 +270,11 @@ Can used special variables like: %{accessible}
Set critical threshold for status (Default: '').
Can used special variables like: %{accessible}
=item B<--warning-*>
=item B<--warning-*> B<--critical-*>
Threshold warning.
Can be: 'usage', 'provisioned'.
=item B<--critical-*>
Threshold critical.
Can be: 'usage', 'provisioned'.
Thresholds.
Can be: Can be: 'usage' (B), 'usage-free' (B), 'usage-prct' (%),
'provisioned'.
=back