From 73f4c9f25f90cc93628b83620fda0b5ba4616008 Mon Sep 17 00:00:00 2001 From: qgarnier Date: Wed, 6 Dec 2017 13:49:14 +0100 Subject: [PATCH] enhance xtremio to manage cluster xms --- .../emc/xtremio/restapi/custom/xtremioapi.pm | 119 ++++++++++++++---- 1 file changed, 92 insertions(+), 27 deletions(-) diff --git a/storage/emc/xtremio/restapi/custom/xtremioapi.pm b/storage/emc/xtremio/restapi/custom/xtremioapi.pm index 5ddc0815c..0fd5c04cf 100644 --- a/storage/emc/xtremio/restapi/custom/xtremioapi.pm +++ b/storage/emc/xtremio/restapi/custom/xtremioapi.pm @@ -23,16 +23,13 @@ package storage::emc::xtremio::restapi::custom::xtremioapi; use strict; use warnings; use centreon::plugins::http; +use centreon::plugins::statefile; use JSON; sub new { my ($class, %options) = @_; my $self = {}; bless $self, $class; - # $options{options} = options object - # $options{output} = output object - # $options{exit_value} = integer - # $options{noptions} = integer if (!defined($options{output})) { print "Class Custom: Need to specify 'output' argument.\n"; @@ -46,11 +43,12 @@ sub new { if (!defined($options{noptions})) { $options{options}->add_options(arguments => { - "hostname:s@" => { name => 'hostname', }, - "xtremio-username:s@" => { name => 'xtremio_username', }, - "xtremio-password:s@" => { name => 'xtremio_password', }, - "proxyurl:s@" => { name => 'proxyurl', }, - "timeout:s@" => { name => 'timeout', }, + "hostname:s@" => { name => 'hostname', }, + "xtremio-username:s@" => { name => 'xtremio_username', }, + "xtremio-password:s@" => { name => 'xtremio_password', }, + "proxyurl:s@" => { name => 'proxyurl', }, + "timeout:s@" => { name => 'timeout', }, + "reload-cache-time:s" => { name => 'reload_cache_time' }, }); } $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1); @@ -58,25 +56,21 @@ sub new { $self->{output} = $options{output}; $self->{mode} = $options{mode}; $self->{http} = centreon::plugins::http->new(output => $self->{output}); + $self->{statefile_cache_cluster} = centreon::plugins::statefile->new(%options); return $self; } -# Method to manage multiples sub set_options { my ($self, %options) = @_; - # options{options_result} $self->{option_results} = $options{option_results}; } -# Method to manage multiples sub set_defaults { my ($self, %options) = @_; - # options{default} - - # Manage default value + foreach (keys %{$options{default}}) { if ($_ eq $self->{mode}) { for (my $i = 0; $i < scalar(@{$options{default}->{$_}}); $i++) { @@ -92,14 +86,13 @@ sub set_defaults { sub check_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->{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->{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->{reload_cache_time} = (defined($self->{option_results}->{reload_cache_time})) ? shift(@{$self->{option_results}->{reload_cache_time}}) : 180; if (!defined($self->{hostname})) { $self->{output}->add_option_msg(short_msg => "Need to specify hostname option."); @@ -113,6 +106,7 @@ sub check_options { if (!defined($self->{hostname}) || scalar(@{$self->{option_results}->{hostname}}) == 0) { + $self->{statefile_cache_cluster}->check_options(option_results => $self->{option_results}); return 0; } return 1; @@ -136,6 +130,26 @@ sub settings { $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 { my ($self, %options) = @_; @@ -157,28 +171,74 @@ sub get_items { my @items; foreach my $context (@{$decoded->{$options{obj}}}) { - push @items,$context->{name}; + push @items, $context->{name}; } 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 { my ($self, %options) = @_; $self->settings(); + my $append = '?'; 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}); - my $decoded; - eval { - $decoded = decode_json($response); - }; - if ($@) { - $self->{output}->add_option_msg(short_msg => "Cannot decode json response"); + #Message when cluster id needed: + #{ + # "message": "cluster_id_is_required", + # "error_code": 400 + #} + # + 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(); } @@ -222,6 +282,11 @@ Proxy URL if any 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 =head1 DESCRIPTION