(plugin) apps::proxmox::ve::restapi - add filtering capabilities to s… (#3665)

This commit is contained in:
qgarnier 2022-05-20 09:56:53 +02:00 committed by GitHub
parent 4d9e3a8dfe
commit a20e81ae00
6 changed files with 164 additions and 168 deletions

View File

@ -233,7 +233,7 @@ sub api_list_vms {
Type => $type, Type => $type,
Vmid => $vmid, Vmid => $vmid,
Node => $vm->{node}, Node => $vm->{node},
Name => $vm->{name}, Name => $vm->{name}
}; };
} }
@ -255,7 +255,7 @@ sub api_list_nodes {
foreach my $node (@{$list_nodes}) { foreach my $node (@{$list_nodes}) {
$nodes->{$node->{id}} = { $nodes->{$node->{id}} = {
State => $node->{status}, State => $node->{status},
Name => $node->{node}, Name => $node->{node}
}; };
} }
@ -278,7 +278,7 @@ sub api_list_storages {
$storages->{$storage->{id}} = { $storages->{$storage->{id}} = {
State => $storage->{status}, State => $storage->{status},
Node => $storage->{node}, Node => $storage->{node},
Name => $storage->{storage}, Name => $storage->{storage}
}; };
} }
@ -319,7 +319,7 @@ sub cache_nodes {
foreach my $node (@{$list_nodes}) { foreach my $node (@{$list_nodes}) {
$nodes->{ $node->{id} } = { $nodes->{ $node->{id} } = {
State => $node->{status}, State => $node->{status},
Name => $node->{node}, Name => $node->{node}
}; };
} }
$options{statefile}->write(data => $nodes); $options{statefile}->write(data => $nodes);
@ -340,7 +340,8 @@ sub cache_storages {
foreach my $storage (@{$list_storages}) { foreach my $storage (@{$list_storages}) {
$storages->{$storage->{id}} = { $storages->{$storage->{id}} = {
State => $storage->{status}, State => $storage->{status},
Name => $storage->{storage}, Node => $storage->{node},
Name => $storage->{storage}
}; };
} }
$options{statefile}->write(data => $storages); $options{statefile}->write(data => $storages);
@ -452,30 +453,42 @@ sub api_get_nodes {
sub api_get_storages { sub api_get_storages {
my ($self, %options) = @_; my ($self, %options) = @_;
my $content_total = $self->cache_storages(statefile => $options{statefile}); my $storages = $self->cache_storages(statefile => $options{statefile});
if (defined($options{storage_id}) && $options{storage_id} ne '') { if (defined($options{storage_id}) && $options{storage_id} ne '') {
if (defined($content_total->{$options{storage_id}})) { if (defined($storages->{ $options{storage_id} })) {
$content_total->{$options{storage_id}}->{Stats} = $self->internal_api_get_storage_stats(storage_id => $options{storage_id}); $storages->{ $options{storage_id} }->{Stats} = $self->internal_api_get_storage_stats(storage_id => $options{storage_id});
}
} elsif (defined($options{node_id}) && $options{node_id} ne '') {
my $nodes = $self->cache_nodes(statefile => $options{statefile});
foreach my $node_id (keys %$nodes) {
if ($node_id eq $options{node_id}) {
foreach my $storage_id (keys %$storages) {
if ($storages->{$storage_id}->{Node} eq $nodes->{$node_id}->{Name}) {
$storages->{$storage_id}->{Stats} = $self->internal_api_get_storage_stats(storage_id => $storage_id);
}
}
}
}
} elsif (defined($options{node_name}) && $options{node_name} ne '') {
foreach my $storage_id (keys %$storages) {
if ($storages->{$storage_id}->{Node} eq $options{node_name}) {
$storages->{$storage_id}->{Stats} = $self->internal_api_get_storage_stats(storage_id => $storage_id);
}
} }
} elsif (defined($options{storage_name}) && $options{storage_name} ne '') { } elsif (defined($options{storage_name}) && $options{storage_name} ne '') {
my $storage_id; foreach my $storage_id (keys %$storages) {
foreach (keys %{$content_total}) { if ($storages->{$storage_id}->{Name} eq $options{storage_name}) {
if ($content_total->{$_}->{Name} eq $options{storage_name}) { $storages->{$storage_id}->{Stats} = $self->internal_api_get_storage_stats(storage_id => $storage_id);
$storage_id = $_;
last;
} }
} }
if (defined($storage_id)) {
$content_total->{$storage_id}->{Stats} = $self->internal_api_get_storage_stats(storage_id => $storage_id);
}
} else { } else {
foreach my $storage_id (keys %{$content_total}) { foreach my $storage_id (keys %$storages) {
$content_total->{$storage_id}->{Stats} = $self->internal_api_get_storage_stats(storage_id => $storage_id); $storages->{$storage_id}->{Stats} = $self->internal_api_get_storage_stats(storage_id => $storage_id);
} }
} }
return $content_total; return $storages;
} }
1; 1;

View File

@ -25,7 +25,7 @@ use base qw(centreon::plugins::templates::counter);
use strict; use strict;
use warnings; use warnings;
use Digest::MD5 qw(md5_hex); use Digest::MD5 qw(md5_hex);
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
sub custom_status_output { sub custom_status_output {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -33,14 +33,6 @@ sub custom_status_output {
return 'state : ' . $self->{result_values}->{state}; return 'state : ' . $self->{result_values}->{state};
} }
sub custom_status_calc {
my ($self, %options) = @_;
$self->{result_values}->{state} = $options{new_datas}->{$self->{instance} . '_state'};
$self->{result_values}->{name} = $options{new_datas}->{$self->{instance} . '_name'};
return 0;
}
sub custom_cpu_calc { sub custom_cpu_calc {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -82,7 +74,7 @@ sub custom_memory_output {
my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}); my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
return sprintf( return sprintf(
'Memory Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)', 'memory total: %s used: %s (%.2f%%) free: %s (%.2f%%)',
$total_size_value . " " . $total_size_unit, $total_size_value . " " . $total_size_unit,
$total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used}, $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
$total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free} $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}
@ -132,7 +124,7 @@ sub custom_swap_output {
my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}); my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
return sprintf( return sprintf(
'Swap Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)', 'swap total: %s used: %s (%.2f%%) free: %s (%.2f%%)',
$total_size_value . " " . $total_size_unit, $total_size_value . " " . $total_size_unit,
$total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used}, $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
$total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free} $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}
@ -182,7 +174,7 @@ sub custom_fs_output {
my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}); my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
return sprintf( return sprintf(
'Root Filesystem Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)', 'root filesystem total: %s used: %s (%.2f%%) free: %s (%.2f%%)',
$total_size_value . " " . $total_size_unit, $total_size_value . " " . $total_size_unit,
$total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used}, $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
$total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free} $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}
@ -201,30 +193,35 @@ sub custom_fs_calc {
return 0; return 0;
} }
sub prefix_nodes_output {
my ($self, %options) = @_;
return "Node '" . $options{instance_value}->{display} . "' ";
}
sub set_counters { sub set_counters {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{maps_counters_type} = [ $self->{maps_counters_type} = [
{ name => 'nodes', type => 1, cb_prefix_output => 'prefix_nodes_output', message_multiple => 'All nodes are ok', skipped_code => { -10 => 1, -11 => 1 } }, { name => 'nodes', type => 1, cb_prefix_output => 'prefix_nodes_output', message_multiple => 'All nodes are ok', skipped_code => { -10 => 1, -11 => 1 } }
]; ];
$self->{maps_counters}->{nodes} = [ $self->{maps_counters}->{nodes} = [
{ label => 'node-status', threshold => 0, set => { { label => 'node-status', type => 2, set => {
key_values => [ { name => 'state' }, { name => 'name' } ], key_values => [ { name => 'state' }, { name => 'name' } ],
closure_custom_calc => $self->can('custom_status_calc'),
closure_custom_output => $self->can('custom_status_output'), closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; }, closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold, closure_custom_threshold_check => \&catalog_status_threshold_ng
} }
}, },
{ label => 'cpu', set => { { label => 'cpu', set => {
key_values => [ { name => 'cpu_total_usage', diff => 1 }, { name => 'cpu_number' }, { name => 'display' } ], key_values => [ { name => 'cpu_total_usage', diff => 1 }, { name => 'cpu_number' }, { name => 'display' } ],
output_template => 'CPU Usage : %.2f %%', output_template => 'cpu usage: %.2f %%',
closure_custom_calc => $self->can('custom_cpu_calc'), closure_custom_calc => $self->can('custom_cpu_calc'),
output_use => 'prct_cpu', threshold_use => 'prct_cpu', output_use => 'prct_cpu', threshold_use => 'prct_cpu',
perfdatas => [ perfdatas => [
{ label => 'cpu', value => 'prct_cpu', template => '%.2f', { label => 'cpu', value => 'prct_cpu', template => '%.2f',
unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display' }, unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display' }
], ],
} }
}, },
@ -233,7 +230,7 @@ sub set_counters {
closure_custom_calc => $self->can('custom_memory_calc'), closure_custom_calc => $self->can('custom_memory_calc'),
closure_custom_output => $self->can('custom_memory_output'), closure_custom_output => $self->can('custom_memory_output'),
closure_custom_perfdata => $self->can('custom_memory_perfdata'), closure_custom_perfdata => $self->can('custom_memory_perfdata'),
closure_custom_threshold_check => $self->can('custom_memory_threshold'), closure_custom_threshold_check => $self->can('custom_memory_threshold')
} }
}, },
{ label => 'fs', set => { { label => 'fs', set => {
@ -241,7 +238,7 @@ sub set_counters {
closure_custom_calc => $self->can('custom_fs_calc'), closure_custom_calc => $self->can('custom_fs_calc'),
closure_custom_output => $self->can('custom_fs_output'), closure_custom_output => $self->can('custom_fs_output'),
closure_custom_perfdata => $self->can('custom_fs_perfdata'), closure_custom_perfdata => $self->can('custom_fs_perfdata'),
closure_custom_threshold_check => $self->can('custom_fs_threshold'), closure_custom_threshold_check => $self->can('custom_fs_threshold')
} }
}, },
{ label => 'swap', set => { { label => 'swap', set => {
@ -249,9 +246,9 @@ sub set_counters {
closure_custom_calc => $self->can('custom_swap_calc'), closure_custom_calc => $self->can('custom_swap_calc'),
closure_custom_output => $self->can('custom_swap_output'), closure_custom_output => $self->can('custom_swap_output'),
closure_custom_perfdata => $self->can('custom_swap_perfdata'), closure_custom_perfdata => $self->can('custom_swap_perfdata'),
closure_custom_threshold_check => $self->can('custom_swap_threshold'), closure_custom_threshold_check => $self->can('custom_swap_threshold')
}
} }
},
]; ];
} }
@ -264,9 +261,7 @@ sub new {
'node-id:s' => { name => 'node_id' }, 'node-id:s' => { name => 'node_id' },
'node-name:s' => { name => 'node_name' }, 'node-name:s' => { name => 'node_name' },
'filter-name:s' => { name => 'filter_name' }, 'filter-name:s' => { name => 'filter_name' },
'use-name' => { name => 'use_name' }, 'use-name' => { name => 'use_name' }
'warning-node-status:s' => { name => 'warning_node_status', default => '' },
'critical-node-status:s' => { name => 'critical_node_status', default => '' },
}); });
$self->{statefile_cache_nodes} = centreon::plugins::statefile->new(%options); $self->{statefile_cache_nodes} = centreon::plugins::statefile->new(%options);
@ -277,16 +272,9 @@ sub check_options {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->SUPER::check_options(%options); $self->SUPER::check_options(%options);
$self->change_macros(macros => ['warning_node_status', 'critical_node_status']);
$self->{statefile_cache_nodes}->check_options(%options); $self->{statefile_cache_nodes}->check_options(%options);
} }
sub prefix_nodes_output {
my ($self, %options) = @_;
return "Node '" . $options{instance_value}->{display} . "' ";
}
sub manage_selection { sub manage_selection {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -307,6 +295,7 @@ sub manage_selection {
$self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1); $self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1);
next; next;
} }
$self->{nodes}->{$node_id} = { $self->{nodes}->{$node_id} = {
display => defined($self->{option_results}->{use_name}) ? $name : $node_id, display => defined($self->{option_results}->{use_name}) ? $name : $node_id,
name => $name, name => $name,
@ -319,7 +308,7 @@ sub manage_selection {
swap_total => swap_total =>
defined($result->{$node_id}->{Stats}->{swap}->{total}) && $result->{$node_id}->{Stats}->{swap}->{total} > 0 ? $result->{$node_id}->{Stats}->{swap}->{total} : undef, defined($result->{$node_id}->{Stats}->{swap}->{total}) && $result->{$node_id}->{Stats}->{swap}->{total} > 0 ? $result->{$node_id}->{Stats}->{swap}->{total} : undef,
fs_usage => $result->{$node_id}->{Stats}->{rootfs}->{used}, fs_usage => $result->{$node_id}->{Stats}->{rootfs}->{used},
fs_total => $result->{$node_id}->{Stats}->{rootfs}->{total}, fs_total => $result->{$node_id}->{Stats}->{rootfs}->{total}
}; };
} }
@ -329,11 +318,13 @@ sub manage_selection {
} }
my $hostnames = $options{custom}->get_hostnames(); my $hostnames = $options{custom}->get_hostnames();
$self->{cache_name} = "proxmox_" . $self->{mode} . '_' .$hostnames . '_' . $options{custom}->get_port() . '_' . $self->{cache_name} = 'proxmox_' . $self->{mode} . '_' .$hostnames . '_' . $options{custom}->get_port() . '_' .
(defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' . md5_hex(
(defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all')) . '_' . (defined($self->{option_results}->{filter_counters}) ? $self->{option_results}->{filter_counters} : '') . '_' .
(defined($self->{option_results}->{node_id}) ? md5_hex($self->{option_results}->{node_id}) : md5_hex('all')) . '_' . (defined($self->{option_results}->{filter_name}) ? $self->{option_results}->{filter_name} : '') . '_' .
(defined($self->{option_results}->{node_name}) ? md5_hex($self->{option_results}->{node_name}) : md5_hex('all')); (defined($self->{option_results}->{node_id}) ? $self->{option_results}->{node_id} : '') . '_' .
(defined($self->{option_results}->{node_name}) ? $self->{option_results}->{node_name} : '')
);
} }
1; 1;
@ -379,12 +370,12 @@ Can be: 'cpu' (%), 'memory' (%), 'swap' (%), 'fs' (%).
=item B<--warning-node-status> =item B<--warning-node-status>
Set warning threshold for status (Default: -) Set warning threshold for status.
Can used special variables like: %{name}, %{state}. Can used special variables like: %{name}, %{state}.
=item B<--critical-node-status> =item B<--critical-node-status>
Set critical threshold for status (Default: -). Set critical threshold for status.
Can used special variables like: %{name}, %{state}. Can used special variables like: %{name}, %{state}.
=back =back

