enh cisco meraki rest api (#2640)

This commit is contained in:
qgarnier 2021-03-10 10:02:21 +01:00 committed by GitHub
parent 6029679103
commit 389cd2d23d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 19 deletions

View File

@ -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}}) {

View File

@ -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.