add name option

This commit is contained in:
Shini31 2015-07-20 19:08:22 +02:00
parent 2436ef00a3
commit 18b9b6bc1a
1 changed files with 101 additions and 4 deletions

View File

@ -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