centreon-plugins/cloud/docker/mode/image.pm

226 lines
6.2 KiB
Perl
Raw Normal View History

2015-07-21 18:28:36 +02:00
#
2016-01-20 19:09:18 +01:00
# Copyright 2016 Centreon (http://www.centreon.com/)
2015-07-21 18:28:36 +02:00
#
2015-08-10 11:12:16 +02:00
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
2015-07-21 18:28:36 +02:00
#
2015-08-10 11:12:16 +02:00
# 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
2015-07-21 18:28:36 +02:00
#
2015-08-10 11:12:16 +02:00
# http://www.apache.org/licenses/LICENSE-2.0
2015-07-21 18:28:36 +02:00
#
2015-08-10 11:12:16 +02:00
# 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.
2015-07-21 18:28:36 +02:00
#
2015-08-10 13:54:20 +02:00
package cloud::docker::mode::image;
2015-07-21 18:28:36 +02:00
use base qw(centreon::plugins::mode);
use strict;
use warnings;
2015-08-10 14:39:02 +02:00
use centreon::plugins::http;
2015-07-21 18:28:36 +02:00
use JSON;
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
2016-08-12 18:56:57 +02:00
$self->{version} = '1.1';
2015-07-21 18:28:36 +02:00
$options{options}->add_options(arguments =>
{
2016-08-12 18:56:57 +02:00
"port:s" => { name => 'port' },
2015-07-21 18:28:36 +02:00
"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' },
2015-08-10 14:39:02 +02:00
"timeout:s" => { name => 'timeout' },
2015-07-21 18:28:36 +02:00
});
2015-08-10 14:39:02 +02:00
$self->{http} = centreon::plugins::http->new(output => $self->{output});
2015-07-21 18:28:36 +02:00
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();
}
2015-08-10 14:39:02 +02:00
$self->{http}->set_options(%{$self->{option_results}});
}
2015-07-21 18:28:36 +02:00
2015-08-10 14:39:02 +02:00
sub run {
my ($self, %options) = @_;
2016-08-12 18:56:57 +02:00
my ($jsoncontent, $webcontent, $webcontent2);
2015-07-21 18:28:36 +02:00
2016-08-12 18:56:57 +02:00
my $urlpath;
if (defined($self->{option_results}->{id})) {
$urlpath = "/containers/".$self->{option_results}->{id}."/stats";
} elsif (defined($self->{option_results}->{name})) {
$urlpath = "/containers/".$self->{option_results}->{name}."/stats";
2015-07-21 18:28:36 +02:00
}
2016-08-12 18:56:57 +02:00
my $port = $self->{option_results}->{port};
my $containerapi = $options{custom};
$webcontent = $containerapi->api_request(urlpath => $urlpath,
port => $port);
2015-07-21 18:28:36 +02:00
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};
2015-08-10 15:44:11 +02:00
$self->{http}->set_options(%{$self->{option_results}});
2015-07-21 18:28:36 +02:00
2016-08-12 18:56:57 +02:00
$jsoncontent = $self->{http}->request();
2015-07-21 18:28:36 +02:00
my $json2 = JSON->new;
eval {
2016-08-12 18:56:57 +02:00
$webcontent2 = $json2->decode($jsoncontent);
2015-07-21 18:28:36 +02:00
};
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
2015-07-24 15:27:20 +02:00
Check Container's image viability with a registry
2015-07-21 18:28:36 +02:00
2016-08-12 18:56:57 +02:00
=head2 DOCKER OPTIONS
2015-07-21 18:28:36 +02:00
=item B<--port>
2016-08-12 18:56:57 +02:00
Port used by Docker
2015-07-21 18:28:36 +02:00
=item B<--id>
Specify the container's id
=item B<--name>
Specify the container's name
2016-08-12 18:56:57 +02:00
=head2 MODE OPTIONS
2015-07-21 18:28:36 +02:00
=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