| 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 network::cisco::aci::apic::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 |
'username:s' => { name => 'username' }, |
| 48 |
'password:s' => { name => 'password' }, |
| 49 |
'hostname:s' => { name => 'hostname' }, |
| 50 |
'timeout:s' => { name => 'timeout' }, |
| 51 |
'port:s' => { name => 'port'}, |
| 52 |
'proto:s' => { name => 'proto'} |
| 53 |
}); |
| 54 |
} |
| 55 |
$options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1); |
| 56 |
|
| 57 |
$self->{output} = $options{output}; |
| 58 |
$self->{http} = centreon::plugins::http->new(%options); |
| 59 |
$self->{cache} = centreon::plugins::statefile->new(%options); |
| 60 |
|
| 61 |
return $self; |
| 62 |
} |
| 63 |
|
| 64 |
sub set_options { |
| 65 |
my ($self, %options) = @_; |
| 66 |
|
| 67 |
$self->{option_results} = $options{option_results}; |
| 68 |
} |
| 69 |
|
| 70 |
sub set_defaults {} |
| 71 |
|
| 72 |
sub check_options { |
| 73 |
my ($self, %options) = @_; |
| 74 |
|
| 75 |
$self->{username} = (defined($self->{option_results}->{username})) ? $self->{option_results}->{username} : undef; |
| 76 |
$self->{password} = (defined($self->{option_results}->{password})) ? $self->{option_results}->{password} : undef; |
| 77 |
$self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : undef; |
| 78 |
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10; |
| 79 |
$self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443; |
| 80 |
$self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https'; |
| 81 |
|
| 82 |
if (!defined($self->{hostname}) || $self->{hostname} eq '') { |
| 83 |
$self->{output}->add_option_msg(short_msg => 'Need to specify --hostname option.'); |
| 84 |
$self->{output}->option_exit(); |
| 85 |
} |
| 86 |
if (!defined($self->{username}) || $self->{username} eq '') { |
| 87 |
$self->{output}->add_option_msg(short_msg => 'Need to specify --username option.'); |
| 88 |
$self->{output}->option_exit(); |
| 89 |
} |
| 90 |
if (!defined($self->{password}) || $self->{password} eq '') { |
| 91 |
$self->{output}->add_option_msg(short_msg => 'Need to specify --password option.'); |
| 92 |
$self->{output}->option_exit(); |
| 93 |
} |
| 94 |
|
| 95 |
$self->{cache}->check_options(option_results => $self->{option_results}); |
| 96 |
return 0; |
| 97 |
} |
| 98 |
|
| 99 |
sub build_options_for_httplib { |
| 100 |
my ($self, %options) = @_; |
| 101 |
|
| 102 |
$self->{option_results}->{hostname} = $self->{hostname}; |
| 103 |
$self->{option_results}->{timeout} = $self->{timeout}; |
| 104 |
$self->{option_results}->{port} = $self->{port}; |
| 105 |
$self->{option_results}->{proto} = $self->{proto}; |
| 106 |
$self->{option_results}->{warning_status} = ''; |
| 107 |
$self->{option_results}->{critical_status} = ''; |
| 108 |
$self->{option_results}->{unknown_status} = ''; |
| 109 |
} |
| 110 |
|
| 111 |
sub settings { |
| 112 |
my ($self, %options) = @_; |
| 113 |
|
| 114 |
$self->build_options_for_httplib(); |
| 115 |
$self->{http}->add_header(key => 'Accept', value => 'application/json'); |
| 116 |
$self->{http}->add_header(key => 'Content-Type', value => 'application/json'); |
| 117 |
$self->{http}->set_options(%{$self->{option_results}}); |
| 118 |
} |
| 119 |
|
| 120 |
sub clean_access_token { |
| 121 |
my ($self, %options) = @_; |
| 122 |
|
| 123 |
my $datas = { last_timestamp => time() }; |
| 124 |
$options{statefile}->write(data => $datas); |
| 125 |
$self->{access_token} = undef; |
| 126 |
$self->{http}->add_header(key => 'Cookie', value => undef); |
| 127 |
} |
| 128 |
|
| 129 |
sub get_access_token { |
| 130 |
my ($self, %options) = @_; |
| 131 |
|
| 132 |
my $has_cache_file = $options{statefile}->read(statefile => 'cisco_aci_apic_' . md5_hex($self->{hostname}) . '_' . md5_hex($self->{username})); |
| 133 |
my $expires_on = $options{statefile}->get(name => 'expires_on'); |
| 134 |
my $access_token = $options{statefile}->get(name => 'access_token'); |
| 135 |
my $last_timestamp = $options{statefile}->get(name => 'last_timestamp'); |
| 136 |
|
| 137 |
if ($has_cache_file == 0 || !defined($access_token) || (($expires_on - time()) < 10)) { |
| 138 |
my $login = { aaaUser => { attributes => { name => $self->{username}, pwd => $self->{password} } } }; |
| 139 |
my $post_json = JSON::XS->new->utf8->encode($login); |
| 140 |
|
| 141 |
$self->settings(); |
| 142 |
|
| 143 |
my $content = $self->{http}->request( |
| 144 |
method => 'POST', |
| 145 |
query_form_post => $post_json, |
| 146 |
url_path => '/api/aaaLogin.json' |
| 147 |
); |
| 148 |
|
| 149 |
my $decoded; |
| 150 |
eval { |
| 151 |
$decoded = JSON::XS->new->utf8->decode($content); |
| 152 |
}; |
| 153 |
if ($@) { |
| 154 |
$self->{output}->output_add(long_msg => $content, debug => 1); |
| 155 |
$self->{output}->add_option_msg(short_msg => 'Cannot get token from API'); |
| 156 |
$self->{output}->option_exit(); |
| 157 |
} |
| 158 |
if (defined($decoded->{imdata}->[0]->{error}->{attributes})) { |
| 159 |
$self->{output}->add_option_msg(short_msg => "Error '" . uc($decoded->{imdata}->[0]->{error}->{attributes}->{code}) . " " |
| 160 |
. $decoded->{imdata}->[0]->{error}->{attributes}->{text} . "'"); |
| 161 |
$self->{output}->option_exit(); |
| 162 |
} |
| 163 |
|
| 164 |
$access_token = $decoded->{imdata}->[0]->{aaaLogin}->{attributes}->{token}; |
| 165 |
my $datas = { |
| 166 |
last_timestamp => time(), |
| 167 |
access_token => $access_token, |
| 168 |
expires_on => time() + $decoded->{imdata}->[0]->{aaaLogin}->{attributes}->{refreshTimeoutSeconds} |
| 169 |
}; |
| 170 |
$options{statefile}->write(data => $datas); |
| 171 |
} |
| 172 |
|
| 173 |
$self->{access_token} = $access_token; |
| 174 |
$self->{http}->add_header(key => 'Cookie', value => 'APIC-Cookie=' . $self->{access_token}); |
| 175 |
} |
| 176 |
|
| 177 |
sub request_api { |
| 178 |
my ($self, %options) = @_; |
| 179 |
|
| 180 |
$self->settings(); |
| 181 |
if (!defined($self->{access_token})) { |
| 182 |
$self->{access_token} = $self->get_access_token(statefile => $self->{cache}); |
| 183 |
} |
| 184 |
|
| 185 |
my $content = $self->{http}->request(%options, warning_status => '', unknown_status => '', critical_status => ''); |
| 186 |
|
| 187 |
# Maybe there is an issue with the access_token. So we retry. |
| 188 |
if ($self->{http}->get_code() != 200) { |
| 189 |
$self->clean_access_token(statefile => $self->{cache}); |
| 190 |
$self->get_access_token(statefile => $self->{cache}); |
| 191 |
$content = $self->{http}->request(%options); |
| 192 |
} |
| 193 |
|
| 194 |
my $decoded; |
| 195 |
eval { |
| 196 |
$decoded = JSON::XS->new->utf8->decode($content); |
| 197 |
}; |
| 198 |
if ($@) { |
| 199 |
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); |
| 200 |
$self->{output}->option_exit(); |
| 201 |
} |
| 202 |
if (defined($decoded->{imdata}->[0]) && defined($decoded->{imdata}->[0]->{error}->{attributes})) { |
| 203 |
$self->{output}->add_option_msg(short_msg => "Error '" . uc($decoded->{imdata}->[0]->{error}->{attributes}->{code}) . " " . $decoded->{imdata}->[0]->{error}->{attributes}->{text} . "'"); |
| 204 |
$self->{output}->option_exit(); |
| 205 |
} |
| 206 |
|
| 207 |
return $decoded; |
| 208 |
} |
| 209 |
|
| 210 |
sub get_fabric_health { |
| 211 |
my ($self, %options) = @_; |
| 212 |
|
| 213 |
my $response = $self->request_api(method => 'GET', url_path => '/api/class/fabricHealthTotal.json'); |
| 214 |
return $response; |
| 215 |
} |
| 216 |
|
| 217 |
sub get_node_health_5m { |
| 218 |
my ($self, %options) = @_; |
| 219 |
|
| 220 |
my $response = $self->request_api(method => 'GET', url_path => '/api/class/fabricNodeHealth5min.json'); |
| 221 |
return $response; |
| 222 |
} |
| 223 |
|
| 224 |
sub get_tenant_health { |
| 225 |
my ($self, %options) = @_; |
| 226 |
|
| 227 |
my $response = $self->request_api(method => 'GET', url_path => '/api/class/fvTenant.json?rsp-subtree-include=health,required'); |
| 228 |
return $response; |
| 229 |
} |
| 230 |
|
| 231 |
1; |
| 232 |
|
| 233 |
__END__ |
| 234 |
|
| 235 |
=head1 NAME |
| 236 |
|
| 237 |
Cisco ACI APIC API Interface |
| 238 |
|
| 239 |
=head1 REST API OPTIONS |
| 240 |
|
| 241 |
Cisco ACI APIC Interface |
| 242 |
|
| 243 |
=over 8 |
| 244 |
|
| 245 |
=item B<--hostname> |
| 246 |
|
| 247 |
IP/FQDN of the Cisco ACI APIC |
| 248 |
|
| 249 |
=item B<--username> |
| 250 |
|
| 251 |
Username to connect to ACI APIC |
| 252 |
|
| 253 |
=item B<--hostname> |
| 254 |
|
| 255 |
Password to connect to ACI APIC |
| 256 |
|
| 257 |
=item B<--timeout> |
| 258 |
|
| 259 |
Set timeout in seconds (default: 10). |
| 260 |
|
| 261 |
=back |
| 262 |
|
| 263 |
=head1 DESCRIPTION |
| 264 |
|
| 265 |
B<custom>. |
| 266 |
|
| 267 |
=cut |