(plugin) cloud::docker::restapi - mode container-usage add option --add-health (#3906)

This commit is contained in:
qgarnier 2022-09-21 11:33:35 +02:00 committed by GitHub
parent 13a24f0c52
commit 07fbe5fbc8
2 changed files with 114 additions and 63 deletions

View File

@ -261,14 +261,16 @@ sub internal_api_info {
sub internal_api_list_containers { sub internal_api_list_containers {
my ($self, %options) = @_; my ($self, %options) = @_;
my $response = $self->{http}->request( my $response = $self->{http}->request(
hostname => $options{node_name}, hostname => $options{node_name},
url_path => '/containers/json?all=true', url_path => '/containers/json',
get_param => ['all=true'],
unknown_status => '', unknown_status => '',
critical_status => '', critical_status => '',
warning_status => '' warning_status => ''
); );
my $containers; my $containers;
eval { eval {
$containers = JSON::XS->new->utf8->decode($response); $containers = JSON::XS->new->utf8->decode($response);
@ -284,16 +286,44 @@ sub internal_api_list_containers {
return $containers; return $containers;
} }
sub internal_api_get_container_inspector {
my ($self, %options) = @_;
my $response = $self->{http}->request(
hostname => $options{node_name},
url_path => '/containers/' . $options{container_id} . '/json',
unknown_status => '',
critical_status => '',
warning_status => ''
);
my $container_inspector;
eval {
$container_inspector = JSON::XS->new->utf8->decode($response);
};
if ($@) {
$container_inspector = {};
$self->{output}->output_add(
severity => 'UNKNOWN',
short_msg => "Node '$options{node_name}': cannot decode json get container inspector response: $@"
);
}
return $container_inspector;
}
sub internal_api_get_container_stats { sub internal_api_get_container_stats {
my ($self, %options) = @_; my ($self, %options) = @_;
my $response = $self->{http}->request( my $response = $self->{http}->request(
hostname => $options{node_name}, hostname => $options{node_name},
url_path => '/containers/' . $options{container_id} . '/stats?stream=false', url_path => '/containers/' . $options{container_id} . '/stats',
get_param => ['stream=false', 'one-shot=true'],
unknown_status => '', unknown_status => '',
critical_status => '', critical_status => '',
warning_status => '' warning_status => ''
); );
my $container_stats; my $container_stats;
eval { eval {
$container_stats = JSON::XS->new->utf8->decode($response); $container_stats = JSON::XS->new->utf8->decode($response);
@ -315,7 +345,8 @@ sub internal_api_list_services {
my $response = $self->{http}->request( my $response = $self->{http}->request(
hostname => $options{node_name}, hostname => $options{node_name},
url_path => '/services', url_path => '/services',
unknown_status => '', critical_status => '', warning_status => ''); unknown_status => '', critical_status => '', warning_status => ''
);
my $services; my $services;
eval { eval {
$services = JSON::XS->new->utf8->decode($response); $services = JSON::XS->new->utf8->decode($response);
@ -432,11 +463,14 @@ sub api_get_containers {
my $content_total = $self->cache_containers(statefile => $options{statefile}); my $content_total = $self->cache_containers(statefile => $options{statefile});
if (defined($options{container_id}) && $options{container_id} ne '') { if (defined($options{container_id}) && $options{container_id} ne '') {
if (defined($content_total->{$options{container_id}})) { 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}); $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});
if (defined($options{add_health})) {
$content_total->{ $options{container_id} }->{Inspector} = $self->internal_api_get_container_inspector(node_name => $content_total->{ $options{container_id} }->{NodeName}, container_id => $options{container_id});
}
} }
} elsif (defined($options{container_name}) && $options{container_name} ne '') { } elsif (defined($options{container_name}) && $options{container_name} ne '') {
my $container_id; my $container_id;
foreach (keys %$content_total) { foreach (keys %$content_total) {
if ($content_total->{$_}->{Name} eq $options{container_name}) { if ($content_total->{$_}->{Name} eq $options{container_name}) {
$container_id = $_; $container_id = $_;
@ -446,10 +480,16 @@ sub api_get_containers {
if (defined($container_id)) { 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); $content_total->{$container_id}->{Stats} = $self->internal_api_get_container_stats(node_name => $content_total->{$container_id}->{NodeName}, container_id => $container_id);
if (defined($options{add_health})) {
$content_total->{$container_id}->{Inspector} = $self->internal_api_get_container_inspector(node_name => $content_total->{$container_id}->{NodeName}, container_id => $container_id);
}
} }
} else { } else {
foreach my $container_id (keys %{$content_total}) { 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); $content_total->{$container_id}->{Stats} = $self->internal_api_get_container_stats(node_name => $content_total->{$container_id}->{NodeName}, container_id => $container_id);
if (defined($options{add_health})) {
$content_total->{$container_id}->{Inspector} = $self->internal_api_get_container_inspector(node_name => $content_total->{$container_id}->{NodeName}, container_id => $container_id);
}
} }
} }

View File

@ -30,7 +30,11 @@ use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_
sub custom_status_output { sub custom_status_output {
my ($self, %options) = @_; my ($self, %options) = @_;
return 'state : ' . $self->{result_values}->{state}; my $msg = 'state: ' . $self->{result_values}->{state};
if (defined($self->{instance_mode}->{option_results}->{add_health})) {
$msg .= ' [health: ' . $self->{result_values}->{health} . ']';
}
return $msg;
} }
sub custom_cpu_calc { sub custom_cpu_calc {
@ -87,7 +91,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}
@ -109,6 +113,18 @@ sub custom_memory_calc {
return 0; return 0;
} }
sub prefix_containers_traffic_output {
my ($self, %options) = @_;
return "Container '" . $options{instance_value}->{display} . "' ";
}
sub prefix_containers_output {
my ($self, %options) = @_;
return "Container '" . $options{instance_value}->{display} . "' ";
}
sub set_counters { sub set_counters {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -116,10 +132,10 @@ sub set_counters {
{ name => 'containers', type => 1, cb_prefix_output => 'prefix_containers_output', message_multiple => 'All containers are ok', skipped_code => { -10 => 1, -11 => 1 } }, { name => 'containers', type => 1, cb_prefix_output => 'prefix_containers_output', message_multiple => 'All containers are ok', skipped_code => { -10 => 1, -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 } } { 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} = [ $self->{maps_counters}->{containers} = [
{ label => 'container-status', type => 2, set => { { label => 'container-status', type => 2, set => {
key_values => [ { name => 'state' }, { name => 'name' } ], key_values => [ { name => 'state' }, { name => 'health' }, { name => 'name' } ],
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_ng closure_custom_threshold_check => \&catalog_status_threshold_ng
@ -127,7 +143,7 @@ sub set_counters {
}, },
{ label => 'cpu', nlabel => 'container.cpu.utilization.percentage', set => { { label => 'cpu', nlabel => 'container.cpu.utilization.percentage', set => {
key_values => [ { name => 'cpu_total_usage', diff => 1 }, { name => 'cpu_system_usage', diff => 1 }, { name => 'cpu_number' }, { name => 'display' } ], key_values => [ { name => 'cpu_total_usage', diff => 1 }, { name => 'cpu_system_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 => [
@ -146,7 +162,7 @@ sub set_counters {
}, },
{ label => 'read-iops', nlabel => 'container.disk.io.read.usage.iops', set => { { label => 'read-iops', nlabel => 'container.disk.io.read.usage.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' }
@ -155,7 +171,7 @@ sub set_counters {
}, },
{ label => 'write-iops', nlabel => 'container.disk.io.write.usage.iops', set => { { label => 'write-iops', nlabel => 'container.disk.io.write.usage.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' }
@ -163,12 +179,12 @@ sub set_counters {
} }
} }
]; ];
$self->{maps_counters}->{containers_traffic} = [ $self->{maps_counters}->{containers_traffic} = [
{ label => 'traffic-in', nlabel => 'container.traffic.in.bitspersecond', set => { { label => 'traffic-in', nlabel => 'container.traffic.in.bitspersecond', 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' }
@ -178,7 +194,7 @@ sub set_counters {
{ label => 'traffic-out', nlabel => 'container.traffic.out.bitspersecond', set => { { label => 'traffic-out', nlabel => 'container.traffic.out.bitspersecond', 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' }
@ -192,14 +208,15 @@ sub new {
my ($class, %options) = @_; my ($class, %options) = @_;
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 => {
'container-id:s' => { name => 'container_id' }, 'container-id:s' => { name => 'container_id' },
'container-name:s' => { name => 'container_name' }, 'container-name:s' => { name => 'container_name' },
'filter-name:s' => { name => 'filter_name' }, 'filter-name:s' => { name => 'filter_name' },
'use-name' => { name => 'use_name' } 'use-name' => { name => 'use_name' },
'add-health' => { name => 'add_health' }
}); });
$self->{statefile_cache_containers} = centreon::plugins::statefile->new(%options); $self->{statefile_cache_containers} = centreon::plugins::statefile->new(%options);
return $self; return $self;
} }
@ -211,39 +228,33 @@ sub check_options {
$self->{statefile_cache_containers}->check_options(%options); $self->{statefile_cache_containers}->check_options(%options);
} }
sub prefix_containers_traffic_output {
my ($self, %options) = @_;
return "Container '" . $options{instance_value}->{display} . "' ";
}
sub prefix_containers_output {
my ($self, %options) = @_;
return "Container '" . $options{instance_value}->{display} . "' ";
}
sub manage_selection { sub manage_selection {
my ($self, %options) = @_; my ($self, %options) = @_;
my $result = $options{custom}->api_get_containers(
container_id => $self->{option_results}->{container_id},
container_name => $self->{option_results}->{container_name},
statefile => $self->{statefile_cache_containers},
add_health => $self->{option_results}->{add_health}
);
$self->{containers} = {}; $self->{containers} = {};
$self->{containers_traffic} = {}; $self->{containers_traffic} = {};
my $result = $options{custom}->api_get_containers(container_id => $self->{option_results}->{container_id}, foreach my $container_id (keys %$result) {
container_name => $self->{option_results}->{container_name}, statefile => $self->{statefile_cache_containers}); next if (!defined($result->{$container_id}->{Stats}));
foreach my $container_id (keys %{$result}) {
next if (!defined($result->{$container_id}->{Stats}));
my $name = $result->{$container_id}->{Name}; my $name = $result->{$container_id}->{Name};
if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
$name !~ /$self->{option_results}->{filter_name}/) { $name !~ /$self->{option_results}->{filter_name}/) {
$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;
} }
my $read_io = $result->{$container_id}->{Stats}->{blkio_stats}->{io_service_bytes_recursive}->[0]->{value}; my $read_io = $result->{$container_id}->{Stats}->{blkio_stats}->{io_service_bytes_recursive}->[0]->{value};
my $write_io = $result->{$container_id}->{Stats}->{blkio_stats}->{io_service_bytes_recursive}->[1]->{value}; my $write_io = $result->{$container_id}->{Stats}->{blkio_stats}->{io_service_bytes_recursive}->[1]->{value};
$self->{containers}->{$container_id} = { $self->{containers}->{$container_id} = {
display => defined($self->{option_results}->{use_name}) ? $name : $container_id, display => defined($self->{option_results}->{use_name}) ? $name : $container_id,
health => defined($result->{$container_id}->{Inspector}) ? $result->{$container_id}->{Inspector}->{State}->{Health}->{Status} : '-',
name => $name, name => $name,
state => $result->{$container_id}->{State}, state => $result->{$container_id}->{State},
read_io => $read_io, read_io => $read_io,
@ -255,29 +266,31 @@ sub manage_selection {
memory_usage => $result->{$container_id}->{Stats}->{memory_stats}->{usage}, memory_usage => $result->{$container_id}->{Stats}->{memory_stats}->{usage},
memory_total => $result->{$container_id}->{Stats}->{memory_stats}->{limit} memory_total => $result->{$container_id}->{Stats}->{memory_stats}->{limit}
}; };
foreach my $interface (keys %{$result->{$container_id}->{Stats}->{networks}}) { foreach my $interface (keys %{$result->{$container_id}->{Stats}->{networks}}) {
my $name = defined($self->{option_results}->{use_name}) ? $name : $container_id; my $name = defined($self->{option_results}->{use_name}) ? $name : $container_id;
$name .= '.' . $interface; $name .= '.' . $interface;
$self->{containers_traffic}->{$name} = { $self->{containers_traffic}->{$name} = {
display => $name, display => $name,
traffic_in => $result->{$container_id}->{Stats}->{networks}->{$interface}->{rx_bytes} * 8, traffic_in => $result->{$container_id}->{Stats}->{networks}->{$interface}->{rx_bytes} * 8,
traffic_out => $result->{$container_id}->{Stats}->{networks}->{$interface}->{tx_bytes} * 8, traffic_out => $result->{$container_id}->{Stats}->{networks}->{$interface}->{tx_bytes} * 8
}; };
} }
} }
if (scalar(keys %{$self->{containers}}) <= 0) { if (scalar(keys %{$self->{containers}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No container found."); $self->{output}->add_option_msg(short_msg => 'No container found.');
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
my $hostnames = $options{custom}->get_hostnames(); my $hostnames = $options{custom}->get_hostnames();
$self->{cache_name} = "docker_" . $self->{mode} . '_' . join('_', @$hostnames) . '_' . $options{custom}->get_port() . '_' . $self->{cache_name} = 'docker_' . $self->{mode} . '_' . join('_', @$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}->{container_id}) ? md5_hex($self->{option_results}->{container_id}) : md5_hex('all')) . '_' . (defined($self->{option_results}->{filter_name}) ? $self->{option_results}->{filter_name} : '') . '_' .
(defined($self->{option_results}->{container_name}) ? md5_hex($self->{option_results}->{container_name}) : md5_hex('all')); (defined($self->{option_results}->{container_id}) ? $self->{option_results}->{container_id} : '') . '_' .
(defined($self->{option_results}->{container_name}) ? $self->{option_results}->{container_name} : '')
);
} }
1; 1;
@ -302,6 +315,10 @@ Exact container name (if multiple names: names separated by ':').
Use docker name for perfdata and display. Use docker name for perfdata and display.
=item B<--add-health>
Get container health status (call inspector endpoint).
=item B<--filter-name> =item B<--filter-name>
Filter by container name (can be a regexp). Filter by container name (can be a regexp).
@ -311,27 +328,21 @@ Filter by container name (can be a regexp).
Only display some counters (regexp can be used). Only display some counters (regexp can be used).
Example: --filter-counters='^container-status$' 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> =item B<--warning-container-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}, %{health}.
=item B<--critical-container-status> =item B<--critical-container-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}, %{health}.
=item B<--warning-*> B<--critical-*>
Thresholds.
Can be: 'read-iops', 'write-iops', 'traffic-in', 'traffic-out',
'cpu' (%), 'memory' (%).
=back =back