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

155 lines
4.7 KiB
Perl
Raw Normal View History

2015-07-17 09:59:07 +02:00
#
2016-01-20 19:09:18 +01:00
# Copyright 2016 Centreon (http://www.centreon.com/)
2015-07-17 09:59:07 +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-17 09:59:07 +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-17 09:59:07 +02:00
#
2015-08-10 11:12:16 +02:00
# http://www.apache.org/licenses/LICENSE-2.0
2015-07-17 09:59:07 +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-17 09:59:07 +02:00
#
2015-08-10 13:54:20 +02:00
package cloud::docker::mode::listcontainers;
2015-07-17 09:59:07 +02:00
use base qw(centreon::plugins::mode);
use strict;
use warnings;
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.2';
2015-07-17 09:59:07 +02:00
$options{options}->add_options(arguments =>
{
2016-08-12 18:56:57 +02:00
"port:s" => { name => 'port' },
"exclude:s" => { name => 'exclude' },
2015-07-17 09:59:07 +02:00
});
$self->{container_infos} = ();
2016-08-12 18:56:57 +02:00
2015-07-17 09:59:07 +02:00
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
}
sub check_exclude {
my ($self, %options) = @_;
if (defined($self->{option_results}->{exclude}) && $self->{option_results}->{exclude} =~ /(^|\s|,)${options{status}}(\s|,|$)/) {
$self->{output}->output_add(long_msg => sprintf("Skipping ${options{status}} container."));
return 1;
}
return 0;
}
2016-08-12 18:56:57 +02:00
sub listcontainer_request {
2015-07-17 09:59:07 +02:00
my ($self, %options) = @_;
2016-08-12 18:56:57 +02:00
my $urlpath = "/containers/json";
my $port = $self->{option_results}->{port};
my $containerapi = $options{custom};
2015-07-17 09:59:07 +02:00
2016-08-12 18:56:57 +02:00
my $webcontent = $containerapi->api_request(urlpath => $urlpath,
port => $port);
2015-07-17 09:59:07 +02:00
foreach my $val (@$webcontent) {
my $containerstate;
if (($val->{Status} =~ m/^Up/) && ($val->{Status} =~ m/^(?:(?!Paused).)*$/)) {
return if ($self->check_exclude(status => 'Running'));
$containerstate = 'Running';
} elsif ($val->{Status} =~ m/^Exited/) {
return if ($self->check_exclude(status => 'Exited'));
$containerstate = 'Exited';
} elsif ($val->{Status} =~ m/\(Paused\)$/) {
return if ($self->check_exclude(status => 'Paused'));
$containerstate = 'Paused';
}
2015-07-17 09:59:07 +02:00
my $containername = $val->{Names}->[0];
$containername =~ s/^\///;
$self->{container_infos}->{$containername}->{id} = $val->{Id};
$self->{container_infos}->{$containername}->{image} = $val->{Image};
$self->{container_infos}->{$containername}->{state} = $containerstate;
}
}
sub disco_format {
my ($self, %options) = @_;
my $names = ['name', 'id', 'image', 'state'];
$self->{output}->add_disco_format(elements => $names);
}
sub disco_show {
my ($self, %options) = @_;
2016-08-12 18:56:57 +02:00
$self->listcontainer_request(%options);
foreach my $containername (keys %{$self->{container_infos}}) {
$self->{output}->add_disco_entry(name => $containername,
id => $self->{container_infos}->{$containername}->{id},
image => $self->{container_infos}->{$containername}->{image},
state => $self->{container_infos}->{$containername}->{state},
);
}
}
sub run {
my ($self, %options) = @_;
2016-08-12 18:56:57 +02:00
$self->listcontainer_request(%options);
foreach my $containername (keys %{$self->{container_infos}}) {
2015-07-24 15:21:07 +02:00
$self->{output}->output_add(long_msg => sprintf("%s [id = %s , image = %s, state = %s]",
2015-09-14 11:36:03 +02:00
$containername,
$self->{container_infos}->{$containername}->{id},
$self->{container_infos}->{$containername}->{image},
$self->{container_infos}->{$containername}->{state}));
2015-07-17 09:59:07 +02:00
}
2015-07-17 09:59:07 +02:00
$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
2015-07-17 11:53:37 +02:00
List Docker containers
2015-07-17 09:59:07 +02:00
2016-08-12 18:56:57 +02:00
=head2 DOCKER OPTIONS
2015-07-17 09:59:07 +02:00
=item B<--port>
2016-08-12 18:56:57 +02:00
Port used by Docker
2015-07-17 09:59:07 +02:00
2016-08-12 18:56:57 +02:00
=head2 MODE OPTIONS
2015-07-17 11:53:37 +02:00
=item B<--exlude>
Exclude specific container's state (comma seperated list) (Example: --exclude=Paused,Running)
2015-07-17 09:59:07 +02:00
=back
=cut