| 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 apps::thales::mistral::vs9::restapi::custom::api; |
| 22 |
|
| 23 |
use strict; |
| 24 |
use warnings; |
| 25 |
use centreon::plugins::http; |
| 26 |
use centreon::plugins::statefile; |
| 27 |
use JSON::XS; |
| 28 |
use Digest::MD5 qw(md5_hex); |
| 29 |
|
| 30 |
sub new { |
| 31 |
my ($class, %options) = @_; |
| 32 |
my $self = {}; |
| 33 |
bless $self, $class; |
| 34 |
|
| 35 |
if (!defined($options{output})) { |
| 36 |
print "Class Custom: Need to specify 'output' argument.\n"; |
| 37 |
exit 3; |
| 38 |
} |
| 39 |
if (!defined($options{options})) { |
| 40 |
$options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument."); |
| 41 |
$options{output}->option_exit(); |
| 42 |
} |
| 43 |
|
| 44 |
if (!defined($options{noptions})) { |
| 45 |
$options{options}->add_options(arguments => { |
| 46 |
'api-username:s' => { name => 'api_username' }, |
| 47 |
'api-password:s' => { name => 'api_password' }, |
| 48 |
'hostname:s' => { name => 'hostname' }, |
| 49 |
'port:s' => { name => 'port' }, |
| 50 |
'proto:s' => { name => 'proto' }, |
| 51 |
'timeout:s' => { name => 'timeout' }, |
| 52 |
'unknown-http-status:s' => { name => 'unknown_http_status' }, |
| 53 |
'warning-http-status:s' => { name => 'warning_http_status' }, |
| 54 |
'critical-http-status:s' => { name => 'critical_http_status' }, |
| 55 |
'reload-cache-time:s' => { name => 'reload_cache_time', default => 60 } |
| 56 |
}); |
| 57 |
} |
| 58 |
$options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1); |
| 59 |
|
| 60 |
$self->{output} = $options{output}; |
| 61 |
$self->{http} = centreon::plugins::http->new(%options, default_backend => 'curl'); |
| 62 |
$self->{cache} = centreon::plugins::statefile->new(%options); |
| 63 |
$self->{cache_connect} = centreon::plugins::statefile->new(output => $options{output}); |
| 64 |
|
| 65 |
return $self; |
| 66 |
} |
| 67 |
|
| 68 |
sub set_options { |
| 69 |
my ($self, %options) = @_; |
| 70 |
|
| 71 |
$self->{option_results} = $options{option_results}; |
| 72 |
} |
| 73 |
|
| 74 |
sub set_defaults {} |
| 75 |
|
| 76 |
sub check_options { |
| 77 |
my ($self, %options) = @_; |
| 78 |
|
| 79 |
$self->{option_results}->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : ''; |
| 80 |
$self->{option_results}->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 5572; |
| 81 |
$self->{option_results}->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https'; |
| 82 |
$self->{option_results}->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 30; |
| 83 |
$self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : ''; |
| 84 |
$self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : ''; |
| 85 |
$self->{unknown_http_status} = (defined($self->{option_results}->{unknown_http_status})) ? $self->{option_results}->{unknown_http_status} : '%{http_code} < 200 or %{http_code} >= 300'; |
| 86 |
$self->{warning_http_status} = (defined($self->{option_results}->{warning_http_status})) ? $self->{option_results}->{warning_http_status} : ''; |
| 87 |
$self->{critical_http_status} = (defined($self->{option_results}->{critical_http_status})) ? $self->{option_results}->{critical_http_status} : ''; |
| 88 |
|
| 89 |
if ($self->{option_results}->{hostname} eq '') { |
| 90 |
$self->{output}->add_option_msg(short_msg => 'Need to specify --hostname option.'); |
| 91 |
$self->{output}->option_exit(); |
| 92 |
} |
| 93 |
if ($self->{api_username} eq '') { |
| 94 |
$self->{output}->add_option_msg(short_msg => 'Need to specify --api-username option.'); |
| 95 |
$self->{output}->option_exit(); |
| 96 |
} |
| 97 |
if ($self->{api_password} eq '') { |
| 98 |
$self->{output}->add_option_msg(short_msg => 'Need to specify --api-password option.'); |
| 99 |
$self->{output}->option_exit(); |
| 100 |
} |
| 101 |
|
| 102 |
$self->{cache}->check_options(option_results => $self->{option_results}); |
| 103 |
$self->{cache_connect}->check_options(option_results => $self->{option_results}); |
| 104 |
|
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
sub settings { |
| 109 |
my ($self, %options) = @_; |
| 110 |
|
| 111 |
return if (defined($self->{settings_done})); |
| 112 |
$self->{http}->set_options(%{$self->{option_results}}); |
| 113 |
$self->{http}->add_header(key => 'Accept', value => 'application/json'); |
| 114 |
$self->{settings_done} = 1; |
| 115 |
} |
| 116 |
|
| 117 |
sub get_connection_info { |
| 118 |
my ($self, %options) = @_; |
| 119 |
|
| 120 |
return $self->{option_results}->{hostname} . ':' . $self->{option_results}->{port}; |
| 121 |
} |
| 122 |
|
| 123 |
sub get_token { |
| 124 |
my ($self, %options) = @_; |
| 125 |
|
| 126 |
my $has_cache_file = $self->{cache_connect}->read(statefile => 'thales_mistral_connect_' . md5_hex($self->get_connection_info() . '_' . $self->{api_username})); |
| 127 |
my $token = $self->{cache_connect}->get(name => 'token'); |
| 128 |
my $md5_secret_cache = $self->{cache_connect}->get(name => 'md5_secret'); |
| 129 |
my $md5_secret = md5_hex($self->{api_username} . $self->{api_password}); |
| 130 |
|
| 131 |
if ($has_cache_file == 0 || |
| 132 |
!defined($token) || |
| 133 |
(defined($md5_secret_cache) && $md5_secret_cache ne $md5_secret) |
| 134 |
) { |
| 135 |
my $json_request = { |
| 136 |
username => $self->{api_username}, |
| 137 |
password => $self->{api_password} |
| 138 |
}; |
| 139 |
my $encoded; |
| 140 |
eval { |
| 141 |
$encoded = encode_json($json_request); |
| 142 |
}; |
| 143 |
if ($@) { |
| 144 |
$self->{output}->add_option_msg(short_msg => 'cannot encode json request'); |
| 145 |
$self->{output}->option_exit(); |
| 146 |
} |
| 147 |
|
| 148 |
$self->settings(); |
| 149 |
my $content = $self->{http}->request( |
| 150 |
method => 'POST', |
| 151 |
url_path => '/api/auth/login', |
| 152 |
query_form_post => $encoded, |
| 153 |
unknown_status => $self->{unknown_http_status}, |
| 154 |
warning_status => $self->{warning_http_status}, |
| 155 |
critical_status => $self->{critical_http_status}, |
| 156 |
header => ['Content-Type: application/json'] |
| 157 |
); |
| 158 |
|
| 159 |
my $decoded; |
| 160 |
eval { |
| 161 |
$decoded = JSON::XS->new->utf8->decode($content); |
| 162 |
}; |
| 163 |
if ($@) { |
| 164 |
$self->{output}->add_option_msg(short_msg => "Cannot decode json response"); |
| 165 |
$self->{output}->option_exit(); |
| 166 |
} |
| 167 |
|
| 168 |
$token = $decoded->{access_token}; |
| 169 |
my $datas = { |
| 170 |
updated => time(), |
| 171 |
token => $token, |
| 172 |
md5_secret => $md5_secret |
| 173 |
}; |
| 174 |
$self->{cache_connect}->write(data => $datas); |
| 175 |
} |
| 176 |
|
| 177 |
return $token; |
| 178 |
} |
| 179 |
|
| 180 |
sub clean_token { |
| 181 |
my ($self, %options) = @_; |
| 182 |
|
| 183 |
my $datas = { updated => time() }; |
| 184 |
$self->{cache_connect}->write(data => $datas); |
| 185 |
} |
| 186 |
|
| 187 |
sub request_api { |
| 188 |
my ($self, %options) = @_; |
| 189 |
|
| 190 |
$self->settings(); |
| 191 |
my $token = $self->get_token(); |
| 192 |
my ($content) = $self->{http}->request( |
| 193 |
url_path => '/api' . $options{endpoint}, |
| 194 |
get_param => $options{get_param}, |
| 195 |
header => ['Authorization: Bearer ' . $token], |
| 196 |
unknown_status => '', |
| 197 |
warning_status => '', |
| 198 |
critical_status => '' |
| 199 |
); |
| 200 |
|
| 201 |
# Maybe token is invalid. so we retry |
| 202 |
if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) { |
| 203 |
$self->clean_token(); |
| 204 |
$token = $self->get_token(); |
| 205 |
$content = $self->{http}->request( |
| 206 |
url_path => '/api' . $options{endpoint}, |
| 207 |
get_param => $options{get_param}, |
| 208 |
header => ['Authorization: Bearer ' . $token], |
| 209 |
unknown_status => $self->{unknown_http_status}, |
| 210 |
warning_status => $self->{warning_http_status}, |
| 211 |
critical_status => $self->{critical_http_status} |
| 212 |
); |
| 213 |
} |
| 214 |
|
| 215 |
if (!defined($content) || $content eq '') { |
| 216 |
$self->{output}->add_option_msg(short_msg => "API returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']"); |
| 217 |
$self->{output}->option_exit(); |
| 218 |
} |
| 219 |
|
| 220 |
my $decoded; |
| 221 |
eval { |
| 222 |
$decoded = JSON::XS->new->allow_nonref(1)->utf8->decode($content); |
| 223 |
}; |
| 224 |
if ($@) { |
| 225 |
$self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)"); |
| 226 |
$self->{output}->option_exit(); |
| 227 |
} |
| 228 |
|
| 229 |
return $decoded; |
| 230 |
} |
| 231 |
|
| 232 |
sub get_clusters { |
| 233 |
my ($self, %options) = @_; |
| 234 |
|
| 235 |
my $has_cache_file = $self->{cache}->read(statefile => 'thales_mistral_clusters_' . md5_hex($self->get_connection_info() . '_' . $self->{api_username})); |
| 236 |
my $updated = $self->{cache}->get(name => 'updated'); |
| 237 |
my $clusters = $self->{cache}->get(name => 'clusters'); |
| 238 |
if ($has_cache_file == 0 || !defined($updated) || ((time() - $updated) > (($self->{option_results}->{reload_cache_time} * 60)))) { |
| 239 |
my $cache = { updated => time() }; |
| 240 |
my $result = $self->request_api( |
| 241 |
endpoint => '/clusters' |
| 242 |
); |
| 243 |
$clusters = $result; |
| 244 |
|
| 245 |
$cache->{clusters} = $clusters; |
| 246 |
$self->{cache}->write(data => $cache); |
| 247 |
} |
| 248 |
|
| 249 |
return $clusters; |
| 250 |
} |
| 251 |
|
| 252 |
sub get_gateway_inventory { |
| 253 |
my ($self, %options) = @_; |
| 254 |
|
| 255 |
my $has_cache_file = $self->{cache}->read(statefile => 'thales_mistral_inventory_' . md5_hex($self->get_connection_info() . '_' . $self->{api_username})); |
| 256 |
my $updated = $self->{cache}->get(name => 'updated'); |
| 257 |
my $inventory = $self->{cache}->get(name => 'gwInventory'); |
| 258 |
if ($has_cache_file == 0 || !defined($updated) || ((time() - $updated) > (($self->{option_results}->{reload_cache_time} * 60)))) { |
| 259 |
my $cache = { updated => time() }; |
| 260 |
my $result = $self->request_api( |
| 261 |
endpoint => '/ssIpsecGwHws', |
| 262 |
get_param => ['projection=gatewayHwData'] |
| 263 |
); |
| 264 |
$inventory = $result->{content}; |
| 265 |
|
| 266 |
$result = $self->request_api(endpoint => '/ssIpsecGwHws/status'); |
| 267 |
for (my $i = 0; $i < scalar(@$inventory); $i++) { |
| 268 |
if (defined($result->{ $inventory->[$i]->{serialNumber} })) { |
| 269 |
$inventory->[$i]->{status} = $result->{ $inventory->[$i]->{serialNumber} }; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
$result = $self->request_api( |
| 274 |
endpoint => '/certificateGws', |
| 275 |
get_param => ['projection=withGatewayHw'] |
| 276 |
); |
| 277 |
for (my $i = 0; $i < scalar(@$inventory); $i++) { |
| 278 |
$inventory->[$i]->{certificates} = []; |
| 279 |
foreach my $cert (@{$result->{content}}) { |
| 280 |
if ($inventory->[$i]->{serialNumber} eq $cert->{ssIpsecGwHw}->{serialNumber}) { |
| 281 |
push @{$inventory->[$i]->{certificates}}, $cert; |
| 282 |
last; |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
$cache->{gwInventory} = $inventory; |
| 288 |
$self->{cache}->write(data => $cache); |
| 289 |
} |
| 290 |
|
| 291 |
return $inventory; |
| 292 |
} |
| 293 |
|
| 294 |
1; |
| 295 |
|
| 296 |
__END__ |
| 297 |
|
| 298 |
=head1 NAME |
| 299 |
|
| 300 |
Mistral vs9 API |
| 301 |
|
| 302 |
=head1 REST API OPTIONS |
| 303 |
|
| 304 |
Mistral vs9 API |
| 305 |
|
| 306 |
=over 8 |
| 307 |
|
| 308 |
=item B<--hostname> |
| 309 |
|
| 310 |
Set MMC hostname. |
| 311 |
|
| 312 |
=item B<--port> |
| 313 |
|
| 314 |
Port used (default: 5572) |
| 315 |
|
| 316 |
=item B<--proto> |
| 317 |
|
| 318 |
Specify https if needed (default: 'https') |
| 319 |
|
| 320 |
=item B<--api-username> |
| 321 |
|
| 322 |
API username. |
| 323 |
|
| 324 |
=item B<--api-password> |
| 325 |
|
| 326 |
API password. |
| 327 |
|
| 328 |
=item B<--timeout> |
| 329 |
|
| 330 |
Set timeout in seconds (default: 30). |
| 331 |
|
| 332 |
=back |
| 333 |
|
| 334 |
=head1 DESCRIPTION |
| 335 |
|
| 336 |
B<custom>. |
| 337 |
|
| 338 |
=cut |