| 1 |
# |
| 2 |
# Copyright 2024 Centreon (http://www.centreon.com/) |
| 3 |
# |
| 4 |
# Centreon is a full-fledged industry-strength solution that meets |
| 5 |
# the needs in IT infrastructure and application monitoring for |
| 6 |
# service performance. |
| 7 |
# |
| 8 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 |
# you may not use this file except in compliance with the License. |
| 10 |
# You may obtain a copy of the License at |
| 11 |
# |
| 12 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
# |
| 14 |
# Unless required by applicable law or agreed to in writing, software |
| 15 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 |
# See the License for the specific language governing permissions and |
| 18 |
# limitations under the License. |
| 19 |
# |
| 20 |
|
| 21 |
package storage::emc::xtremio::restapi::custom::xtremioapi; |
| 22 |
|
| 23 |
use strict; |
| 24 |
use warnings; |
| 25 |
use centreon::plugins::http; |
| 26 |
use centreon::plugins::statefile; |
| 27 |
use JSON; |
| 28 |
|
| 29 |
sub new { |
| 30 |
my ($class, %options) = @_; |
| 31 |
my $self = {}; |
| 32 |
bless $self, $class; |
| 33 |
|
| 34 |
if (!defined($options{output})) { |
| 35 |
print "Class Custom: Need to specify 'output' argument.\n"; |
| 36 |
exit 3; |
| 37 |
} |
| 38 |
if (!defined($options{options})) { |
| 39 |
$options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument."); |
| 40 |
$options{output}->option_exit(); |
| 41 |
} |
| 42 |
|
| 43 |
if (!defined($options{noptions})) { |
| 44 |
$options{options}->add_options(arguments => { |
| 45 |
'hostname:s' => { name => 'hostname' }, |
| 46 |
'xtremio-username:s' => { name => 'xtremio_username' }, |
| 47 |
'xtremio-password:s' => { name => 'xtremio_password' }, |
| 48 |
'timeout:s' => { name => 'timeout' }, |
| 49 |
'reload-cache-time:s' => { name => 'reload_cache_time' } |
| 50 |
}); |
| 51 |
} |
| 52 |
$options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1); |
| 53 |
|
| 54 |
$self->{output} = $options{output}; |
| 55 |
$self->{http} = centreon::plugins::http->new(%options); |
| 56 |
$self->{statefile_cache_cluster} = centreon::plugins::statefile->new(%options); |
| 57 |
|
| 58 |
return $self; |
| 59 |
} |
| 60 |
|
| 61 |
sub set_options { |
| 62 |
my ($self, %options) = @_; |
| 63 |
|
| 64 |
$self->{option_results} = $options{option_results}; |
| 65 |
} |
| 66 |
|
| 67 |
sub set_defaults {} |
| 68 |
|
| 69 |
sub check_options { |
| 70 |
my ($self, %options) = @_; |
| 71 |
|
| 72 |
$self->{hostname} = defined($self->{option_results}->{hostname}) ? $self->{option_results}->{hostname} : ''; |
| 73 |
$self->{xtremio_username} = defined($self->{option_results}->{xtremio_username}) ? $self->{option_results}->{xtremio_username} : ''; |
| 74 |
$self->{xtremio_password} = defined($self->{option_results}->{xtremio_password}) ? $self->{option_results}->{xtremio_password} : ''; |
| 75 |
$self->{timeout} = defined($self->{option_results}->{timeout}) ? $self->{option_results}->{timeout} : 10; |
| 76 |
$self->{reload_cache_time} = defined($self->{option_results}->{reload_cache_time}) ? $self->{option_results}->{reload_cache_time} : 180; |
| 77 |
|
| 78 |
if ($self->{hostname} eq '') { |
| 79 |
$self->{output}->add_option_msg(short_msg => "Need to specify hostname option."); |
| 80 |
$self->{output}->option_exit(); |
| 81 |
} |
| 82 |
|
| 83 |
if ($self->{xtremio_username} eq '' || $self->{xtremio_password} eq '') { |
| 84 |
$self->{output}->add_option_msg(short_msg => "Need to specify --xtremio-username and --xtremio-password options."); |
| 85 |
$self->{output}->option_exit(); |
| 86 |
} |
| 87 |
|
| 88 |
$self->{statefile_cache_cluster}->check_options(option_results => $self->{option_results}); |
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
sub build_options_for_httplib { |
| 93 |
my ($self, %options) = @_; |
| 94 |
|
| 95 |
$self->{option_results}->{hostname} = $self->{xtremio_username}.':'.$self->{xtremio_password}.'@'.$self->{hostname}; |
| 96 |
$self->{option_results}->{timeout} = $self->{timeout}; |
| 97 |
$self->{option_results}->{port} = 443; |
| 98 |
$self->{option_results}->{proto} = 'https'; |
| 99 |
} |
| 100 |
|
| 101 |
sub settings { |
| 102 |
my ($self, %options) = @_; |
| 103 |
|
| 104 |
$self->build_options_for_httplib(); |
| 105 |
$self->{http}->set_options(%{$self->{option_results}}); |
| 106 |
} |
| 107 |
|
| 108 |
sub cache_clusters { |
| 109 |
my ($self, %options) = @_; |
| 110 |
|
| 111 |
my $has_cache_file = $self->{statefile_cache_cluster}->read(statefile => 'cache_xtremio_clusters_' . $self->{hostname}); |
| 112 |
my $timestamp_cache = $self->{statefile_cache_cluster}->get(name => 'last_timestamp'); |
| 113 |
my $clusters = $self->{statefile_cache_cluster}->get(name => 'clusters'); |
| 114 |
if ($has_cache_file == 0 || !defined($timestamp_cache) || ((time() - $timestamp_cache) > (($self->{reload_cache_time}) * 60))) { |
| 115 |
$clusters = {}; |
| 116 |
my $datas = { last_timestamp => time(), clusters => $clusters }; |
| 117 |
my @items = $self->get_items( |
| 118 |
url => '/api/json/types/', |
| 119 |
obj => 'clusters' |
| 120 |
); |
| 121 |
foreach (@items) { |
| 122 |
$clusters->{$_} = 1; |
| 123 |
} |
| 124 |
$self->{statefile_cache_cluster}->write(data => $datas); |
| 125 |
} |
| 126 |
|
| 127 |
return $clusters; |
| 128 |
} |
| 129 |
|
| 130 |
sub get_items { |
| 131 |
my ($self, %options) = @_; |
| 132 |
|
| 133 |
$self->settings(); |
| 134 |
|
| 135 |
if (defined($options{obj}) && $options{obj} ne '') { |
| 136 |
$options{url} .= $options{obj} . '/'; |
| 137 |
} |
| 138 |
|
| 139 |
my $response = $self->{http}->request(url_path => $options{url}); |
| 140 |
|
| 141 |
my $decoded; |
| 142 |
eval { |
| 143 |
$decoded = decode_json($response); |
| 144 |
}; |
| 145 |
if ($@) { |
| 146 |
$self->{output}->add_option_msg(short_msg => "Cannot decode json response"); |
| 147 |
$self->{output}->option_exit(); |
| 148 |
} |
| 149 |
|
| 150 |
my @items; |
| 151 |
foreach my $context (@{$decoded->{$options{obj}}}) { |
| 152 |
push @items, $context->{name}; |
| 153 |
} |
| 154 |
|
| 155 |
return @items; |
| 156 |
} |
| 157 |
|
| 158 |
sub get_details_data { |
| 159 |
my ($self, %options) = @_; |
| 160 |
|
| 161 |
my $response = $self->{http}->request( |
| 162 |
url_path => $options{url}, |
| 163 |
critical_status => '', warning_status => '', unknown_status => '' |
| 164 |
); |
| 165 |
|
| 166 |
my $decoded; |
| 167 |
eval { |
| 168 |
$decoded = decode_json($response); |
| 169 |
}; |
| 170 |
if ($@) { |
| 171 |
$self->{output}->add_option_msg(short_msg => "Cannot decode json response"); |
| 172 |
$self->{output}->option_exit(); |
| 173 |
} |
| 174 |
|
| 175 |
return $decoded; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
sub get_details_lookup_clusters { |
| 180 |
my ($self, %options) = @_; |
| 181 |
|
| 182 |
#Message if object not found: |
| 183 |
#{ |
| 184 |
# "message": "obj_not_found", |
| 185 |
# "error_code": 400 |
| 186 |
#} |
| 187 |
# |
| 188 |
if (!defined($self->{cache_clusters})) { |
| 189 |
$self->{cache_clusters} = $self->cache_clusters(); |
| 190 |
} |
| 191 |
foreach my $cluster_name (keys %{$self->{cache_clusters}}) { |
| 192 |
my $url = $options{url} . $options{append} . 'cluster-name=' . $cluster_name; |
| 193 |
my $decoded = $self->get_details_data(url => $url); |
| 194 |
return $decoded if (!defined($decoded->{error_code})); |
| 195 |
} |
| 196 |
|
| 197 |
# object is not found. |
| 198 |
$self->{output}->add_option_msg(short_msg => "xtremio api issue: cannot find object details"); |
| 199 |
$self->{output}->option_exit(); |
| 200 |
} |
| 201 |
|
| 202 |
sub get_details { |
| 203 |
my ($self, %options) = @_; |
| 204 |
|
| 205 |
$self->settings(); |
| 206 |
|
| 207 |
my $append = '?'; |
| 208 |
if ((defined($options{obj}) && $options{obj} ne '') && (defined($options{name}) && $options{name} ne '')) { |
| 209 |
$options{url} .= $options{obj} . '/?name=' . $options{name}; |
| 210 |
$append = '&'; |
| 211 |
} |
| 212 |
|
| 213 |
#Message when cluster id needed: |
| 214 |
#{ |
| 215 |
# "message": "cluster_id_is_required", |
| 216 |
# "error_code": 400 |
| 217 |
#} |
| 218 |
# |
| 219 |
my $decoded = $self->get_details_data(%options); |
| 220 |
if (defined($decoded->{error_code}) && |
| 221 |
($decoded->{error_code} == 400 && $decoded->{message} eq 'cluster_id_is_required')) { |
| 222 |
$decoded = $self->get_details_lookup_clusters(%options, append => $append); |
| 223 |
} elsif (defined($decoded->{error_code})) { |
| 224 |
$self->{output}->add_option_msg(short_msg => "xtremio api issue: $decoded->{message}"); |
| 225 |
$self->{output}->option_exit(); |
| 226 |
} |
| 227 |
|
| 228 |
return $decoded->{content}; |
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
1; |
| 233 |
|
| 234 |
__END__ |
| 235 |
|
| 236 |
=head1 NAME |
| 237 |
|
| 238 |
XREMIO REST API |
| 239 |
|
| 240 |
=head1 SYNOPSIS |
| 241 |
|
| 242 |
Xtremio Rest API custom mode |
| 243 |
|
| 244 |
=head1 REST API OPTIONS |
| 245 |
|
| 246 |
=over 8 |
| 247 |
|
| 248 |
=item B<--hostname> |
| 249 |
|
| 250 |
Xtremio hostname. |
| 251 |
|
| 252 |
=item B<--xtremio-username> |
| 253 |
|
| 254 |
Xtremio username. |
| 255 |
|
| 256 |
=item B<--xtremio-password> |
| 257 |
|
| 258 |
Xtremio password. |
| 259 |
|
| 260 |
=item B<--timeout> |
| 261 |
|
| 262 |
Set HTTP timeout |
| 263 |
|
| 264 |
=item B<--reload-cache-time> |
| 265 |
|
| 266 |
Time in seconds before reloading cache file (default: 180). |
| 267 |
The cache is used when XMS manages multiple clusters. |
| 268 |
|
| 269 |
=back |
| 270 |
|
| 271 |
=head1 DESCRIPTION |
| 272 |
|
| 273 |
B<custom>. |
| 274 |
|
| 275 |
=cut |