| 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 hardware::devices::hms::netbiter::argos::restapi::custom::api; |
| 22 |
|
| 23 |
use strict; |
| 24 |
use warnings; |
| 25 |
use DateTime; |
| 26 |
use centreon::plugins::http; |
| 27 |
use centreon::plugins::statefile; |
| 28 |
use JSON::XS; |
| 29 |
use Digest::MD5 qw(md5_hex); |
| 30 |
|
| 31 |
sub new { |
| 32 |
my ($class, %options) = @_; |
| 33 |
my $self = {}; |
| 34 |
bless $self, $class; |
| 35 |
|
| 36 |
if (!defined($options{output})) { |
| 37 |
print "Class Custom: Need to specify 'output' argument.\n"; |
| 38 |
exit 3; |
| 39 |
} |
| 40 |
if (!defined($options{options})) { |
| 41 |
$options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument."); |
| 42 |
$options{output}->option_exit(); |
| 43 |
} |
| 44 |
|
| 45 |
if (!defined($options{noptions})) { |
| 46 |
$options{options}->add_options(arguments => { |
| 47 |
'access-key:s' => { name => 'access_key' }, |
| 48 |
'api-endpoint:s' => { name => 'api_endpoint' }, |
| 49 |
'api-password:s' => { name => 'api_password' }, |
| 50 |
'api-username:s' => { name => 'api_username' }, |
| 51 |
'force-cache-reload' => { name => 'force_cache_reload' }, |
| 52 |
'hostname:s' => { name => 'hostname' }, |
| 53 |
'port:s' => { name => 'port' }, |
| 54 |
'proto:s' => { name => 'proto' }, |
| 55 |
'reload-cache-time:s' => { name => 'reload_cache_time' }, |
| 56 |
'timeout:s' => { name => 'timeout' } |
| 57 |
}); |
| 58 |
} |
| 59 |
$options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1); |
| 60 |
|
| 61 |
$self->{output} = $options{output}; |
| 62 |
$self->{http} = centreon::plugins::http->new(%options, default_backend => 'curl'); |
| 63 |
$self->{cache_auth} = centreon::plugins::statefile->new(%options); |
| 64 |
$self->{cache_sensors} = centreon::plugins::statefile->new(%options); |
| 65 |
|
| 66 |
return $self; |
| 67 |
} |
| 68 |
|
| 69 |
sub set_options { |
| 70 |
my ($self, %options) = @_; |
| 71 |
|
| 72 |
$self->{option_results} = $options{option_results}; |
| 73 |
} |
| 74 |
|
| 75 |
sub set_defaults {} |
| 76 |
|
| 77 |
sub check_options { |
| 78 |
my ($self, %options) = @_; |
| 79 |
|
| 80 |
$self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : 'api.netbiter.net'; |
| 81 |
$self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443; |
| 82 |
$self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https'; |
| 83 |
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10; |
| 84 |
$self->{api_endpoint} = (defined($self->{option_results}->{api_endpoint})) ? $self->{option_results}->{api_endpoint} : '/operation/v1/rest/json'; |
| 85 |
$self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : ''; |
| 86 |
$self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : ''; |
| 87 |
$self->{access_key} = (defined($self->{option_results}->{access_key})) ? $self->{option_results}->{access_key} : ''; |
| 88 |
$self->{reload_cache_time} = (defined($self->{option_results}->{reload_cache_time})) ? $self->{option_results}->{reload_cache_time} : 3600; |
| 89 |
$self->{force_cache_reload} = (defined($self->{option_results}->{force_cache_reload})) ? $self->{option_results}->{force_cache_reload} : undef; |
| 90 |
|
| 91 |
if ( !(($self->{api_username} ne '' && $self->{api_password} ne '') || ($self->{access_key} ne '')) ) { |
| 92 |
$self->{output}->add_option_msg(short_msg => 'At least one of --api-username / --api-password (login) ; or --access-key must be set'); |
| 93 |
$self->{output}->option_exit(); |
| 94 |
} |
| 95 |
|
| 96 |
$self->{cache_auth}->check_options(option_results => $self->{option_results}); |
| 97 |
$self->{cache_sensors}->check_options(option_results => $self->{option_results}); |
| 98 |
|
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
sub build_options_for_httplib { |
| 103 |
my ($self, %options) = @_; |
| 104 |
|
| 105 |
$self->{option_results}->{hostname} = $self->{hostname}; |
| 106 |
$self->{option_results}->{timeout} = $self->{timeout}; |
| 107 |
$self->{option_results}->{port} = $self->{port}; |
| 108 |
$self->{option_results}->{proto} = $self->{proto}; |
| 109 |
$self->{option_results}->{timeout} = $self->{timeout}; |
| 110 |
$self->{option_results}->{warning_status} = ''; |
| 111 |
$self->{option_results}->{critical_status} = ''; |
| 112 |
$self->{option_results}->{unknown_status} = '%{http_code} < 200 or %{http_code} > 400'; |
| 113 |
} |
| 114 |
|
| 115 |
sub settings { |
| 116 |
my ($self, %options) = @_; |
| 117 |
|
| 118 |
$self->build_options_for_httplib(); |
| 119 |
$self->{http}->add_header(key => 'Accept', value => 'application/json'); |
| 120 |
$self->{http}->set_options(%{$self->{option_results}}); |
| 121 |
} |
| 122 |
|
| 123 |
sub convert_iso8601_to_epoch { |
| 124 |
my ($self, %options) = @_; |
| 125 |
|
| 126 |
if ($options{time_string} =~ /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z/) { |
| 127 |
my $dt = DateTime->new( |
| 128 |
year => $1, |
| 129 |
month => $2, |
| 130 |
day => $3, |
| 131 |
hour => $4, |
| 132 |
minute => $5, |
| 133 |
second => $6 |
| 134 |
); |
| 135 |
|
| 136 |
my $epoch_time = $dt->epoch(); |
| 137 |
return $epoch_time; |
| 138 |
} |
| 139 |
|
| 140 |
$self->{output}->add_option_msg(short_msg => "Wrong date format: $options{time_string}"); |
| 141 |
$self->{output}->option_exit(); |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
sub get_access_key { |
| 146 |
my ($self, %options) = @_; |
| 147 |
|
| 148 |
my $has_cache_file = $self->{cache_auth}->read(statefile => 'netbiter_argos_api_' . md5_hex($self->{hostname}) . '_' . $self->{cache_key}); |
| 149 |
my $expires_on = $self->{cache_auth}->get(name => 'expires_on'); |
| 150 |
my $access_key = $self->{cache_auth}->get(name => 'access_key'); |
| 151 |
if ( $has_cache_file == 0 || !defined($access_key) || (($expires_on - time()) < 10) ) { |
| 152 |
my $login; |
| 153 |
if ($self->{api_username} ne '') { |
| 154 |
$login = { userName => $self->{api_username}, password => $self->{api_password} }; |
| 155 |
} |
| 156 |
my $post_json = JSON::XS->new->utf8->encode($login); |
| 157 |
|
| 158 |
$self->settings(); |
| 159 |
|
| 160 |
my $content = $self->{http}->request( |
| 161 |
method => 'POST', |
| 162 |
header => ['Content-type: application/json'], |
| 163 |
query_form_post => $post_json, |
| 164 |
url_path => $self->{api_endpoint} . '/user/authenticate' |
| 165 |
); |
| 166 |
|
| 167 |
if (!defined($content) || $content eq '') { |
| 168 |
$self->{output}->add_option_msg(short_msg => "Authentication endpoint returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']"); |
| 169 |
$self->{output}->option_exit(); |
| 170 |
} |
| 171 |
|
| 172 |
my $decoded; |
| 173 |
eval { |
| 174 |
$decoded = JSON::XS->new->utf8->decode($content); |
| 175 |
}; |
| 176 |
if ($@) { |
| 177 |
$self->{output}->output_add(long_msg => $content, debug => 1); |
| 178 |
$self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)"); |
| 179 |
$self->{output}->option_exit(); |
| 180 |
} |
| 181 |
if (defined($decoded->{error_code})) { |
| 182 |
$self->{output}->output_add(long_msg => "Error message : " . $decoded->{error}, debug => 1); |
| 183 |
$self->{output}->add_option_msg(short_msg => "Authentication endpoint returns error code '" . $decoded->{error_code} . "' (add --debug option for detailed message)"); |
| 184 |
$self->{output}->option_exit(); |
| 185 |
} |
| 186 |
|
| 187 |
$access_key = $decoded->{access_key}; |
| 188 |
my $expires_on = $self->convert_iso8601_to_epoch(time_string => $decoded->{expires}); |
| 189 |
my $datas = { last_timestamp => time(), access_key => $decoded->{accessKey}, expires_on => $expires_on }; |
| 190 |
$self->{cache_auth}->write(data => $datas); |
| 191 |
} |
| 192 |
|
| 193 |
return $access_key; |
| 194 |
} |
| 195 |
|
| 196 |
sub request_api { |
| 197 |
my ($self, %options) = @_; |
| 198 |
|
| 199 |
if (!defined($self->{access_key})) { |
| 200 |
$self->{access_key} = $self->get_access_key(statefile => $self->{cache}); |
| 201 |
} |
| 202 |
|
| 203 |
$self->settings(); |
| 204 |
my $url_path = $self->{api_endpoint} . $options{request}; |
| 205 |
my $parameters = defined($options{get_params}) ? $options{get_params} : []; |
| 206 |
my $response = $self->{http}->request( |
| 207 |
method => 'GET', |
| 208 |
get_param => [ 'accesskey=' . $self->{access_key}, @$parameters ], |
| 209 |
url_path => $url_path |
| 210 |
); |
| 211 |
# check rate limit => to do |
| 212 |
#my $rate_limit = $self->{http}->get_header(name => 'argos-ratelimit-remaining'); |
| 213 |
if (!defined($response) || $response eq '') { |
| 214 |
$self->{output}->add_option_msg(short_msg => "API returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']"); |
| 215 |
$self->{output}->option_exit(); |
| 216 |
} |
| 217 |
|
| 218 |
my $decoded; |
| 219 |
eval { |
| 220 |
$decoded = JSON::XS->new->decode($response); |
| 221 |
}; |
| 222 |
|
| 223 |
if ($@) { |
| 224 |
$self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)"); |
| 225 |
$self->{output}->option_exit(); |
| 226 |
} |
| 227 |
return $decoded; |
| 228 |
} |
| 229 |
|
| 230 |
sub list_sensors { |
| 231 |
my ($self, %options) = @_; |
| 232 |
# Results are cached to avoid too many API calls |
| 233 |
my $has_cache_file = $self->{cache_sensors}->read(statefile => 'netbiter_cache_sensors_' . md5_hex($options{system_id})); |
| 234 |
my $response = $self->{cache_sensors}->get(name => 'response'); |
| 235 |
my $freshness = defined($self->{cache_sensors}->get(name => 'update_time')) ? time() - $self->{cache_sensors}->get(name => 'update_time') : undef; |
| 236 |
$self->{force_cache_reload} = 1 if defined($options{force}); |
| 237 |
|
| 238 |
if ( $has_cache_file == 0 || !defined($response) || (defined($freshness)) && ($freshness > $self->{reload_cache_time}) || defined($self->{force_cache_reload}) ) { |
| 239 |
my $request = '/system/' . $options{system_id} . '/log/config'; |
| 240 |
$response = $self->request_api(request => $request); |
| 241 |
} |
| 242 |
|
| 243 |
$self->{cache_sensors}->write(data => { |
| 244 |
update_time => time(), |
| 245 |
response => $response |
| 246 |
}); |
| 247 |
|
| 248 |
return $response |
| 249 |
} |
| 250 |
|
| 251 |
1; |
| 252 |
|
| 253 |
__END__ |
| 254 |
|
| 255 |
=head1 NAME |
| 256 |
|
| 257 |
Netbiter Argos RestAPI |
| 258 |
|
| 259 |
=head1 REST API OPTIONS |
| 260 |
|
| 261 |
Netbiter Argos RestAPI |
| 262 |
|
| 263 |
=over 8 |
| 264 |
|
| 265 |
=item B<--hostname> |
| 266 |
|
| 267 |
Argos API hostname (default: api.netbiter.net). |
| 268 |
|
| 269 |
=item B<--port> |
| 270 |
|
| 271 |
Port used (default: 443) |
| 272 |
|
| 273 |
=item B<--proto> |
| 274 |
|
| 275 |
Specify https if needed (default: 'https') |
| 276 |
|
| 277 |
=item B<--api-endpoint> |
| 278 |
|
| 279 |
Argos API requests endpoint (default: '/operation/v1/rest/json') |
| 280 |
|
| 281 |
=item B<--access-key> |
| 282 |
|
| 283 |
For Access Key "direct" authentication method. |
| 284 |
Example: --access-key='ABCDEFG1234567890' |
| 285 |
|
| 286 |
=item B<--api-username> |
| 287 |
|
| 288 |
For Username/Password authentication method. |
| 289 |
Must be used with --api-password option. |
| 290 |
|
| 291 |
=item B<--api-password> |
| 292 |
|
| 293 |
For Username/Password authentication method. |
| 294 |
Must be used with --api-username option. |
| 295 |
|
| 296 |
=item B<--timeout> |
| 297 |
|
| 298 |
Set timeout in seconds (default: 10). |
| 299 |
|
| 300 |
=back |
| 301 |
|
| 302 |
=head1 DESCRIPTION |
| 303 |
|
| 304 |
B<custom>. |
| 305 |
|
| 306 |
=cut |