diff --git a/cloud/docker/mode/blockio.pm b/cloud/docker/mode/blockio.pm new file mode 100644 index 000000000..553908a83 --- /dev/null +++ b/cloud/docker/mode/blockio.pm @@ -0,0 +1,283 @@ +# +# Copyright 2015 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package cloud::docker::mode::blockio; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::http; +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' }, + }); + + $self->{statefile_value} = centreon::plugins::statefile->new(%options); + $self->{http} = centreon::plugins::http->new(output => $self->{output}); + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + 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->{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 $new_datas = {}; + + if (defined($self->{option_results}->{id})) { + $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} . '_' . $self->{http}->get_port() . '_' . $self->{mode}); + } + + my $jsoncontent = $self->{http}->request(); + + 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'); + + # First execution + 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_bytes = $read_bytes - $old_read_bytes; + my $delta_write_bytes = $write_bytes - $old_write_bytes; + 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' } ]); + + 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 diff --git a/cloud/docker/mode/containerstate.pm b/cloud/docker/mode/containerstate.pm new file mode 100644 index 000000000..301c46bfd --- /dev/null +++ b/cloud/docker/mode/containerstate.pm @@ -0,0 +1,297 @@ +# +# Copyright 2015 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package cloud::docker::mode::containerstate; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::http; +use JSON; + +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 => '/' }, + "name:s" => { name => 'name' }, + "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' }, + "threshold-overload:s@" => { name => 'threshold_overload' }, + }); + + $self->{http} = centreon::plugins::http->new(output => $self->{output}); + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + 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(); + } + + $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}; + } + + 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 { + 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 = $self->{http}->request(); + + 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,$containertime); + my $exit = 'OK'; + + 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}; + $containername =~ s/^\///; + 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; + } + } + + $exit = $self->get_severity(section => 'state', value => $result); + $self->{output}->output_add(severity => $exit, + 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")); + + my ($nbrunning,$nbpaused,$nbexited) = '0'; + + foreach my $val (@$webcontent) { + $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++; + } 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 (!$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")); + } + $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<--name> + +Specify one container's name + +=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: section,status,regexp) +It used before default thresholds (order stays). +Example: --threshold-overload='state,CRITICAL,^(?!(Paused)$)' + +=back + +=cut diff --git a/cloud/docker/mode/cpu.pm b/cloud/docker/mode/cpu.pm new file mode 100644 index 000000000..74a0506ae --- /dev/null +++ b/cloud/docker/mode/cpu.pm @@ -0,0 +1,250 @@ +# +# Copyright 2015 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package cloud::docker::mode::cpu; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::http; +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: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' }, + }); + + $self->{statefile_value} = centreon::plugins::statefile->new(%options); + $self->{http} = centreon::plugins::http->new(output => $self->{output}); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + 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->{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) = @_; + + if (defined($self->{option_results}->{id})) { + $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} . '_' . $self->{http}->get_port() . '_' . $self->{mode}); + } + + my $jsoncontent = $self->{http}->request(); + + 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 = {}; + $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; + } + 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); + + $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 diff --git a/cloud/docker/mode/image.pm b/cloud/docker/mode/image.pm new file mode 100644 index 000000000..5f9e18f1c --- /dev/null +++ b/cloud/docker/mode/image.pm @@ -0,0 +1,244 @@ +# +# Copyright 2015 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package cloud::docker::mode::image; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::http; +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' }, + }); + + $self->{http} = centreon::plugins::http->new(output => $self->{output}); + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + 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(); + } + + 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, $webcontent, $webcontent2); + + $jsoncontent = $self->{http}->request(); + + my $json = JSON->new; + + 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 = $self->{http}->request(); + + 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 with a registry + +=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/cloud/docker/mode/info.pm b/cloud/docker/mode/info.pm new file mode 100644 index 000000000..430d031c6 --- /dev/null +++ b/cloud/docker/mode/info.pm @@ -0,0 +1,176 @@ +# +# Copyright 2015 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package cloud::docker::mode::info; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::http; +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' }, + "key-file:s" => { name => 'key_file' }, + "cacert-file:s" => { name => 'cacert_file' }, + "timeout:s" => { name => 'timeout' }, + }); + + $self->{http} = centreon::plugins::http->new(output => $self->{output}); + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + +} + +sub run { + my ($self, %options) = @_; + + + my $jsoncontent = $self->{http}->request();; + + 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 Docker information + +=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 information (Default: '/') + +=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/cloud/docker/mode/listcontainers.pm b/cloud/docker/mode/listcontainers.pm new file mode 100644 index 000000000..5a78fa46a --- /dev/null +++ b/cloud/docker/mode/listcontainers.pm @@ -0,0 +1,169 @@ +# +# Copyright 2015 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package cloud::docker::mode::listcontainers; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::http; +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' }, + "key-file:s" => { name => 'key_file' }, + "cacert-file:s" => { name => 'cacert_file' }, + "timeout:s" => { name => 'timeout' }, + }); + + $self->{http}->set_options(%{$self->{option_results}}); + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + $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"; + + $self->{http}->set_options(%{$self->{option_results}}) +} + +sub run { + my ($self, %options) = @_; + + my $jsoncontent = $self->{http}->request(); + + 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 $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:'); + + $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); + $self->{output}->exit(); + +} + +1; + +__END__ + +=head1 MODE + +List Docker containers + +=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 containers (Default: '/') + +=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/cloud/docker/mode/memory.pm b/cloud/docker/mode/memory.pm new file mode 100644 index 000000000..8222a8d53 --- /dev/null +++ b/cloud/docker/mode/memory.pm @@ -0,0 +1,219 @@ +# +# Copyright 2015 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package cloud::docker::mode::memory; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::http; +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' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + 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->{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 = $self->{http}->request(); + + 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', 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}, + ); + + $self->{output}->display(); + $self->{output}->exit(); + +} + +1; + +__END__ + +=head1 MODE + +Check Container's memory 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 diff --git a/cloud/docker/mode/traffic.pm b/cloud/docker/mode/traffic.pm new file mode 100644 index 000000000..a869e3b46 --- /dev/null +++ b/cloud/docker/mode/traffic.pm @@ -0,0 +1,282 @@ +# +# Copyright 2015 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package cloud::docker::mode::traffic; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::http; +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' }, + }); + + $self->{statefile_value} = centreon::plugins::statefile->new(%options); + $self->{http} = centreon::plugins::http->new(output => $self->{output}); + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + 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->{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 $new_datas = {}; + + if (defined($self->{option_results}->{id})) { + $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} . '_' . $self->{http}->get_port() . '_' . $self->{mode}); + } + + my $jsoncontent = $self->{http}->request(); + + 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 diff --git a/cloud/docker/plugin.pm b/cloud/docker/plugin.pm new file mode 100644 index 000000000..0f57d0112 --- /dev/null +++ b/cloud/docker/plugin.pm @@ -0,0 +1,56 @@ +# +# Copyright 2015 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package cloud::docker::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' => '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; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check Docker and containers through its API. +Requirements: Docker 1.7.1+ and Docker API 1.19+ + +=cut