enh ovirt plugin

This commit is contained in:
Colin Gagnaire 2019-04-29 18:20:59 +02:00
parent 12cc3dd22a
commit fb7d5b9a53
6 changed files with 236 additions and 18 deletions

View File

@ -45,12 +45,13 @@ sub new {
if (!defined($options{noptions})) {
$options{options}->add_options(arguments => {
"api-username:s" => { name => 'api_username' },
"api-password:s" => { name => 'api_password' },
"hostname:s" => { name => 'hostname' },
"port:s" => { name => 'port' },
"proto:s" => { name => 'proto' },
"timeout:s" => { name => 'timeout' },
"api-username:s" => { name => 'api_username' },
"api-password:s" => { name => 'api_password' },
"hostname:s" => { name => 'hostname' },
"port:s" => { name => 'port' },
"proto:s" => { name => 'proto' },
"timeout:s" => { name => 'timeout' },
"reload-cache-time:s" => { name => 'reload_cache_time' },
});
}
$options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
@ -94,6 +95,7 @@ sub check_options {
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
$self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : '';
$self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : '';
$self->{reload_cache_time} = (defined($self->{option_results}->{reload_cache_time})) ? $self->{option_results}->{reload_cache_time} : 180;
$self->{cache}->check_options(option_results => $self->{option_results});
@ -214,6 +216,72 @@ sub request_api {
return $decoded;
}
sub list_vms {
my ($self, %options) = @_;
my $url_path = '/ovirt-engine/api/vms';
my $response = $self->request_api(method => 'GET', url_path => $url_path);
return $response->{vm};
}
sub list_hosts {
my ($self, %options) = @_;
my $url_path = '/ovirt-engine/api/hosts';
my $response = $self->request_api(method => 'GET', url_path => $url_path);
return $response->{host};
}
sub get_host_statistics {
my ($self, %options) = @_;
my $url_path = '/ovirt-engine/api/hosts/' . $options{id} . '/statistics';
my $response = $self->request_api(method => 'GET', url_path => $url_path);
return $response->{statistic};
}
sub cache_hosts {
my ($self, %options) = @_;
$self->{cache_hosts} = centreon::plugins::statefile->new(%options);
$self->{cache_hosts}->check_options(option_results => $self->{option_results});
my $has_cache_file = $self->{cache_hosts}->read(statefile => 'cache_ovirt_hosts_' . md5_hex($self->{hostname}) . '_' . md5_hex($self->{api_username}));
my $timestamp_cache = $self->{cache_hosts}->get(name => 'last_timestamp');
my $hosts = $self->{cache_hosts}->get(name => 'hosts');
if ($has_cache_file == 0 || !defined($timestamp_cache) || ((time() - $timestamp_cache) > (($self->{reload_cache_time}) * 60))) {
$hosts = [];
my $datas = { last_timestamp => time(), hosts => $hosts };
my $list = $self->list_hosts();
foreach (@{$list}) {
push @{$hosts}, { id => $_->{id}, name => $_->{name} };
}
$self->{cache_hosts}->write(data => $datas);
}
return $hosts;
}
sub list_clusters {
my ($self, %options) = @_;
my $url_path = '/ovirt-engine/api/clusters';
my $response = $self->request_api(method => 'GET', url_path => $url_path);
return $response->{cluster};
}
sub list_datacenters {
my ($self, %options) = @_;
my $url_path = '/ovirt-engine/api/datacenters';
my $response = $self->request_api(method => 'GET', url_path => $url_path);
return $response->{data_center};
}
1;
__END__

View File

@ -0,0 +1,144 @@
#
# Copyright 2019 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::virtualization::ovirt::mode::cpuhost;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'host', type => 1, cb_prefix_output => 'prefix_output', message_multiple => 'All hosts are ok' },
];
$self->{maps_counters}->{host} = [
{ label => 'cpu-user', nlabel => 'host.cpu.user.utilization.percentage', set => {
key_values => [ { name => 'user' }, { name => 'display' } ],
output_template => 'user: %.2f %%',
perfdatas => [
{ value => 'user_absolute', template => '%.2f', unit => '%',
min => 0, max => 100, label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'cpu-system', nlabel => 'host.cpu.system.utilization.percentage', set => {
key_values => [ { name => 'system' }, { name => 'display' } ],
output_template => 'system: %.2f %%',
perfdatas => [
{ value => 'system_absolute', template => '%.2f', unit => '%',
min => 0, max => 100, label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
];
}
sub prefix_output {
my ($self, %options) = @_;
return "Host '" . $options{instance_value}->{display} . "' CPU utilization ";
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
bless $self, $class;
$self->{version} = '1.0';
$options{options}->add_options(arguments => {
"filter-name:s" => { name => 'filter_name' },
"filter-id:s" => { name => 'filter_id' },
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
}
sub manage_selection {
my ($self, %options) = @_;
$self->{host} = {};
if (!defined($self->{cache_hosts})) {
$self->{cache_hosts} = $options{custom}->cache_hosts();
}
foreach my $host (@{$self->{cache_hosts}}) {
next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne ''
&& $host->{name} !~ /$self->{option_results}->{filter_name}/);
next if (defined($self->{option_results}->{filter_id}) && $self->{option_results}->{filter_id} ne ''
&& $host->{id} !~ /$self->{option_results}->{filter_id}/);
my $stats = $options{custom}->get_host_statistics(id => $host->{id});
foreach my $stat (@{$stats}) {
next if ($stat->{name} !~ /^cpu\.current\..*/);
$self->{host}->{$host->{id}}->{display} = $host->{name};
$self->{host}->{$host->{id}}->{user} = $stat->{values}->{value}[0]->{datum} if ($stat->{name} =~ /^cpu.current.user$/);
$self->{host}->{$host->{id}}->{system} = $stat->{values}->{value}[0]->{datum} if ($stat->{name} =~ /^cpu.current.system$/);
}
}
if (scalar(keys %{$self->{host}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No hosts found.");
$self->{output}->option_exit();
}
}
1;
__END__
=head1 MODE
Check host cpu utilization.
=over 8
=item B<--filter-name>
Filter host name (Can be a regexp).
=item B<--filter-id>
Filter host id (Can be a regexp).
=item B<--warning-*>
Threshold warning.
Can be: 'host-cpu-user-utilization-percentage', 'host-cpu-system-utilization-percentage'.
=item B<--critical-*>
Threshold critical.
Can be: 'host-cpu-user-utilization-percentage', 'host-cpu-system-utilization-percentage'.
=back
=cut

View File

@ -46,14 +46,14 @@ sub check_options {
sub manage_selection {
my ($self, %options) = @_;
$self->{clusters} = $options{custom}->request_api(url_path => '/ovirt-engine/api/clusters');
$self->{clusters} = $options{custom}->list_clusters();
}
sub run {
my ($self, %options) = @_;
$self->manage_selection(%options);
foreach my $cluster (@{$self->{clusters}->{cluster}}) {
foreach my $cluster (@{$self->{clusters}}) {
next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne ''
&& $cluster->{name} !~ /$self->{option_results}->{filter_name}/);
@ -77,7 +77,7 @@ sub disco_show {
my ($self, %options) = @_;
$self->manage_selection(%options);
foreach my $cluster (@{$self->{clusters}->{data_center}}) {
foreach my $cluster (@{$self->{clusters}}) {
$self->{output}->add_disco_entry(
id => $cluster->{id},
name => $cluster->{name},

View File

@ -46,14 +46,14 @@ sub check_options {
sub manage_selection {
my ($self, %options) = @_;
$self->{datacenters} = $options{custom}->request_api(url_path => '/ovirt-engine/api/datacenters');
$self->{datacenters} = $options{custom}->list_datacenters();
}
sub run {
my ($self, %options) = @_;
$self->manage_selection(%options);
foreach my $datacenter (@{$self->{datacenters}->{data_center}}) {
foreach my $datacenter (@{$self->{datacenters}}) {
next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne ''
&& $datacenter->{name} !~ /$self->{option_results}->{filter_name}/);
@ -77,7 +77,7 @@ sub disco_show {
my ($self, %options) = @_;
$self->manage_selection(%options);
foreach my $datacenter (@{$self->{datacenters}->{data_center}}) {
foreach my $datacenter (@{$self->{datacenters}}) {
$self->{output}->add_disco_entry(
id => $datacenter->{id},
name => $datacenter->{name},

View File

@ -46,19 +46,22 @@ sub check_options {
sub manage_selection {
my ($self, %options) = @_;
$self->{hosts} = $options{custom}->request_api(url_path => '/ovirt-engine/api/hosts');
$self->{hosts} = $options{custom}->list_hosts();
}
sub run {
my ($self, %options) = @_;
$self->manage_selection(%options);
foreach my $host (@{$self->{hosts}->{host}}) {
foreach my $host (@{$self->{hosts}}) {
next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne ''
&& $host->{name} !~ /$self->{option_results}->{filter_name}/);
$self->{output}->output_add(long_msg => sprintf("[id = %s][name = %s]",
$host->{id}, $host->{name}));
$self->{output}->output_add(long_msg => sprintf("[id = %s][name = %s][address = %s][status = %s]",
$host->{id},
$host->{name},
$host->{address},
$host->{status}));
}
$self->{output}->output_add(severity => 'OK',
@ -70,17 +73,19 @@ sub run {
sub disco_format {
my ($self, %options) = @_;
$self->{output}->add_disco_format(elements => ['id', 'name']);
$self->{output}->add_disco_format(elements => ['id', 'name', 'address', 'status']);
}
sub disco_show {
my ($self, %options) = @_;
$self->manage_selection(%options);
foreach my $host (@{$self->{hosts}->{data_center}}) {
foreach my $host (@{$self->{hosts}}) {
$self->{output}->add_disco_entry(
id => $host->{id},
name => $host->{name},
address => $host->{address},
status => $host->{status},
);
}
}

View File

@ -31,6 +31,7 @@ sub new {
$self->{version} = '1.0';
%{$self->{modes}} = (
'cpu-host' => 'apps::virtualization::ovirt::mode::cpuhost',
'list-clusters' => 'apps::virtualization::ovirt::mode::listclusters',
'list-datacenters' => 'apps::virtualization::ovirt::mode::listdatacenters',
'list-hosts' => 'apps::virtualization::ovirt::mode::listhosts',