New plugin: cAdvisor - monitors running containers on one or several docker nodes

This commit is contained in:
Olivier MERCIER 2018-05-24 16:12:21 +02:00
parent 89a3afc649
commit 19dc776b45
5 changed files with 1189 additions and 0 deletions

View File

@ -0,0 +1,409 @@
#
# Copyright 2018 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 cloud::docker::cadvisor::custom::api;
use strict;
use warnings;
use centreon::plugins::misc;
use centreon::plugins::http;
use JSON::XS;
use FileHandle;
sub new {
my ($class, %options) = @_;
my $self = {};
bless $self, $class;
if (!defined($options{output})) {
print "Class Custom: Need to specify 'output' argument.\n";
exit 3;
}
if (!defined($options{options})) {
$options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
$options{output}->option_exit();
}
if (!defined($options{noptions})) {
$options{options}->add_options(arguments =>
{
"hostname:s@" => { name => 'hostname' },
"port:s" => { name => 'port', default => 8080 },
"proto:s" => { name => 'proto', default => 'http' },
"credentials" => { name => 'credentials' },
"username:s" => { name => 'username' },
"password:s" => { name => 'password' },
"proxyurl:s" => { name => 'proxyurl' },
"proxypac:s" => { name => 'proxypac' },
"timeout:s" => { name => 'timeout', default => 10 },
"ssl:s" => { name => 'ssl' },
"cert-file:s" => { name => 'cert_file' },
"key-file:s" => { name => 'key_file' },
"cacert-file:s" => { name => 'cacert_file' },
"cert-pwd:s" => { name => 'cert_pwd' },
"cert-pkcs12" => { name => 'cert_pkcs12' },
"api-version:s" => { name => 'api_version', default => 'v1.3' },
});
}
$options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
$self->{output} = $options{output};
$self->{mode} = $options{mode};
return $self;
}
sub set_options {
my ($self, %options) = @_;
$self->{option_results} = $options{option_results};
}
sub set_defaults {
my ($self, %options) = @_;
foreach (keys %{$options{default}}) {
if ($_ eq $self->{mode}) {
for (my $i = 0; $i < scalar(@{$options{default}->{$_}}); $i++) {
foreach my $opt (keys %{$options{default}->{$_}[$i]}) {
if (!defined($self->{option_results}->{$opt}[$i])) {
$self->{option_results}->{$opt}[$i] = $options{default}->{$_}[$i]->{$opt};
}
}
}
}
}
}
sub check_options {
my ($self, %options) = @_;
# return 1 = ok still hostname
# return 0 = no hostname left
$self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : undef;
return 0 if (defined($self->{option_results}->{api_read_file}) && $self->{option_results}->{api_read_file} ne '');
if (!defined($self->{hostname})) {
$self->{output}->add_option_msg(short_msg => "Need to specify hostname option.");
$self->{output}->option_exit();
}
$self->{http} = {};
foreach my $node_name (@{$self->{hostname}}) {
if ($node_name ne '') {
$self->{http}->{$node_name} = centreon::plugins::http->new(output => $self->{output});
$self->{option_results}->{hostname} = $node_name;
$self->{http}->{$node_name}->set_options(%{$self->{option_results}});
}
}
return 0;
}
sub get_hostnames {
my ($self, %options) = @_;
return $self->{hostname};
}
sub get_port {
my ($self, %options) = @_;
return $self->{option_results}->{port};
}
sub internal_api_list_nodes {
my ($self, %options) = @_;
my $response = $self->{http}->{$options{node_name}}->request(
url_path => '/api/' . $self->{option_results}->{api_version} . '/containers/docker/',
unknown_status => '', critical_status => '', warning_status => '');
my $nodes;
eval {
$nodes = JSON::XS->new->utf8->decode($response);
};
if ($@) {
$nodes = {};
$self->{output}->output_add(severity => 'UNKNOWN',
short_msg => "Node '$options{node_name}': cannot decode json list nodes response: $@");
} else {
$nodes = {} if (ref($nodes) eq 'ARRAY'); # nodes is not in a swarm
}
return $nodes;
}
sub internal_api_info {
my ($self, %options) = @_;
my $response = $self->{http}->{$options{node_name}}->request(
url_path => '/api/' . $self->{option_results}->{api_version} . '/machine/',
unknown_status => '', critical_status => '', warning_status => '');
my $nodes;
eval {
$nodes = JSON::XS->new->utf8->decode($response);
};
if ($@) {
$nodes = [];
$self->{output}->output_add(severity => 'UNKNOWN',
short_msg => "Node '$options{node_name}': cannot decode json info response: $@");
}
return $nodes;
}
sub internal_api_list_containers {
my ($self, %options) = @_;
my $response = $self->{http}->{$options{node_name}}->request(
url_path => '/api/' . $self->{option_results}->{api_version} . '/containers/docker/',
unknown_status => '', critical_status => '', warning_status => '');
my $containers = [];
my $containers_ids;
eval {
$containers_ids = JSON::XS->new->utf8->decode($response);
};
if ($@) {
$containers = [];
$self->{output}->output_add(severity => 'UNKNOWN',
short_msg => "Node '$options{node_name}': cannot decode json get containers response: $@");
}
foreach my $container (@{$containers_ids->{subcontainers}}) {
my $json_response = JSON::XS->new->utf8->decode(
$self->{http}->{$options{node_name}}->request(
url_path => '/api/' . $self->{option_results}->{api_version} . '/containers/' . $container->{name}
)
);
push @$containers, {id => $json_response->{id}, names => $json_response->{aliases}, node => $options{node_name}};
}
return $containers;
}
sub internal_api_get_machine_stats {
my ($self, %options) = @_;
my $response = $self->{http}->{$options{node_name}}->request(
url_path => '/api/' . $self->{option_results}->{api_version} . '/machine',
unknown_status => '', critical_status => '', warning_status => '');
my $machine_stats;
my $full_machine_stats;
eval {
$full_machine_stats = JSON::XS->new->utf8->decode($response);
};
if ($@) {
$machine_stats = {};
$self->output_add(severity => 'UNKNOWN',
short_msg => "Node '$options{node_name}': cannot decode json get container stats response: $@");
} else {
$machine_stats->{num_cores} = $full_machine_stats->{num_cores};
$machine_stats->{memory_capacity} = $full_machine_stats->{memory_capacity};
}
return $machine_stats;
}
sub internal_api_get_container_stats {
my ($self, %options) = @_;
my $response = $self->{http}->{$options{node_name}}->request(
url_path => '/api/' . $self->{option_results}->{api_version} . '/containers/docker/' . $options{container_id},
unknown_status => '', critical_status => '', warning_status => '');
my $container_stats;
my $full_container_stats;
eval {
$full_container_stats = JSON::XS->new->utf8->decode($response);
};
if ($@) {
$container_stats = [];
$self->output_add(severity => 'UNKNOWN',
short_msg => "Node '$options{node_name}': cannot decode json get container stats response: $@");
} else {
$container_stats->[0] = $full_container_stats->{stats}[0];
$container_stats->[1] = $full_container_stats->{stats}[scalar(@{$full_container_stats->{stats}}) - 1];
}
return $container_stats;
}
sub api_list_containers {
my ($self, %options) = @_;
my $containers = {};
foreach my $node_name (keys %{$self->{http}}) {
my $list_containers = $self->internal_api_list_containers(node_name => $node_name);
foreach my $container (@$list_containers) {
$containers->{$container->{id}} = {
NodeName => $node_name,
Name => $container->{names}->[0],
};
}
}
return $containers;
}
sub api_get_machine_stats {
my ($self, %options) = @_;
my $machine_stats = {};
foreach my $node_name (keys %{$self->{http}}) {
$machine_stats->{$node_name} = $self->internal_api_get_machine_stats(node_name => $node_name);
}
return $machine_stats;
}
sub api_list_nodes {
my ($self, %options) = @_;
my $nodes = {};
foreach my $node_name (keys %{$self->{http}}) {
my $info_node = $self->internal_api_info(node_name => $node_name);
my $list_nodes = $self->internal_api_list_nodes(node_name => $node_name);
$nodes->{$node_name} = { nodes => [],
num_cores => $info_node->{num_cores},
cpu_frequency_khz => $info_node->{cpu_frequency_khz},
memory_capacity => $info_node->{memory_capacity},
#containers_stopped => $info_node->{ContainersStopped},
#containers_paused => $info_node->{ContainersPaused},
};
foreach my $node (@{$list_nodes->{subcontainers}}) {
push @{$nodes->{$node_name}->{nodes}}, {
name => $node->{name}
}
}
}
return $nodes;
}
sub api_get_containers {
my ($self, %options) = @_;
if (defined($self->{option_results}->{api_read_file}) && $self->{option_results}->{api_read_file} ne '') {
return $self->api_read_file();
}
my $content_total = $self->api_list_containers();
if (defined($options{container_id}) && $options{container_id} ne '') {
if (defined($content_total->{$options{container_id}})) {
$content_total->{$options{container_id}}->{Stats} = $self->internal_api_get_container_stats(node_name => $content_total->{$options{container_id}}->{NodeName}, container_id => $options{container_id});
}
} elsif (defined($options{container_name}) && $options{container_name} ne '') {
my $container_id;
foreach (keys %$content_total) {
if ($content_total->{$_}->{Name} eq $options{container_name}) {
$container_id = $_;
last;
}
}
if (defined($container_id)) {
$content_total->{$container_id}->{Stats} = $self->internal_api_get_container_stats(node_name => $content_total->{$container_id}->{NodeName}, container_id => $container_id);
}
} else {
foreach my $container_id (keys %{$content_total}) {
$content_total->{$container_id}->{Stats} = $self->internal_api_get_container_stats(node_name => $content_total->{$container_id}->{NodeName}, container_id => $container_id);
}
}
return $content_total;
}
1;
__END__
=head1 NAME
Docker REST API
=head1 SYNOPSIS
Docker Rest API custom mode
=head1 REST API OPTIONS
=over 8
=item B<--hostname>
IP Addr/FQDN of the docker node (can be multiple).
=item B<--port>
Port used (Default: 8080)
=item B<--proto>
Specify https if needed (Default: 'http')
=item B<--credentials>
Specify this option if you access webpage over basic authentification
=item B<--username>
Specify username for basic authentification (Mandatory if --credentials is specidied)
=item B<--password>
Specify password for basic authentification (Mandatory if --credentials is specidied)
=item B<--proxyurl>
Proxy URL
=item B<--proxypac>
Proxy pac file (can be an url or local file)
=item B<--timeout>
Threshold for HTTP timeout (Default: 10)
=item B<--ssl>
Specify SSL version (example : 'sslv3', 'tlsv1'...)
=item B<--cert-file>
Specify certificate to send to the webserver
=item B<--key-file>
Specify key to send to the webserver
=item B<--cacert-file>
Specify root certificate to send to the webserver
=item B<--cert-pwd>
Specify certificate's password
=item B<--cert-pkcs12>
Specify type of certificate (PKCS12)
=back
=head1 DESCRIPTION
B<custom>.
=cut

