working on docker

This commit is contained in:
qgarnier 2017-07-12 12:57:11 +02:00
parent 12e5eaa741
commit cfae84db4c
3 changed files with 248 additions and 6 deletions

View File

@ -171,6 +171,18 @@ sub api_read_file {
return $content;
}
sub get_hostnames {
my ($self, %options) = @_;
return $self->{hostname};
}
sub get_port {
my ($self, %options) = @_;
return $self->{option_results}->{port};
}
sub cache_containers {
my ($self, %options) = @_;
@ -197,6 +209,25 @@ sub cache_containers {
return $containers;
}
sub internal_api_list_nodes {
my ($self, %options) = @_;
my $response = $self->{http}->{$options{node_name}}->request(
url_path => '/nodes',
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: $@");
}
return $nodes;
}
sub internal_api_list_containers {
my ($self, %options) = @_;
@ -253,6 +284,21 @@ sub api_list_containers {
return $containers;
}
sub api_list_nodes {
my ($self, %options) = @_;
my $nodes = {};
foreach my $node_name (keys %{$self->{http}}) {
$nodes->{$node_name} = [];
my $list_nodes = $self->internal_api_list_nodes(node_name => $node_name);
foreach my $node (@$list_nodes) {
push @{$nodes->{$node_name}}, { Status => $node->{Status}->{State}, ManagerStatus => $node->{ManagerStatus}->{Reachability}, Addr => $node->{Status}->{Addr} };
}
}
return $nodes;
}
sub api_get_containers {
my ($self, %options) = @_;

View File

@ -130,6 +130,7 @@ sub set_counters {
$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_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} = [
@ -180,6 +181,9 @@ sub set_counters {
],
}
},
];
$self->{maps_counters}->{containers_traffic} = [
{ label => 'traffic-in', set => {
key_values => [ { name => 'traffic_in', diff => 1 }, { name => 'display' } ],
per_second => 1, output_change_bytes => 2,
@ -231,6 +235,12 @@ sub check_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) = @_;
@ -251,9 +261,11 @@ sub manage_selection {
my ($self, %options) = @_;
$self->{containers} = {};
$self->{containers_traffic} = {};
my $result = $options{custom}->api_get_containers(container_id => $self->{option_results}->{container_id}, statefile => $self->{statefile_cache_containers});
foreach my $container_id (keys %{$result}) {
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}/) {
@ -263,20 +275,28 @@ sub manage_selection {
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};
$self->{containers}->{$container_id} = {
$self->{containers}->{$container_id} = {
display => defined($self->{option_results}->{use_name}) ? $name : $container_id,
name => $name,
state => $result->{$container_id}->{State},
read_io => $read_io,
write_io => $write_io,
traffic_in => $result->{$container_id}->{Stats}->{network}->{rx_bytes},
traffic_out => $result->{$container_id}->{Stats}->{network}->{tx_bytes},
cpu_total_usage => $result->{$container_id}->{Stats}->{cpu_stats}->{cpu_usage}->{total_usage},
cpu_system_usage => $result->{$container_id}->{Stats}->{cpu_stats}->{system_cpu_usage},
cpu_number => scalar(@{$result->{$container_id}->{Stats}->{cpu_stats}->{cpu_usage}->{percpu_usage}}),
memory_usage => $result->{$container_id}->{Stats}->{memory_stats}->{usage},
memory_total => $result->{$container_id}->{Stats}->{memory_stats}->{limit},
};
foreach my $interface (keys %{$result->{$container_id}->{Stats}->{networks}}) {
my $name = defined($self->{option_results}->{use_name}) ? $name : $container_id;
$name .= '.' . $interface;
$self->{containers_traffic}->{$name} = {
display => $name,
traffic_in => $result->{$container_id}->{Stats}->{networks}->{$interface}->{rx_bytes},
traffic_out => $result->{$container_id}->{Stats}->{networks}->{$interface}->{tx_bytes},
};
}
}
if (scalar(keys %{$self->{containers}}) <= 0) {
@ -284,9 +304,11 @@ sub manage_selection {
$self->{output}->option_exit();
}
$self->{cache_name} = "docker_" . $self->{mode} . '_' . $options{custom}->{hostname} . '_' . $options{custom}->{port} . '_' .
my $hostnames = $options{custom}->get_hostnames();
$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')) . '_' .
(defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all'));
(defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all')) . '_' .
(defined($self->{option_results}->{container_id}) ? md5_hex($self->{option_results}->{container_id}) : md5_hex('all'));
}
1;

View File

@ -0,0 +1,174 @@
#
# Copyright 2017 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::restapi::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 => 'nodes', type => 1, cb_prefix_output => 'prefix_nodes_output', message_multiple => 'All nodes 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'),
}
},
];
}
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->{nodes} = {};
my $result = $options{custom}->api_list_nodes();
foreach my $node_name (keys %{$result}) {
foreach my $entry (@{$result->{$node_name}}) {
my $name = $node_name . '/' . $entry->{Addr};
$self->{nodes}->{$name} = {
display => $name ,
status => $entry->{Status},
manager_status => $entry->{ManagerStatus},
};
}
}
if (scalar(keys %{$self->{nodes}}) <= 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}.
=back
=cut