Debian Codegraph @

src/network/cisco/meraki/cloudcontroller/restapi/custom/api.pm

raw
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::meraki::cloudcontroller::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 'hostname:s' => { name => 'hostname' },
47 'port:s' => { name => 'port' },
48 'proto:s' => { name => 'proto' },
49 'api-token:s' => { name => 'api_token' },
50 'timeout:s' => { name => 'timeout' },
51 'reload-cache-time:s' => { name => 'reload_cache_time' },
52 'ignore-permission-errors' => { name => 'ignore_permission_errors' },
53 'ignore-orgs-api-disabled' => { name => 'ignore_orgs_api_disabled' },
54 'api-filter-orgs:s' => { name => 'api_filter_orgs' },
55 'timespan:s' => { name => 'timespan' },
56 'cache-use' => { name => 'cache_use' }
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);
63 $self->{cache} = centreon::plugins::statefile->new(%options);
64
65 $self->{datas} = {
66 uplink_statuses => {},
67 uplinks_loss_latency => {}
68 };
69 $self->{devices_connection_stats} = {};
70
71 return $self;
72 }
73
74 sub set_options {
75 my ($self, %options) = @_;
76
77 $self->{option_results} = $options{option_results};
78 }
79
80 sub set_defaults {}
81
82 sub check_options {
83 my ($self, %options) = @_;
84
85 $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : 'api.meraki.com';
86 $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443;
87 $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
88 $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
89 $self->{api_token} = (defined($self->{option_results}->{api_token})) ? $self->{option_results}->{api_token} : '';
90 $self->{reload_cache_time} = (defined($self->{option_results}->{reload_cache_time})) ? $self->{option_results}->{reload_cache_time} : 180;
91 $self->{reload_extra_cache_time} = (defined($self->{option_results}->{reload_extra_cache_time})) ? $self->{option_results}->{reload_extra_cache_time} : 10;
92 $self->{ignore_permission_errors} = (defined($self->{option_results}->{ignore_permission_errors})) ? 1 : 0;
93 $self->{ignore_orgs_api_disabled} = (defined($self->{option_results}->{ignore_orgs_api_disabled})) ? 1 : 0;
94 $self->{timespan} = (defined($self->{option_results}->{timespan})) ? $self->{option_results}->{timespan} : 300;
95
96 if ($self->{hostname} eq '') {
97 $self->{output}->add_option_msg(short_msg => "Need to specify --hostname option.");
98 $self->{output}->option_exit();
99 }
100 if ($self->{api_token} eq '') {
101 $self->{output}->add_option_msg(short_msg => "Need to specify --api-token option.");
102 $self->{output}->option_exit();
103 }
104
105 # we force to use storable module
106 $self->{option_results}->{statefile_storable} = 1;
107 $self->{cache}->check_options(option_results => $self->{option_results});
108 return 0;
109 }
110
111 sub get_token {
112 my ($self, %options) = @_;
113
114 return md5_hex($self->{api_token});
115 }
116
117 sub get_shard_hostname {
118 my ($self, %options) = @_;
119
120 my $organization_id = $options{organization_id};
121 my $network_id = $options{network_id};
122 if (defined($options{serial}) && defined($self->{datas}->{devices}->{ $options{serial} })) {
123 $network_id = $self->{datas}->{devices}->{ $options{serial} }->{networkId};
124 }
125 if (defined($network_id) && defined($self->{datas}->{networks}->{$network_id})) {
126 $organization_id = $self->{datas}->{networks}->{$network_id}->{organizationId};
127 }
128
129 if (defined($organization_id)) {
130 if (defined($self->{datas}->{orgs}->{$organization_id})
131 && $self->{datas}->{orgs}->{$organization_id}->{url} =~ /^(?:http|https):\/\/(.*?)\//) {
132 return $1;
133 }
134 }
135
136 return undef;
137 }
138
139 sub build_options_for_httplib {
140 my ($self, %options) = @_;
141
142 $self->{option_results}->{timeout} = $self->{timeout};
143 $self->{option_results}->{port} = $self->{port};
144 $self->{option_results}->{proto} = $self->{proto};
145 $self->{http}->add_header(key => 'X-Cisco-Meraki-API-Key', value => $self->{api_token});
146 $self->{http}->add_header(key => 'User-Agent', value => 'CentreonPlugin/1.0 Centreon');
147 }
148
149 sub settings {
150 my ($self, %options) = @_;
151
152 $self->build_options_for_httplib();
153 $self->{http}->set_options(%{$self->{option_results}});
154 }
155
156 sub request_api {
157 my ($self, %options) = @_;
158
159 $self->settings();
160
161 my $hostname = $self->{hostname};
162 if (defined($options{hostname})) {
163 $hostname = $options{hostname};
164 }
165
166 #400: Bad Request- You did something wrong, for example a malformed request or missing parameter.
167 #403: Forbidden- You don't have permission to do that.
168 #404: Not found- No such URL, or you don't have access to the API or organization at all.
169 #429: Too Many Requests- You submitted more than 5 calls in 1 second to an Organization, triggering rate limiting. This also applies for API calls made across multiple organizations that triggers rate limiting for one of the organizations.
170 my $results = [];
171 my $full_url;
172 my $get_param = defined($options{get_param}) ? $options{get_param} : [];
173 if (defined($options{paginate})) {
174 push @$get_param, 'perPage=' . $options{paginate};
175 }
176 while (1) {
177 my $response = $self->{http}->request(
178 full_url => $full_url,
179 hostname => $hostname,
180 url_path => '/api/v1' . $options{endpoint},
181 get_param => $get_param,
182 critical_status => '',
183 warning_status => '',
184 unknown_status => ''
185 );
186
187 my $code = $self->{http}->get_code();
188
189 return [] if ($code == 403 && $self->{ignore_permission_errors} == 1);
190 return undef if (defined($options{ignore_codes}) && defined($options{ignore_codes}->{$code}));
191
192 if ($code == 429) {
193 my ($retry) = $self->{http}->get_header(name => 'Retry-After');
194 $retry = defined($retry) && $retry =~ /^\s*(\d+)\s*/ ? $retry : 1;
195 sleep($retry);
196 next;
197 }
198
199 if ($code < 200 || $code >= 300) {
200 $self->{output}->add_option_msg(short_msg => $code . ' ' . $self->{http}->get_message());
201 $self->{output}->option_exit();
202 }
203
204 my $content;
205 eval {
206 $content = JSON::XS->new->utf8->allow_nonref(1)->decode($response);
207 };
208 if ($@) {
209 $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
210 $self->{output}->option_exit();
211 }
212
213 if (defined($options{paginate})) {
214 push @$results, @$content;
215
216 my ($link) = $self->{http}->get_header(name => 'Link');
217 return $results if (!defined($link) || $link !~ /,\s+<([^;]*?)>;\s+rel=next/);
218
219 $get_param = undef;
220 $full_url = $1;
221 next;
222 }
223
224 return ($content);
225 }
226 }
227
228 sub write_cache_file {
229 my ($self, %options) = @_;
230
231 $self->{cache}->read(
232 statefile => 'cache_meraki_' . md5_hex(
233 $self->{api_token} . '_' .
234 (defined($self->{option_results}->{api_filter_orgs}) ? $self->{option_results}->{api_filter_orgs} : '')
235 )
236 );
237 $self->{cache}->write(data => {
238 update_time => time(),
239 response => $self->{datas}
240 });
241 }
242
243 sub get_cache_file_response {
244 my ($self, %options) = @_;
245 my $cache_filename = 'cache_meraki_'
246 . md5_hex($self->{api_token} . '_' .(defined($self->{option_results}->{api_filter_orgs}) ?
247 $self->{option_results}->{api_filter_orgs} : '')
248 );
249
250 $self->{cache}->read(
251 statefile => $cache_filename
252 );
253 $self->{datas} = $self->{cache}->get(name => 'response');
254 if (!defined($self->{datas})) {
255 $self->{output}->add_option_msg(short_msg => 'Cache file missing or could not load ' . $cache_filename);
256 $self->{output}->option_exit();
257 }
258
259 return $self->{datas};
260 }
261
262 sub call_datas {
263 my ($self, %options) = @_;
264
265 $self->get_organizations();
266 if (!defined($options{skipNetworks})) {
267 $self->get_networks(orgs => [keys %{$self->{datas}->{orgs}}]);
268 }
269
270 if (!defined($options{skipDevices})) {
271 $self->get_devices(orgs => [keys %{$self->{datas}->{orgs}}]);
272 }
273 if (!defined($options{skipDevicesStatus})) {
274 $self->get_organization_device_statuses(orgs => [keys %{$self->{datas}->{orgs}}]);
275 }
276 if (!defined($options{skipVpnTunnelsStatus})) {
277 $self->get_organization_vpn_tunnels_statuses(orgs => [keys %{$self->{datas}->{orgs}}]);
278 }
279
280 if (defined($options{cache})) {
281 foreach my $orgId (keys %{$self->{datas}->{orgs}}) {
282 $self->get_network_device_uplink(orgId => $orgId);
283 }
284 foreach my $orgId (keys %{$self->{datas}->{orgs}}) {
285 $self->get_organization_uplink_loss_and_latency(orgId => $orgId);
286 }
287 }
288 }
289
290 sub cache_datas {
291 my ($self, %options) = @_;
292
293 $self->call_datas(cache => 1);
294 $self->write_cache_file();
295
296 return $self->{datas};
297 }
298
299 sub get_datas {
300 my ($self, %options) = @_;
301
302 return $self->get_cache_file_response() if (defined($self->{option_results}->{cache_use}));
303 $self->call_datas(%options);
304 return $self->{datas};
305 }
306
307 sub get_organizations {
308 my ($self, %options) = @_;
309
310 my $datas = $self->request_api(endpoint => '/organizations');
311 $self->{datas}->{orgs} = {};
312 if (defined($datas)) {
313 foreach (@$datas) {
314 next if (defined($self->{option_results}->{api_filter_orgs}) && $self->{option_results}->{api_filter_orgs} ne '' &&
315 $_->{name} !~ /$self->{option_results}->{api_filter_orgs}/);
316
317 $self->{datas}->{orgs}->{ $_->{id} } = {
318 name => $_->{name},
319 url => $_->{url}
320 };
321 }
322 }
323
324 return $self->{datas}->{orgs};
325 }
326
327 sub get_networks {
328 my ($self, %options) = @_;
329
330 my $ignore_codes = {};
331 $ignore_codes = { 404 => 1 } if ($self->{ignore_orgs_api_disabled} == 1);
332
333 $self->{datas}->{networks} = {};
334 foreach my $id (@{$options{orgs}}) {
335 my $datas = $self->request_api(
336 endpoint => '/organizations/' . $id . '/networks',
337 hostname => $self->get_shard_hostname(organization_id => $id),
338 ignore_codes => $ignore_codes
339 );
340
341 if (!defined($datas) && $self->{ignore_orgs_api_disabled} == 1) {
342 delete $self->{datas}->{orgs}->{$id};
343 next;
344 }
345
346 if (defined($datas)) {
347 foreach (@$datas) {
348 if (defined($options{extended})) {
349 $self->{datas}->{networks}->{ $_->{id} } = $_;
350 } else {
351 $self->{datas}->{networks}->{ $_->{id} } = {
352 name => $_->{name},
353 organizationId => $_->{organizationId}
354 };
355 }
356 }
357 }
358 }
359
360 return $self->{datas}->{networks};
361 }
362
363 sub get_devices {
364 my ($self, %options) = @_;
365
366 $self->{datas}->{devices} = {};
367 foreach my $id (@{$options{orgs}}) {
368 my $datas = $self->request_api(
369 endpoint => '/organizations/' . $id . '/devices',
370 paginate => 1000,
371 hostname => $self->get_shard_hostname(organization_id => $id)
372 );
373 if (defined($datas)) {
374 foreach (@$datas) {
375 if (defined($options{extended})) {
376 $self->{datas}->{devices}->{ $_->{serial} } = $_;
377 $self->{datas}->{devices}->{ $_->{serial} }->{orgId} = $id;
378 } else {
379 $self->{datas}->{devices}->{ $_->{serial} } = {
380 name => $_->{name},
381 networkId => $_->{networkId},
382 orgId => $id,
383 tags => $_->{tags},
384 model => $_->{model}
385 };
386 }
387 }
388 }
389 }
390
391 return $self->{datas}->{devices};
392 }
393
394 sub get_organization_device_statuses {
395 my ($self, %options) = @_;
396
397 $self->{datas}->{devices_status} = {};
398 foreach my $id (@{$options{orgs}}) {
399 my $datas = $self->request_api(
400 endpoint => '/organizations/' . $id . '/devices/statuses',
401 paginate => 1000,
402 hostname => $self->get_shard_hostname(organization_id => $id)
403 );
404 foreach (@$datas) {
405 if (defined($options{extended})) {
406 $self->{datas}->{devices_status}->{ $_->{serial} } = $_;
407 } else {
408 $self->{datas}->{devices_status}->{ $_->{serial} } = {
409 status => $_->{status}
410 };
411 }
412 }
413 }
414
415 return $self->{datas}->{devices_status};
416 }
417
418 sub get_organization_vpn_tunnels_statuses {
419 my ($self, %options) = @_;
420
421 $self->{datas}->{vpn_tunnels_status} = {};
422 foreach my $id (@{$options{orgs}}) {
423 my $datas = $self->request_api(
424 endpoint => '/organizations/' . $id . '/appliance/vpn/statuses',
425 paginate => 300,
426 hostname => $self->get_shard_hostname(organization_id => $id),
427 ignore_codes => { 400 => 1 } # it can be disabled
428 );
429 foreach (@$datas) {
430 $self->{datas}->{vpn_tunnels_status}->{ $_->{deviceSerial} } = $_;
431 $self->{datas}->{vpn_tunnels_status}->{ $_->{deviceSerial} }->{organizationId} = $id;
432 }
433 }
434
435 return $self->{datas}->{vpn_tunnels_status};
436 }
437
438 sub get_network_device_uplink {
439 my ($self, %options) = @_;
440
441 if (!defined($self->{datas}->{uplink_statuses}->{ $options{orgId} })) {
442 $self->{datas}->{uplink_statuses}->{ $options{orgId} } = {};
443 my $datas = $self->request_api(
444 endpoint => '/organizations/' . $options{orgId} . '/uplinks/statuses',
445 paginate => 1000,
446 hostname => $self->get_shard_hostname(organization_id => $options{orgId})
447 );
448 foreach (@$datas) {
449 $self->{datas}->{uplink_statuses}->{ $options{orgId} }->{ $_->{serial} } = $_->{uplinks};
450 }
451 }
452
453 return defined($options{serial}) ?
454 $self->{datas}->{uplink_statuses}->{ $options{orgId} }->{ $options{serial} } :
455 $self->{datas}->{uplink_statuses}->{ $options{orgId} };
456 }
457
458 sub get_organization_uplink_loss_and_latency {
459 my ($self, %options) = @_;
460
461 if (!defined($self->{datas}->{uplinks_loss_latency}->{ $options{orgId} })) {
462 $self->{datas}->{uplinks_loss_latency}->{ $options{orgId} } = {};
463 my $datas = $self->request_api(
464 endpoint => '/organizations/' . $options{orgId} . '/devices/uplinksLossAndLatency',
465 get_param => [ 'timespan=' . $self->{timespan} ],
466 paginate => 1000,
467 hostname => $self->get_shard_hostname(organization_id => $options{orgId}),
468 ignore_codes => { 404 => 1 }
469 );
470
471 if (defined($datas)) {
472 foreach (@$datas) {
473 # sometimes uplink is undef. so we skip
474 next if (!defined($_->{uplink}) || !defined($_->{serial}));
475
476 $self->{datas}->{uplinks_loss_latency}->{ $options{orgId} }->{ $_->{serial} } = {}
477 if (!defined($self->{datas}->{uplinks_loss_latency}->{ $options{orgId} }->{ $_->{serial} }));
478 $self->{datas}->{uplinks_loss_latency}->{ $options{orgId} }->{ $_->{serial} }->{ $_->{uplink} } = $_;
479 }
480 }
481 }
482
483 return defined($options{serial}) ?
484 $self->{datas}->{uplinks_loss_latency}->{ $options{orgId} }->{ $options{serial} } :
485 $self->{datas}->{uplinks_loss_latency}->{ $options{orgId} };
486 }
487
488 sub get_device_clients {
489 my ($self, %options) = @_;
490
491 return $self->request_api(
492 endpoint => '/devices/' . $options{serial} . '/clients',
493 get_param => [ 'timespan=' . $self->{timespan} ],
494 hostname => $self->get_shard_hostname(serial => $options{serial})
495 )
496 }
497
498 sub get_device_switch_port_statuses {
499 my ($self, %options) = @_;
500
501 return $self->request_api(
502 endpoint => '/devices/' . $options{serial} . '/switch/ports/statuses',
503 get_param => [ 'timespan=' . $self->{timespan} ],
504 hostname => $self->get_shard_hostname(serial => $options{serial})
505 );
506 }
507
508 sub get_network_device_connection_stats {
509 my ($self, %options) = @_;
510
511 if (!defined($self->{devices_connection_stats}->{ $options{network_id} })) {
512 $self->{devices_connection_stats}->{ $options{network_id} } = {};
513 my $datas = $self->request_api(
514 endpoint => '/networks/' . $options{network_id} . '/wireless/devices/connectionStats',
515 get_param => [ 'timespan=' . $self->{timespan} ],
516 hostname => $self->get_shard_hostname(network_id => $options{network_id})
517 );
518 foreach (@$datas) {
519 $self->{devices_connection_stats}->{ $options{network_id} }->{ $_->{serial} } = $_->{connectionStats};
520 }
521 }
522
523 return defined($options{serial}) ?
524 $self->{devices_connection_stats}->{ $options{network_id} }->{ $options{serial} } :
525 $self->{devices_connection_stats}->{ $options{network_id} };
526 }
527
528 sub get_network_device_performance {
529 my ($self, %options) = @_;
530
531 $self->request_api(
532 endpoint => '/devices/' . $options{serial} . '/appliance/performance',
533 hostname => $self->get_shard_hostname(network_id => $options{network_id}),
534 ignore_codes => { 400 => 1, 204 => 1 }
535 );
536 }
537
538 sub get_organization_api_requests_overview {
539 my ($self, %options) = @_;
540
541 my $results = {};
542 foreach my $id (@{$options{orgs}}) {
543 $results->{$id} = $self->request_api(
544 endpoint => '/organizations/' . $id . '/apiRequests/overview',
545 get_param => [ 'timespan=' . $self->{timespan} ],
546 hostname => $self->get_shard_hostname(organization_id => $id)
547 );
548 }
549
550 return $results;
551 }
552
553 sub get_networks_connection_stats {
554 my ($self, %options) = @_;
555
556 return $self->request_api(
557 endpoint => '/networks/' . $options{network_id},
558 hostname => $self->get_shard_hostname(network_id => $options{network_id}),
559 ignore_codes => { 400 => 1 }
560 );
561 }
562
563 sub get_networks_clients {
564 my ($self, %options) = @_;
565
566 return $self->request_api(
567 endpoint => '/networks/' . $options{network_id} . '/clients',
568 get_param => [ 'timespan=' . $self->{timespan} ],
569 hostname => $self->get_shard_hostname(network_id => $options{network_id}),
570 ignore_codes => { 400 => 1 }
571 );
572 }
573
574 1;
575
576 __END__
577
578 =head1 NAME
579
580 Meraki REST API
581
582 =head1 SYNOPSIS
583
584 Rest API custom mode
585
586 =head1 REST API OPTIONS
587
588 =over 8
589
590 =item B<--hostname>
591
592 Meraki API hostname (default: 'api.meraki.com')
593
594 =item B<--port>
595
596 Define the TCP port to use to reach the API (default: 443).
597
598 =item B<--proto>
599
600 Define the protocol to reach the API (default: 'https').
601
602 =item B<--api-token>
603
604 Meraki API token.
605
606 =item B<--timeout>
607
608 Define the timeout for HTTP requests.
609
610 =item B<--ignore-permission-errors>
611
612 Ignore permission errors (403 status code).
613
614 =item B<--ignore-orgs-api-disabled>
615
616 Ignore organizations where the API is disabled.
617
618 =item B<--api-filter-orgs>
619
620 Define the organizations to monitor (regular expression).
621
622 =item B<--cache-use>
623
624 Use the cache file instead of requesting the API (the cache file can be created with the cache mode).
625
626 =back
627
628 =head1 DESCRIPTION
629
630 B<custom>.
631
632 =cut