(plugin) apps::proxmox::ve::restapi - metric v2 + discovery mode (#3676)

This commit is contained in:
qgarnier 2022-05-20 13:07:06 +02:00 committed by GitHub
parent a20e81ae00
commit c3dbcd04a4
6 changed files with 202 additions and 56 deletions

View File

@ -0,0 +1,147 @@
#
# Copyright 2022 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package apps::proxmox::ve::restapi::mode::discovery;
use base qw(centreon::plugins::mode);
use strict;
use warnings;
use JSON::XS;
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments => {
'resource-type:s' => { name => 'resource_type' },
'prettify' => { name => 'prettify' }
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
if (!defined($self->{option_results}->{resource_type}) || $self->{option_results}->{resource_type} eq '') {
$self->{option_results}->{resource_type} = 'nodes';
}
if ($self->{option_results}->{resource_type} !~ /^node|vm$/) {
$self->{output}->add_option_msg(short_msg => 'unknown resource type');
$self->{output}->option_exit();
}
}
sub discovery_vm {
my ($self, %options) = @_;
my $vms = $options{custom}->api_list_vms();
my $disco_data = [];
foreach my $vm_id (keys %$vms) {
my $vm = {};
$vm->{uuid} = $vm_id;
$vm->{name} = $vms->{$vm_id}->{Name};
$vm->{state} = $vms->{$vm_id}->{State};
$vm->{node_name} = $vms->{$vm_id}->{Node};
push @$disco_data, $vm;
}
return $disco_data;
}
sub discovery_node {
my ($self, %options) = @_;
my $nodes = $options{custom}->api_list_nodes();
my $disco_data = [];
foreach my $node_id (keys %$nodes) {
my $node = {};
$node->{uuid} = $node_id;
$node->{name} = $nodes->{$node_id}->{Name};
$node->{state} = $nodes->{$node_id}->{State};
push @$disco_data, $node;
}
return $disco_data;
}
sub run {
my ($self, %options) = @_;
my $disco_stats;
$disco_stats->{start_time} = time();
my $results = [];
if ($self->{option_results}->{resource_type} eq 'vm') {
$results = $self->discovery_vm(
custom => $options{custom}
);
} else {
$results = $self->discovery_node(
custom => $options{custom}
);
}
$disco_stats->{end_time} = time();
$disco_stats->{duration} = $disco_stats->{end_time} - $disco_stats->{start_time};
$disco_stats->{discovered_items} = scalar(@$results);
$disco_stats->{results} = $results;
my $encoded_data;
eval {
if (defined($self->{option_results}->{prettify})) {
$encoded_data = JSON::XS->new->utf8->pretty->encode($disco_stats);
} else {
$encoded_data = JSON::XS->new->utf8->encode($disco_stats);
}
};
if ($@) {
$encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
}
$self->{output}->output_add(short_msg => $encoded_data);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);
$self->{output}->exit();
}
1;
__END__
=head1 MODE
Resources discovery.
=over 8
=item B<--resource-type>
Choose the type of resources to discover (Can be: 'vm', 'node').
=back
=cut

View File

@ -30,9 +30,8 @@ sub new {
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments =>
{
});
$options{options}->add_options(arguments => {});
return $self;
}

View File