View File

@ -25,22 +25,12 @@ use base qw(centreon::plugins::templates::counter);
use strict; use strict;
use warnings; use warnings;
use Digest::MD5 qw(md5_hex); use Digest::MD5 qw(md5_hex);
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
sub custom_status_output { sub custom_status_output {
my ($self, %options) = @_; my ($self, %options) = @_;
my $msg = 'state : ' . $self->{result_values}->{state};
return $msg; return 'state : ' . $self->{result_values}->{state};
}
sub custom_status_calc {
my ($self, %options) = @_;
$self->{result_values}->{state} = $options{new_datas}->{$self->{instance} . '_state'};
$self->{result_values}->{name} = $options{new_datas}->{$self->{instance} . '_name'};
return 0;
} }
sub custom_storage_perfdata { sub custom_storage_perfdata {
@ -59,8 +49,10 @@ sub custom_storage_perfdata {
sub custom_storage_threshold { sub custom_storage_threshold {
my ($self, %options) = @_; my ($self, %options) = @_;
my $exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{prct_used}, my $exit = $self->{perfdata}->threshold_check(
threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]); value => $self->{result_values}->{prct_used},
threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]
);
return $exit; return $exit;
} }
@ -71,11 +63,12 @@ sub custom_storage_output {
my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}); 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 ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
my $msg = sprintf("Storage Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", return sprintf(
"storage total: %s used: %s (%.2f%%) free: %s (%.2f%%)",
$total_size_value . " " . $total_size_unit, $total_size_value . " " . $total_size_unit,
$total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used}, $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
$total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}); $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}
return $msg; );
} }
sub custom_storage_calc { sub custom_storage_calc {
@ -87,24 +80,30 @@ sub custom_storage_calc {
$self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used}; $self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used};
$self->{result_values}->{prct_free} = $self->{result_values}->{free} * 100 / $self->{result_values}->{total}; $self->{result_values}->{prct_free} = $self->{result_values}->{free} * 100 / $self->{result_values}->{total};
$self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total}; $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
return 0; return 0;
} }
sub prefix_storages_output {
my ($self, %options) = @_;
return "Storage '" . $options{instance_value}->{display} . "' ";
}
sub set_counters { sub set_counters {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{maps_counters_type} = [ $self->{maps_counters_type} = [
{ name => 'storages', type => 1, cb_prefix_output => 'prefix_storages_output', { name => 'storages', type => 1, cb_prefix_output => 'prefix_storages_output',
message_multiple => 'All storages are ok', skipped_code => { -11 => 1 } }, message_multiple => 'All storages are ok', skipped_code => { -11 => 1 } }
]; ];
$self->{maps_counters}->{storages} = [ $self->{maps_counters}->{storages} = [
{ label => 'storage-status', threshold => 0, set => { { label => 'storage-status', type => 2, set => {
key_values => [ { name => 'state' }, { name => 'name' } ], key_values => [ { name => 'state' }, { name => 'name' } ],
closure_custom_calc => $self->can('custom_status_calc'),
closure_custom_output => $self->can('custom_status_output'), closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; }, closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold, closure_custom_threshold_check => \&catalog_status_threshold_ng
} }
}, },
{ label => 'storage', set => { { label => 'storage', set => {
@ -123,14 +122,13 @@ sub new {
my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1); my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1);
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => $options{options}->add_options(arguments => {
{ 'storage-id:s' => { name => 'storage_id' },
"storage-id:s" => { name => 'storage_id' }, 'storage-name:s' => { name => 'storage_name' },
"storage-name:s" => { name => 'storage_name' }, 'filter-name:s' => { name => 'filter_name' },
"filter-name:s" => { name => 'filter_name' }, 'use-name' => { name => 'use_name' },
"use-name" => { name => 'use_name' }, 'node-id:s' => { name => 'node_id' },
"warning-storage-status:s" => { name => 'warning_storage_status', default => '' }, 'node-name:s' => { name => 'node_name' }
"critical-storage-status:s" => { name => 'critical_storage_status', default => '' },
}); });
$self->{statefile_cache_storages} = centreon::plugins::statefile->new(%options); $self->{statefile_cache_storages} = centreon::plugins::statefile->new(%options);
@ -141,27 +139,21 @@ sub check_options {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->SUPER::check_options(%options); $self->SUPER::check_options(%options);
$self->change_macros(macros => ['warning_storage_status', 'critical_storage_status']);
$self->{statefile_cache_storages}->check_options(%options); $self->{statefile_cache_storages}->check_options(%options);
} }
sub prefix_storages_output {
my ($self, %options) = @_;
return "Storage '" . $options{instance_value}->{display} . "' ";
}
sub manage_selection { sub manage_selection {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{storages} = {};
my $result = $options{custom}->api_get_storages( my $result = $options{custom}->api_get_storages(
storage_id => $self->{option_results}->{storage_id}, storage_id => $self->{option_results}->{storage_id},
storage_name => $self->{option_results}->{storage_name}, storage_name => $self->{option_results}->{storage_name},
node_id => $self->{option_results}->{node_id},
node_name => $self->{option_results}->{node_name},
statefile => $self->{statefile_cache_storages} statefile => $self->{statefile_cache_storages}
); );
$self->{storages} = {};
foreach my $storage_id (keys %{$result}) { foreach my $storage_id (keys %{$result}) {
next if (!defined($result->{$storage_id}->{Stats})); next if (!defined($result->{$storage_id}->{Stats}));
@ -171,12 +163,13 @@ sub manage_selection {
$self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1); $self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1);
next; next;
} }
$self->{storages}->{$storage_id} = { $self->{storages}->{$storage_id} = {
display => defined($self->{option_results}->{use_name}) ? $name : $storage_id, display => defined($self->{option_results}->{use_name}) ? $name : $storage_id,
name => $name, name => $name,
state => $result->{$storage_id}->{State}, state => $result->{$storage_id}->{State},
storage_usage => $result->{$storage_id}->{Stats}->{used}, storage_usage => $result->{$storage_id}->{Stats}->{used},
storage_total => $result->{$storage_id}->{Stats}->{total}, storage_total => $result->{$storage_id}->{Stats}->{total}
}; };
} }
@ -186,11 +179,16 @@ sub manage_selection {
} }
my $hostnames = $options{custom}->get_hostnames(); my $hostnames = $options{custom}->get_hostnames();
$self->{cache_name} = "proxmox_" . $self->{mode} . '_' .$hostnames . '_' . $options{custom}->get_port() . '_' . $self->{cache_name} = 'proxmox_' . $self->{mode} . '_' . $hostnames . '_' . $options{custom}->get_port() . '_' .
(defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' . md5_hex(
(defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all')) . '_' . (defined($self->{option_results}->{filter_counters}) ? $self->{option_results}->{filter_counters} : '') . '_' .
(defined($self->{option_results}->{storage_id}) ? md5_hex($self->{option_results}->{storage_id}) : md5_hex('all')) . '_' . (defined($self->{option_results}->{filter_name}) ? $self->{option_results}->{filter_name} : '') . '_' .
(defined($self->{option_results}->{storage_name}) ? md5_hex($self->{option_results}->{storage_name}) : md5_hex('all')); (defined($self->{option_results}->{storage_id}) ? $self->{option_results}->{storage_id} : '') . '_' .
(defined($self->{option_results}->{storage_name}) ? $self->{option_results}->{storage_name} : '') . '_' .
(defined($self->{option_results}->{node_id}) ? $self->{option_results}->{node_id} : '') . '_' .
(defined($self->{option_results}->{node_name}) ? $self->{option_results}->{node_name} : '')
);
} }
1; 1;
@ -203,6 +201,11 @@ Check storage usage.
=over 8 =over 8
=item B<--filter-counters>
Only display some counters (regexp can be used).
Example: --filter-counters='^storage-status$'
=item B<--storage-id> =item B<--storage-id>
Exact storage ID. Exact storage ID.
@ -219,31 +222,29 @@ Use storage name for perfdata and display.
Filter by storage name (can be a regexp). Filter by storage name (can be a regexp).
=item B<--filter-counters> =item B<--node-id>
Only display some counters (regexp can be used). Exact node ID.
Example: --filter-counters='^storage-status$'
=item B<--warning-*> =item B<--node-name>
Threshold warning. Exact node name.
Can be: 'storage' (%).
=item B<--critical-*>
Threshold critical.
Can be: 'storage' (%).
=item B<--warning-storage-status> =item B<--warning-storage-status>
Set warning threshold for status (Default: -) Set warning threshold for status.
Can used special variables like: %{name}, %{state}. Can used special variables like: %{name}, %{state}.
=item B<--critical-storage-status> =item B<--critical-storage-status>
Set critical threshold for status (Default: -). Set critical threshold for status.
Can used special variables like: %{name}, %{state}. Can used special variables like: %{name}, %{state}.
=item B<--warning-*> B<--critical-*>
Thresholds.
Can be: 'storage' (%).
=back =back
=cut =cut

View File

@ -44,8 +44,11 @@ sub check_options {
sub run { sub run {
my ($self, %options) = @_; my ($self, %options) = @_;
my $result = $options{custom}->get_version(); my $result = $options{custom}->get_version();
$self->{output}->output_add(severity => 'OK',
short_msg => "Version is '" . $result . "'"); $self->{output}->output_add(
severity => 'OK',
short_msg => "Version is '" . $result . "'"
);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
$self->{output}->exit(); $self->{output}->exit();
} }

View File

@ -73,7 +73,7 @@ sub custom_memory_output {
my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}); my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
return sprintf( return sprintf(
'Memory Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)', 'memory total: %s used: %s (%.2f%%) free: %s (%.2f%%)',
$total_size_value . " " . $total_size_unit, $total_size_value . " " . $total_size_unit,
$total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used}, $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
$total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free} $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}
@ -108,8 +108,10 @@ sub custom_swap_perfdata {
sub custom_swap_threshold { sub custom_swap_threshold {
my ($self, %options) = @_; my ($self, %options) = @_;
my $exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{prct_used}, my $exit = $self->{perfdata}->threshold_check(
threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]); value => $self->{result_values}->{prct_used},
threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]
);
return $exit; return $exit;
} }
@ -121,7 +123,7 @@ sub custom_swap_output {
my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}); my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
return sprintf( return sprintf(
'Swap Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)', 'swap total: %s used: %s (%.2f%%) free: %s (%.2f%%)',
$total_size_value . " " . $total_size_unit, $total_size_value . " " . $total_size_unit,
$total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used}, $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
$total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free} $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}
@ -140,12 +142,17 @@ sub custom_swap_calc {
return 0; return 0;
} }
sub prefix_vms_output {
my ($self, %options) = @_;
return "VM '" . $options{instance_value}->{display} . "' ";
}
sub set_counters { sub set_counters {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{maps_counters_type} = [ $self->{maps_counters_type} = [
{ name => 'vms', type => 1, cb_prefix_output => 'prefix_vms_output', message_multiple => 'All vms are ok', skipped_code => { -11 => 1 } }, { name => 'vms', type => 1, cb_prefix_output => 'prefix_vms_output', message_multiple => 'All vms are ok', skipped_code => { -10 => 1, -11 => 1 } }
{ name => 'vms_traffic', type => 1, cb_prefix_output => 'prefix_vms_traffic_output', message_multiple => 'All vm traffics are ok', skipped_code => { -11 => 1 } },
]; ];
$self->{maps_counters}->{vms} = [ $self->{maps_counters}->{vms} = [
@ -158,7 +165,7 @@ sub set_counters {
}, },
{ label => 'cpu', set => { { label => 'cpu', set => {
key_values => [ { name => 'cpu_total_usage', diff => 1 }, { name => 'cpu_number' }, { name => 'display' } ], key_values => [ { name => 'cpu_total_usage', diff => 1 }, { name => 'cpu_number' }, { name => 'display' } ],
output_template => 'CPU Usage : %.2f %%', output_template => 'cpu usage: %.2f %%',
closure_custom_calc => $self->can('custom_cpu_calc'), closure_custom_calc => $self->can('custom_cpu_calc'),
output_use => 'prct_cpu', threshold_use => 'prct_cpu', output_use => 'prct_cpu', threshold_use => 'prct_cpu',
perfdatas => [ perfdatas => [
@ -177,7 +184,7 @@ sub set_counters {
}, },
{ label => 'read-iops', set => { { label => 'read-iops', set => {
key_values => [ { name => 'read_io', per_second => 1 }, { name => 'display' } ], key_values => [ { name => 'read_io', per_second => 1 }, { name => 'display' } ],
output_template => 'Read IOPs : %.2f', output_error_template => "Read IOPs : %s", output_template => 'read iops: %.2f',
perfdatas => [ perfdatas => [
{ label => 'read_iops', template => '%.2f', { label => 'read_iops', template => '%.2f',
unit => 'iops', min => 0, label_extra_instance => 1, instance_use => 'display' } unit => 'iops', min => 0, label_extra_instance => 1, instance_use => 'display' }
@ -186,7 +193,7 @@ sub set_counters {
}, },
{ label => 'write-iops', set => { { label => 'write-iops', set => {
key_values => [ { name => 'write_io', per_second => 1 }, { name => 'display' } ], key_values => [ { name => 'write_io', per_second => 1 }, { name => 'display' } ],
output_template => 'Write IOPs : %.2f', output_error_template => "Write IOPs : %s", output_template => 'write iops: %.2f',
perfdatas => [ perfdatas => [
{ label => 'write_iops', template => '%.2f', { label => 'write_iops', template => '%.2f',
unit => 'iops', min => 0, label_extra_instance => 1, instance_use => 'display' } unit => 'iops', min => 0, label_extra_instance => 1, instance_use => 'display' }
@ -200,14 +207,11 @@ sub set_counters {
closure_custom_perfdata => $self->can('custom_swap_perfdata'), closure_custom_perfdata => $self->can('custom_swap_perfdata'),
closure_custom_threshold_check => $self->can('custom_swap_threshold') closure_custom_threshold_check => $self->can('custom_swap_threshold')
} }
} },
];
$self->{maps_counters}->{vms_traffic} = [
{ label => 'traffic-in', set => { { label => 'traffic-in', set => {
key_values => [ { name => 'traffic_in', per_second => 1 }, { name => 'display' } ], key_values => [ { name => 'traffic_in', per_second => 1 }, { name => 'display' } ],
output_change_bytes => 2, output_change_bytes => 2,
output_template => 'Traffic In : %s %s/s', output_template => 'traffic in: %s %s/s',
perfdatas => [ perfdatas => [
{ label => 'traffic_in', template => '%.2f', { label => 'traffic_in', template => '%.2f',
min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'display' } min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'display' }
@ -217,7 +221,7 @@ sub set_counters {
{ label => 'traffic-out', set => { { label => 'traffic-out', set => {
key_values => [ { name => 'traffic_out', per_second => 1 }, { name => 'display' } ], key_values => [ { name => 'traffic_out', per_second => 1 }, { name => 'display' } ],
output_change_bytes => 2, output_change_bytes => 2,
output_template => 'Traffic Out : %s %s/s', output_template => 'traffic out: %s %s/s',
perfdatas => [ perfdatas => [
{ label => 'traffic_out', template => '%.2f', { label => 'traffic_out', template => '%.2f',
min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'display' } min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'display' }
@ -250,30 +254,17 @@ sub check_options {
$self->{statefile_cache_vms}->check_options(%options); $self->{statefile_cache_vms}->check_options(%options);
} }
sub prefix_vms_traffic_output {
my ($self, %options) = @_;
return "VM '" . $options{instance_value}->{display} . "' ";
}
sub prefix_vms_output {
my ($self, %options) = @_;
return "VM '" . $options{instance_value}->{display} . "' ";
}
sub manage_selection { sub manage_selection {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{vms} = {};
$self->{vms_traffic} = {};
my $result = $options{custom}->api_get_vms( my $result = $options{custom}->api_get_vms(
vm_id => $self->{option_results}->{vm_id}, vm_id => $self->{option_results}->{vm_id},
vm_name => $self->{option_results}->{vm_name}, vm_name => $self->{option_results}->{vm_name},
statefile => $self->{statefile_cache_vms} statefile => $self->{statefile_cache_vms}
); );
$self->{vms} = {};
$self->{vms_traffic} = {};
foreach my $vm_id (keys %{$result}) { foreach my $vm_id (keys %{$result}) {
next if (!defined($result->{$vm_id}->{Stats})); next if (!defined($result->{$vm_id}->{Stats}));
@ -295,10 +286,7 @@ sub manage_selection {
memory_usage => $result->{$vm_id}->{Stats}->{mem}, memory_usage => $result->{$vm_id}->{Stats}->{mem},
memory_total => $result->{$vm_id}->{Stats}->{maxmem}, memory_total => $result->{$vm_id}->{Stats}->{maxmem},
swap_usage => $result->{$vm_id}->{Stats}->{swap}, swap_usage => $result->{$vm_id}->{Stats}->{swap},
swap_total => defined($result->{$vm_id}->{Stats}->{maxswap}) && $result->{$vm_id}->{Stats}->{maxswap} > 0 ? $result->{$vm_id}->{Stats}->{maxswap} : undef swap_total => defined($result->{$vm_id}->{Stats}->{maxswap}) && $result->{$vm_id}->{Stats}->{maxswap} > 0 ? $result->{$vm_id}->{Stats}->{maxswap} : undef,
};
$self->{vms_traffic}->{$name} = {
display => $name,
traffic_in => $result->{$vm_id}->{Stats}->{netin} * 8, traffic_in => $result->{$vm_id}->{Stats}->{netin} * 8,
traffic_out => $result->{$vm_id}->{Stats}->{netout} * 8 traffic_out => $result->{$vm_id}->{Stats}->{netout} * 8
}; };
@ -310,11 +298,13 @@ sub manage_selection {
} }
my $hostnames = $options{custom}->get_hostnames(); my $hostnames = $options{custom}->get_hostnames();
$self->{cache_name} = "proxmox_" . $self->{mode} . '_' .$hostnames . '_' . $options{custom}->get_port() . '_' . $self->{cache_name} = 'proxmox_' . $self->{mode} . '_' .$hostnames . '_' . $options{custom}->get_port() . '_' .
(defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' . md5_hex(
(defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all')) . '_' . (defined($self->{option_results}->{filter_counters}) ? $self->{option_results}->{filter_counters} : '') . '_' .
(defined($self->{option_results}->{vm_id}) ? md5_hex($self->{option_results}->{vm_id}) : md5_hex('all')) . '_' . (defined($self->{option_results}->{filter_name}) ? $self->{option_results}->{filter_name} : '') . '_' .
(defined($self->{option_results}->{vm_name}) ? md5_hex($self->{option_results}->{vm_name}) : md5_hex('all')); (defined($self->{option_results}->{vm_id}) ? $self->{option_results}->{vm_id} : '') . '_' .
(defined($self->{option_results}->{vm_name}) ? $self->{option_results}->{vm_name} : '')
);
} }
1; 1;

View File

@ -26,11 +26,9 @@ use base qw(centreon::plugins::script_custom);
sub new { sub new {
my ($class, %options) = @_; my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options); my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class; bless $self, $class;
$self->{version} = '1.0';
$self->{modes} = { $self->{modes} = {
'list-nodes' => 'apps::proxmox::ve::restapi::mode::listnodes', 'list-nodes' => 'apps::proxmox::ve::restapi::mode::listnodes',
'list-storages' => 'apps::proxmox::ve::restapi::mode::liststorages', 'list-storages' => 'apps::proxmox::ve::restapi::mode::liststorages',