From 778c82450e7ed7dc5dfcf194da47bcbabfc66595 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Fri, 17 Jul 2015 09:59:07 +0200 Subject: [PATCH 01/27] info and list container mode --- apps/docker/mode/info.pm | 190 +++++++++++++++++++++++++++++ apps/docker/mode/listcontainers.pm | 174 ++++++++++++++++++++++++++ apps/docker/plugin.pm | 70 +++++++++++ 3 files changed, 434 insertions(+) create mode 100644 apps/docker/mode/info.pm create mode 100644 apps/docker/mode/listcontainers.pm create mode 100644 apps/docker/plugin.pm diff --git a/apps/docker/mode/info.pm b/apps/docker/mode/info.pm new file mode 100644 index 000000000..bf0d6a9c0 --- /dev/null +++ b/apps/docker/mode/info.pm @@ -0,0 +1,190 @@ +############################################################################### +# Copyright 2005-2015 CENTREON +# Centreon is developped by : Julien Mathis and Romain Le Merlus under +# GPL Licence 2.0. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation ; either version 2 of the License. +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, see . +# +# Linking this program statically or dynamically with other modules is making a +# combined work based on this program. Thus, the terms and conditions of the GNU +# General Public License cover the whole combination. +# +# As a special exception, the copyright holders of this program give CENTREON +# permission to link this program with independent modules to produce an timeelapsedutable, +# regardless of the license terms of these independent modules, and to copy and +# distribute the resulting timeelapsedutable under terms of CENTREON choice, provided that +# CENTREON also meet, for each linked independent module, the terms and conditions +# of the license of that module. An independent module is a module which is not +# derived from this program. If you modify this program, you may extend this +# exception to your version of the program, but you are not obliged to do so. If you +# do not wish to do so, delete this exception statement from your version. +# +# For more information : contact@centreon.com +# Authors : Mathieu Cinquin +# +#################################################################################### + +package apps::docker::mode::info; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::httplib; +use JSON; + +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 => + { + "hostname:s" => { name => 'hostname' }, + "port:s" => { name => 'port', default => '2376'}, + "proto:s" => { name => 'proto', default => 'https' }, + "urlpath:s" => { name => 'url_path', default => '/info' }, + "credentials" => { name => 'credentials' }, + "username:s" => { name => 'username' }, + "password:s" => { name => 'password' }, + "ssl:s" => { name => 'ssl', }, + "cert-file:s" => { name => 'cert_file' }, + "cert-file:s" => { name => 'cert_file' }, + "key-file:s" => { name => 'key_file' }, + "cacert-file:s" => { name => 'cacert_file' }, + "timeout:s" => { name => 'timeout', default => '3' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + if (!defined($self->{option_results}->{hostname})) { + $self->{output}->add_option_msg(short_msg => "Please set the hostname option"); + $self->{output}->option_exit(); + } + + if ((defined($self->{option_results}->{credentials})) && (!defined($self->{option_results}->{username}) || !defined($self->{option_results}->{password}))) { + $self->{output}->add_option_msg(short_msg => "You need to set --username= and --password= options when --credentials is used"); + $self->{output}->option_exit(); + } +} + +sub run { + my ($self, %options) = @_; + + + my $jsoncontent = centreon::plugins::httplib::connect($self, connection_exit => 'critical'); + + my $json = JSON->new; + + my $webcontent; + + eval { + $webcontent = $json->decode($jsoncontent); + }; + + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode json response"); + $self->{output}->option_exit(); + } + + + $self->{output}->output_add(severity => 'OK', + short_msg => sprintf("Docker is running")); + + $self->{output}->perfdata_add(label => "containers", + value => $webcontent->{Containers}, + min => 0, + ); + + $self->{output}->perfdata_add(label => "events_listener", + value => $webcontent->{NEventsListener}, + min => 0, + ); + + $self->{output}->perfdata_add(label => "file_descriptor", + value => $webcontent->{NFd}, + min => 0, + ); + + $self->{output}->perfdata_add(label => "go_routines", + value => $webcontent->{NGoroutines}, + min => 0, + ); + + $self->{output}->perfdata_add(label => "images", + value => $webcontent->{Images}, + min => 0, + ); + + + $self->{output}->display(); + $self->{output}->exit(); + +} + +1; + +__END__ + +=head1 MODE + +Check Container's state + +=over 8 + +=item B<--hostname> + +IP Addr/FQDN of the GitHub's status website (Default: status.github.com) + +=item B<--port> + +Port used by GitHub's status website (Default: '443') + +=item B<--proto> + +Specify https if needed (Default: 'https') + +=item B<--urlpath> + +Set path to get GitHub's status information (Default: '/api/last-message.json') + +=item B<--credentials> + +Specify this option if you access webpage over basic authentification + +=item B<--username> + +Specify username + +=item B<--password> + +Specify password + +=item B<--timeout> + +Threshold for HTTP timeout (Default: 3) + +=item B<--threshold-overload> + +Set to overload default threshold values (syntax: status,regexp) +It used before default thresholds (order stays). +Example: --threshold-overload='CRITICAL,^(?!(good)$)' + +=back + +=cut diff --git a/apps/docker/mode/listcontainers.pm b/apps/docker/mode/listcontainers.pm new file mode 100644 index 000000000..397e9cfa5 --- /dev/null +++ b/apps/docker/mode/listcontainers.pm @@ -0,0 +1,174 @@ +############################################################################### +# Copyright 2005-2015 CENTREON +# Centreon is developped by : Julien Mathis and Romain Le Merlus under +# GPL Licence 2.0. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation ; either version 2 of the License. +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, see . +# +# Linking this program statically or dynamically with other modules is making a +# combined work based on this program. Thus, the terms and conditions of the GNU +# General Public License cover the whole combination. +# +# As a special exception, the copyright holders of this program give CENTREON +# permission to link this program with independent modules to produce an timeelapsedutable, +# regardless of the license terms of these independent modules, and to copy and +# distribute the resulting timeelapsedutable under terms of CENTREON choice, provided that +# CENTREON also meet, for each linked independent module, the terms and conditions +# of the license of that module. An independent module is a module which is not +# derived from this program. If you modify this program, you may extend this +# exception to your version of the program, but you are not obliged to do so. If you +# do not wish to do so, delete this exception statement from your version. +# +# For more information : contact@centreon.com +# Authors : Mathieu Cinquin +# +#################################################################################### + +package apps::docker::mode::listcontainers; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::httplib; +use JSON; + +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 => + { + "hostname:s" => { name => 'hostname' }, + "port:s" => { name => 'port', default => '2376'}, + "proto:s" => { name => 'proto', default => 'https' }, + "urlpath:s" => { name => 'url_path', default => '/' }, + "credentials" => { name => 'credentials' }, + "username:s" => { name => 'username' }, + "password:s" => { name => 'password' }, + "ssl:s" => { name => 'ssl', }, + "cert-file:s" => { name => 'cert_file' }, + "cert-file:s" => { name => 'cert_file' }, + "key-file:s" => { name => 'key_file' }, + "cacert-file:s" => { name => 'cacert_file' }, + "timeout:s" => { name => 'timeout', default => '3' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + if (!defined($self->{option_results}->{hostname})) { + $self->{output}->add_option_msg(short_msg => "Please set the hostname option"); + $self->{output}->option_exit(); + } + + if ((defined($self->{option_results}->{credentials})) && (!defined($self->{option_results}->{username}) || !defined($self->{option_results}->{password}))) { + $self->{output}->add_option_msg(short_msg => "You need to set --username= and --password= options when --credentials is used"); + $self->{output}->option_exit(); + } +} + +sub run { + my ($self, %options) = @_; + + my $jsoncontent; + + $self->{option_results}->{url_path} = $self->{option_results}->{url_path}."containers/json"; + my $query_form_get = { all => 'true' }; + $jsoncontent = centreon::plugins::httplib::connect($self, query_form_get => $query_form_get, connection_exit => 'critical'); + + my $json = JSON->new; + + my $webcontent; + + eval { + $webcontent = $json->decode($jsoncontent); + }; + + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode json response"); + $self->{output}->option_exit(); + } + + foreach my $val (@$webcontent) { + my $containername = $val->{Names}->[0]; + $containername =~ s/^\///; + my $containerid = $val->{Id}; + my $containerstate = $val->{Status}; + $self->{output}->output_add(long_msg => sprintf("%s [id = %s , state = %s]", + $containername, $containerid, $containerstate)); + } + $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(); + +} + +1; + +__END__ + +=head1 MODE + +Check Container's state + +=over 8 + +=item B<--hostname> + +IP Addr/FQDN of the GitHub's status website (Default: status.github.com) + +=item B<--port> + +Port used by GitHub's status website (Default: '443') + +=item B<--proto> + +Specify https if needed (Default: 'https') + +=item B<--urlpath> + +Set path to get GitHub's status information (Default: '/api/last-message.json') + +=item B<--credentials> + +Specify this option if you access webpage over basic authentification + +=item B<--username> + +Specify username + +=item B<--password> + +Specify password + +=item B<--timeout> + +Threshold for HTTP timeout (Default: 3) + +=item B<--threshold-overload> + +Set to overload default threshold values (syntax: status,regexp) +It used before default thresholds (order stays). +Example: --threshold-overload='CRITICAL,^(?!(good)$)' + +=back + +=cut diff --git a/apps/docker/plugin.pm b/apps/docker/plugin.pm new file mode 100644 index 000000000..65946cf2d --- /dev/null +++ b/apps/docker/plugin.pm @@ -0,0 +1,70 @@ +################################################################################ +# Copyright 2005-2015 CENTREON +# Centreon is developped by : Julien Mathis and Romain Le Merlus under +# GPL Licence 2.0. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation ; either version 2 of the License. +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, see . +# +# Linking this program statically or dynamically with other modules is making a +# combined work based on this program. Thus, the terms and conditions of the GNU +# General Public License cover the whole combination. +# +# As a special exception, the copyright holders of this program give CENTREON +# permission to link this program with independent modules to produce an executable, +# regardless of the license terms of these independent modules, and to copy and +# distribute the resulting executable under terms of CENTREON choice, provided that +# CENTREON also meet, for each linked independent module, the terms and conditions +# of the license of that module. An independent module is a module which is not +# derived from this program. If you modify this program, you may extend this +# exception to your version of the program, but you are not obliged to do so. If you +# do not wish to do so, delete this exception statement from your version. +# +# For more information : contact@centreon.com +# Authors : Mathieu Cinquin +# +#################################################################################### + +package apps::docker::plugin; + +use strict; +use warnings; +use base qw(centreon::plugins::script_simple); + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + # $options->{options} = options object + + $self->{version} = '0.1'; + %{$self->{modes}} = ( + 'blockio' => 'apps::docker::mode::blockio', + 'containerstate' => 'apps::docker::mode::containerstate', + 'cpu' => 'apps::docker::mode::cpu', + 'info' => 'apps::docker::mode::info', + 'list-containers' => 'apps::docker::mode::listcontainers', + 'memory' => 'apps::docker::mode::memory', + 'traffic' => 'apps::docker::mode::traffic', + ); + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check Docker and containers through its API. +Requirements: Docker 1.5+ + +=cut From 4faa681fe3750111c43373ac0862708f114a3e69 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Fri, 17 Jul 2015 11:53:37 +0200 Subject: [PATCH 02/27] fix help --- apps/docker/mode/info.pm | 31 +++++++++++++++++++----------- apps/docker/mode/listcontainers.pm | 31 +++++++++++++++++++----------- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/apps/docker/mode/info.pm b/apps/docker/mode/info.pm index bf0d6a9c0..32bf5403a 100644 --- a/apps/docker/mode/info.pm +++ b/apps/docker/mode/info.pm @@ -59,7 +59,6 @@ sub new { "password:s" => { name => 'password' }, "ssl:s" => { name => 'ssl', }, "cert-file:s" => { name => 'cert_file' }, - "cert-file:s" => { name => 'cert_file' }, "key-file:s" => { name => 'key_file' }, "cacert-file:s" => { name => 'cacert_file' }, "timeout:s" => { name => 'timeout', default => '3' }, @@ -143,17 +142,17 @@ __END__ =head1 MODE -Check Container's state +Check Docker information =over 8 =item B<--hostname> -IP Addr/FQDN of the GitHub's status website (Default: status.github.com) +IP Addr/FQDN of Docker's API =item B<--port> -Port used by GitHub's status website (Default: '443') +Port used by Docker's API (Default: '2576') =item B<--proto> @@ -161,7 +160,7 @@ Specify https if needed (Default: 'https') =item B<--urlpath> -Set path to get GitHub's status information (Default: '/api/last-message.json') +Set path to get Docker information (Default: '/') =item B<--credentials> @@ -175,16 +174,26 @@ Specify username Specify password +=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<--timeout> Threshold for HTTP timeout (Default: 3) -=item B<--threshold-overload> - -Set to overload default threshold values (syntax: status,regexp) -It used before default thresholds (order stays). -Example: --threshold-overload='CRITICAL,^(?!(good)$)' - =back =cut diff --git a/apps/docker/mode/listcontainers.pm b/apps/docker/mode/listcontainers.pm index 397e9cfa5..b0ea93e7f 100644 --- a/apps/docker/mode/listcontainers.pm +++ b/apps/docker/mode/listcontainers.pm @@ -59,7 +59,6 @@ sub new { "password:s" => { name => 'password' }, "ssl:s" => { name => 'ssl', }, "cert-file:s" => { name => 'cert_file' }, - "cert-file:s" => { name => 'cert_file' }, "key-file:s" => { name => 'key_file' }, "cacert-file:s" => { name => 'cacert_file' }, "timeout:s" => { name => 'timeout', default => '3' }, @@ -127,17 +126,17 @@ __END__ =head1 MODE -Check Container's state +List Docker containers =over 8 =item B<--hostname> -IP Addr/FQDN of the GitHub's status website (Default: status.github.com) +IP Addr/FQDN of Docker's API =item B<--port> -Port used by GitHub's status website (Default: '443') +Port used by Docker's API (Default: '2576') =item B<--proto> @@ -145,7 +144,7 @@ Specify https if needed (Default: 'https') =item B<--urlpath> -Set path to get GitHub's status information (Default: '/api/last-message.json') +Set path to get Docker containers (Default: '/') =item B<--credentials> @@ -159,16 +158,26 @@ Specify username Specify password +=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<--timeout> Threshold for HTTP timeout (Default: 3) -=item B<--threshold-overload> - -Set to overload default threshold values (syntax: status,regexp) -It used before default thresholds (order stays). -Example: --threshold-overload='CRITICAL,^(?!(good)$)' - =back =cut From 6373a60660d3e56585f5f5da56c8b792a9ff092d Mon Sep 17 00:00:00 2001 From: Shini31 Date: Fri, 17 Jul 2015 11:54:00 +0200 Subject: [PATCH 03/27] add container state mode --- apps/docker/mode/containerstate.pm | 304 +++++++++++++++++++++++++++++ 1 file changed, 304 insertions(+) create mode 100644 apps/docker/mode/containerstate.pm diff --git a/apps/docker/mode/containerstate.pm b/apps/docker/mode/containerstate.pm new file mode 100644 index 000000000..2848cc6d5 --- /dev/null +++ b/apps/docker/mode/containerstate.pm @@ -0,0 +1,304 @@ +############################################################################### +# Copyright 2005-2015 CENTREON +# Centreon is developped by : Julien Mathis and Romain Le Merlus under +# GPL Licence 2.0. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation ; either version 2 of the License. +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, see . +# +# Linking this program statically or dynamically with other modules is making a +# combined work based on this program. Thus, the terms and conditions of the GNU +# General Public License cover the whole combination. +# +# As a special exception, the copyright holders of this program give CENTREON +# permission to link this program with independent modules to produce an timeelapsedutable, +# regardless of the license terms of these independent modules, and to copy and +# distribute the resulting timeelapsedutable under terms of CENTREON choice, provided that +# CENTREON also meet, for each linked independent module, the terms and conditions +# of the license of that module. An independent module is a module which is not +# derived from this program. If you modify this program, you may extend this +# exception to your version of the program, but you are not obliged to do so. If you +# do not wish to do so, delete this exception statement from your version. +# +# For more information : contact@centreon.com +# Authors : Mathieu Cinquin +# +#################################################################################### + +package apps::docker::mode::containerstate; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::httplib; +use JSON; +use Data::Dumper; + +my $thresholds = { + state => [ + ['Running', 'OK'], + ['Paused', 'WARNING'], + ['Restarting', 'WARNING'], + ['OOMKilled', 'CRITICAL'], + ['Dead', 'CRITICAL'], + ['Exited', 'CRITICAL'], + ], +}; + +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 => + { + "hostname:s" => { name => 'hostname' }, + "port:s" => { name => 'port', default => '2376'}, + "proto:s" => { name => 'proto', default => 'https' }, + "urlpath:s" => { name => 'url_path', default => '/' }, + "id:s" => { name => 'id' }, + "credentials" => { name => 'credentials' }, + "username:s" => { name => 'username' }, + "password:s" => { name => 'password' }, + "ssl:s" => { name => 'ssl', }, + "cert-file:s" => { name => 'cert_file' }, + "key-file:s" => { name => 'key_file' }, + "cacert-file:s" => { name => 'cacert_file' }, + "timeout:s" => { name => 'timeout', default => '3' }, + "threshold-overload:s@" => { name => 'threshold_overload' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + if (!defined($self->{option_results}->{hostname})) { + $self->{output}->add_option_msg(short_msg => "Please set the hostname option"); + $self->{output}->option_exit(); + } + + if ((defined($self->{option_results}->{credentials})) && (!defined($self->{option_results}->{username}) || !defined($self->{option_results}->{password}))) { + $self->{output}->add_option_msg(short_msg => "You need to set --username= and --password= options when --credentials is used"); + $self->{output}->option_exit(); + } + + if ((defined($self->{option_results}->{id})) && ($self->{option_results}->{id} eq '')) { + $self->{output}->add_option_msg(short_msg => "You need to specify the id option"); + $self->{output}->option_exit(); + } + + $self->{overload_th} = {}; + foreach my $val (@{$self->{option_results}->{threshold_overload}}) { + if ($val !~ /^(.*?),(.*?),(.*)$/) { + $self->{output}->add_option_msg(short_msg => "Wrong treshold-overload option '" . $val . "'."); + $self->{output}->option_exit(); + } + my ($section, $status, $filter) = ($1, $2, $3); + if ($self->{output}->is_litteral_status(status => $status) == 0) { + $self->{output}->add_option_msg(short_msg => "Wrong treshold-overload status '" . $val . "'."); + $self->{output}->option_exit(); + } + $self->{overload_th}->{$section} = [] if (!defined($self->{overload_th}->{$section})); + push @{$self->{overload_th}->{$section}}, {filter => $filter, status => $status}; + } +} + +sub get_severity { + my ($self, %options) = @_; + my $status = 'UNKNOWN'; # default + + if (defined($self->{overload_th}->{$options{section}})) { + foreach (@{$self->{overload_th}->{$options{section}}}) { + if ($options{value} =~ /$_->{filter}/i) { + $status = $_->{status}; + return $status; + } + } + } + foreach (@{$thresholds->{$options{section}}}) { + if ($options{value} =~ /$$_[0]/i) { + $status = $$_[1]; + return $status; + } + } + return $status; +} + +sub run { + my ($self, %options) = @_; + + my $jsoncontent; + + if (defined($self->{option_results}->{id})) { + $self->{option_results}->{url_path} = $self->{option_results}->{url_path}."containers/".$self->{option_results}->{id}."/json"; + $jsoncontent = centreon::plugins::httplib::connect($self, connection_exit => 'critical'); + } else { + $self->{option_results}->{url_path} = $self->{option_results}->{url_path}."containers/json"; + my $query_form_get = { all => 'true' }; + $jsoncontent = centreon::plugins::httplib::connect($self, query_form_get => $query_form_get, connection_exit => 'critical'); + } + + my $json = JSON->new; + + my $webcontent; + + eval { + $webcontent = $json->decode($jsoncontent); + }; + + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode json response"); + $self->{output}->option_exit(); + } + + my ($result, $containername); + my $exit = 'OK'; + + if (defined($self->{option_results}->{id})) { + while ( my ($keys,$values) = each(%{$webcontent->{State}})) { + if ($values eq 'true') { + $result = $keys; + $containername = $webcontent->{Name}; + $containername =~ s/^\///; + last; + } + } + $exit = $self->get_severity(section => 'state', value => $result); + $self->{output}->output_add(severity => $exit, + short_msg => sprintf("Container %s (%s) is %s", $containername, $self->{option_results}->{id}, $result)); + } else { + $self->{output}->output_add(severity => 'OK', + short_msg => sprintf("All containers are in Running state")); + + my ($nbrunning,$nbpaused,$nbexited) = '0'; + + foreach my $val (@$webcontent) { + $containername = $val->{Names}->[0]; + $containername =~ s/^\///; + + if (($val->{Status} =~ m/^Up/) && ($val->{Status} =~ m/^(?:(?!Paused).)*$/)) { + $nbrunning++; + $result = 'Running'; + } elsif ($val->{Status} =~ m/^Exited/) { + $result = 'Exited'; + $nbexited++; + } elsif ($val->{Status} =~ m/\(Paused\)$/) { + $result = 'Paused'; + $nbpaused++; + } + + my $tmp_exit = $self->get_severity(section => 'state', value => $result); + $exit = $self->{output}->get_most_critical(status => [ $tmp_exit, $exit ]); + if (!$self->{output}->is_status(value => $tmp_exit, compare => 'OK', litteral => 1)) { + $self->{output}->output_add(long_msg => sprintf("Containers %s is in %s state", + $containername, $result)); + } + } + + if ($exit ne 'OK') { + $self->{output}->output_add(severity => $exit, + short_msg => sprintf("Some containers are in wrong state")); + } + $self->{output}->perfdata_add(label => "running", + value => $nbrunning, + min => 0, + ); + $self->{output}->perfdata_add(label => "paused", + value => $nbpaused, + min => 0, + ); + $self->{output}->perfdata_add(label => "exited", + value => $nbexited, + min => 0, + ); + } + + $self->{output}->display(); + $self->{output}->exit(); + +} + +1; + +__END__ + +=head1 MODE + +Check Container's state + +=over 8 + +=item B<--hostname> + +IP Addr/FQDN of Docker's API + +=item B<--port> + +Port used by Docker's API (Default: '2576') + +=item B<--proto> + +Specify https if needed (Default: 'https') + +=item B<--urlpath> + +Set path to get Docker's container information (Default: '/') + +=item B<--id> + +Specify one container's id + +=item B<--credentials> + +Specify this option if you access webpage over basic authentification + +=item B<--username> + +Specify username + +=item B<--password> + +Specify password + +=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<--timeout> + +Threshold for HTTP timeout (Default: 3) + +=item B<--threshold-overload> + +Set to overload default threshold values (syntax: status,regexp) +It used before default thresholds (order stays). +Example: --threshold-overload='CRITICAL,^(?!(good)$)' + +=back + +=cut From 44e4a34bb2550a45a55f1a9607b3ced274e61b98 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Mon, 20 Jul 2015 11:42:42 +0200 Subject: [PATCH 04/27] fix help for threshold-overload --- apps/docker/mode/containerstate.pm | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/apps/docker/mode/containerstate.pm b/apps/docker/mode/containerstate.pm index 2848cc6d5..be4b13e23 100644 --- a/apps/docker/mode/containerstate.pm +++ b/apps/docker/mode/containerstate.pm @@ -41,7 +41,6 @@ use strict; use warnings; use centreon::plugins::httplib; use JSON; -use Data::Dumper; my $thresholds = { state => [ @@ -76,6 +75,7 @@ sub new { "cacert-file:s" => { name => 'cacert_file' }, "timeout:s" => { name => 'timeout', default => '3' }, "threshold-overload:s@" => { name => 'threshold_overload' }, + "exclude:s" => { name => 'exclude' }, }); return $self; @@ -137,6 +137,22 @@ sub get_severity { return $status; } +sub check_exclude { + my ($self, %options) = @_; + + if (defined($self->{option_results}->{exclude}) && $self->{option_results}->{exclude} eq 'Running') { + $self->{output}->output_add(long_msg => sprintf("Skipping Running containers.")); + return 1; + } elsif (defined($self->{option_results}->{exclude}) && $self->{option_results}->{exclude} eq 'Paused') { + $self->{output}->output_add(long_msg => sprintf("Skipping Paused containers.")); + return 1; + } elsif (defined($self->{option_results}->{exclude}) && $self->{option_results}->{exclude} eq 'Exited') { + $self->{output}->output_add(long_msg => sprintf("Skipping Exited containers.")); + return 1; + } + return 0; +} + sub run { my ($self, %options) = @_; @@ -295,9 +311,9 @@ Threshold for HTTP timeout (Default: 3) =item B<--threshold-overload> -Set to overload default threshold values (syntax: status,regexp) +Set to overload default threshold values (syntax: section,status,regexp) It used before default thresholds (order stays). -Example: --threshold-overload='CRITICAL,^(?!(good)$)' +Example: --threshold-overload='state,CRITICAL,^(?!(Paused)$)' =back From 2d7115486d6c815696724eacd3dca2433a3e50c3 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Mon, 20 Jul 2015 14:02:24 +0200 Subject: [PATCH 05/27] delete exclude option --- apps/docker/mode/containerstate.pm | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/apps/docker/mode/containerstate.pm b/apps/docker/mode/containerstate.pm index be4b13e23..4d9e070a7 100644 --- a/apps/docker/mode/containerstate.pm +++ b/apps/docker/mode/containerstate.pm @@ -75,7 +75,6 @@ sub new { "cacert-file:s" => { name => 'cacert_file' }, "timeout:s" => { name => 'timeout', default => '3' }, "threshold-overload:s@" => { name => 'threshold_overload' }, - "exclude:s" => { name => 'exclude' }, }); return $self; @@ -137,22 +136,6 @@ sub get_severity { return $status; } -sub check_exclude { - my ($self, %options) = @_; - - if (defined($self->{option_results}->{exclude}) && $self->{option_results}->{exclude} eq 'Running') { - $self->{output}->output_add(long_msg => sprintf("Skipping Running containers.")); - return 1; - } elsif (defined($self->{option_results}->{exclude}) && $self->{option_results}->{exclude} eq 'Paused') { - $self->{output}->output_add(long_msg => sprintf("Skipping Paused containers.")); - return 1; - } elsif (defined($self->{option_results}->{exclude}) && $self->{option_results}->{exclude} eq 'Exited') { - $self->{output}->output_add(long_msg => sprintf("Skipping Exited containers.")); - return 1; - } - return 0; -} - sub run { my ($self, %options) = @_; @@ -206,8 +189,8 @@ sub run { $containername =~ s/^\///; if (($val->{Status} =~ m/^Up/) && ($val->{Status} =~ m/^(?:(?!Paused).)*$/)) { - $nbrunning++; $result = 'Running'; + $nbrunning++; } elsif ($val->{Status} =~ m/^Exited/) { $result = 'Exited'; $nbexited++; From 7f85bcc349e5a37402bb4271d772d45c322bea0e Mon Sep 17 00:00:00 2001 From: Shini31 Date: Mon, 20 Jul 2015 19:08:22 +0200 Subject: [PATCH 06/27] add name option --- apps/docker/mode/containerstate.pm | 105 +++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 4 deletions(-) diff --git a/apps/docker/mode/containerstate.pm b/apps/docker/mode/containerstate.pm index 4d9e070a7..0a2d853b3 100644 --- a/apps/docker/mode/containerstate.pm +++ b/apps/docker/mode/containerstate.pm @@ -41,6 +41,8 @@ use strict; use warnings; use centreon::plugins::httplib; use JSON; +use centreon::plugins::statefile; +use Digest::MD5 qw(md5_hex); my $thresholds = { state => [ @@ -65,6 +67,7 @@ sub new { "port:s" => { name => 'port', default => '2376'}, "proto:s" => { name => 'proto', default => 'https' }, "urlpath:s" => { name => 'url_path', default => '/' }, + "name:s" => { name => 'name' }, "id:s" => { name => 'id' }, "credentials" => { name => 'credentials' }, "username:s" => { name => 'username' }, @@ -75,8 +78,12 @@ sub new { "cacert-file:s" => { name => 'cacert_file' }, "timeout:s" => { name => 'timeout', default => '3' }, "threshold-overload:s@" => { name => 'threshold_overload' }, + "show-cache" => { name => 'show_cache' }, }); + $self->{container_id_selected} = []; + $self->{statefile_cache} = centreon::plugins::statefile->new(%options); + return $self; } @@ -94,6 +101,11 @@ sub check_options { $self->{output}->option_exit(); } + if ((defined($self->{option_results}->{name})) && ($self->{option_results}->{name} eq '')) { + $self->{output}->add_option_msg(short_msg => "You need to specify the name option"); + $self->{output}->option_exit(); + } + if ((defined($self->{option_results}->{id})) && ($self->{option_results}->{id} eq '')) { $self->{output}->add_option_msg(short_msg => "You need to specify the id option"); $self->{output}->option_exit(); @@ -113,6 +125,8 @@ sub check_options { $self->{overload_th}->{$section} = [] if (!defined($self->{overload_th}->{$section})); push @{$self->{overload_th}->{$section}}, {filter => $filter, status => $status}; } + + $self->{statefile_cache}->check_options(%options); } sub get_severity { @@ -136,16 +150,94 @@ sub get_severity { return $status; } +sub manage_selection { + my ($self, %options) = @_; + + # init cache file + my $has_cache_file = $self->{statefile_cache}->read(statefile => 'cache_json_' . $self->{option_results}->{hostname} . '_' . $self->{option_results}->{port} . '_' . $self->{mode}); + if (defined($self->{option_results}->{show_cache})) { + $self->{output}->add_option_msg(long_msg => $self->{statefile_cache}->get_string_content()); + $self->{output}->option_exit(); + } + + my $timestamp_cache = $self->{statefile_cache}->get(name => 'last_timestamp'); + $self->reload_cache(); + $self->{statefile_cache}->read(); + + my $all_containers = $self->{statefile_cache}->get(name => 'all_containers'); + foreach my $val (@{$all_containers}) { + while( my ($containername,$containerid) = each(%{$val}) ) { + if ($containername eq $self->{option_results}->{name}) { + $self->{container_id_selected} = $containerid; + last; + } + } + } + + if (!defined($self->{container_id_selected})) { + $self->{output}->add_option_msg(short_msg => "No container found for name '" . $self->{option_results}->{name} . "' (maybe you should reload cache file)."); + $self->{output}->option_exit(); + } + + return $self->{container_id_selected}; +} + +sub reload_cache { + my ($self) = @_; + my $datas = {}; + + $datas->{last_timestamp} = time(); + + my $jsoncontent; + + $self->{option_results}->{url_path} = $self->{option_results}->{url_path}."containers/json"; + my $query_form_get = { all => 'true' }; + $jsoncontent = centreon::plugins::httplib::connect($self, query_form_get => $query_form_get, connection_exit => 'critical'); + + my $json = JSON->new; + + my $webcontent; + + eval { + $webcontent = $json->decode($jsoncontent); + }; + + if ($@) { + $self->{output}->add_option_msg(short_msg => "Can't construct cache..."); + $self->{output}->option_exit(); + } + + foreach my $val (@$webcontent) { + my $containername = $val->{Names}->[0]; + $containername =~ s/^\///; + my %container_link = ($containername => $val->{Id}); + my $container_link_ref = \%container_link; + push @{$datas->{all_containers}}, $container_link_ref; + } + + if (scalar(@{$datas->{all_containers}}) <= 0) { + $self->{output}->add_option_msg(short_msg => "Can't construct cache..."); + $self->{output}->option_exit(); + } + + $self->{statefile_cache}->write(data => $datas); +} + + sub run { my ($self, %options) = @_; my $jsoncontent; if (defined($self->{option_results}->{id})) { - $self->{option_results}->{url_path} = $self->{option_results}->{url_path}."containers/".$self->{option_results}->{id}."/json"; + $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{id}."/json"; + $jsoncontent = centreon::plugins::httplib::connect($self, connection_exit => 'critical'); + } elsif (defined($self->{option_results}->{name})) { + $self->manage_selection(); + $self->{option_results}->{url_path} = "/containers/".$self->{container_id_selected}."/json"; $jsoncontent = centreon::plugins::httplib::connect($self, connection_exit => 'critical'); } else { - $self->{option_results}->{url_path} = $self->{option_results}->{url_path}."containers/json"; + $self->{option_results}->{url_path} = "/containers/json"; my $query_form_get = { all => 'true' }; $jsoncontent = centreon::plugins::httplib::connect($self, query_form_get => $query_form_get, connection_exit => 'critical'); } @@ -166,7 +258,7 @@ sub run { my ($result, $containername); my $exit = 'OK'; - if (defined($self->{option_results}->{id})) { + if (defined($self->{option_results}->{id}) || defined($self->{option_results}->{name})) { while ( my ($keys,$values) = each(%{$webcontent->{State}})) { if ($values eq 'true') { $result = $keys; @@ -175,9 +267,10 @@ sub run { last; } } + $exit = $self->get_severity(section => 'state', value => $result); $self->{output}->output_add(severity => $exit, - short_msg => sprintf("Container %s (%s) is %s", $containername, $self->{option_results}->{id}, $result)); + short_msg => sprintf("Container %s is %s", $containername, $result)); } else { $self->{output}->output_add(severity => 'OK', short_msg => sprintf("All containers are in Running state")); @@ -260,6 +353,10 @@ Set path to get Docker's container information (Default: '/') Specify one container's id +=item B<--name> + +Specify one container's name + =item B<--credentials> Specify this option if you access webpage over basic authentification From 37e919db71b59053abb430df87f8a0e3ac4098bb Mon Sep 17 00:00:00 2001 From: Shini31 Date: Tue, 21 Jul 2015 14:08:40 +0200 Subject: [PATCH 07/27] delete statefile cache --- apps/docker/mode/containerstate.pm | 85 +----------------------------- 1 file changed, 1 insertion(+), 84 deletions(-) diff --git a/apps/docker/mode/containerstate.pm b/apps/docker/mode/containerstate.pm index 0a2d853b3..293ed2bab 100644 --- a/apps/docker/mode/containerstate.pm +++ b/apps/docker/mode/containerstate.pm @@ -41,8 +41,6 @@ use strict; use warnings; use centreon::plugins::httplib; use JSON; -use centreon::plugins::statefile; -use Digest::MD5 qw(md5_hex); my $thresholds = { state => [ @@ -78,12 +76,8 @@ sub new { "cacert-file:s" => { name => 'cacert_file' }, "timeout:s" => { name => 'timeout', default => '3' }, "threshold-overload:s@" => { name => 'threshold_overload' }, - "show-cache" => { name => 'show_cache' }, }); - $self->{container_id_selected} = []; - $self->{statefile_cache} = centreon::plugins::statefile->new(%options); - return $self; } @@ -125,8 +119,6 @@ sub check_options { $self->{overload_th}->{$section} = [] if (!defined($self->{overload_th}->{$section})); push @{$self->{overload_th}->{$section}}, {filter => $filter, status => $status}; } - - $self->{statefile_cache}->check_options(%options); } sub get_severity { @@ -150,80 +142,6 @@ sub get_severity { return $status; } -sub manage_selection { - my ($self, %options) = @_; - - # init cache file - my $has_cache_file = $self->{statefile_cache}->read(statefile => 'cache_json_' . $self->{option_results}->{hostname} . '_' . $self->{option_results}->{port} . '_' . $self->{mode}); - if (defined($self->{option_results}->{show_cache})) { - $self->{output}->add_option_msg(long_msg => $self->{statefile_cache}->get_string_content()); - $self->{output}->option_exit(); - } - - my $timestamp_cache = $self->{statefile_cache}->get(name => 'last_timestamp'); - $self->reload_cache(); - $self->{statefile_cache}->read(); - - my $all_containers = $self->{statefile_cache}->get(name => 'all_containers'); - foreach my $val (@{$all_containers}) { - while( my ($containername,$containerid) = each(%{$val}) ) { - if ($containername eq $self->{option_results}->{name}) { - $self->{container_id_selected} = $containerid; - last; - } - } - } - - if (!defined($self->{container_id_selected})) { - $self->{output}->add_option_msg(short_msg => "No container found for name '" . $self->{option_results}->{name} . "' (maybe you should reload cache file)."); - $self->{output}->option_exit(); - } - - return $self->{container_id_selected}; -} - -sub reload_cache { - my ($self) = @_; - my $datas = {}; - - $datas->{last_timestamp} = time(); - - my $jsoncontent; - - $self->{option_results}->{url_path} = $self->{option_results}->{url_path}."containers/json"; - my $query_form_get = { all => 'true' }; - $jsoncontent = centreon::plugins::httplib::connect($self, query_form_get => $query_form_get, connection_exit => 'critical'); - - my $json = JSON->new; - - my $webcontent; - - eval { - $webcontent = $json->decode($jsoncontent); - }; - - if ($@) { - $self->{output}->add_option_msg(short_msg => "Can't construct cache..."); - $self->{output}->option_exit(); - } - - foreach my $val (@$webcontent) { - my $containername = $val->{Names}->[0]; - $containername =~ s/^\///; - my %container_link = ($containername => $val->{Id}); - my $container_link_ref = \%container_link; - push @{$datas->{all_containers}}, $container_link_ref; - } - - if (scalar(@{$datas->{all_containers}}) <= 0) { - $self->{output}->add_option_msg(short_msg => "Can't construct cache..."); - $self->{output}->option_exit(); - } - - $self->{statefile_cache}->write(data => $datas); -} - - sub run { my ($self, %options) = @_; @@ -233,8 +151,7 @@ sub run { $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{id}."/json"; $jsoncontent = centreon::plugins::httplib::connect($self, connection_exit => 'critical'); } elsif (defined($self->{option_results}->{name})) { - $self->manage_selection(); - $self->{option_results}->{url_path} = "/containers/".$self->{container_id_selected}."/json"; + $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{name}."/json"; $jsoncontent = centreon::plugins::httplib::connect($self, connection_exit => 'critical'); } else { $self->{option_results}->{url_path} = "/containers/json"; From b5c321c56ee9c3a1745249a84b476c6441632383 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Tue, 21 Jul 2015 18:28:36 +0200 Subject: [PATCH 08/27] add image mode --- apps/docker/mode/image.pm | 268 ++++++++++++++++++++++++++++++++++++++ apps/docker/plugin.pm | 3 +- 2 files changed, 270 insertions(+), 1 deletion(-) create mode 100644 apps/docker/mode/image.pm diff --git a/apps/docker/mode/image.pm b/apps/docker/mode/image.pm new file mode 100644 index 000000000..e5ad60c35 --- /dev/null +++ b/apps/docker/mode/image.pm @@ -0,0 +1,268 @@ +############################################################################### +# Copyright 2005-2015 CENTREON +# Centreon is developped by : Julien Mathis and Romain Le Merlus under +# GPL Licence 2.0. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation ; either version 2 of the License. +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, see . +# +# Linking this program statically or dynamically with other modules is making a +# combined work based on this program. Thus, the terms and conditions of the GNU +# General Public License cover the whole combination. +# +# As a special exception, the copyright holders of this program give CENTREON +# permission to link this program with independent modules to produce an timeelapsedutable, +# regardless of the license terms of these independent modules, and to copy and +# distribute the resulting timeelapsedutable under terms of CENTREON choice, provided that +# CENTREON also meet, for each linked independent module, the terms and conditions +# of the license of that module. An independent module is a module which is not +# derived from this program. If you modify this program, you may extend this +# exception to your version of the program, but you are not obliged to do so. If you +# do not wish to do so, delete this exception statement from your version. +# +# For more information : contact@centreon.com +# Authors : Mathieu Cinquin +# +#################################################################################### + +package apps::docker::mode::image; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::httplib; +use JSON; + +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 => + { + "hostname:s" => { name => 'hostname' }, + "port:s" => { name => 'port', default => '2376'}, + "proto:s" => { name => 'proto', default => 'https' }, + "urlpath:s" => { name => 'url_path', default => '/' }, + "name:s" => { name => 'name' }, + "id:s" => { name => 'id' }, + "image:s" => { name => 'image' }, + "registry-hostname:s" => { name => 'registry_hostname' }, + "registry-proto:s" => { name => 'registry_proto', default => 'https' }, + "registry-port:s" => { name => 'registry_port' }, + "credentials" => { name => 'credentials' }, + "username:s" => { name => 'username' }, + "password:s" => { name => 'password' }, + "ssl:s" => { name => 'ssl', }, + "cert-file:s" => { name => 'cert_file' }, + "key-file:s" => { name => 'key_file' }, + "cacert-file:s" => { name => 'cacert_file' }, + "timeout:s" => { name => 'timeout', default => '3' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + if (!defined($self->{option_results}->{hostname})) { + $self->{output}->add_option_msg(short_msg => "Please set the hostname option"); + $self->{output}->option_exit(); + } + + if ((defined($self->{option_results}->{credentials})) && (!defined($self->{option_results}->{username}) || !defined($self->{option_results}->{password}))) { + $self->{output}->add_option_msg(short_msg => "You need to set --username= and --password= options when --credentials is used"); + $self->{output}->option_exit(); + } + + if ((defined($self->{option_results}->{name})) && (defined($self->{option_results}->{id}))) { + $self->{output}->add_option_msg(short_msg => "Please set the name or id option"); + $self->{output}->option_exit(); + } + + if ((!defined($self->{option_results}->{name})) && (!defined($self->{option_results}->{id}))) { + $self->{output}->add_option_msg(short_msg => "Please set the name or id option"); + $self->{output}->option_exit(); + } + + if (!defined($self->{option_results}->{image})) { + $self->{output}->add_option_msg(short_msg => "Please set the image option"); + $self->{output}->option_exit(); + } + + if (!defined($self->{option_results}->{registry_hostname})) { + $self->{output}->add_option_msg(short_msg => "Please set the registry-hostname option"); + $self->{output}->option_exit(); + } + + if (!defined($self->{option_results}->{registry_proto})) { + $self->{output}->add_option_msg(short_msg => "Please set the registry-proto option"); + $self->{output}->option_exit(); + } +} + +sub run { + my ($self, %options) = @_; + + my ($jsoncontent,$jsoncontent2); + + if (defined($self->{option_results}->{id})) { + $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{id}."/json"; + $jsoncontent = centreon::plugins::httplib::connect($self, connection_exit => 'critical'); + } elsif (defined($self->{option_results}->{name})) { + $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{name}."/json"; + $jsoncontent = centreon::plugins::httplib::connect($self, connection_exit => 'critical'); + } + + my $json = JSON->new; + + my ($webcontent,$webcontent2); + + eval { + $webcontent = $json->decode($jsoncontent); + }; + + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode json response"); + $self->{output}->option_exit(); + } + + my $container_id = $webcontent->{Image}; + + $self->{option_results}->{url_path} = "/v1/repositories/".$self->{option_results}->{image}."/tags"; + $self->{option_results}->{port} = $self->{option_results}->{registry_port}; + $self->{option_results}->{proto} = $self->{option_results}->{registry_proto}; + $self->{option_results}->{hostname} = $self->{option_results}->{registry_hostname}; + + $jsoncontent2 = centreon::plugins::httplib::connect($self, connection_exit => 'critical'); + + my $json2 = JSON->new; + + eval { + $webcontent2 = $json2->decode($jsoncontent2); + }; + + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode json response"); + $self->{output}->option_exit(); + } + + my $result; + + foreach (@{$webcontent2}) { + if (($container_id =~ /^$_->{layer}\w+$/)) { + $result="1"; + last; + } + } + + if ($result eq "1") { + $self->{output}->output_add(severity => "OK", + short_msg => sprintf("Container's image and Registry image are identical")); + } else { + $self->{output}->output_add(severity => "CRITICAL", + short_msg => sprintf("Container's image and Registry image are different")); + } + + $self->{output}->display(); + $self->{output}->exit(); + +} + +1; + +__END__ + +=head1 MODE + +Check Container's image viability + +=over 8 + +=item B<--hostname> + +IP Addr/FQDN of Docker's API + +=item B<--port> + +Port used by Docker's API (Default: '2576') + +=item B<--proto> + +Specify https if needed (Default: 'https') + +=item B<--urlpath> + +Set path to get Docker's container information (Default: '/') + +=item B<--id> + +Specify the container's id + +=item B<--name> + +Specify the container's name + +=item B<--image> + +Specify the image's name + +=item B<--registry-hostname> + +IP Addr/FQDN of Docker's Registry + +=item B<--registry-port> + +Port used by Docker's Registry + +=item B<--registry-proto> + +Specify https if needed (Default: 'https') + +=item B<--credentials> + +Specify this option if you access webpage over basic authentification + +=item B<--username> + +Specify username + +=item B<--password> + +Specify password + +=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<--timeout> + +Threshold for HTTP timeout (Default: 3) + +=back + +=cut diff --git a/apps/docker/plugin.pm b/apps/docker/plugin.pm index 65946cf2d..23c9572d0 100644 --- a/apps/docker/plugin.pm +++ b/apps/docker/plugin.pm @@ -50,6 +50,7 @@ sub new { 'blockio' => 'apps::docker::mode::blockio', 'containerstate' => 'apps::docker::mode::containerstate', 'cpu' => 'apps::docker::mode::cpu', + 'image' => 'apps::docker::mode::image', 'info' => 'apps::docker::mode::info', 'list-containers' => 'apps::docker::mode::listcontainers', 'memory' => 'apps::docker::mode::memory', @@ -65,6 +66,6 @@ __END__ =head1 PLUGIN DESCRIPTION Check Docker and containers through its API. -Requirements: Docker 1.5+ +Requirements: Docker 1.7.1+ and Docker API 1.19+ =cut From 114e6fdd50ed6afb506b4e29f455f0803e602f9d Mon Sep 17 00:00:00 2001 From: Shini31 Date: Tue, 21 Jul 2015 18:28:51 +0200 Subject: [PATCH 09/27] add memory mode --- apps/docker/mode/memory.pm | 243 +++++++++++++++++++++++++++++++++++++ 1 file changed, 243 insertions(+) create mode 100644 apps/docker/mode/memory.pm diff --git a/apps/docker/mode/memory.pm b/apps/docker/mode/memory.pm new file mode 100644 index 000000000..33d3acd5f --- /dev/null +++ b/apps/docker/mode/memory.pm @@ -0,0 +1,243 @@ +############################################################################### +# Copyright 2005-2015 CENTREON +# Centreon is developped by : Julien Mathis and Romain Le Merlus under +# GPL Licence 2.0. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation ; either version 2 of the License. +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, see . +# +# Linking this program statically or dynamically with other modules is making a +# combined work based on this program. Thus, the terms and conditions of the GNU +# General Public License cover the whole combination. +# +# As a special exception, the copyright holders of this program give CENTREON +# permission to link this program with independent modules to produce an timeelapsedutable, +# regardless of the license terms of these independent modules, and to copy and +# distribute the resulting timeelapsedutable under terms of CENTREON choice, provided that +# CENTREON also meet, for each linked independent module, the terms and conditions +# of the license of that module. An independent module is a module which is not +# derived from this program. If you modify this program, you may extend this +# exception to your version of the program, but you are not obliged to do so. If you +# do not wish to do so, delete this exception statement from your version. +# +# For more information : contact@centreon.com +# Authors : Mathieu Cinquin +# +#################################################################################### + +package apps::docker::mode::memory; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::httplib; +use JSON; + +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 => + { + "hostname:s" => { name => 'hostname' }, + "port:s" => { name => 'port', default => '2376'}, + "proto:s" => { name => 'proto', default => 'https' }, + "urlpath:s" => { name => 'url_path', default => '/' }, + "name:s" => { name => 'name' }, + "id:s" => { name => 'id' }, + "warning:s" => { name => 'warning' }, + "critical:s" => { name => 'critical' }, + "credentials" => { name => 'credentials' }, + "username:s" => { name => 'username' }, + "password:s" => { name => 'password' }, + "ssl:s" => { name => 'ssl', }, + "cert-file:s" => { name => 'cert_file' }, + "key-file:s" => { name => 'key_file' }, + "cacert-file:s" => { name => 'cacert_file' }, + "timeout:s" => { name => 'timeout', default => '3' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + if (!defined($self->{option_results}->{hostname})) { + $self->{output}->add_option_msg(short_msg => "Please set the hostname option"); + $self->{output}->option_exit(); + } + + if ((defined($self->{option_results}->{credentials})) && (!defined($self->{option_results}->{username}) || !defined($self->{option_results}->{password}))) { + $self->{output}->add_option_msg(short_msg => "You need to set --username= and --password= options when --credentials is used"); + $self->{output}->option_exit(); + } + + if ((defined($self->{option_results}->{name})) && (defined($self->{option_results}->{id}))) { + $self->{output}->add_option_msg(short_msg => "Please set the name or id option"); + $self->{output}->option_exit(); + } + + if ((!defined($self->{option_results}->{name})) && (!defined($self->{option_results}->{id}))) { + $self->{output}->add_option_msg(short_msg => "Please set the name or id option"); + $self->{output}->option_exit(); + } + + if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) { + $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'."); + $self->{output}->option_exit(); + } + + if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) { + $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'."); + $self->{output}->option_exit(); + } +} + +sub run { + my ($self, %options) = @_; + + my $jsoncontent; + my $query_form_get = { stream => 'false' }; + + if (defined($self->{option_results}->{id})) { + $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{id}."/stats"; + $jsoncontent = centreon::plugins::httplib::connect($self, query_form_get => $query_form_get, connection_exit => 'critical'); + } elsif (defined($self->{option_results}->{name})) { + $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{name}."/stats"; + $jsoncontent = centreon::plugins::httplib::connect($self, query_form_get => $query_form_get, connection_exit => 'critical'); + } + + my $json = JSON->new; + + my $webcontent; + + eval { + $webcontent = $json->decode($jsoncontent); + }; + + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode json response"); + $self->{output}->option_exit(); + } + + my $total_size = $webcontent->{memory_stats}->{limit}; + my $memory_used = $webcontent->{memory_stats}->{usage}; + my $memory_free = $webcontent->{memory_stats}->{limit} - $webcontent->{memory_stats}->{usage}; + my $prct_used = $memory_used * 100 / $total_size; + my $prct_free = 100 - $prct_used; + + my $exit = $self->{perfdata}->threshold_check(value => $prct_used, threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); + my ($total_value, $total_unit) = $self->{perfdata}->change_bytes(value => $total_size); + my ($used_value, $used_unit) = $self->{perfdata}->change_bytes(value => $memory_used); + my ($free_value, $free_unit) = $self->{perfdata}->change_bytes(value => $memory_free); + + $self->{output}->output_add(severity => $exit, + short_msg => sprintf("Memory Total: %s Used: %s (%.2f%%) Free: %s %.2f%%)", + $total_value . " " . $total_unit, + $used_value . " " . $used_unit, $prct_used, + $free_value . " " . $free_unit, $prct_free) + ); + + $self->{output}->perfdata_add(label => "used", + value => $webcontent->{memory_stats}->{usage}, + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'), + min => 0, + max => $webcontent->{memory_stats}->{limit}, + ); + + $self->{output}->display(); + $self->{output}->exit(); + +} + +1; + +__END__ + +=head1 MODE + +Check Container's memory + +=over 8 + +=item B<--hostname> + +IP Addr/FQDN of Docker's API + +=item B<--port> + +Port used by Docker's API (Default: '2576') + +=item B<--proto> + +Specify https if needed (Default: 'https') + +=item B<--urlpath> + +Set path to get Docker's container information (Default: '/') + +=item B<--id> + +Specify one container's id + +=item B<--name> + +Specify one container's name + +=item B<--warning> + +Threshold warning in percent. + +=item B<--critical> + +Threshold critical in percent. + +=item B<--credentials> + +Specify this option if you access webpage over basic authentification + +=item B<--username> + +Specify username + +=item B<--password> + +Specify password + +=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<--timeout> + +Threshold for HTTP timeout (Default: 3) + +=back + +=cut From 2f31289bfa955a3e22977b27dcc2c0c7712a351a Mon Sep 17 00:00:00 2001 From: Shini31 Date: Wed, 22 Jul 2015 09:58:44 +0200 Subject: [PATCH 10/27] add started time info for running container --- apps/docker/mode/containerstate.pm | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/apps/docker/mode/containerstate.pm b/apps/docker/mode/containerstate.pm index 293ed2bab..b67fd4573 100644 --- a/apps/docker/mode/containerstate.pm +++ b/apps/docker/mode/containerstate.pm @@ -41,6 +41,7 @@ use strict; use warnings; use centreon::plugins::httplib; use JSON; +use DateTime::Format::ISO8601; my $thresholds = { state => [ @@ -172,7 +173,7 @@ sub run { $self->{output}->option_exit(); } - my ($result, $containername); + my ($result,$containername,$containertime); my $exit = 'OK'; if (defined($self->{option_results}->{id}) || defined($self->{option_results}->{name})) { @@ -181,13 +182,15 @@ sub run { $result = $keys; $containername = $webcontent->{Name}; $containername =~ s/^\///; + my $dt = DateTime::Format::ISO8601->parse_datetime($webcontent->{State}->{StartedAt}); + $containertime = $dt->strftime('%F %T'); last; } } $exit = $self->get_severity(section => 'state', value => $result); $self->{output}->output_add(severity => $exit, - short_msg => sprintf("Container %s is %s", $containername, $result)); + short_msg => sprintf("Container %s is %s (started since %s)", $containername, $result, $containertime)); } else { $self->{output}->output_add(severity => 'OK', short_msg => sprintf("All containers are in Running state")); @@ -217,7 +220,7 @@ sub run { } } - if ($exit ne 'OK') { + if (!$self->{output}->is_status(value => $exit, compare => 'OK', litteral => 1)) { $self->{output}->output_add(severity => $exit, short_msg => sprintf("Some containers are in wrong state")); } From 7569d802d5b4c58fb98b5e622685c9a6fb9cc42b Mon Sep 17 00:00:00 2001 From: Shini31 Date: Wed, 22 Jul 2015 17:48:32 +0200 Subject: [PATCH 11/27] fix help mode --- apps/docker/mode/memory.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/docker/mode/memory.pm b/apps/docker/mode/memory.pm index 33d3acd5f..490e0a92d 100644 --- a/apps/docker/mode/memory.pm +++ b/apps/docker/mode/memory.pm @@ -170,7 +170,7 @@ __END__ =head1 MODE -Check Container's memory +Check Container's memory usage =over 8 From 560f4e1302f6095b96edc613b9029abd1b13dda0 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Wed, 22 Jul 2015 17:48:45 +0200 Subject: [PATCH 12/27] add cpu mode --- apps/docker/mode/cpu.pm | 268 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 268 insertions(+) create mode 100644 apps/docker/mode/cpu.pm diff --git a/apps/docker/mode/cpu.pm b/apps/docker/mode/cpu.pm new file mode 100644 index 000000000..e7075b5d2 --- /dev/null +++ b/apps/docker/mode/cpu.pm @@ -0,0 +1,268 @@ +############################################################################### +# Copyright 2005-2015 CENTREON +# Centreon is developped by : Julien Mathis and Romain Le Merlus under +# GPL Licence 2.0. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation ; either version 2 of the License. +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, see . +# +# Linking this program statically or dynamically with other modules is making a +# combined work based on this program. Thus, the terms and conditions of the GNU +# General Public License cover the whole combination. +# +# As a special exception, the copyright holders of this program give CENTREON +# permission to link this program with independent modules to produce an timeelapsedutable, +# regardless of the license terms of these independent modules, and to copy and +# distribute the resulting timeelapsedutable under terms of CENTREON choice, provided that +# CENTREON also meet, for each linked independent module, the terms and conditions +# of the license of that module. An independent module is a module which is not +# derived from this program. If you modify this program, you may extend this +# exception to your version of the program, but you are not obliged to do so. If you +# do not wish to do so, delete this exception statement from your version. +# +# For more information : contact@centreon.com +# Authors : Mathieu Cinquin +# +#################################################################################### + +package apps::docker::mode::cpu; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::httplib; +use centreon::plugins::statefile; +use JSON; +use Data::Dumper; + +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 => + { + "hostname:s" => { name => 'hostname' }, + "port:s" => { name => 'port', default => '2376'}, + "proto:s" => { name => 'proto', default => 'https' }, + "urlpath:s" => { name => 'url_path', default => '/' }, + "name:s" => { name => 'name' }, + "id:s" => { name => 'id' }, + "warning:s" => { name => 'warning' }, + "critical:s" => { name => 'critical' }, + "credentials" => { name => 'credentials' }, + "username:s" => { name => 'username' }, + "password:s" => { name => 'password' }, + "ssl:s" => { name => 'ssl', }, + "cert-file:s" => { name => 'cert_file' }, + "key-file:s" => { name => 'key_file' }, + "cacert-file:s" => { name => 'cacert_file' }, + "timeout:s" => { name => 'timeout', default => '3' }, + }); + + $self->{statefile_value} = centreon::plugins::statefile->new(%options); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + if (!defined($self->{option_results}->{hostname})) { + $self->{output}->add_option_msg(short_msg => "Please set the hostname option"); + $self->{output}->option_exit(); + } + + if ((defined($self->{option_results}->{credentials})) && (!defined($self->{option_results}->{username}) || !defined($self->{option_results}->{password}))) { + $self->{output}->add_option_msg(short_msg => "You need to set --username= and --password= options when --credentials is used"); + $self->{output}->option_exit(); + } + + if ((defined($self->{option_results}->{name})) && (defined($self->{option_results}->{id}))) { + $self->{output}->add_option_msg(short_msg => "Please set the name or id option"); + $self->{output}->option_exit(); + } + + if ((!defined($self->{option_results}->{name})) && (!defined($self->{option_results}->{id}))) { + $self->{output}->add_option_msg(short_msg => "Please set the name or id option"); + $self->{output}->option_exit(); + } + + if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) { + $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'."); + $self->{output}->option_exit(); + } + + if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) { + $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'."); + $self->{output}->option_exit(); + } + + $self->{statefile_value}->check_options(%options); +} + +sub run { + my ($self, %options) = @_; + + my $jsoncontent; + my $query_form_get = { stream => 'false' }; + + if (defined($self->{option_results}->{id})) { + $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{id}."/stats"; + $jsoncontent = centreon::plugins::httplib::connect($self, query_form_get => $query_form_get, connection_exit => 'critical'); + $self->{statefile_value}->read(statefile => 'docker_' . $self->{option_results}->{id} . '_' . centreon::plugins::httplib::get_port($self) . '_' . $self->{mode}); + } elsif (defined($self->{option_results}->{name})) { + $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{name}."/stats"; + $jsoncontent = centreon::plugins::httplib::connect($self, query_form_get => $query_form_get, connection_exit => 'critical'); + $self->{statefile_value}->read(statefile => 'docker_' . $self->{option_results}->{name} . '_' . centreon::plugins::httplib::get_port($self) . '_' . $self->{mode}); + } + + my $json = JSON->new; + + my $webcontent; + + eval { + $webcontent = $json->decode($jsoncontent); + }; + + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode json response"); + $self->{output}->option_exit(); + } + + my $cpu_totalusage = $webcontent->{cpu_stats}->{cpu_usage}->{total_usage}; + my $cpu_systemusage = $webcontent->{cpu_stats}->{system_cpu_usage}; + my @cpu_number = @{$webcontent->{cpu_stats}->{cpu_usage}->{percpu_usage}}; + + my $new_datas = {}; + my $buffer_creation = 0; + $new_datas->{cpu_totalusage} = $cpu_totalusage; + $new_datas->{cpu_systemusage} = $cpu_systemusage; + my $old_cpu_totalusage = $self->{statefile_value}->get(name => 'cpu_totalusage'); + my $old_cpu_systemusage = $self->{statefile_value}->get(name => 'cpu_systemusage'); + + if ($new_datas->{cpu_totalusage} < $old_cpu_totalusage) { + # We set 0. Has reboot. + $old_cpu_totalusage = 0; + } + if ($new_datas->{cpu_systemusage} < $old_cpu_systemusage) { + # We set 0. Has reboot. + $old_cpu_systemusage = 0; + } + + my $delta_totalusage = $cpu_totalusage - $old_cpu_totalusage; + my $delta_systemusage = $cpu_systemusage - $old_cpu_systemusage; + my $prct_cpu = (($delta_totalusage / $delta_systemusage) * scalar(@cpu_number)) * 100; + + my $exit = $self->{perfdata}->threshold_check(value => $prct_cpu, threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); + + $self->{output}->output_add(severity => $exit, + short_msg => sprintf("CPU Usage is %.2f%%", $prct_cpu)); + + $self->{output}->perfdata_add(label => "cpu", unit => '%', + value => $prct_cpu, + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'), + min => 0, + max => 100, + ); + + $self->{statefile_value}->write(data => $new_datas); + if ($buffer_creation == 1) { + $self->{output}->output_add(severity => 'OK', + short_msg => "Buffer creation..."); + } + + $self->{output}->display(); + $self->{output}->exit(); + +} + +1; + +__END__ + +=head1 MODE + +Check Container's CPU usage + +=over 8 + +=item B<--hostname> + +IP Addr/FQDN of Docker's API + +=item B<--port> + +Port used by Docker's API (Default: '2576') + +=item B<--proto> + +Specify https if needed (Default: 'https') + +=item B<--urlpath> + +Set path to get Docker's container information (Default: '/') + +=item B<--id> + +Specify one container's id + +=item B<--name> + +Specify one container's name + +=item B<--warning> + +Threshold warning in percent. + +=item B<--critical> + +Threshold critical in percent. + +=item B<--credentials> + +Specify this option if you access webpage over basic authentification + +=item B<--username> + +Specify username + +=item B<--password> + +Specify password + +=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<--timeout> + +Threshold for HTTP timeout (Default: 3) + +=back + +=cut From 0921a899c748f77f3a6770195b6ff9777b83bc54 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Wed, 22 Jul 2015 17:59:30 +0200 Subject: [PATCH 13/27] fix bug in cache file when first execution --- apps/docker/mode/cpu.pm | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/apps/docker/mode/cpu.pm b/apps/docker/mode/cpu.pm index e7075b5d2..a83cea947 100644 --- a/apps/docker/mode/cpu.pm +++ b/apps/docker/mode/cpu.pm @@ -146,12 +146,19 @@ sub run { my @cpu_number = @{$webcontent->{cpu_stats}->{cpu_usage}->{percpu_usage}}; my $new_datas = {}; - my $buffer_creation = 0; $new_datas->{cpu_totalusage} = $cpu_totalusage; $new_datas->{cpu_systemusage} = $cpu_systemusage; my $old_cpu_totalusage = $self->{statefile_value}->get(name => 'cpu_totalusage'); my $old_cpu_systemusage = $self->{statefile_value}->get(name => 'cpu_systemusage'); + if ((!defined($old_cpu_totalusage)) || (!defined($old_cpu_systemusage))) { + $self->{output}->output_add(severity => 'OK', + short_msg => "Buffer creation..."); + $self->{statefile_value}->write(data => $new_datas); + $self->{output}->display(); + $self->{output}->exit(); + } + if ($new_datas->{cpu_totalusage} < $old_cpu_totalusage) { # We set 0. Has reboot. $old_cpu_totalusage = 0; @@ -179,10 +186,6 @@ sub run { ); $self->{statefile_value}->write(data => $new_datas); - if ($buffer_creation == 1) { - $self->{output}->output_add(severity => 'OK', - short_msg => "Buffer creation..."); - } $self->{output}->display(); $self->{output}->exit(); From 950cb45f28d196f7b7ca418c7e99bf14a8d54bf2 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Thu, 23 Jul 2015 11:48:07 +0200 Subject: [PATCH 14/27] delete Data::Dumper lib --- apps/docker/mode/cpu.pm | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/docker/mode/cpu.pm b/apps/docker/mode/cpu.pm index a83cea947..f1f476f90 100644 --- a/apps/docker/mode/cpu.pm +++ b/apps/docker/mode/cpu.pm @@ -42,7 +42,6 @@ use warnings; use centreon::plugins::httplib; use centreon::plugins::statefile; use JSON; -use Data::Dumper; sub new { my ($class, %options) = @_; From d1b78ef911f8565b36049677ae41fcb342167e6a Mon Sep 17 00:00:00 2001 From: Shini31 Date: Thu, 23 Jul 2015 14:41:43 +0200 Subject: [PATCH 15/27] add traffic mode --- apps/docker/mode/traffic.pm | 305 ++++++++++++++++++++++++++++++++++++ 1 file changed, 305 insertions(+) create mode 100644 apps/docker/mode/traffic.pm diff --git a/apps/docker/mode/traffic.pm b/apps/docker/mode/traffic.pm new file mode 100644 index 000000000..d75bc8fa8 --- /dev/null +++ b/apps/docker/mode/traffic.pm @@ -0,0 +1,305 @@ +############################################################################### +# Copyright 2005-2015 CENTREON +# Centreon is developped by : Julien Mathis and Romain Le Merlus under +# GPL Licence 2.0. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation ; either version 2 of the License. +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, see . +# +# Linking this program statically or dynamically with other modules is making a +# combined work based on this program. Thus, the terms and conditions of the GNU +# General Public License cover the whole combination. +# +# As a special exception, the copyright holders of this program give CENTREON +# permission to link this program with independent modules to produce an timeelapsedutable, +# regardless of the license terms of these independent modules, and to copy and +# distribute the resulting timeelapsedutable under terms of CENTREON choice, provided that +# CENTREON also meet, for each linked independent module, the terms and conditions +# of the license of that module. An independent module is a module which is not +# derived from this program. If you modify this program, you may extend this +# exception to your version of the program, but you are not obliged to do so. If you +# do not wish to do so, delete this exception statement from your version. +# +# For more information : contact@centreon.com +# Authors : Mathieu Cinquin +# +#################################################################################### + +package apps::docker::mode::traffic; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::httplib; +use centreon::plugins::statefile; +use JSON; + +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 => + { + "hostname:s" => { name => 'hostname' }, + "port:s" => { name => 'port', default => '2376'}, + "proto:s" => { name => 'proto', default => 'https' }, + "urlpath:s" => { name => 'url_path', default => '/' }, + "name:s" => { name => 'name' }, + "id:s" => { name => 'id' }, + "warning-in:s" => { name => 'warning_in' }, + "critical-in:s" => { name => 'critical_in' }, + "warning-out:s" => { name => 'warning_out' }, + "critical-out:s" => { name => 'critical_out' }, + "credentials" => { name => 'credentials' }, + "username:s" => { name => 'username' }, + "password:s" => { name => 'password' }, + "ssl:s" => { name => 'ssl', }, + "cert-file:s" => { name => 'cert_file' }, + "key-file:s" => { name => 'key_file' }, + "cacert-file:s" => { name => 'cacert_file' }, + "timeout:s" => { name => 'timeout', default => '3' }, + }); + + $self->{statefile_value} = centreon::plugins::statefile->new(%options); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + if (!defined($self->{option_results}->{hostname})) { + $self->{output}->add_option_msg(short_msg => "Please set the hostname option"); + $self->{output}->option_exit(); + } + if ((defined($self->{option_results}->{credentials})) && (!defined($self->{option_results}->{username}) || !defined($self->{option_results}->{password}))) { + $self->{output}->add_option_msg(short_msg => "You need to set --username= and --password= options when --credentials is used"); + $self->{output}->option_exit(); + } + if ((defined($self->{option_results}->{name})) && (defined($self->{option_results}->{id}))) { + $self->{output}->add_option_msg(short_msg => "Please set the name or id option"); + $self->{output}->option_exit(); + } + if ((!defined($self->{option_results}->{name})) && (!defined($self->{option_results}->{id}))) { + $self->{output}->add_option_msg(short_msg => "Please set the name or id option"); + $self->{output}->option_exit(); + } + if (($self->{perfdata}->threshold_validate(label => 'warning-in', value => $self->{option_results}->{warning_in})) == 0) { + $self->{output}->add_option_msg(short_msg => "Wrong warning 'in' threshold '" . $self->{option_results}->{warning_in} . "'."); + $self->{output}->option_exit(); + } + if (($self->{perfdata}->threshold_validate(label => 'critical-in', value => $self->{option_results}->{critical_in})) == 0) { + $self->{output}->add_option_msg(short_msg => "Wrong critical 'in' threshold '" . $self->{option_results}->{critical_in} . "'."); + $self->{output}->option_exit(); + } + if (($self->{perfdata}->threshold_validate(label => 'warning-out', value => $self->{option_results}->{warning_out})) == 0) { + $self->{output}->add_option_msg(short_msg => "Wrong warning 'out' threshold '" . $self->{option_results}->{warning_out} . "'."); + $self->{output}->option_exit(); + } + if (($self->{perfdata}->threshold_validate(label => 'critical-out', value => $self->{option_results}->{critical_out})) == 0) { + $self->{output}->add_option_msg(short_msg => "Wrong critical 'out' threshold '" . $self->{option_results}->{critical_out} . "'."); + $self->{output}->option_exit(); + } + + $self->{statefile_value}->check_options(%options); +} + +sub run { + my ($self, %options) = @_; + + my $new_datas = {}; + + if (defined($self->{option_results}->{id})) { + $self->{statefile_value}->read(statefile => 'docker_' . $self->{option_results}->{id} . '_' . centreon::plugins::httplib::get_port($self) . '_' . $self->{mode}); + } elsif (defined($self->{option_results}->{name})) { + $self->{statefile_value}->read(statefile => 'docker_' . $self->{option_results}->{name} . '_' . centreon::plugins::httplib::get_port($self) . '_' . $self->{mode}); + } + + my $jsoncontent; + my $query_form_get = { stream => 'false' }; + + if (defined($self->{option_results}->{id})) { + $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{id}."/stats"; + $jsoncontent = centreon::plugins::httplib::connect($self, query_form_get => $query_form_get, connection_exit => 'critical'); + } elsif (defined($self->{option_results}->{name})) { + $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{name}."/stats"; + $jsoncontent = centreon::plugins::httplib::connect($self, query_form_get => $query_form_get, connection_exit => 'critical'); + } + + my $json = JSON->new; + + my $webcontent; + + eval { + $webcontent = $json->decode($jsoncontent); + }; + + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode json response"); + $self->{output}->option_exit(); + } + + my $rx_bytes = $webcontent->{network}->{rx_bytes}; + my $tx_bytes = $webcontent->{network}->{tx_bytes}; + $new_datas->{rx_bytes} = $rx_bytes; + $new_datas->{tx_bytes} = $tx_bytes; + $new_datas->{last_timestamp} = time(); + my $old_timestamp = $self->{statefile_value}->get(name => 'last_timestamp'); + + if (!defined($old_timestamp)) { + $self->{output}->output_add(severity => 'OK', + short_msg => "Buffer creation..."); + $self->{statefile_value}->write(data => $new_datas); + $self->{output}->display(); + $self->{output}->exit(); + } + + my $time_delta = $new_datas->{last_timestamp} - $old_timestamp; + if ($time_delta <= 0) { + # At least one second. two fast calls ;) + $time_delta = 1; + } + + my $old_rx_bytes = $self->{statefile_value}->get(name => 'rx_bytes'); + my $old_tx_bytes = $self->{statefile_value}->get(name => 'tx_bytes'); + + if ($new_datas->{rx_bytes} < $old_rx_bytes) { + # We set 0. Has reboot. + $old_rx_bytes = 0; + } + if ($new_datas->{tx_bytes} < $old_tx_bytes) { + # We set 0. Has reboot. + $old_tx_bytes = 0; + } + + my $delta_rx_bits = ($rx_bytes - $old_rx_bytes) * 8; + my $delta_tx_bits = ($tx_bytes - $old_tx_bytes) * 8; + my $rx_absolute_per_sec = $delta_rx_bits / $time_delta; + my $tx_absolute_per_sec = $delta_tx_bits / $time_delta; + + my $exit1 = $self->{perfdata}->threshold_check(value => $rx_absolute_per_sec, threshold => [ { label => 'critical-in', 'exit_litteral' => 'critical' }, { label => 'warning-in', exit_litteral => 'warning' } ]); + my $exit2 = $self->{perfdata}->threshold_check(value => $tx_absolute_per_sec, threshold => [ { label => 'critical-out', 'exit_litteral' => 'critical' }, { label => 'warning-out', exit_litteral => 'warning' } ]); + + my ($rx_value, $rx_unit) = $self->{perfdata}->change_bytes(value => $rx_absolute_per_sec, network => 1); + my ($tx_value, $tx_unit) = $self->{perfdata}->change_bytes(value => $tx_absolute_per_sec, network => 1); + my $exit = $self->{output}->get_most_critical(status => [ $exit1, $exit2 ]); + $self->{output}->output_add(severity => $exit, + short_msg => sprintf("Traffic In : %s/s, Out : %s/s", + $rx_value . $rx_unit, + $tx_value . $tx_unit)); + + $self->{output}->perfdata_add(label => 'traffic_in', unit => 'b/s', + value => sprintf("%.2f", $rx_absolute_per_sec), + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-in'), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-in'), + min => 0); + $self->{output}->perfdata_add(label => 'traffic_out', unit => 'b/s', + value => sprintf("%.2f", $tx_absolute_per_sec), + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-out'), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-out'), + min => 0); + + $self->{statefile_value}->write(data => $new_datas); + + $self->{output}->display(); + $self->{output}->exit(); + +} + +1; + +__END__ + +=head1 MODE + +Check Container's Network traffic usage + +=over 8 + +=item B<--hostname> + +IP Addr/FQDN of Docker's API + +=item B<--port> + +Port used by Docker's API (Default: '2576') + +=item B<--proto> + +Specify https if needed (Default: 'https') + +=item B<--urlpath> + +Set path to get Docker's container information (Default: '/') + +=item B<--id> + +Specify one container's id + +=item B<--name> + +Specify one container's name + +=item B<--warning-in> + +Threshold warning in b/s for 'in' traffic. + +=item B<--critical-in> + +Threshold critical in b/s for 'in' traffic. + +=item B<--warning-out> + +Threshold warning in b/s for 'out' traffic. + +=item B<--critical-out> + +Threshold critical in b/s for 'out' traffic. + +=item B<--credentials> + +Specify this option if you access webpage over basic authentification + +=item B<--username> + +Specify username + +=item B<--password> + +Specify password + +=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<--timeout> + +Threshold for HTTP timeout (Default: 3) + +=back + +=cut From add12b1a9c85aa730a748d2392faaa98337efcdf Mon Sep 17 00:00:00 2001 From: Shini31 Date: Thu, 23 Jul 2015 17:16:10 +0200 Subject: [PATCH 16/27] add blockio mode --- apps/docker/mode/blockio.pm | 305 ++++++++++++++++++++++++++++++++++++ 1 file changed, 305 insertions(+) create mode 100644 apps/docker/mode/blockio.pm diff --git a/apps/docker/mode/blockio.pm b/apps/docker/mode/blockio.pm new file mode 100644 index 000000000..d088c45bc --- /dev/null +++ b/apps/docker/mode/blockio.pm @@ -0,0 +1,305 @@ +############################################################################### +# Copyright 2005-2015 CENTREON +# Centreon is developped by : Julien Mathis and Romain Le Merlus under +# GPL Licence 2.0. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation ; either version 2 of the License. +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, see . +# +# Linking this program statically or dynamically with other modules is making a +# combined work based on this program. Thus, the terms and conditions of the GNU +# General Public License cover the whole combination. +# +# As a special exception, the copyright holders of this program give CENTREON +# permission to link this program with independent modules to produce an timeelapsedutable, +# regardless of the license terms of these independent modules, and to copy and +# distribute the resulting timeelapsedutable under terms of CENTREON choice, provided that +# CENTREON also meet, for each linked independent module, the terms and conditions +# of the license of that module. An independent module is a module which is not +# derived from this program. If you modify this program, you may extend this +# exception to your version of the program, but you are not obliged to do so. If you +# do not wish to do so, delete this exception statement from your version. +# +# For more information : contact@centreon.com +# Authors : Mathieu Cinquin +# +#################################################################################### + +package apps::docker::mode::blockio; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::httplib; +use centreon::plugins::statefile; +use JSON; + +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 => + { + "hostname:s" => { name => 'hostname' }, + "port:s" => { name => 'port', default => '2376'}, + "proto:s" => { name => 'proto', default => 'https' }, + "urlpath:s" => { name => 'url_path', default => '/' }, + "name:s" => { name => 'name' }, + "id:s" => { name => 'id' }, + "warning-read:s" => { name => 'warning-read' }, + "critical-read:s" => { name => 'critical-read' }, + "warning-write:s" => { name => 'warning-write' }, + "critical-write:s" => { name => 'critical-write' }, + "credentials" => { name => 'credentials' }, + "username:s" => { name => 'username' }, + "password:s" => { name => 'password' }, + "ssl:s" => { name => 'ssl', }, + "cert-file:s" => { name => 'cert_file' }, + "key-file:s" => { name => 'key_file' }, + "cacert-file:s" => { name => 'cacert_file' }, + "timeout:s" => { name => 'timeout', default => '3' }, + }); + + $self->{statefile_value} = centreon::plugins::statefile->new(%options); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + if (!defined($self->{option_results}->{hostname})) { + $self->{output}->add_option_msg(short_msg => "Please set the hostname option"); + $self->{output}->option_exit(); + } + if ((defined($self->{option_results}->{credentials})) && (!defined($self->{option_results}->{username}) || !defined($self->{option_results}->{password}))) { + $self->{output}->add_option_msg(short_msg => "You need to set --username= and --password= options when --credentials is used"); + $self->{output}->option_exit(); + } + if ((defined($self->{option_results}->{name})) && (defined($self->{option_results}->{id}))) { + $self->{output}->add_option_msg(short_msg => "Please set the name or id option"); + $self->{output}->option_exit(); + } + if ((!defined($self->{option_results}->{name})) && (!defined($self->{option_results}->{id}))) { + $self->{output}->add_option_msg(short_msg => "Please set the name or id option"); + $self->{output}->option_exit(); + } + if (($self->{perfdata}->threshold_validate(label => 'warning-read', value => $self->{option_results}->{warning_read})) == 0) { + $self->{output}->add_option_msg(short_msg => "Wrong warning 'read' threshold '" . $self->{option_results}->{warning_read} . "'."); + $self->{output}->option_exit(); + } + if (($self->{perfdata}->threshold_validate(label => 'critical-read', value => $self->{option_results}->{critical_read})) == 0) { + $self->{output}->add_option_msg(short_msg => "Wrong critical 'read' threshold '" . $self->{option_results}->{critical_read} . "'."); + $self->{output}->option_exit(); + } + if (($self->{perfdata}->threshold_validate(label => 'warning-write', value => $self->{option_results}->{warning_write})) == 0) { + $self->{output}->add_option_msg(short_msg => "Wrong warning 'write' threshold '" . $self->{option_results}->{warning_write} . "'."); + $self->{output}->option_exit(); + } + if (($self->{perfdata}->threshold_validate(label => 'critical-write', value => $self->{option_results}->{critical_write})) == 0) { + $self->{output}->add_option_msg(short_msg => "Wrong critical 'write' threshold '" . $self->{option_results}->{critical_write} . "'."); + $self->{output}->option_exit(); + } + + $self->{statefile_value}->check_options(%options); +} + +sub run { + my ($self, %options) = @_; + + my $new_datas = {}; + + if (defined($self->{option_results}->{id})) { + $self->{statefile_value}->read(statefile => 'docker_' . $self->{option_results}->{id} . '_' . centreon::plugins::httplib::get_port($self) . '_' . $self->{mode}); + } elsif (defined($self->{option_results}->{name})) { + $self->{statefile_value}->read(statefile => 'docker_' . $self->{option_results}->{name} . '_' . centreon::plugins::httplib::get_port($self) . '_' . $self->{mode}); + } + + my $jsoncontent; + my $query_form_get = { stream => 'false' }; + + if (defined($self->{option_results}->{id})) { + $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{id}."/stats"; + $jsoncontent = centreon::plugins::httplib::connect($self, query_form_get => $query_form_get, connection_exit => 'critical'); + } elsif (defined($self->{option_results}->{name})) { + $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{name}."/stats"; + $jsoncontent = centreon::plugins::httplib::connect($self, query_form_get => $query_form_get, connection_exit => 'critical'); + } + + my $json = JSON->new; + + my $webcontent; + + eval { + $webcontent = $json->decode($jsoncontent); + }; + + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode json response"); + $self->{output}->option_exit(); + } + + my $read_bytes = $webcontent->{blkio_stats}->{io_service_bytes_recursive}->[0]->{value}; + my $write_bytes = $webcontent->{blkio_stats}->{io_service_bytes_recursive}->[1]->{value}; + $new_datas->{read_bytes} = $read_bytes; + $new_datas->{write_bytes} = $write_bytes; + $new_datas->{last_timestamp} = time(); + my $old_timestamp = $self->{statefile_value}->get(name => 'last_timestamp'); + + if (!defined($old_timestamp)) { + $self->{output}->output_add(severity => 'OK', + short_msg => "Buffer creation..."); + $self->{statefile_value}->write(data => $new_datas); + $self->{output}->display(); + $self->{output}->exit(); + } + + my $time_delta = $new_datas->{last_timestamp} - $old_timestamp; + if ($time_delta <= 0) { + # At least one second. two fast calls ;) + $time_delta = 1; + } + + my $old_read_bytes = $self->{statefile_value}->get(name => 'read_bytes'); + my $old_write_bytes = $self->{statefile_value}->get(name => 'write_bytes'); + + if ($new_datas->{read_bytes} < $old_read_bytes) { + # We set 0. Has reboot. + $old_read_bytes = 0; + } + if ($new_datas->{write_bytes} < $old_write_bytes) { + # We set 0. Has reboot. + $old_write_bytes = 0; + } + + my $delta_read_bits = ($read_bytes - $old_read_bytes) * 8; + my $delta_write_bits = ($write_bytes - $old_write_bytes) * 8; + my $read_absolute_per_sec = $delta_read_bits / $time_delta; + my $write_absolute_per_sec = $delta_write_bits / $time_delta; + + my $exit1 = $self->{perfdata}->threshold_check(value => $read_absolute_per_sec, threshold => [ { label => 'critical-read', 'exit_litteral' => 'critical' }, { label => 'warning-read', exit_litteral => 'warning' } ]); + my $exit2 = $self->{perfdata}->threshold_check(value => $write_absolute_per_sec, threshold => [ { label => 'critical-write', 'exit_litteral' => 'critical' }, { label => 'warning-write', exit_litteral => 'warning' } ]); + + my ($read_value, $read_unit) = $self->{perfdata}->change_bytes(value => $read_absolute_per_sec, network => 1); + my ($write_value, $write_unit) = $self->{perfdata}->change_bytes(value => $write_absolute_per_sec, network => 1); + my $exit = $self->{output}->get_most_critical(status => [ $exit1, $exit2 ]); + $self->{output}->output_add(severity => $exit, + short_msg => sprintf("Read I/O : %s/s, Write I/O : %s/s", + $read_value . $read_unit, + $write_value . $write_unit)); + + $self->{output}->perfdata_add(label => 'read_io', unit => 'b/s', + value => sprintf("%.2f", $read_absolute_per_sec), + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-read'), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-read'), + min => 0); + $self->{output}->perfdata_add(label => 'write_io', unit => 'b/s', + value => sprintf("%.2f", $write_absolute_per_sec), + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-write'), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-write'), + min => 0); + + $self->{statefile_value}->write(data => $new_datas); + + $self->{output}->display(); + $self->{output}->exit(); + +} + +1; + +__END__ + +=head1 MODE + +Check Container's Block I/O usage + +=over 8 + +=item B<--hostname> + +IP Addr/FQDN of Docker's API + +=item B<--port> + +Port used by Docker's API (Default: '2576') + +=item B<--proto> + +Specify https if needed (Default: 'https') + +=item B<--urlpath> + +Set path to get Docker's container information (Default: '/') + +=item B<--id> + +Specify one container's id + +=item B<--name> + +Specify one container's name + +=item B<--warning-read> + +Threshold warning in b/s for Read I/O. + +=item B<--critical-read> + +Threshold critical in b/s for Read I/O. + +=item B<--warning-write> + +Threshold warning in b/s for Write I/O. + +=item B<--critical-write> + +Threshold critical in b/s for Write I/O. + +=item B<--credentials> + +Specify this option if you access webpage over basic authentification + +=item B<--username> + +Specify username + +=item B<--password> + +Specify password + +=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<--timeout> + +Threshold for HTTP timeout (Default: 3) + +=back + +=cut From 1a2c95b4098b93b18f1dc3c9b31915a808892823 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Thu, 23 Jul 2015 18:21:55 +0200 Subject: [PATCH 17/27] change directory --- {apps => cloud}/docker/mode/blockio.pm | 0 {apps => cloud}/docker/mode/containerstate.pm | 0 {apps => cloud}/docker/mode/cpu.pm | 0 {apps => cloud}/docker/mode/image.pm | 0 {apps => cloud}/docker/mode/info.pm | 0 {apps => cloud}/docker/mode/listcontainers.pm | 0 {apps => cloud}/docker/mode/memory.pm | 0 {apps => cloud}/docker/mode/traffic.pm | 0 {apps => cloud}/docker/plugin.pm | 0 9 files changed, 0 insertions(+), 0 deletions(-) rename {apps => cloud}/docker/mode/blockio.pm (100%) rename {apps => cloud}/docker/mode/containerstate.pm (100%) rename {apps => cloud}/docker/mode/cpu.pm (100%) rename {apps => cloud}/docker/mode/image.pm (100%) rename {apps => cloud}/docker/mode/info.pm (100%) rename {apps => cloud}/docker/mode/listcontainers.pm (100%) rename {apps => cloud}/docker/mode/memory.pm (100%) rename {apps => cloud}/docker/mode/traffic.pm (100%) rename {apps => cloud}/docker/plugin.pm (100%) diff --git a/apps/docker/mode/blockio.pm b/cloud/docker/mode/blockio.pm similarity index 100% rename from apps/docker/mode/blockio.pm rename to cloud/docker/mode/blockio.pm diff --git a/apps/docker/mode/containerstate.pm b/cloud/docker/mode/containerstate.pm similarity index 100% rename from apps/docker/mode/containerstate.pm rename to cloud/docker/mode/containerstate.pm diff --git a/apps/docker/mode/cpu.pm b/cloud/docker/mode/cpu.pm similarity index 100% rename from apps/docker/mode/cpu.pm rename to cloud/docker/mode/cpu.pm diff --git a/apps/docker/mode/image.pm b/cloud/docker/mode/image.pm similarity index 100% rename from apps/docker/mode/image.pm rename to cloud/docker/mode/image.pm diff --git a/apps/docker/mode/info.pm b/cloud/docker/mode/info.pm similarity index 100% rename from apps/docker/mode/info.pm rename to cloud/docker/mode/info.pm diff --git a/apps/docker/mode/listcontainers.pm b/cloud/docker/mode/listcontainers.pm similarity index 100% rename from apps/docker/mode/listcontainers.pm rename to cloud/docker/mode/listcontainers.pm diff --git a/apps/docker/mode/memory.pm b/cloud/docker/mode/memory.pm similarity index 100% rename from apps/docker/mode/memory.pm rename to cloud/docker/mode/memory.pm diff --git a/apps/docker/mode/traffic.pm b/cloud/docker/mode/traffic.pm similarity index 100% rename from apps/docker/mode/traffic.pm rename to cloud/docker/mode/traffic.pm diff --git a/apps/docker/plugin.pm b/cloud/docker/plugin.pm similarity index 100% rename from apps/docker/plugin.pm rename to cloud/docker/plugin.pm From 50dcd19cec4e2247c1ec0581ec0c7cac24502320 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Fri, 24 Jul 2015 12:48:15 +0200 Subject: [PATCH 18/27] change I/O value in B/s --- cloud/docker/mode/blockio.pm | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cloud/docker/mode/blockio.pm b/cloud/docker/mode/blockio.pm index d088c45bc..d5e40b3fa 100644 --- a/cloud/docker/mode/blockio.pm +++ b/cloud/docker/mode/blockio.pm @@ -184,8 +184,8 @@ sub run { $old_write_bytes = 0; } - my $delta_read_bits = ($read_bytes - $old_read_bytes) * 8; - my $delta_write_bits = ($write_bytes - $old_write_bytes) * 8; + my $delta_read_bytes = $read_bytes - $old_read_bytes; + my $delta_write_bytes = $write_bytes - $old_write_bytes; my $read_absolute_per_sec = $delta_read_bits / $time_delta; my $write_absolute_per_sec = $delta_write_bits / $time_delta; @@ -200,12 +200,12 @@ sub run { $read_value . $read_unit, $write_value . $write_unit)); - $self->{output}->perfdata_add(label => 'read_io', unit => 'b/s', + $self->{output}->perfdata_add(label => 'read_io', unit => 'B/s', value => sprintf("%.2f", $read_absolute_per_sec), warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-read'), critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-read'), min => 0); - $self->{output}->perfdata_add(label => 'write_io', unit => 'b/s', + $self->{output}->perfdata_add(label => 'write_io', unit => 'B/s', value => sprintf("%.2f", $write_absolute_per_sec), warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-write'), critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-write'), @@ -254,19 +254,19 @@ Specify one container's name =item B<--warning-read> -Threshold warning in b/s for Read I/O. +Threshold warning in B/s for Read I/O. =item B<--critical-read> -Threshold critical in b/s for Read I/O. +Threshold critical in B/s for Read I/O. =item B<--warning-write> -Threshold warning in b/s for Write I/O. +Threshold warning in B/s for Write I/O. =item B<--critical-write> -Threshold critical in b/s for Write I/O. +Threshold critical in B/s for Write I/O. =item B<--credentials> From b9e815010df63ac42691d3b7bd664c3a64b7032a Mon Sep 17 00:00:00 2001 From: Shini31 Date: Fri, 24 Jul 2015 12:48:44 +0200 Subject: [PATCH 19/27] use regexp instead of Date parser --- cloud/docker/mode/containerstate.pm | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cloud/docker/mode/containerstate.pm b/cloud/docker/mode/containerstate.pm index b67fd4573..3d8348646 100644 --- a/cloud/docker/mode/containerstate.pm +++ b/cloud/docker/mode/containerstate.pm @@ -41,7 +41,6 @@ use strict; use warnings; use centreon::plugins::httplib; use JSON; -use DateTime::Format::ISO8601; my $thresholds = { state => [ @@ -182,8 +181,8 @@ sub run { $result = $keys; $containername = $webcontent->{Name}; $containername =~ s/^\///; - my $dt = DateTime::Format::ISO8601->parse_datetime($webcontent->{State}->{StartedAt}); - $containertime = $dt->strftime('%F %T'); + my ( $y, $m, $d, $h, $mi, $s ) = $webcontent->{State}->{StartedAt} =~ /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/; + $containertime = $y."-".$m."-".$d." ".$h.":".$mi.":".$s; last; } } From 37926e3e9adda11452ac0152f7085d2a865c99ff Mon Sep 17 00:00:00 2001 From: Shini31 Date: Fri, 24 Jul 2015 15:21:07 +0200 Subject: [PATCH 20/27] add image in container description --- cloud/docker/mode/listcontainers.pm | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/cloud/docker/mode/listcontainers.pm b/cloud/docker/mode/listcontainers.pm index b0ea93e7f..947b0edf5 100644 --- a/cloud/docker/mode/listcontainers.pm +++ b/cloud/docker/mode/listcontainers.pm @@ -108,9 +108,17 @@ sub run { my $containername = $val->{Names}->[0]; $containername =~ s/^\///; my $containerid = $val->{Id}; - my $containerstate = $val->{Status}; - $self->{output}->output_add(long_msg => sprintf("%s [id = %s , state = %s]", - $containername, $containerid, $containerstate)); + my $containerimage = $val->{Image}; + my $containerstate; + if (($val->{Status} =~ m/^Up/) && ($val->{Status} =~ m/^(?:(?!Paused).)*$/)) { + $containerstate = 'Running'; + } elsif ($val->{Status} =~ m/^Exited/) { + $containerstate = 'Exited'; + } elsif ($val->{Status} =~ m/\(Paused\)$/) { + $containerstate = 'Paused'; + } + $self->{output}->output_add(long_msg => sprintf("%s [id = %s , image = %s, state = %s]", + $containername, $containerid, $containerimage, $containerstate)); } $self->{output}->output_add(severity => 'OK', short_msg => 'List containers:'); From 91935ee9fdff453385ba975d7450ac13c84b8999 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Fri, 24 Jul 2015 15:26:09 +0200 Subject: [PATCH 21/27] fix bug with perfdata --- cloud/docker/mode/memory.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cloud/docker/mode/memory.pm b/cloud/docker/mode/memory.pm index 490e0a92d..a8b4b7ff6 100644 --- a/cloud/docker/mode/memory.pm +++ b/cloud/docker/mode/memory.pm @@ -153,8 +153,8 @@ sub run { $self->{output}->perfdata_add(label => "used", value => $webcontent->{memory_stats}->{usage}, - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'), + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning', total => $total_size, cast_int => 1), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical', total => $total_size, cast_int => 1), min => 0, max => $webcontent->{memory_stats}->{limit}, ); From 311ac12ca089ae05c8480e0e183d87353f241e92 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Fri, 24 Jul 2015 15:27:20 +0200 Subject: [PATCH 22/27] add few comments --- cloud/docker/mode/blockio.pm | 17 +++++++++-------- cloud/docker/mode/containerstate.pm | 16 +++++++++------- cloud/docker/mode/image.pm | 2 +- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/cloud/docker/mode/blockio.pm b/cloud/docker/mode/blockio.pm index d5e40b3fa..00aa0ae9c 100644 --- a/cloud/docker/mode/blockio.pm +++ b/cloud/docker/mode/blockio.pm @@ -158,6 +158,7 @@ sub run { $new_datas->{last_timestamp} = time(); my $old_timestamp = $self->{statefile_value}->get(name => 'last_timestamp'); + # First execution if (!defined($old_timestamp)) { $self->{output}->output_add(severity => 'OK', short_msg => "Buffer creation..."); @@ -201,15 +202,15 @@ sub run { $write_value . $write_unit)); $self->{output}->perfdata_add(label => 'read_io', unit => 'B/s', - value => sprintf("%.2f", $read_absolute_per_sec), - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-read'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-read'), - min => 0); + value => sprintf("%.2f", $read_absolute_per_sec), + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-read'), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-read'), + min => 0); $self->{output}->perfdata_add(label => 'write_io', unit => 'B/s', - value => sprintf("%.2f", $write_absolute_per_sec), - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-write'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-write'), - min => 0); + value => sprintf("%.2f", $write_absolute_per_sec), + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-write'), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-write'), + min => 0); $self->{statefile_value}->write(data => $new_datas); diff --git a/cloud/docker/mode/containerstate.pm b/cloud/docker/mode/containerstate.pm index 3d8348646..b8076149a 100644 --- a/cloud/docker/mode/containerstate.pm +++ b/cloud/docker/mode/containerstate.pm @@ -177,6 +177,7 @@ sub run { if (defined($self->{option_results}->{id}) || defined($self->{option_results}->{name})) { while ( my ($keys,$values) = each(%{$webcontent->{State}})) { + # Why not set a variable that contains the state? if ($values eq 'true') { $result = $keys; $containername = $webcontent->{Name}; @@ -200,6 +201,7 @@ sub run { $containername = $val->{Names}->[0]; $containername =~ s/^\///; + # Thanks to Docker API for the paused state... if (($val->{Status} =~ m/^Up/) && ($val->{Status} =~ m/^(?:(?!Paused).)*$/)) { $result = 'Running'; $nbrunning++; @@ -215,7 +217,7 @@ sub run { $exit = $self->{output}->get_most_critical(status => [ $tmp_exit, $exit ]); if (!$self->{output}->is_status(value => $tmp_exit, compare => 'OK', litteral => 1)) { $self->{output}->output_add(long_msg => sprintf("Containers %s is in %s state", - $containername, $result)); + $containername, $result)); } } @@ -224,16 +226,16 @@ sub run { short_msg => sprintf("Some containers are in wrong state")); } $self->{output}->perfdata_add(label => "running", - value => $nbrunning, - min => 0, + value => $nbrunning, + min => 0, ); $self->{output}->perfdata_add(label => "paused", - value => $nbpaused, - min => 0, + value => $nbpaused, + min => 0, ); $self->{output}->perfdata_add(label => "exited", - value => $nbexited, - min => 0, + value => $nbexited, + min => 0, ); } diff --git a/cloud/docker/mode/image.pm b/cloud/docker/mode/image.pm index e5ad60c35..ce4bde967 100644 --- a/cloud/docker/mode/image.pm +++ b/cloud/docker/mode/image.pm @@ -187,7 +187,7 @@ __END__ =head1 MODE -Check Container's image viability +Check Container's image viability with a registry =over 8 From 72ca2dcb6e3595f9cc8bc2ef0ddddc694a9dc6f0 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Mon, 10 Aug 2015 11:12:16 +0200 Subject: [PATCH 23/27] + License now Apache 2.0 --- cloud/docker/mode/blockio.pm | 41 +++++++++-------------------- cloud/docker/mode/containerstate.pm | 41 +++++++++-------------------- cloud/docker/mode/cpu.pm | 41 +++++++++-------------------- cloud/docker/mode/image.pm | 41 +++++++++-------------------- cloud/docker/mode/info.pm | 41 +++++++++-------------------- cloud/docker/mode/listcontainers.pm | 41 +++++++++-------------------- cloud/docker/mode/memory.pm | 41 +++++++++-------------------- cloud/docker/mode/traffic.pm | 41 +++++++++-------------------- cloud/docker/plugin.pm | 41 +++++++++-------------------- 9 files changed, 117 insertions(+), 252 deletions(-) diff --git a/cloud/docker/mode/blockio.pm b/cloud/docker/mode/blockio.pm index 00aa0ae9c..2e7691eb2 100644 --- a/cloud/docker/mode/blockio.pm +++ b/cloud/docker/mode/blockio.pm @@ -1,37 +1,22 @@ -############################################################################### -# Copyright 2005-2015 CENTREON -# Centreon is developped by : Julien Mathis and Romain Le Merlus under -# GPL Licence 2.0. # -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free Software -# Foundation ; either version 2 of the License. +# Copyright 2015 Centreon (http://www.centreon.com/) # -# This program is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. # -# You should have received a copy of the GNU General Public License along with -# this program; if not, see . +# 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 # -# Linking this program statically or dynamically with other modules is making a -# combined work based on this program. Thus, the terms and conditions of the GNU -# General Public License cover the whole combination. +# http://www.apache.org/licenses/LICENSE-2.0 # -# As a special exception, the copyright holders of this program give CENTREON -# permission to link this program with independent modules to produce an timeelapsedutable, -# regardless of the license terms of these independent modules, and to copy and -# distribute the resulting timeelapsedutable under terms of CENTREON choice, provided that -# CENTREON also meet, for each linked independent module, the terms and conditions -# of the license of that module. An independent module is a module which is not -# derived from this program. If you modify this program, you may extend this -# exception to your version of the program, but you are not obliged to do so. If you -# do not wish to do so, delete this exception statement from your version. +# 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. # -# For more information : contact@centreon.com -# Authors : Mathieu Cinquin -# -#################################################################################### package apps::docker::mode::blockio; diff --git a/cloud/docker/mode/containerstate.pm b/cloud/docker/mode/containerstate.pm index b8076149a..0f645666e 100644 --- a/cloud/docker/mode/containerstate.pm +++ b/cloud/docker/mode/containerstate.pm @@ -1,37 +1,22 @@ -############################################################################### -# Copyright 2005-2015 CENTREON -# Centreon is developped by : Julien Mathis and Romain Le Merlus under -# GPL Licence 2.0. # -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free Software -# Foundation ; either version 2 of the License. +# Copyright 2015 Centreon (http://www.centreon.com/) # -# This program is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. # -# You should have received a copy of the GNU General Public License along with -# this program; if not, see . +# 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 # -# Linking this program statically or dynamically with other modules is making a -# combined work based on this program. Thus, the terms and conditions of the GNU -# General Public License cover the whole combination. +# http://www.apache.org/licenses/LICENSE-2.0 # -# As a special exception, the copyright holders of this program give CENTREON -# permission to link this program with independent modules to produce an timeelapsedutable, -# regardless of the license terms of these independent modules, and to copy and -# distribute the resulting timeelapsedutable under terms of CENTREON choice, provided that -# CENTREON also meet, for each linked independent module, the terms and conditions -# of the license of that module. An independent module is a module which is not -# derived from this program. If you modify this program, you may extend this -# exception to your version of the program, but you are not obliged to do so. If you -# do not wish to do so, delete this exception statement from your version. +# 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. # -# For more information : contact@centreon.com -# Authors : Mathieu Cinquin -# -#################################################################################### package apps::docker::mode::containerstate; diff --git a/cloud/docker/mode/cpu.pm b/cloud/docker/mode/cpu.pm index f1f476f90..ca540a443 100644 --- a/cloud/docker/mode/cpu.pm +++ b/cloud/docker/mode/cpu.pm @@ -1,37 +1,22 @@ -############################################################################### -# Copyright 2005-2015 CENTREON -# Centreon is developped by : Julien Mathis and Romain Le Merlus under -# GPL Licence 2.0. # -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free Software -# Foundation ; either version 2 of the License. +# Copyright 2015 Centreon (http://www.centreon.com/) # -# This program is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. # -# You should have received a copy of the GNU General Public License along with -# this program; if not, see . +# 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 # -# Linking this program statically or dynamically with other modules is making a -# combined work based on this program. Thus, the terms and conditions of the GNU -# General Public License cover the whole combination. +# http://www.apache.org/licenses/LICENSE-2.0 # -# As a special exception, the copyright holders of this program give CENTREON -# permission to link this program with independent modules to produce an timeelapsedutable, -# regardless of the license terms of these independent modules, and to copy and -# distribute the resulting timeelapsedutable under terms of CENTREON choice, provided that -# CENTREON also meet, for each linked independent module, the terms and conditions -# of the license of that module. An independent module is a module which is not -# derived from this program. If you modify this program, you may extend this -# exception to your version of the program, but you are not obliged to do so. If you -# do not wish to do so, delete this exception statement from your version. +# 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. # -# For more information : contact@centreon.com -# Authors : Mathieu Cinquin -# -#################################################################################### package apps::docker::mode::cpu; diff --git a/cloud/docker/mode/image.pm b/cloud/docker/mode/image.pm index ce4bde967..78417eb6c 100644 --- a/cloud/docker/mode/image.pm +++ b/cloud/docker/mode/image.pm @@ -1,37 +1,22 @@ -############################################################################### -# Copyright 2005-2015 CENTREON -# Centreon is developped by : Julien Mathis and Romain Le Merlus under -# GPL Licence 2.0. # -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free Software -# Foundation ; either version 2 of the License. +# Copyright 2015 Centreon (http://www.centreon.com/) # -# This program is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. # -# You should have received a copy of the GNU General Public License along with -# this program; if not, see . +# 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 # -# Linking this program statically or dynamically with other modules is making a -# combined work based on this program. Thus, the terms and conditions of the GNU -# General Public License cover the whole combination. +# http://www.apache.org/licenses/LICENSE-2.0 # -# As a special exception, the copyright holders of this program give CENTREON -# permission to link this program with independent modules to produce an timeelapsedutable, -# regardless of the license terms of these independent modules, and to copy and -# distribute the resulting timeelapsedutable under terms of CENTREON choice, provided that -# CENTREON also meet, for each linked independent module, the terms and conditions -# of the license of that module. An independent module is a module which is not -# derived from this program. If you modify this program, you may extend this -# exception to your version of the program, but you are not obliged to do so. If you -# do not wish to do so, delete this exception statement from your version. +# 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. # -# For more information : contact@centreon.com -# Authors : Mathieu Cinquin -# -#################################################################################### package apps::docker::mode::image; diff --git a/cloud/docker/mode/info.pm b/cloud/docker/mode/info.pm index 32bf5403a..bdd0f062e 100644 --- a/cloud/docker/mode/info.pm +++ b/cloud/docker/mode/info.pm @@ -1,37 +1,22 @@ -############################################################################### -# Copyright 2005-2015 CENTREON -# Centreon is developped by : Julien Mathis and Romain Le Merlus under -# GPL Licence 2.0. # -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free Software -# Foundation ; either version 2 of the License. +# Copyright 2015 Centreon (http://www.centreon.com/) # -# This program is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. # -# You should have received a copy of the GNU General Public License along with -# this program; if not, see . +# 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 # -# Linking this program statically or dynamically with other modules is making a -# combined work based on this program. Thus, the terms and conditions of the GNU -# General Public License cover the whole combination. +# http://www.apache.org/licenses/LICENSE-2.0 # -# As a special exception, the copyright holders of this program give CENTREON -# permission to link this program with independent modules to produce an timeelapsedutable, -# regardless of the license terms of these independent modules, and to copy and -# distribute the resulting timeelapsedutable under terms of CENTREON choice, provided that -# CENTREON also meet, for each linked independent module, the terms and conditions -# of the license of that module. An independent module is a module which is not -# derived from this program. If you modify this program, you may extend this -# exception to your version of the program, but you are not obliged to do so. If you -# do not wish to do so, delete this exception statement from your version. +# 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. # -# For more information : contact@centreon.com -# Authors : Mathieu Cinquin -# -#################################################################################### package apps::docker::mode::info; diff --git a/cloud/docker/mode/listcontainers.pm b/cloud/docker/mode/listcontainers.pm index 947b0edf5..500879390 100644 --- a/cloud/docker/mode/listcontainers.pm +++ b/cloud/docker/mode/listcontainers.pm @@ -1,37 +1,22 @@ -############################################################################### -# Copyright 2005-2015 CENTREON -# Centreon is developped by : Julien Mathis and Romain Le Merlus under -# GPL Licence 2.0. # -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free Software -# Foundation ; either version 2 of the License. +# Copyright 2015 Centreon (http://www.centreon.com/) # -# This program is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. # -# You should have received a copy of the GNU General Public License along with -# this program; if not, see . +# 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 # -# Linking this program statically or dynamically with other modules is making a -# combined work based on this program. Thus, the terms and conditions of the GNU -# General Public License cover the whole combination. +# http://www.apache.org/licenses/LICENSE-2.0 # -# As a special exception, the copyright holders of this program give CENTREON -# permission to link this program with independent modules to produce an timeelapsedutable, -# regardless of the license terms of these independent modules, and to copy and -# distribute the resulting timeelapsedutable under terms of CENTREON choice, provided that -# CENTREON also meet, for each linked independent module, the terms and conditions -# of the license of that module. An independent module is a module which is not -# derived from this program. If you modify this program, you may extend this -# exception to your version of the program, but you are not obliged to do so. If you -# do not wish to do so, delete this exception statement from your version. +# 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. # -# For more information : contact@centreon.com -# Authors : Mathieu Cinquin -# -#################################################################################### package apps::docker::mode::listcontainers; diff --git a/cloud/docker/mode/memory.pm b/cloud/docker/mode/memory.pm index a8b4b7ff6..fcfa6d38e 100644 --- a/cloud/docker/mode/memory.pm +++ b/cloud/docker/mode/memory.pm @@ -1,37 +1,22 @@ -############################################################################### -# Copyright 2005-2015 CENTREON -# Centreon is developped by : Julien Mathis and Romain Le Merlus under -# GPL Licence 2.0. # -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free Software -# Foundation ; either version 2 of the License. +# Copyright 2015 Centreon (http://www.centreon.com/) # -# This program is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. # -# You should have received a copy of the GNU General Public License along with -# this program; if not, see . +# 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 # -# Linking this program statically or dynamically with other modules is making a -# combined work based on this program. Thus, the terms and conditions of the GNU -# General Public License cover the whole combination. +# http://www.apache.org/licenses/LICENSE-2.0 # -# As a special exception, the copyright holders of this program give CENTREON -# permission to link this program with independent modules to produce an timeelapsedutable, -# regardless of the license terms of these independent modules, and to copy and -# distribute the resulting timeelapsedutable under terms of CENTREON choice, provided that -# CENTREON also meet, for each linked independent module, the terms and conditions -# of the license of that module. An independent module is a module which is not -# derived from this program. If you modify this program, you may extend this -# exception to your version of the program, but you are not obliged to do so. If you -# do not wish to do so, delete this exception statement from your version. +# 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. # -# For more information : contact@centreon.com -# Authors : Mathieu Cinquin -# -#################################################################################### package apps::docker::mode::memory; diff --git a/cloud/docker/mode/traffic.pm b/cloud/docker/mode/traffic.pm index d75bc8fa8..9cda80515 100644 --- a/cloud/docker/mode/traffic.pm +++ b/cloud/docker/mode/traffic.pm @@ -1,37 +1,22 @@ -############################################################################### -# Copyright 2005-2015 CENTREON -# Centreon is developped by : Julien Mathis and Romain Le Merlus under -# GPL Licence 2.0. # -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free Software -# Foundation ; either version 2 of the License. +# Copyright 2015 Centreon (http://www.centreon.com/) # -# This program is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. # -# You should have received a copy of the GNU General Public License along with -# this program; if not, see . +# 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 # -# Linking this program statically or dynamically with other modules is making a -# combined work based on this program. Thus, the terms and conditions of the GNU -# General Public License cover the whole combination. +# http://www.apache.org/licenses/LICENSE-2.0 # -# As a special exception, the copyright holders of this program give CENTREON -# permission to link this program with independent modules to produce an timeelapsedutable, -# regardless of the license terms of these independent modules, and to copy and -# distribute the resulting timeelapsedutable under terms of CENTREON choice, provided that -# CENTREON also meet, for each linked independent module, the terms and conditions -# of the license of that module. An independent module is a module which is not -# derived from this program. If you modify this program, you may extend this -# exception to your version of the program, but you are not obliged to do so. If you -# do not wish to do so, delete this exception statement from your version. +# 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. # -# For more information : contact@centreon.com -# Authors : Mathieu Cinquin -# -#################################################################################### package apps::docker::mode::traffic; diff --git a/cloud/docker/plugin.pm b/cloud/docker/plugin.pm index 23c9572d0..6fcb0c2c2 100644 --- a/cloud/docker/plugin.pm +++ b/cloud/docker/plugin.pm @@ -1,37 +1,22 @@ -################################################################################ -# Copyright 2005-2015 CENTREON -# Centreon is developped by : Julien Mathis and Romain Le Merlus under -# GPL Licence 2.0. # -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free Software -# Foundation ; either version 2 of the License. +# Copyright 2015 Centreon (http://www.centreon.com/) # -# This program is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. # -# You should have received a copy of the GNU General Public License along with -# this program; if not, see . +# 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 # -# Linking this program statically or dynamically with other modules is making a -# combined work based on this program. Thus, the terms and conditions of the GNU -# General Public License cover the whole combination. +# http://www.apache.org/licenses/LICENSE-2.0 # -# As a special exception, the copyright holders of this program give CENTREON -# permission to link this program with independent modules to produce an executable, -# regardless of the license terms of these independent modules, and to copy and -# distribute the resulting executable under terms of CENTREON choice, provided that -# CENTREON also meet, for each linked independent module, the terms and conditions -# of the license of that module. An independent module is a module which is not -# derived from this program. If you modify this program, you may extend this -# exception to your version of the program, but you are not obliged to do so. If you -# do not wish to do so, delete this exception statement from your version. +# 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. # -# For more information : contact@centreon.com -# Authors : Mathieu Cinquin -# -#################################################################################### package apps::docker::plugin; From 3f34e92dc86c42056926a56ac75c067b7c72ef28 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Mon, 10 Aug 2015 13:54:20 +0200 Subject: [PATCH 24/27] fix error with directory --- cloud/docker/mode/blockio.pm | 2 +- cloud/docker/mode/containerstate.pm | 2 +- cloud/docker/mode/cpu.pm | 2 +- cloud/docker/mode/image.pm | 2 +- cloud/docker/mode/info.pm | 2 +- cloud/docker/mode/listcontainers.pm | 2 +- cloud/docker/mode/memory.pm | 2 +- cloud/docker/mode/traffic.pm | 2 +- cloud/docker/plugin.pm | 18 +++++++++--------- 9 files changed, 17 insertions(+), 17 deletions(-) diff --git a/cloud/docker/mode/blockio.pm b/cloud/docker/mode/blockio.pm index 2e7691eb2..dfee0ea62 100644 --- a/cloud/docker/mode/blockio.pm +++ b/cloud/docker/mode/blockio.pm @@ -18,7 +18,7 @@ # limitations under the License. # -package apps::docker::mode::blockio; +package docker::docker::mode::blockio; use base qw(centreon::plugins::mode); diff --git a/cloud/docker/mode/containerstate.pm b/cloud/docker/mode/containerstate.pm index 0f645666e..08b08fb5f 100644 --- a/cloud/docker/mode/containerstate.pm +++ b/cloud/docker/mode/containerstate.pm @@ -18,7 +18,7 @@ # limitations under the License. # -package apps::docker::mode::containerstate; +package cloud::docker::mode::containerstate; use base qw(centreon::plugins::mode); diff --git a/cloud/docker/mode/cpu.pm b/cloud/docker/mode/cpu.pm index ca540a443..51bbaa9b7 100644 --- a/cloud/docker/mode/cpu.pm +++ b/cloud/docker/mode/cpu.pm @@ -18,7 +18,7 @@ # limitations under the License. # -package apps::docker::mode::cpu; +package cloud::docker::mode::cpu; use base qw(centreon::plugins::mode); diff --git a/cloud/docker/mode/image.pm b/cloud/docker/mode/image.pm index 78417eb6c..8ed00d3cc 100644 --- a/cloud/docker/mode/image.pm +++ b/cloud/docker/mode/image.pm @@ -18,7 +18,7 @@ # limitations under the License. # -package apps::docker::mode::image; +package cloud::docker::mode::image; use base qw(centreon::plugins::mode); diff --git a/cloud/docker/mode/info.pm b/cloud/docker/mode/info.pm index bdd0f062e..d9492d2db 100644 --- a/cloud/docker/mode/info.pm +++ b/cloud/docker/mode/info.pm @@ -18,7 +18,7 @@ # limitations under the License. # -package apps::docker::mode::info; +package cloud::docker::mode::info; use base qw(centreon::plugins::mode); diff --git a/cloud/docker/mode/listcontainers.pm b/cloud/docker/mode/listcontainers.pm index 500879390..f05359daf 100644 --- a/cloud/docker/mode/listcontainers.pm +++ b/cloud/docker/mode/listcontainers.pm @@ -18,7 +18,7 @@ # limitations under the License. # -package apps::docker::mode::listcontainers; +package cloud::docker::mode::listcontainers; use base qw(centreon::plugins::mode); diff --git a/cloud/docker/mode/memory.pm b/cloud/docker/mode/memory.pm index fcfa6d38e..be36deb5e 100644 --- a/cloud/docker/mode/memory.pm +++ b/cloud/docker/mode/memory.pm @@ -18,7 +18,7 @@ # limitations under the License. # -package apps::docker::mode::memory; +package cloud::docker::mode::memory; use base qw(centreon::plugins::mode); diff --git a/cloud/docker/mode/traffic.pm b/cloud/docker/mode/traffic.pm index 9cda80515..6bfe6a6a1 100644 --- a/cloud/docker/mode/traffic.pm +++ b/cloud/docker/mode/traffic.pm @@ -18,7 +18,7 @@ # limitations under the License. # -package apps::docker::mode::traffic; +package cloud::docker::mode::traffic; use base qw(centreon::plugins::mode); diff --git a/cloud/docker/plugin.pm b/cloud/docker/plugin.pm index 6fcb0c2c2..2aea860a5 100644 --- a/cloud/docker/plugin.pm +++ b/cloud/docker/plugin.pm @@ -18,7 +18,7 @@ # limitations under the License. # -package apps::docker::plugin; +package cloud::docker::plugin; use strict; use warnings; @@ -32,14 +32,14 @@ sub new { $self->{version} = '0.1'; %{$self->{modes}} = ( - 'blockio' => 'apps::docker::mode::blockio', - 'containerstate' => 'apps::docker::mode::containerstate', - 'cpu' => 'apps::docker::mode::cpu', - 'image' => 'apps::docker::mode::image', - 'info' => 'apps::docker::mode::info', - 'list-containers' => 'apps::docker::mode::listcontainers', - 'memory' => 'apps::docker::mode::memory', - 'traffic' => 'apps::docker::mode::traffic', + 'blockio' => ':cloud:docker::mode::blockio', + 'containerstate' => ':cloud:docker::mode::containerstate', + 'cpu' => ':cloud:docker::mode::cpu', + 'image' => ':cloud:docker::mode::image', + 'info' => ':cloud:docker::mode::info', + 'list-containers' => ':cloud:docker::mode::listcontainers', + 'memory' => ':cloud:docker::mode::memory', + 'traffic' => ':cloud:docker::mode::traffic', ); return $self; } From 76bcfc475cdd138986344f8b3febce9a7273a874 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Mon, 10 Aug 2015 14:08:32 +0200 Subject: [PATCH 25/27] fix typo --- cloud/docker/mode/blockio.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cloud/docker/mode/blockio.pm b/cloud/docker/mode/blockio.pm index dfee0ea62..f74ed86c2 100644 --- a/cloud/docker/mode/blockio.pm +++ b/cloud/docker/mode/blockio.pm @@ -172,8 +172,8 @@ sub run { my $delta_read_bytes = $read_bytes - $old_read_bytes; my $delta_write_bytes = $write_bytes - $old_write_bytes; - my $read_absolute_per_sec = $delta_read_bits / $time_delta; - my $write_absolute_per_sec = $delta_write_bits / $time_delta; + my $read_absolute_per_sec = $delta_read_bytes / $time_delta; + my $write_absolute_per_sec = $delta_write_bytes / $time_delta; my $exit1 = $self->{perfdata}->threshold_check(value => $read_absolute_per_sec, threshold => [ { label => 'critical-read', 'exit_litteral' => 'critical' }, { label => 'warning-read', exit_litteral => 'warning' } ]); my $exit2 = $self->{perfdata}->threshold_check(value => $write_absolute_per_sec, threshold => [ { label => 'critical-write', 'exit_litteral' => 'critical' }, { label => 'warning-write', exit_litteral => 'warning' } ]); From abbb32f2f0d1b4f5ade7bf2a4dc6306e0ba0b1a6 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Mon, 10 Aug 2015 14:35:04 +0200 Subject: [PATCH 26/27] fix sed bad typo --- cloud/docker/plugin.pm | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cloud/docker/plugin.pm b/cloud/docker/plugin.pm index 2aea860a5..0f57d0112 100644 --- a/cloud/docker/plugin.pm +++ b/cloud/docker/plugin.pm @@ -32,14 +32,14 @@ sub new { $self->{version} = '0.1'; %{$self->{modes}} = ( - 'blockio' => ':cloud:docker::mode::blockio', - 'containerstate' => ':cloud:docker::mode::containerstate', - 'cpu' => ':cloud:docker::mode::cpu', - 'image' => ':cloud:docker::mode::image', - 'info' => ':cloud:docker::mode::info', - 'list-containers' => ':cloud:docker::mode::listcontainers', - 'memory' => ':cloud:docker::mode::memory', - 'traffic' => ':cloud:docker::mode::traffic', + 'blockio' => 'cloud::docker::mode::blockio', + 'containerstate' => 'cloud::docker::mode::containerstate', + 'cpu' => 'cloud::docker::mode::cpu', + 'image' => 'cloud::docker::mode::image', + 'info' => 'cloud::docker::mode::info', + 'list-containers' => 'cloud::docker::mode::listcontainers', + 'memory' => 'cloud::docker::mode::memory', + 'traffic' => 'cloud::docker::mode::traffic', ); return $self; } From da581a30514c8ac0e6699582b3c36a2d39ddd7d9 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Mon, 10 Aug 2015 14:39:02 +0200 Subject: [PATCH 27/27] change httplib class to http class --- cloud/docker/mode/blockio.pm | 40 +++++++++++----------------- cloud/docker/mode/containerstate.pm | 41 +++++++++++------------------ cloud/docker/mode/cpu.pm | 37 +++++++++++--------------- cloud/docker/mode/image.pm | 37 ++++++++++---------------- cloud/docker/mode/info.pm | 16 +++-------- cloud/docker/mode/listcontainers.pm | 23 ++++++---------- cloud/docker/mode/memory.pm | 35 +++++++++--------------- cloud/docker/mode/traffic.pm | 38 +++++++++++--------------- 8 files changed, 102 insertions(+), 165 deletions(-) diff --git a/cloud/docker/mode/blockio.pm b/cloud/docker/mode/blockio.pm index f74ed86c2..553908a83 100644 --- a/cloud/docker/mode/blockio.pm +++ b/cloud/docker/mode/blockio.pm @@ -18,13 +18,13 @@ # limitations under the License. # -package docker::docker::mode::blockio; +package cloud::docker::mode::blockio; use base qw(centreon::plugins::mode); use strict; use warnings; -use centreon::plugins::httplib; +use centreon::plugins::http; use centreon::plugins::statefile; use JSON; @@ -53,11 +53,11 @@ sub new { "cert-file:s" => { name => 'cert_file' }, "key-file:s" => { name => 'key_file' }, "cacert-file:s" => { name => 'cacert_file' }, - "timeout:s" => { name => 'timeout', default => '3' }, + "timeout:s" => { name => 'timeout' }, }); $self->{statefile_value} = centreon::plugins::statefile->new(%options); - + $self->{http} = centreon::plugins::http->new(output => $self->{output}); return $self; } @@ -65,14 +65,6 @@ sub check_options { my ($self, %options) = @_; $self->SUPER::init(%options); - if (!defined($self->{option_results}->{hostname})) { - $self->{output}->add_option_msg(short_msg => "Please set the hostname option"); - $self->{output}->option_exit(); - } - if ((defined($self->{option_results}->{credentials})) && (!defined($self->{option_results}->{username}) || !defined($self->{option_results}->{password}))) { - $self->{output}->add_option_msg(short_msg => "You need to set --username= and --password= options when --credentials is used"); - $self->{output}->option_exit(); - } if ((defined($self->{option_results}->{name})) && (defined($self->{option_results}->{id}))) { $self->{output}->add_option_msg(short_msg => "Please set the name or id option"); $self->{output}->option_exit(); @@ -98,6 +90,15 @@ sub check_options { $self->{output}->option_exit(); } + $self->{option_results}->{get_param} = []; + push @{$self->{option_results}->{get_param}}, "stream=false"; + if (defined($self->{option_results}->{id})) { + $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{id}."/stats"; + } elsif (defined($self->{option_results}->{name})) { + $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{name}."/stats"; + } + + $self->{http}->set_options(%{$self->{option_results}}); $self->{statefile_value}->check_options(%options); } @@ -107,21 +108,12 @@ sub run { my $new_datas = {}; if (defined($self->{option_results}->{id})) { - $self->{statefile_value}->read(statefile => 'docker_' . $self->{option_results}->{id} . '_' . centreon::plugins::httplib::get_port($self) . '_' . $self->{mode}); + $self->{statefile_value}->read(statefile => 'docker_' . $self->{option_results}->{id} . '_' . $self->{http}->get_port() . '_' . $self->{mode}); } elsif (defined($self->{option_results}->{name})) { - $self->{statefile_value}->read(statefile => 'docker_' . $self->{option_results}->{name} . '_' . centreon::plugins::httplib::get_port($self) . '_' . $self->{mode}); + $self->{statefile_value}->read(statefile => 'docker_' . $self->{option_results}->{name} . '_' . $self->{http}->get_port() . '_' . $self->{mode}); } - my $jsoncontent; - my $query_form_get = { stream => 'false' }; - - if (defined($self->{option_results}->{id})) { - $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{id}."/stats"; - $jsoncontent = centreon::plugins::httplib::connect($self, query_form_get => $query_form_get, connection_exit => 'critical'); - } elsif (defined($self->{option_results}->{name})) { - $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{name}."/stats"; - $jsoncontent = centreon::plugins::httplib::connect($self, query_form_get => $query_form_get, connection_exit => 'critical'); - } + my $jsoncontent = $self->{http}->request(); my $json = JSON->new; diff --git a/cloud/docker/mode/containerstate.pm b/cloud/docker/mode/containerstate.pm index 08b08fb5f..301c46bfd 100644 --- a/cloud/docker/mode/containerstate.pm +++ b/cloud/docker/mode/containerstate.pm @@ -24,7 +24,7 @@ use base qw(centreon::plugins::mode); use strict; use warnings; -use centreon::plugins::httplib; +use centreon::plugins::http; use JSON; my $thresholds = { @@ -59,10 +59,11 @@ sub new { "cert-file:s" => { name => 'cert_file' }, "key-file:s" => { name => 'key_file' }, "cacert-file:s" => { name => 'cacert_file' }, - "timeout:s" => { name => 'timeout', default => '3' }, + "timeout:s" => { name => 'timeout' }, "threshold-overload:s@" => { name => 'threshold_overload' }, }); + $self->{http} = centreon::plugins::http->new(output => $self->{output}); return $self; } @@ -70,16 +71,6 @@ sub check_options { my ($self, %options) = @_; $self->SUPER::init(%options); - if (!defined($self->{option_results}->{hostname})) { - $self->{output}->add_option_msg(short_msg => "Please set the hostname option"); - $self->{output}->option_exit(); - } - - if ((defined($self->{option_results}->{credentials})) && (!defined($self->{option_results}->{username}) || !defined($self->{option_results}->{password}))) { - $self->{output}->add_option_msg(short_msg => "You need to set --username= and --password= options when --credentials is used"); - $self->{output}->option_exit(); - } - if ((defined($self->{option_results}->{name})) && ($self->{option_results}->{name} eq '')) { $self->{output}->add_option_msg(short_msg => "You need to specify the name option"); $self->{output}->option_exit(); @@ -104,6 +95,18 @@ sub check_options { $self->{overload_th}->{$section} = [] if (!defined($self->{overload_th}->{$section})); push @{$self->{overload_th}->{$section}}, {filter => $filter, status => $status}; } + + if (defined($self->{option_results}->{id})) { + $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{id}."/json"; + } elsif (defined($self->{option_results}->{name})) { + $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{name}."/json"; + } else { + $self->{option_results}->{url_path} = "/containers/json"; + $self->{option_results}->{get_param} = []; + push @{$self->{option_results}->{get_param}}, "all=true"; + } + $self->{http}->set_options(%{$self->{option_results}}); + } sub get_severity { @@ -130,19 +133,7 @@ sub get_severity { sub run { my ($self, %options) = @_; - my $jsoncontent; - - if (defined($self->{option_results}->{id})) { - $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{id}."/json"; - $jsoncontent = centreon::plugins::httplib::connect($self, connection_exit => 'critical'); - } elsif (defined($self->{option_results}->{name})) { - $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{name}."/json"; - $jsoncontent = centreon::plugins::httplib::connect($self, connection_exit => 'critical'); - } else { - $self->{option_results}->{url_path} = "/containers/json"; - my $query_form_get = { all => 'true' }; - $jsoncontent = centreon::plugins::httplib::connect($self, query_form_get => $query_form_get, connection_exit => 'critical'); - } + my $jsoncontent = $self->{http}->request(); my $json = JSON->new; diff --git a/cloud/docker/mode/cpu.pm b/cloud/docker/mode/cpu.pm index 51bbaa9b7..74a0506ae 100644 --- a/cloud/docker/mode/cpu.pm +++ b/cloud/docker/mode/cpu.pm @@ -24,7 +24,7 @@ use base qw(centreon::plugins::mode); use strict; use warnings; -use centreon::plugins::httplib; +use centreon::plugins::http; use centreon::plugins::statefile; use JSON; @@ -51,10 +51,11 @@ sub new { "cert-file:s" => { name => 'cert_file' }, "key-file:s" => { name => 'key_file' }, "cacert-file:s" => { name => 'cacert_file' }, - "timeout:s" => { name => 'timeout', default => '3' }, + "timeout:s" => { name => 'timeout' }, }); $self->{statefile_value} = centreon::plugins::statefile->new(%options); + $self->{http} = centreon::plugins::http->new(output => $self->{output}); return $self; } @@ -63,16 +64,6 @@ sub check_options { my ($self, %options) = @_; $self->SUPER::init(%options); - if (!defined($self->{option_results}->{hostname})) { - $self->{output}->add_option_msg(short_msg => "Please set the hostname option"); - $self->{output}->option_exit(); - } - - if ((defined($self->{option_results}->{credentials})) && (!defined($self->{option_results}->{username}) || !defined($self->{option_results}->{password}))) { - $self->{output}->add_option_msg(short_msg => "You need to set --username= and --password= options when --credentials is used"); - $self->{output}->option_exit(); - } - if ((defined($self->{option_results}->{name})) && (defined($self->{option_results}->{id}))) { $self->{output}->add_option_msg(short_msg => "Please set the name or id option"); $self->{output}->option_exit(); @@ -93,25 +84,29 @@ sub check_options { $self->{output}->option_exit(); } + $self->{option_results}->{get_param} = []; + push @{$self->{option_results}->{get_param}}, "stream=false"; + if (defined($self->{option_results}->{id})) { + $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{id}."/stats"; + } elsif (defined($self->{option_results}->{name})) { + $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{name}."/stats"; + } + + $self->{http}->set_options(%{$self->{option_results}}); $self->{statefile_value}->check_options(%options); } sub run { my ($self, %options) = @_; - my $jsoncontent; - my $query_form_get = { stream => 'false' }; - if (defined($self->{option_results}->{id})) { - $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{id}."/stats"; - $jsoncontent = centreon::plugins::httplib::connect($self, query_form_get => $query_form_get, connection_exit => 'critical'); - $self->{statefile_value}->read(statefile => 'docker_' . $self->{option_results}->{id} . '_' . centreon::plugins::httplib::get_port($self) . '_' . $self->{mode}); + $self->{statefile_value}->read(statefile => 'docker_' . $self->{option_results}->{id} . '_' . $self->{http}->get_port() . '_' . $self->{mode}); } elsif (defined($self->{option_results}->{name})) { - $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{name}."/stats"; - $jsoncontent = centreon::plugins::httplib::connect($self, query_form_get => $query_form_get, connection_exit => 'critical'); - $self->{statefile_value}->read(statefile => 'docker_' . $self->{option_results}->{name} . '_' . centreon::plugins::httplib::get_port($self) . '_' . $self->{mode}); + $self->{statefile_value}->read(statefile => 'docker_' . $self->{option_results}->{name} . '_' . $self->{http}->get_port() . '_' . $self->{mode}); } + my $jsoncontent = $self->{http}->request(); + my $json = JSON->new; my $webcontent; diff --git a/cloud/docker/mode/image.pm b/cloud/docker/mode/image.pm index 8ed00d3cc..5f9e18f1c 100644 --- a/cloud/docker/mode/image.pm +++ b/cloud/docker/mode/image.pm @@ -24,7 +24,7 @@ use base qw(centreon::plugins::mode); use strict; use warnings; -use centreon::plugins::httplib; +use centreon::plugins::http; use JSON; sub new { @@ -52,9 +52,10 @@ sub new { "cert-file:s" => { name => 'cert_file' }, "key-file:s" => { name => 'key_file' }, "cacert-file:s" => { name => 'cacert_file' }, - "timeout:s" => { name => 'timeout', default => '3' }, + "timeout:s" => { name => 'timeout' }, }); + $self->{http} = centreon::plugins::http->new(output => $self->{output}); return $self; } @@ -62,16 +63,6 @@ sub check_options { my ($self, %options) = @_; $self->SUPER::init(%options); - if (!defined($self->{option_results}->{hostname})) { - $self->{output}->add_option_msg(short_msg => "Please set the hostname option"); - $self->{output}->option_exit(); - } - - if ((defined($self->{option_results}->{credentials})) && (!defined($self->{option_results}->{username}) || !defined($self->{option_results}->{password}))) { - $self->{output}->add_option_msg(short_msg => "You need to set --username= and --password= options when --credentials is used"); - $self->{output}->option_exit(); - } - if ((defined($self->{option_results}->{name})) && (defined($self->{option_results}->{id}))) { $self->{output}->add_option_msg(short_msg => "Please set the name or id option"); $self->{output}->option_exit(); @@ -96,25 +87,25 @@ sub check_options { $self->{output}->add_option_msg(short_msg => "Please set the registry-proto option"); $self->{output}->option_exit(); } + + if (defined($self->{option_results}->{id})) { + $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{id}."/json"; + } elsif (defined($self->{option_results}->{name})) { + $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{name}."/json"; + } + + $self->{http}->set_options(%{$self->{option_results}}); } sub run { my ($self, %options) = @_; - my ($jsoncontent,$jsoncontent2); + my ($jsoncontent,$jsoncontent2, $webcontent, $webcontent2); - if (defined($self->{option_results}->{id})) { - $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{id}."/json"; - $jsoncontent = centreon::plugins::httplib::connect($self, connection_exit => 'critical'); - } elsif (defined($self->{option_results}->{name})) { - $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{name}."/json"; - $jsoncontent = centreon::plugins::httplib::connect($self, connection_exit => 'critical'); - } + $jsoncontent = $self->{http}->request(); my $json = JSON->new; - my ($webcontent,$webcontent2); - eval { $webcontent = $json->decode($jsoncontent); }; @@ -131,7 +122,7 @@ sub run { $self->{option_results}->{proto} = $self->{option_results}->{registry_proto}; $self->{option_results}->{hostname} = $self->{option_results}->{registry_hostname}; - $jsoncontent2 = centreon::plugins::httplib::connect($self, connection_exit => 'critical'); + $jsoncontent2 = $self->{http}->request(); my $json2 = JSON->new; diff --git a/cloud/docker/mode/info.pm b/cloud/docker/mode/info.pm index d9492d2db..430d031c6 100644 --- a/cloud/docker/mode/info.pm +++ b/cloud/docker/mode/info.pm @@ -24,7 +24,7 @@ use base qw(centreon::plugins::mode); use strict; use warnings; -use centreon::plugins::httplib; +use centreon::plugins::http; use JSON; sub new { @@ -46,9 +46,10 @@ sub new { "cert-file:s" => { name => 'cert_file' }, "key-file:s" => { name => 'key_file' }, "cacert-file:s" => { name => 'cacert_file' }, - "timeout:s" => { name => 'timeout', default => '3' }, + "timeout:s" => { name => 'timeout' }, }); + $self->{http} = centreon::plugins::http->new(output => $self->{output}); return $self; } @@ -56,22 +57,13 @@ sub check_options { my ($self, %options) = @_; $self->SUPER::init(%options); - if (!defined($self->{option_results}->{hostname})) { - $self->{output}->add_option_msg(short_msg => "Please set the hostname option"); - $self->{output}->option_exit(); - } - - if ((defined($self->{option_results}->{credentials})) && (!defined($self->{option_results}->{username}) || !defined($self->{option_results}->{password}))) { - $self->{output}->add_option_msg(short_msg => "You need to set --username= and --password= options when --credentials is used"); - $self->{output}->option_exit(); - } } sub run { my ($self, %options) = @_; - my $jsoncontent = centreon::plugins::httplib::connect($self, connection_exit => 'critical'); + my $jsoncontent = $self->{http}->request();; my $json = JSON->new; diff --git a/cloud/docker/mode/listcontainers.pm b/cloud/docker/mode/listcontainers.pm index f05359daf..5a78fa46a 100644 --- a/cloud/docker/mode/listcontainers.pm +++ b/cloud/docker/mode/listcontainers.pm @@ -24,7 +24,7 @@ use base qw(centreon::plugins::mode); use strict; use warnings; -use centreon::plugins::httplib; +use centreon::plugins::http; use JSON; sub new { @@ -46,9 +46,10 @@ sub new { "cert-file:s" => { name => 'cert_file' }, "key-file:s" => { name => 'key_file' }, "cacert-file:s" => { name => 'cacert_file' }, - "timeout:s" => { name => 'timeout', default => '3' }, + "timeout:s" => { name => 'timeout' }, }); + $self->{http}->set_options(%{$self->{option_results}}); return $self; } @@ -56,25 +57,17 @@ sub check_options { my ($self, %options) = @_; $self->SUPER::init(%options); - if (!defined($self->{option_results}->{hostname})) { - $self->{output}->add_option_msg(short_msg => "Please set the hostname option"); - $self->{output}->option_exit(); - } + $self->{option_results}->{url_path} = $self->{option_results}->{url_path}."containers/json"; + $self->{option_results}->{get_param} = []; + push @{$self->{option_results}->{get_param}}, "all=true"; - if ((defined($self->{option_results}->{credentials})) && (!defined($self->{option_results}->{username}) || !defined($self->{option_results}->{password}))) { - $self->{output}->add_option_msg(short_msg => "You need to set --username= and --password= options when --credentials is used"); - $self->{output}->option_exit(); - } + $self->{http}->set_options(%{$self->{option_results}}) } sub run { my ($self, %options) = @_; - my $jsoncontent; - - $self->{option_results}->{url_path} = $self->{option_results}->{url_path}."containers/json"; - my $query_form_get = { all => 'true' }; - $jsoncontent = centreon::plugins::httplib::connect($self, query_form_get => $query_form_get, connection_exit => 'critical'); + my $jsoncontent = $self->{http}->request(); my $json = JSON->new; diff --git a/cloud/docker/mode/memory.pm b/cloud/docker/mode/memory.pm index be36deb5e..8222a8d53 100644 --- a/cloud/docker/mode/memory.pm +++ b/cloud/docker/mode/memory.pm @@ -24,7 +24,7 @@ use base qw(centreon::plugins::mode); use strict; use warnings; -use centreon::plugins::httplib; +use centreon::plugins::http; use JSON; sub new { @@ -50,7 +50,7 @@ sub new { "cert-file:s" => { name => 'cert_file' }, "key-file:s" => { name => 'key_file' }, "cacert-file:s" => { name => 'cacert_file' }, - "timeout:s" => { name => 'timeout', default => '3' }, + "timeout:s" => { name => 'timeout' }, }); return $self; @@ -60,16 +60,6 @@ sub check_options { my ($self, %options) = @_; $self->SUPER::init(%options); - if (!defined($self->{option_results}->{hostname})) { - $self->{output}->add_option_msg(short_msg => "Please set the hostname option"); - $self->{output}->option_exit(); - } - - if ((defined($self->{option_results}->{credentials})) && (!defined($self->{option_results}->{username}) || !defined($self->{option_results}->{password}))) { - $self->{output}->add_option_msg(short_msg => "You need to set --username= and --password= options when --credentials is used"); - $self->{output}->option_exit(); - } - if ((defined($self->{option_results}->{name})) && (defined($self->{option_results}->{id}))) { $self->{output}->add_option_msg(short_msg => "Please set the name or id option"); $self->{output}->option_exit(); @@ -89,21 +79,22 @@ sub check_options { $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'."); $self->{output}->option_exit(); } + + $self->{option_results}->{get_param} = []; + push @{$self->{option_results}->{get_param}}, "stream=false"; + if (defined($self->{option_results}->{id})) { + $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{id}."/stats"; + } elsif (defined($self->{option_results}->{name})) { + $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{name}."/stats"; + } + + $self->{http}->set_options(%{$self->{option_results}}); } sub run { my ($self, %options) = @_; - my $jsoncontent; - my $query_form_get = { stream => 'false' }; - - if (defined($self->{option_results}->{id})) { - $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{id}."/stats"; - $jsoncontent = centreon::plugins::httplib::connect($self, query_form_get => $query_form_get, connection_exit => 'critical'); - } elsif (defined($self->{option_results}->{name})) { - $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{name}."/stats"; - $jsoncontent = centreon::plugins::httplib::connect($self, query_form_get => $query_form_get, connection_exit => 'critical'); - } + my $jsoncontent = $self->{http}->request(); my $json = JSON->new; diff --git a/cloud/docker/mode/traffic.pm b/cloud/docker/mode/traffic.pm index 6bfe6a6a1..a869e3b46 100644 --- a/cloud/docker/mode/traffic.pm +++ b/cloud/docker/mode/traffic.pm @@ -24,7 +24,7 @@ use base qw(centreon::plugins::mode); use strict; use warnings; -use centreon::plugins::httplib; +use centreon::plugins::http; use centreon::plugins::statefile; use JSON; @@ -53,11 +53,11 @@ sub new { "cert-file:s" => { name => 'cert_file' }, "key-file:s" => { name => 'key_file' }, "cacert-file:s" => { name => 'cacert_file' }, - "timeout:s" => { name => 'timeout', default => '3' }, + "timeout:s" => { name => 'timeout' }, }); $self->{statefile_value} = centreon::plugins::statefile->new(%options); - + $self->{http} = centreon::plugins::http->new(output => $self->{output}); return $self; } @@ -65,14 +65,6 @@ sub check_options { my ($self, %options) = @_; $self->SUPER::init(%options); - if (!defined($self->{option_results}->{hostname})) { - $self->{output}->add_option_msg(short_msg => "Please set the hostname option"); - $self->{output}->option_exit(); - } - if ((defined($self->{option_results}->{credentials})) && (!defined($self->{option_results}->{username}) || !defined($self->{option_results}->{password}))) { - $self->{output}->add_option_msg(short_msg => "You need to set --username= and --password= options when --credentials is used"); - $self->{output}->option_exit(); - } if ((defined($self->{option_results}->{name})) && (defined($self->{option_results}->{id}))) { $self->{output}->add_option_msg(short_msg => "Please set the name or id option"); $self->{output}->option_exit(); @@ -98,6 +90,15 @@ sub check_options { $self->{output}->option_exit(); } + $self->{option_results}->{get_param} = []; + push @{$self->{option_results}->{get_param}}, "stream=false"; + if (defined($self->{option_results}->{id})) { + $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{id}."/stats"; + } elsif (defined($self->{option_results}->{name})) { + $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{name}."/stats"; + } + + $self->{http}->set_options(%{$self->{option_results}}); $self->{statefile_value}->check_options(%options); } @@ -107,21 +108,12 @@ sub run { my $new_datas = {}; if (defined($self->{option_results}->{id})) { - $self->{statefile_value}->read(statefile => 'docker_' . $self->{option_results}->{id} . '_' . centreon::plugins::httplib::get_port($self) . '_' . $self->{mode}); + $self->{statefile_value}->read(statefile => 'docker_' . $self->{option_results}->{id} . '_' . $self->{http}->get_port() . '_' . $self->{mode}); } elsif (defined($self->{option_results}->{name})) { - $self->{statefile_value}->read(statefile => 'docker_' . $self->{option_results}->{name} . '_' . centreon::plugins::httplib::get_port($self) . '_' . $self->{mode}); + $self->{statefile_value}->read(statefile => 'docker_' . $self->{option_results}->{name} . '_' . $self->{http}->get_port() . '_' . $self->{mode}); } - my $jsoncontent; - my $query_form_get = { stream => 'false' }; - - if (defined($self->{option_results}->{id})) { - $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{id}."/stats"; - $jsoncontent = centreon::plugins::httplib::connect($self, query_form_get => $query_form_get, connection_exit => 'critical'); - } elsif (defined($self->{option_results}->{name})) { - $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{name}."/stats"; - $jsoncontent = centreon::plugins::httplib::connect($self, query_form_get => $query_form_get, connection_exit => 'critical'); - } + my $jsoncontent = $self->{http}->request(); my $json = JSON->new;