From 389cd2d23dfcde8b7d73121365b7c5976e555722 Mon Sep 17 00:00:00 2001 From: qgarnier Date: Wed, 10 Mar 2021 10:02:21 +0100 Subject: [PATCH] enh cisco meraki rest api (#2640) --- .../cloudcontroller/restapi/custom/api.pm | 63 ++++++++++++++----- .../cloudcontroller/restapi/mode/devices.pm | 9 ++- 2 files changed, 53 insertions(+), 19 deletions(-) diff --git a/network/cisco/meraki/cloudcontroller/restapi/custom/api.pm b/network/cisco/meraki/cloudcontroller/restapi/custom/api.pm index 54d84fc5c..0207707fc 100644 --- a/network/cisco/meraki/cloudcontroller/restapi/custom/api.pm +++ b/network/cisco/meraki/cloudcontroller/restapi/custom/api.pm @@ -52,7 +52,8 @@ sub new { 'ignore-permission-errors' => { name => 'ignore_permission_errors' }, 'use-extra-cache' => { name => 'use_extra_cache' }, 'reload-extra-cache-time:s' => { name => 'reload_extra_cache_time' }, - 'trace-api:s' => { name => 'trace_api' } + 'trace-api:s' => { name => 'trace_api' }, + 'api-requests-disabled' => { name => 'api_requests_disabled' } }); } $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1); @@ -67,7 +68,9 @@ sub new { uplink_statuses => {}, uplinks_loss_latency => {}, devices_statuses => {}, - devices_connection_stats => {} + devices_connection_stats => {}, + devices_performance => {}, + devices_clients => {} }; return $self; @@ -225,6 +228,8 @@ sub settings { sub request_api { my ($self, %options) = @_; + return undef if (defined($self->{option_results}->{api_requests_disabled})); + $self->settings(); my $hostname = $self->{hostname}; @@ -290,7 +295,6 @@ sub cache_meraki_entities { $self->{cache_devices} = $self->{cache}->get(name => 'devices'); if ($has_cache_file == 0 || !defined($timestamp_cache) || ((time() - $timestamp_cache) > (($self->{reload_cache_time}) * 60))) { - $self->{cache_organizations} = {}; $self->{cache_organizations} = $self->get_organizations( disable_cache => 1 ); @@ -319,7 +323,9 @@ sub get_organizations { return $self->{cache_organizations} if (!defined($options{disable_cache}) || $options{disable_cache} == 0); my $datas = $self->request_api(endpoint => '/organizations'); my $results = {}; - $results->{$_->{id}} = $_ foreach (@$datas); + if (defined($datas)) { + $results->{$_->{id}} = $_ foreach (@$datas); + } return $results; } @@ -336,7 +342,9 @@ sub get_networks { endpoint => '/organizations/' . $id . '/networks', hostname => $self->get_shard_hostname(organization_id => $id) ); - $results->{$_->{id}} = $_ foreach (@$datas); + if (defined($datas)) { + $results->{ $_->{id} } = $_ foreach (@$datas); + } } return $results; @@ -354,7 +362,9 @@ sub get_devices { endpoint => '/organizations/' . $id . '/devices', hostname => $self->get_shard_hostname(organization_id => $id) ); - $results->{$_->{serial}} = $_ foreach (@$datas); + if (defined($datas)) { + $results->{ $_->{serial} } = $_ foreach (@$datas); + } } return $results; @@ -512,13 +522,23 @@ sub get_device_clients { my ($self, %options) = @_; $self->cache_meraki_entities(); + $self->load_extra_cache(); my $timespan = defined($options{timespan}) ? $options{timespan} : 300; $timespan = 1 if ($timespan <= 0); - return $self->request_api( - endpoint => '/devices/' . $options{serial} . '/clients?timespan=' . $options{timespan}, - hostname => $self->get_shard_hostname(serial => $options{serial}) - ); + if (!defined($self->{cached}->{devices_clients}->{ $options{serial} })) { + $self->{cached}->{updated} = 1; + # 400 = feature not supported. 204 = no content + $self->{cached}->{devices_clients}->{ $options{serial} } = { + update_time => time(), + data => $self->request_api( + endpoint => '/devices/' . $options{serial} . '/clients?timespan=' . $options{timespan}, + hostname => $self->get_shard_hostname(serial => $options{serial}) + ) + }; + } + + return $self->{cached}->{devices_clients}->{ $options{serial} }->{data}; } sub get_device_switch_port_statuses { @@ -538,13 +558,22 @@ sub get_network_device_performance { my ($self, %options) = @_; $self->cache_meraki_entities(); + $self->load_extra_cache(); - # 400 = feature not supported. 204 = no content - return $self->request_api( - endpoint => '/devices/' . $options{serial} . '/appliance/performance', - hostname => $self->get_shard_hostname(network_id => $options{network_id}), - ignore_codes => { 400 => 1, 204 => 1 } - ); + if (!defined($self->{cached}->{devices_performance}->{ $options{serial} })) { + $self->{cached}->{updated} = 1; + # 400 = feature not supported. 204 = no content + $self->{cached}->{devices_performance}->{ $options{serial} } = { + update_time => time(), + data => $self->request_api( + endpoint => '/devices/' . $options{serial} . '/appliance/performance', + hostname => $self->get_shard_hostname(network_id => $options{network_id}), + ignore_codes => { 400 => 1, 204 => 1 } + ) + }; + } + + return $self->{cached}->{devices_performance}->{ $options{serial} }->{data}; } sub get_organization_uplink_loss_and_latency { @@ -593,7 +622,7 @@ sub load_extra_cache { $self->{cached}->{devices_statuses} = {}; } - foreach my $entry (('uplink_statuses', 'uplinks_loss_latency', 'devices_connection_stats')) { + foreach my $entry (('uplink_statuses', 'uplinks_loss_latency', 'devices_connection_stats', 'devices_performance', 'devices_clients')) { next if (!defined($self->{cached}->{$entry})); foreach (keys %{$self->{cached}->{$entry}}) { diff --git a/network/cisco/meraki/cloudcontroller/restapi/mode/devices.pm b/network/cisco/meraki/cloudcontroller/restapi/mode/devices.pm index 459f77d79..e12cf28df 100644 --- a/network/cisco/meraki/cloudcontroller/restapi/mode/devices.pm +++ b/network/cisco/meraki/cloudcontroller/restapi/mode/devices.pm @@ -301,7 +301,8 @@ sub new { 'add-switch-ports' => { name => 'add_switch_ports' }, 'skip-traffic-disconnect-port' => { name => 'skip_traffic_disconnect_port' }, 'skip-clients' => { name => 'skip_clients' }, - 'skip-performance' => { name => 'skip_performance' } + 'skip-performance' => { name => 'skip_performance' }, + 'skip-connections' => { name => 'skip_connections' } }); return $self; @@ -512,7 +513,7 @@ sub manage_selection { device_ports => {} }; - if ($devices->{$serial}->{model} =~ /^(?:MG|MR)/) { + if (!defined($self->{option_results}->{skip_connections}) && $devices->{$serial}->{model} =~ /^(?:MG|MR)/) { $self->add_connection_stats( custom => $options{custom}, timespan => $timespan, @@ -618,6 +619,10 @@ Don't monitor clients traffic on device. Don't monitor appliance perfscore. +=item B<--skip-connections> + +Don't monitor connection stats. + =item B<--skip-traffic-disconnect-port> Skip port traffic counters if port status is disconnected.