View File

@ -0,0 +1,401 @@
#
# Copyright 2018 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 cloud::docker::cadvisor::mode::containerusage;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use Digest::MD5 qw(md5_hex);
use DateTime;
my $instance_mode;
sub custom_memory_output {
my ($self, %options) = @_;
my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{memory_total_absolute});
my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{memory_used_absolute});
my $msg = sprintf("Memory Used: %s (%.2f%%) Total: %s" ,
$total_used_value . " " . $total_used_unit, 100 * $self->{result_values}->{memory_used_absolute} / $self->{result_values}->{memory_total_absolute},
$total_size_value . " " . $total_size_unit);
return $msg;
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'containers', type => 1, cb_prefix_output => 'prefix_containers_output', message_multiple => 'All containers are ok', skipped_code => { -11 => 1 } },
{ name => 'containers_diskio', type => 1, cb_prefix_output => 'prefix_containers_diskio_output', message_multiple => 'All container disk IOps are ok', skipped_code => { -11 => 1 } },
{ name => 'containers_traffic', type => 1, cb_prefix_output => 'prefix_containers_traffic_output', message_multiple => 'All container traffics are ok', skipped_code => { -11 => 1 } },
];
$self->{maps_counters}->{containers} = [
{ label => 'cpu-number', set => {
key_values => [ { name => 'cpu_number'}, { name => 'display' } ],
output_template => 'CPU: %d core(s)',
output_use => 'cpu_number_absolute',
perfdatas => [
{ label => 'cpu_number', value => 'cpu_number_absolute', template => '%d',
min => 0, label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'cpu-total', set => {
key_values => [ { name => 'cpu_total'}, { name => 'display' } ],
output_template => 'CPU Usage: %.2f %%',
output_use => 'cpu_total_absolute',
perfdatas => [
{ label => 'cpu_total', value => 'cpu_total_absolute', template => '%.2f',
unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'cpu-user', set => {
key_values => [ { name => 'cpu_user'}, { name => 'display' } ],
output_template => 'CPU User: %.2f %%',
output_use => 'cpu_user_absolute',
perfdatas => [
{ label => 'cpu_user', value => 'cpu_user_absolute', template => '%.2f',
unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'cpu-system', set => {
key_values => [ { name => 'cpu_system' }, { name => 'display' } ],
output_template => 'CPU System: %.2f %%',
output_use => 'cpu_system_absolute',
perfdatas => [
{ label => 'cpu_system', value => 'cpu_system_absolute', template => '%.2f',
unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'memory', set => {
key_values => [ { name => 'memory_used' }, { name => 'memory_total' }, { name => 'display' } ],
output_change_bytes => 1,
closure_custom_output => $self->can('custom_memory_output'),
perfdatas => [
{ label => 'memory_used', value => 'memory_used_absolute', template => '%s',
min => 0, max => 'memory_total_absolute',unit => 'B', label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'memory-cache', set => {
key_values => [ { name => 'memory_cache' }, { name => 'display' } ],
output_change_bytes => 1,
output_template => 'Memory Cache: %s %s',
perfdatas => [
{ label => 'memory_cache', value => 'memory_cache_absolute', template => '%s',
min => 0, unit => 'B', label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'memory-rss', set => {
key_values => [ { name => 'memory_rss' }, { name => 'display' } ],
output_change_bytes => 1,
output_template => 'Memory RSS: %s %s',
perfdatas => [
{ label => 'memory_rss', value => 'memory_rss_absolute', template => '%s',
min => 0, unit => 'B', label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'swap', set => {
key_values => [ { name => 'swap' }, { name => 'display' } ],
output_change_bytes => 1,
output_template => 'Swap: %s %s',
perfdatas => [
{ label => 'swap', value => 'swap_absolute', template => '%s',
min => 0, unit => 'B', label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
];
$self->{maps_counters}->{containers_diskio} = [
{ label => 'diskio-read', set => {
key_values => [ { name => 'diskio_read' }, { name => 'display' } ],
output_change_bytes => 1,
output_template => 'Disk IO Read: %s %s/s',
perfdatas => [
{ label => 'diskio_read', value => 'diskio_read_absolute', template => '%.2f',
min => 0, unit => 'B/s', label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'diskio-write', set => {
key_values => [ { name => 'diskio_write' }, { name => 'display' } ],
output_change_bytes => 1,
output_template => 'Disk IO Write: %s %s/s',
perfdatas => [
{ label => 'diskio_write', value => 'diskio_write_absolute', template => '%.2f',
min => 0, unit => 'B/s', label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
];
$self->{maps_counters}->{containers_traffic} = [
{ label => 'traffic-in', set => {
key_values => [ { name => 'traffic_in' }, { name => 'display' } ],
output_change_bytes => 2,
output_template => 'Traffic In: %s %s/s',
perfdatas => [
{ label => 'traffic_in', value => 'traffic_in_absolute', template => '%.2f',
min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'traffic-out', set => {
key_values => [ { name => 'traffic_out' }, { name => 'display' } ],
output_change_bytes => 2,
output_template => 'Traffic Out: %s %s/s',
perfdatas => [
{ label => 'traffic_out', value => 'traffic_out_absolute', template => '%.2f',
min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
];
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$self->{version} = '1.0';
$options{options}->add_options(arguments =>
{
"container-id:s" => { name => 'container_id' },
"container-name:s" => { name => 'container_name' },
"filter-name:s" => { name => 'filter_name' },
"use-name" => { name => 'use_name' },
"warning-container-status:s" => { name => 'warning_container_status', default => '' },
"critical-container-status:s" => { name => 'critical_container_status', default => '' },
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
$instance_mode = $self;
$self->change_macros();
}
sub prefix_containers_traffic_output {
my ($self, %options) = @_;
return "Container '" . $options{instance_value}->{display} . "' ";
}
sub prefix_containers_diskio_output {
my ($self, %options) = @_;
return "Container '" . $options{instance_value}->{display} . "' ";
}
sub prefix_containers_output {
my ($self, %options) = @_;
return "Container '" . $options{instance_value}->{display} . "' ";
}
sub change_macros {
my ($self, %options) = @_;
foreach (('warning_container_status', 'critical_container_status')) {
if (defined($self->{option_results}->{$_})) {
$self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{result_values}->{$1}/g;
}
}
}
sub manage_selection {
my ($self, %options) = @_;
$self->{containers} = {};
$self->{containers_traffic} = {};
$self->{containers_diskio} = {};
my $result = $options{custom}->api_get_containers(
container_id => $self->{option_results}->{container_id},
container_name => $self->{option_results}->{container_name}
);
my $machine_stats = $options{custom}->api_get_machine_stats();
foreach my $container_id (keys %{$result}) {
next if (!defined($result->{$container_id}->{Stats}));
my $name = $result->{$container_id}->{Name};
if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
$name !~ /$self->{option_results}->{filter_name}/) {
$self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1);
next;
}
my $first_index = 0;
my $first_stat = $result->{$container_id}->{Stats}[$first_index];
my $first_ts = $first_stat->{timestamp};
my $first_dt = $self->parse_date(date => $first_ts);
my $first_cpu_total = $first_stat->{cpu}{usage}{total};
my $first_cpu_user = $first_stat->{cpu}{usage}{user};
my $first_cpu_system = $first_stat->{cpu}{usage}{system};
my $last_index = scalar @{$result->{$container_id}->{Stats}} - 1;
my $last_stat = $result->{$container_id}->{Stats}[$last_index];
my $last_ts = $last_stat->{timestamp};
my $last_dt = $self->parse_date(date => $last_ts);
my $last_cpu_total = $last_stat->{cpu}{usage}{total};
my $last_cpu_user = $last_stat->{cpu}{usage}{user};
my $last_cpu_system = $last_stat->{cpu}{usage}{system};
my $diff_ts = $last_dt - $first_dt;
my $cpu_number = $machine_stats->{$result->{$container_id}->{NodeName}}->{num_cores};
my $read_io = {};
my $write_io = {};
$self->{containers}->{$container_id} = {
node_name => $result->{$container_id}->{NodeName},
display => defined($self->{option_results}->{use_name}) ? $name : $container_id,
name => $name,
cpu_total => ($last_cpu_total - $first_cpu_total) / ($diff_ts * 1_000_000_000) * 100 / $cpu_number,
cpu_user => ($last_cpu_user - $first_cpu_user) / ($diff_ts * 1_000_000_000) * 100 / $cpu_number,
cpu_system => ($last_cpu_system - $first_cpu_system) / ($diff_ts * 1_000_000_000) * 100 / $cpu_number,
cpu_number => $cpu_number,
memory_used => $last_stat->{memory}{usage},
memory_cache => $last_stat->{memory}{cache},
memory_rss => $last_stat->{memory}{rss},
swap => $last_stat->{memory}{swap},
memory_total => $machine_stats->{$result->{$container_id}->{NodeName}}->{memory_capacity},
};
# The API does not present the devices in the same order between the first and the last stats sample, so we can't just compare [0] with [0] and [1] with [1], we have to check the name of the device
foreach my $diskio_index (0..(scalar(@{$first_stat->{diskio}->{io_service_bytes}}) - 1)) {
my $name = defined($self->{option_results}->{use_name}) ? $name : $container_id;
my $device = $first_stat->{diskio}->{io_service_bytes}->[$diskio_index]->{device};
$name .= ':' . $device;
$read_io->{$name} = {first => $first_stat->{diskio}->{io_service_bytes}->[$diskio_index]->{stats}->{Read}};
$write_io->{$name} = {first => $first_stat->{diskio}->{io_service_bytes}->[$diskio_index]->{stats}->{Write}};
}
foreach my $diskio_index (0..(scalar(@{$last_stat->{diskio}->{io_service_bytes}}) - 1)) {
my $name = defined($self->{option_results}->{use_name}) ? $name : $container_id;
my $device = $last_stat->{diskio}->{io_service_bytes}->[$diskio_index]->{device};
$name .= ':' . $device;
$read_io->{$name}->{last} = $last_stat->{diskio}->{io_service_bytes}->[$diskio_index]->{stats}->{Read};
$write_io->{$name}->{last} = $last_stat->{diskio}->{io_service_bytes}->[$diskio_index]->{stats}->{Write};
}
foreach my $diskio_disk (keys %$read_io) {
$self->{containers_diskio}->{$diskio_disk} = {
display => $diskio_disk,
diskio_read => ($read_io->{$diskio_disk}->{last} - $read_io->{$diskio_disk}->{first}) / $diff_ts ,
diskio_write => ($write_io->{$diskio_disk}->{last} - $write_io->{$diskio_disk}->{first}) / $diff_ts,
};
}
foreach my $interface_index (0..(scalar(@{$first_stat->{network}->{interfaces}}) - 1)) {
my $name = defined($self->{option_results}->{use_name}) ? $name : $container_id;
$name .= '.' . $first_stat->{network}->{interfaces}->[$interface_index]->{name};
$self->{containers_traffic}->{$name} = {
display => $name,
traffic_in => ($last_stat->{network}->{interfaces}->[$interface_index]->{rx_packets} - $first_stat->{network}->{interfaces}->[$interface_index]->{rx_packets}) / $diff_ts * 8,
traffic_out => ($last_stat->{network}->{interfaces}->[$interface_index]->{tx_packets} - $first_stat->{network}->{interfaces}->[$interface_index]->{tx_packets}) / $diff_ts * 8,
};
}
}
if (scalar(keys %{$self->{containers}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No containers found.");
$self->{output}->option_exit();
}
my $hostnames = $options{custom}->get_hostnames();
}
sub parse_date {
my ($self, %options) = @_;
if ($options{date} !~ /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.(\d*)([^\d]+)$/) {
$self->{output}->add_option_msg(short_msg => "Wrong time found '" . $options{date} . "'.");
$self->{output}->option_exit();
}
my $dt = DateTime->new(
year => $1, month => $2, day => $3,
hour => $4, minute => $5, second => $6,
time_zone => $8
);
# return epoch time with nanoseconds
return $dt->epoch.".".$7;
}
1;
__END__
=head1 MODE
Check container usage.
=over 8
=item B<--container-id>
Exact container ID.
=item B<--container-name>
Exact container name (if multiple names: names separated by ':').
=item B<--use-name>
Use docker name for perfdata and display.
=item B<--filter-name>
Filter by container name (can be a regexp).
=item B<--filter-counters>
Only display some counters (regexp can be used).
Example: --filter-counters='^container-status$'
=item B<--warning-*>
Threshold warning.
Can be: 'read-iops', 'write-iops', 'traffic-in', 'traffic-out',
'cpu' (%), 'memory' (%).
=item B<--critical-*>
Threshold critical.
Can be: 'read-iops', 'write-iops', 'traffic-in', 'traffic-out',
'cpu' (%), 'memory' (%).
=item B<--warning-container-status>
Set warning threshold for status (Default: -)
Can used special variables like: %{name}, %{state}.
=item B<--critical-container-status>
Set critical threshold for status (Default: -).
Can used special variables like: %{name}, %{state}.
=back
=cut

View File

@ -0,0 +1,98 @@
#
# Copyright 2018 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 cloud::docker::cadvisor::mode::listcontainers;
use base qw(centreon::plugins::mode);
use strict;
use warnings;
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$self->{version} = '1.0';
$options{options}->add_options(arguments =>
{
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
}
sub manage_selection {
my ($self, %options) = @_;
$self->{containers} = $options{custom}->api_list_containers();
}
sub run {
my ($self, %options) = @_;
$self->manage_selection(%options);
foreach my $container_id (sort keys %{$self->{containers}}) {
$self->{output}->output_add(long_msg => '[id = ' . $container_id . "] [name = '" . $self->{containers}->{$container_id}->{Name} . "']" .
" [node = '" . $self->{containers}->{$container_id}->{NodeName} . "']");
}
$self->{output}->output_add(severity => 'OK',
short_msg => 'List containers:');
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
$self->{output}->exit();
}
sub disco_format {
my ($self, %options) = @_;
$self->{output}->add_disco_format(elements => ['id', 'name', 'node', 'state']);
}
sub disco_show {
my ($self, %options) = @_;
$self->manage_selection(%options);
foreach my $container_id (sort keys %{$self->{containers}}) {
$self->{output}->add_disco_entry(name => $self->{containers}->{$container_id}->{Name},
node => $self->{containers}->{$container_id}->{NodeName},
state => $self->{containers}->{$container_id}->{State},
id => $container_id,
);
}
}
1;
__END__
=head1 MODE
List containers.
=over 8
=back
=cut

View File

@ -0,0 +1,222 @@
#
# Copyright 2018 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 cloud::docker::cadvisor::mode::nodestatus;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
my $instance_mode;
sub custom_status_threshold {
my ($self, %options) = @_;
my $status = 'ok';
my $message;
eval {
local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; };
if (defined($instance_mode->{option_results}->{critical_node_status}) && $instance_mode->{option_results}->{critical_node_status} ne '' &&
eval "$instance_mode->{option_results}->{critical_node_status}") {
$status = 'critical';
} elsif (defined($instance_mode->{option_results}->{warning_node_status}) && $instance_mode->{option_results}->{warning_node_status} ne '' &&
eval "$instance_mode->{option_results}->{warning_node_status}") {
$status = 'warning';
}
};
if (defined($message)) {
$self->{output}->output_add(long_msg => 'filter status issue: ' . $message);
}
return $status;
}
sub custom_status_output {
my ($self, %options) = @_;
my $msg = 'status : ' . $self->{result_values}->{status} . ' [manager status: ' . $self->{result_values}->{manager_status} . ']';
return $msg;
}
sub custom_status_calc {
my ($self, %options) = @_;
$self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'};
$self->{result_values}->{manager_status} = $options{new_datas}->{$self->{instance} . '_manager_status'};
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
return 0;
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'node', type => 1, cb_prefix_output => 'prefix_node_output', message_multiple => 'All node informations are ok', skipped_code => { -11 => 1 } },
{ name => 'nodes', type => 1, cb_prefix_output => 'prefix_node_output', message_multiple => 'All node status are ok', skipped_code => { -11 => 1 } },
];
$self->{maps_counters}->{nodes} = [
{ label => 'node-status', threshold => 0, set => {
key_values => [ { name => 'status' }, { name => 'manager_status' }, { name => 'display' } ],
closure_custom_calc => $self->can('custom_status_calc'),
closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => $self->can('custom_status_threshold'),
}
},
];
$self->{maps_counters}->{node} = [
{ label => 'containers-running', set => {
key_values => [ { name => 'containers_running' }, { name => 'display' } ],
output_template => 'Containers Running : %s',
perfdatas => [
{ label => 'containers_running', value => 'containers_running_absolute', template => '%s',
min => 0, label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'num-cores', set => {
key_values => [ { name => 'num_cores' }, { name => 'display' } ],
output_template => 'CPU cores: %s',
perfdatas => [
{ label => 'num_cores', value => 'num_cores_absolute', template => '%s',
min => 0, label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'memory-capacity', set => {
key_values => [ { name => 'memory_capacity' }, { name => 'display' } ],
output_template => 'Mem capacity %s %s',
perfdatas => [
{ label => 'memory_capacity', value => 'memory_capacity_absolute', unit => 'B', output_change_bytes => 1, template => '%s',
min => 0, label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'cpu-frequency', set => {
key_values => [ { name => 'cpu_frequency' }, { name => 'display' } ],
output_template => 'CPU frequency %s %s',
perfdatas => [
{ label => 'cpu_frequency', value => 'cpu_frequency_absolute', unit => 'Hz', output_change_bytes => 1, template => '%s',
min => 0, label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
];
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$self->{version} = '1.0';
$options{options}->add_options(arguments =>
{
"warning-node-status:s" => { name => 'warning_node_status', default => '' },
"critical-node-status:s" => { name => 'critical_node_status', default => '%{status} !~ /ready/ || %{manager_status} !~ /reachable|-/' },
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
$instance_mode = $self;
$self->change_macros();
}
sub prefix_node_output {
my ($self, %options) = @_;
return "Node '" . $options{instance_value}->{display} . "' ";
}
sub change_macros {
my ($self, %options) = @_;
foreach (('warning_node_status', 'critical_node_status')) {
if (defined($self->{option_results}->{$_})) {
$self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{result_values}->{$1}/g;
}
}
}
sub manage_selection {
my ($self, %options) = @_;
$self->{node} = {};
$self->{nodes} = {};
my $result = $options{custom}->api_list_nodes();
foreach my $node_name (keys %{$result}) {
$self->{node}->{$node_name} = {
display => $node_name,
num_cores => $result->{$node_name}->{num_cores},
cpu_frequency => $result->{$node_name}->{cpu_frequency_khz} * 1000,
memory_capacity => $result->{$node_name}->{memory_capacity},
containers_running => scalar(@{$result->{$node_name}->{nodes}}),
};
}
if (scalar(keys %{$self->{node}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No node found.");
$self->{output}->option_exit();
}
}
1;
__END__
=head1 MODE
Check node status.
=over 8
=item B<--warning-node-status>
Set warning threshold for status (Default: -)
Can used special variables like: %{display}, %{status}, %{manager_status}.
=item B<--critical-node-status>
Set critical threshold for status (Default: '%{status} !~ /ready/ || %{manager_status} !~ /reachable|-/').
Can used special variables like: %{display}, %{status}, %{manager_status}.
=item B<--warning-*>
Threshold warning.
Can be: 'containers-running', 'containers-paused', 'containers-stopped'.
=item B<--critical-*>
Threshold critical.
Can be: 'containers-running', 'containers-paused', 'containers-stopped'.,
=back
=cut

View File

@ -0,0 +1,59 @@
#
# Copyright 2018 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 cloud::docker::cadvisor::plugin;
use strict;
use warnings;
use base qw(centreon::plugins::script_custom);
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$self->{version} = '0.3';
%{$self->{modes}} = (
'container-usage' => 'cloud::docker::cadvisor::mode::containerusage',
'list-containers' => 'cloud::docker::cadvisor::mode::listcontainers',
'node-status' => 'cloud::docker::cadvisor::mode::nodestatus',
);
$self->{custom_modes}{api} = 'cloud::docker::cadvisor::custom::api';
return $self;
}
sub init {
my ( $self, %options ) = @_;
$self->SUPER::init(%options);
}
1;
__END__
=head1 PLUGIN DESCRIPTION
Check Docker nodes and containers through cAdvisor API.
Requirements: cAdvisor supporting API version 1.3+
=cut