@ -30,7 +30,7 @@ use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_
sub custom_status_output {
my ($self, %options) = @_;
return 'state : ' . $self->{result_values}->{state};
return 'state: ' . $self->{result_values}->{state};
}
sub custom_cpu_calc {
@ -47,8 +47,9 @@ sub custom_memory_perfdata {
my ($self, %options) = @_;
$self->{output}->perfdata_add(
label => 'memory_used', unit => 'B',
instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
nlabel => 'node.memory.usage.bytes',
unit => 'B',
instances => $self->{result_values}->{display},
value => $self->{result_values}->{used},
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
@ -97,8 +98,9 @@ sub custom_swap_perfdata {
my ($self, %options) = @_;
$self->{output}->perfdata_add(
label => 'swap_used', unit => 'B',
instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
nlabel => 'node.swap.usage.bytes',
unit => 'B',
instances => $self->{result_values}->{display},
value => $self->{result_values}->{used},
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
@ -147,8 +149,9 @@ sub custom_fs_perfdata {
my ($self, %options) = @_;
$self->{output}->perfdata_add(
label => 'fs_used', unit => 'B',
instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
nlabel => 'node.filesystem.usage.bytes',
unit => 'B',
instances => $self->{result_values}->{display},
value => $self->{result_values}->{used},
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
@ -214,15 +217,15 @@ sub set_counters {
closure_custom_threshold_check => \&catalog_status_threshold_ng
}
},
{ label => 'cpu', set => {
{ label => 'cpu', nlabel => 'node.cpu.utilization.percentage', set => {
key_values => [ { name => 'cpu_total_usage', diff => 1 }, { name => 'cpu_number' }, { name => 'display' } ],
output_template => 'cpu usage: %.2f %%',
closure_custom_calc => $self->can('custom_cpu_calc'),
output_use => 'prct_cpu', threshold_use => 'prct_cpu',
perfdatas => [
{ label => 'cpu', value => 'prct_cpu', template => '%.2f',
{ value => 'prct_cpu', template => '%.2f',
unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display' }
],
]
}
},
{ label => 'memory', set => {
@ -254,7 +257,7 @@ sub set_counters {
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1);
my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1);
bless $self, $class;
$options{options}->add_options(arguments => {
@ -358,14 +361,9 @@ Filter by node name (can be a regexp).
Only display some counters (regexp can be used).
Example: --filter-counters='^node-status$'
=item B<--warning-*>
=item B<--warning-*> B<--critical-*>
Threshold warning.
Can be: 'cpu' (%), 'memory' (%), 'swap' (%), 'fs' (%).
=item B<--critical-*>
Threshold critical.
Thresholds.
Can be: 'cpu' (%), 'memory' (%), 'swap' (%), 'fs' (%).
=item B<--warning-node-status>

View File

@ -30,15 +30,16 @@ use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_
sub custom_status_output {
my ($self, %options) = @_;
return 'state : ' . $self->{result_values}->{state};
return 'state: ' . $self->{result_values}->{state};
}
sub custom_storage_perfdata {
my ($self, %options) = @_;
$self->{output}->perfdata_add(
label => 'storage_used', unit => 'B',
instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
nlabel => 'storage.space.usage.bytes',
unit => 'B',
instances => $self->{result_values}->{display},
value => $self->{result_values}->{used},
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
@ -64,7 +65,7 @@ sub custom_storage_output {
my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
return sprintf(
"storage total: %s used: %s (%.2f%%) free: %s (%.2f%%)",
"space 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}
@ -78,6 +79,9 @@ sub custom_storage_calc {
$self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_storage_total'};
$self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_storage_usage'};
$self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used};
return -10 if ($self->{result_values}->{total} <= 0);
$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};
@ -95,7 +99,7 @@ sub set_counters {
$self->{maps_counters_type} = [
{ 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 => { -10 => 1, -11 => 1 } }
];
$self->{maps_counters}->{storages} = [
@ -111,15 +115,15 @@ sub set_counters {
closure_custom_calc => $self->can('custom_storage_calc'),
closure_custom_output => $self->can('custom_storage_output'),
closure_custom_perfdata => $self->can('custom_storage_perfdata'),
closure_custom_threshold_check => $self->can('custom_storage_threshold'),
closure_custom_threshold_check => $self->can('custom_storage_threshold')
}
},
}
];
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1);
my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1);
bless $self, $class;
$options{options}->add_options(arguments => {

View File

@ -46,8 +46,9 @@ sub custom_memory_perfdata {
my ($self, %options) = @_;
$self->{output}->perfdata_add(
label => 'memory_used', unit => 'B',
instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
nlabel => 'vm.memory.usage.bytes',
unit => 'B',
instances => $self->{result_values}->{display},
value => $self->{result_values}->{used},
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
@ -96,8 +97,9 @@ sub custom_swap_perfdata {
my ($self, %options) = @_;
$self->{output}->perfdata_add(
label => 'swap_used', unit => 'B',
instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
nlabel => 'vm.swap.usage.bytes',
unit => 'B',
instances => $self->{result_values}->{display},
value => $self->{result_values}->{used},
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
@ -163,14 +165,13 @@ sub set_counters {
closure_custom_threshold_check => \&catalog_status_threshold_ng
}
},
{ label => 'cpu', set => {
{ label => 'cpu', nlabel => 'vm.cpu.utilization.percentage', set => {
key_values => [ { name => 'cpu_total_usage', diff => 1 }, { name => 'cpu_number' }, { name => 'display' } ],
output_template => 'cpu usage: %.2f %%',
closure_custom_calc => $self->can('custom_cpu_calc'),
output_use => 'prct_cpu', threshold_use => 'prct_cpu',
perfdatas => [
{ label => 'cpu', value => 'prct_cpu', template => '%.2f',
unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display' }
{ value => 'prct_cpu', template => '%.2f', unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display' }
]
}
},
@ -182,21 +183,19 @@ sub set_counters {
closure_custom_threshold_check => $self->can('custom_memory_threshold')
}
},
{ label => 'read-iops', set => {
{ label => 'read-iops', nlabel => 'vm.read.usage.iops', set => {
key_values => [ { name => 'read_io', per_second => 1 }, { name => 'display' } ],
output_template => 'read iops: %.2f',
perfdatas => [
{ label => 'read_iops', template => '%.2f',
unit => 'iops', min => 0, label_extra_instance => 1, instance_use => 'display' }
{ template => '%.2f', unit => 'iops', min => 0, label_extra_instance => 1, instance_use => 'display' }
]
}
},
{ label => 'write-iops', set => {
{ label => 'write-iops', nlabel => 'vm.write.usage.iops', set => {
key_values => [ { name => 'write_io', per_second => 1 }, { name => 'display' } ],
output_template => 'write iops: %.2f',
perfdatas => [
{ label => 'write_iops', template => '%.2f',
unit => 'iops', min => 0, label_extra_instance => 1, instance_use => 'display' }
{ template => '%.2f', unit => 'iops', min => 0, label_extra_instance => 1, instance_use => 'display' }
]
}
},
@ -208,23 +207,21 @@ sub set_counters {
closure_custom_threshold_check => $self->can('custom_swap_threshold')
}
},
{ label => 'traffic-in', set => {
{ label => 'traffic-in', nlabel => 'vm.traffic.in.bitspersecond', set => {
key_values => [ { name => 'traffic_in', per_second => 1 }, { name => 'display' } ],
output_change_bytes => 2,
output_template => 'traffic in: %s %s/s',
perfdatas => [
{ label => 'traffic_in', template => '%.2f',
min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'display' }
{ template => '%.2f', min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'display' }
]
}
},
{ label => 'traffic-out', set => {
{ label => 'traffic-out', nlabel => 'vm.traffic.out.bitspersecond', set => {
key_values => [ { name => 'traffic_out', per_second => 1 }, { name => 'display' } ],
output_change_bytes => 2,
output_template => 'traffic out: %s %s/s',
perfdatas => [
{ label => 'traffic_out', template => '%.2f',
min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'display' }
{ template => '%.2f', min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'display' }
]
}
}
@ -233,7 +230,7 @@ sub set_counters {
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1);
my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1);
bless $self, $class;
$options{options}->add_options(arguments => {

View File

@ -30,13 +30,14 @@ sub new {
bless $self, $class;
$self->{modes} = {
'list-nodes' => 'apps::proxmox::ve::restapi::mode::listnodes',
'list-storages' => 'apps::proxmox::ve::restapi::mode::liststorages',
'list-vms' => 'apps::proxmox::ve::restapi::mode::listvms',
'node-usage' => 'apps::proxmox::ve::restapi::mode::nodeusage',
'storage-usage' => 'apps::proxmox::ve::restapi::mode::storageusage',
'version' => 'apps::proxmox::ve::restapi::mode::version',
'vm-usage' => 'apps::proxmox::ve::restapi::mode::vmusage'
'discovery' => 'apps::proxmox::ve::restapi::mode::discovery',
'list-nodes' => 'apps::proxmox::ve::restapi::mode::listnodes',
'list-storages' => 'apps::proxmox::ve::restapi::mode::liststorages',
'list-vms' => 'apps::proxmox::ve::restapi::mode::listvms',
'node-usage' => 'apps::proxmox::ve::restapi::mode::nodeusage',
'storage-usage' => 'apps::proxmox::ve::restapi::mode::storageusage',
'version' => 'apps::proxmox::ve::restapi::mode::version',
'vm-usage' => 'apps::proxmox::ve::restapi::mode::vmusage'
};
$self->{custom_modes}->{api} = 'apps::proxmox::ve::restapi::custom::api';