enhance xtremio to manage cluster xms

This commit is contained in:
qgarnier 2017-12-06 13:49:14 +01:00
parent f51485377e
commit 73f4c9f25f
1 changed files with 92 additions and 27 deletions

View File

@ -23,16 +23,13 @@ package storage::emc::xtremio::restapi::custom::xtremioapi;
use strict; use strict;
use warnings; use warnings;
use centreon::plugins::http; use centreon::plugins::http;
use centreon::plugins::statefile;
use JSON; use JSON;
sub new { sub new {
my ($class, %options) = @_; my ($class, %options) = @_;
my $self = {}; my $self = {};
bless $self, $class; bless $self, $class;
# $options{options} = options object
# $options{output} = output object
# $options{exit_value} = integer
# $options{noptions} = integer
if (!defined($options{output})) { if (!defined($options{output})) {
print "Class Custom: Need to specify 'output' argument.\n"; print "Class Custom: Need to specify 'output' argument.\n";
@ -46,11 +43,12 @@ sub new {
if (!defined($options{noptions})) { if (!defined($options{noptions})) {
$options{options}->add_options(arguments => $options{options}->add_options(arguments =>
{ {
"hostname:s@" => { name => 'hostname', }, "hostname:s@" => { name => 'hostname', },
"xtremio-username:s@" => { name => 'xtremio_username', }, "xtremio-username:s@" => { name => 'xtremio_username', },
"xtremio-password:s@" => { name => 'xtremio_password', }, "xtremio-password:s@" => { name => 'xtremio_password', },
"proxyurl:s@" => { name => 'proxyurl', }, "proxyurl:s@" => { name => 'proxyurl', },
"timeout:s@" => { name => 'timeout', }, "timeout:s@" => { name => 'timeout', },
"reload-cache-time:s" => { name => 'reload_cache_time' },
}); });
} }
$options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1); $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
@ -58,25 +56,21 @@ sub new {
$self->{output} = $options{output}; $self->{output} = $options{output};
$self->{mode} = $options{mode}; $self->{mode} = $options{mode};
$self->{http} = centreon::plugins::http->new(output => $self->{output}); $self->{http} = centreon::plugins::http->new(output => $self->{output});
$self->{statefile_cache_cluster} = centreon::plugins::statefile->new(%options);
return $self; return $self;
} }
# Method to manage multiples
sub set_options { sub set_options {
my ($self, %options) = @_; my ($self, %options) = @_;
# options{options_result}
$self->{option_results} = $options{option_results}; $self->{option_results} = $options{option_results};
} }
# Method to manage multiples
sub set_defaults { sub set_defaults {
my ($self, %options) = @_; my ($self, %options) = @_;
# options{default}
# Manage default value
foreach (keys %{$options{default}}) { foreach (keys %{$options{default}}) {
if ($_ eq $self->{mode}) { if ($_ eq $self->{mode}) {
for (my $i = 0; $i < scalar(@{$options{default}->{$_}}); $i++) { for (my $i = 0; $i < scalar(@{$options{default}->{$_}}); $i++) {
@ -92,14 +86,13 @@ sub set_defaults {
sub check_options { sub check_options {
my ($self, %options) = @_; my ($self, %options) = @_;
# # return 1 = ok still hostname
# # return 0 = no hostname left
$self->{hostname} = (defined($self->{option_results}->{hostname})) ? shift(@{$self->{option_results}->{hostname}}) : undef; $self->{hostname} = (defined($self->{option_results}->{hostname})) ? shift(@{$self->{option_results}->{hostname}}) : undef;
$self->{xtremio_username} = (defined($self->{option_results}->{xtremio_username})) ? shift(@{$self->{option_results}->{xtremio_username}}) : ''; $self->{xtremio_username} = (defined($self->{option_results}->{xtremio_username})) ? shift(@{$self->{option_results}->{xtremio_username}}) : '';
$self->{xtremio_password} = (defined($self->{option_results}->{xtremio_password})) ? shift(@{$self->{option_results}->{xtremio_password}}) : ''; $self->{xtremio_password} = (defined($self->{option_results}->{xtremio_password})) ? shift(@{$self->{option_results}->{xtremio_password}}) : '';
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? shift(@{$self->{option_results}->{timeout}}) : 10; $self->{timeout} = (defined($self->{option_results}->{timeout})) ? shift(@{$self->{option_results}->{timeout}}) : 10;
$self->{proxyurl} = (defined($self->{option_results}->{proxyurl})) ? shift(@{$self->{option_results}->{proxyurl}}) : undef; $self->{proxyurl} = (defined($self->{option_results}->{proxyurl})) ? shift(@{$self->{option_results}->{proxyurl}}) : undef;
$self->{reload_cache_time} = (defined($self->{option_results}->{reload_cache_time})) ? shift(@{$self->{option_results}->{reload_cache_time}}) : 180;
if (!defined($self->{hostname})) { if (!defined($self->{hostname})) {
$self->{output}->add_option_msg(short_msg => "Need to specify hostname option."); $self->{output}->add_option_msg(short_msg => "Need to specify hostname option.");
@ -113,6 +106,7 @@ sub check_options {
if (!defined($self->{hostname}) || if (!defined($self->{hostname}) ||
scalar(@{$self->{option_results}->{hostname}}) == 0) { scalar(@{$self->{option_results}->{hostname}}) == 0) {
$self->{statefile_cache_cluster}->check_options(option_results => $self->{option_results});
return 0; return 0;
} }
return 1; return 1;
@ -136,6 +130,26 @@ sub settings {
$self->{http}->set_options(%{$self->{option_results}}); $self->{http}->set_options(%{$self->{option_results}});
} }
sub cache_clusters {
my ($self, %options) = @_;
my $has_cache_file = $self->{statefile_cache_cluster}->read(statefile => 'cache_xtremio_clusters_' . $self->{hostname} . '_' . $self->{port});
my $timestamp_cache = $self->{statefile_cache_cluster}->get(name => 'last_timestamp');
my $clusters = $self->{statefile_cache_cluster}->get(name => 'clusters');
if ($has_cache_file == 0 || !defined($timestamp_cache) || ((time() - $timestamp_cache) > (($self->{reload_cache_time}) * 60))) {
$clusters = {};
my $datas = { last_timestamp => time(), clusters => $clusters };
my @items = $self->get_items(url => '/api/json/types/',
obj => 'clusters');
foreach (@items) {
$clusters->{$_} = 1;
}
$self->{statefile_cache_cluster}->write(data => $datas);
}
return $clusters;
}
sub get_items { sub get_items {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -157,28 +171,74 @@ sub get_items {
my @items; my @items;
foreach my $context (@{$decoded->{$options{obj}}}) { foreach my $context (@{$decoded->{$options{obj}}}) {
push @items,$context->{name}; push @items, $context->{name};
} }
return @items; return @items;
} }
sub get_details_data {
my ($self, %options) = @_;
my $response = $self->{http}->request(url_path => $options{url},
critical_status => '', warning_status => '', unknown_status => '');
my $decoded;
eval {
$decoded = decode_json($response);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response");
$self->{output}->option_exit();
}
return $decoded;
}
sub get_details_lookup_clusters {
my ($self, %options) = @_;
#Message if object not found:
#{
# "message": "obj_not_found",
# "error_code": 400
#}
#
my $clusters = $self->cache_clusters();
foreach my $cluster_name (keys %$clusters) {
my $url = $options{url} . $options{append} . 'cluster-name=' . $cluster_name;
my $decoded = $self->get_details_data(url => $url);
return $decoded if (!defined($decoded->{error_code}));
}
# object is not found.
$self->{output}->add_option_msg(short_msg => "xtremio api issue: cannot found object details");
$self->{output}->option_exit();
}
sub get_details { sub get_details {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->settings(); $self->settings();
my $append = '?';
if ((defined($options{obj}) && $options{obj} ne '') && (defined($options{name}) && $options{name} ne '')) { if ((defined($options{obj}) && $options{obj} ne '') && (defined($options{name}) && $options{name} ne '')) {
$options{url} .= $options{obj} . '/?name=' . $options{name} ; $options{url} .= $options{obj} . '/?name=' . $options{name};
} $append = '&';
}
my $response = $self->{http}->request(url_path => $options{url}); #Message when cluster id needed:
my $decoded; #{
eval { # "message": "cluster_id_is_required",
$decoded = decode_json($response); # "error_code": 400
}; #}
if ($@) { #
$self->{output}->add_option_msg(short_msg => "Cannot decode json response"); my $decoded = $self->get_details_data(%options);
if (defined($decoded->{error_code}) &&
($decoded->{error_code} == 400 && $decoded->{message} eq 'cluster_id_is_required')) {
$decoded = $self->get_details_lookup_clusters(%options, append => $append);
} elsif (defined($decoded->{error_code})) {
$self->{output}->add_option_msg(short_msg => "xtremio api issue: $decoded->{message}");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
@ -222,6 +282,11 @@ Proxy URL if any
Set HTTP timeout Set HTTP timeout
=item B<--reload-cache-time>
Time in seconds before reloading cache file (default: 180).
The cache is used when XMS manages multiple clusters.
=back =back
=head1 DESCRIPTION =head1 DESCRIPTION