add organization filter for networks
This commit is contained in:
parent
a38f2f3427
commit
eee2b808cd
|
@ -315,21 +315,6 @@ sub get_devices {
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub filter_networks {
|
|
||||||
my ($self, %options) = @_;
|
|
||||||
|
|
||||||
my $network_ids = [];
|
|
||||||
foreach (values %{$self->{cache_networks}}) {
|
|
||||||
if (!defined($options{filter_name}) || $options{filter_name} eq '') {
|
|
||||||
push @$network_ids, $_->{id};
|
|
||||||
} elsif ($_->{name} =~ /$options{filter_name}/) {
|
|
||||||
push @$network_ids, $_->{id};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $network_ids;
|
|
||||||
}
|
|
||||||
|
|
||||||
sub filter_organizations {
|
sub filter_organizations {
|
||||||
my ($self, %options) = @_;
|
my ($self, %options) = @_;
|
||||||
|
|
||||||
|
@ -349,42 +334,30 @@ sub get_networks_connection_stats {
|
||||||
my ($self, %options) = @_;
|
my ($self, %options) = @_;
|
||||||
|
|
||||||
$self->cache_meraki_entities();
|
$self->cache_meraki_entities();
|
||||||
my $network_ids = $self->filter_networks(filter_name => $options{filter_name});
|
|
||||||
|
|
||||||
my $timespan = defined($options{timespan}) ? $options{timespan} : 300;
|
my $timespan = defined($options{timespan}) ? $options{timespan} : 300;
|
||||||
$timespan = 1 if ($timespan <= 0);
|
$timespan = 1 if ($timespan <= 0);
|
||||||
my $results = {};
|
|
||||||
foreach my $id (@$network_ids) {
|
return $self->request_api(
|
||||||
my $datas = $self->request_api(
|
endpoint => '/networks/' . $options{network_id} . '/connectionStats?timespan=' . $options{timespan},
|
||||||
endpoint => '/networks/' . $id . '/connectionStats?timespan=' . $options{timespan},
|
hostname => $self->get_shard_hostname(network_id => $options{network_id}),
|
||||||
hostname => $self->get_shard_hostname(network_id => $id),
|
|
||||||
ignore_codes => { 400 => 1 }
|
ignore_codes => { 400 => 1 }
|
||||||
);
|
);
|
||||||
$results->{$id} = $datas;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $results;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sub get_networks_clients {
|
sub get_networks_clients {
|
||||||
my ($self, %options) = @_;
|
my ($self, %options) = @_;
|
||||||
|
|
||||||
$self->cache_meraki_entities();
|
$self->cache_meraki_entities();
|
||||||
my $network_ids = $self->filter_networks(filter_name => $options{filter_name});
|
|
||||||
|
|
||||||
my $timespan = defined($options{timespan}) ? $options{timespan} : 300;
|
my $timespan = defined($options{timespan}) ? $options{timespan} : 300;
|
||||||
$timespan = 1 if ($timespan <= 0);
|
$timespan = 1 if ($timespan <= 0);
|
||||||
my $results = {};
|
|
||||||
foreach my $id (@$network_ids) {
|
return $self->request_api(
|
||||||
my $datas = $self->request_api(
|
endpoint => '/networks/' . $options{network_id} . '/clients?timespan=' . $options{timespan},
|
||||||
endpoint => '/networks/' . $id . '/clients?timespan=' . $options{timespan},
|
hostname => $self->get_shard_hostname(network_id => $options{network_id}),
|
||||||
hostname => $self->get_shard_hostname(network_id => $id),
|
|
||||||
ignore_codes => { 400 => 1 }
|
ignore_codes => { 400 => 1 }
|
||||||
);
|
);
|
||||||
$results->{$id} = $datas;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $results;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sub get_organization_device_statuses {
|
sub get_organization_device_statuses {
|
||||||
|
|
|
@ -107,7 +107,9 @@ sub new {
|
||||||
bless $self, $class;
|
bless $self, $class;
|
||||||
|
|
||||||
$options{options}->add_options(arguments => {
|
$options{options}->add_options(arguments => {
|
||||||
'filter-network-name:s' => { name => 'filter_network_name' }
|
'filter-network-name:s' => { name => 'filter_network_name' },
|
||||||
|
'filter-organization-name:s' => { name => 'filter_organization_name' },
|
||||||
|
'filter-organization-id:s' => { name => 'filter_organization_id' }
|
||||||
});
|
});
|
||||||
|
|
||||||
return $self;
|
return $self;
|
||||||
|
@ -118,37 +120,64 @@ sub manage_selection {
|
||||||
|
|
||||||
$self->{cache_name} = 'meraki_' . $self->{mode} . '_' . $options{custom}->get_token() . '_' .
|
$self->{cache_name} = 'meraki_' . $self->{mode} . '_' . $options{custom}->get_token() . '_' .
|
||||||
(defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' .
|
(defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' .
|
||||||
(defined($self->{option_results}->{filter_network_name}) ? md5_hex($self->{option_results}->{filter_network_name}) : md5_hex('all'));
|
(defined($self->{option_results}->{filter_network_name}) ? md5_hex($self->{option_results}->{filter_network_name}) : md5_hex('all')) . '_' .
|
||||||
|
(defined($self->{option_results}->{filter_organization_id}) ? md5_hex($self->{option_results}->{filter_organization_id}) : md5_hex('all')) . '_' .
|
||||||
|
(defined($self->{option_results}->{filter_organization_name}) ? md5_hex($self->{option_results}->{filter_organization_name}) : md5_hex('all'));
|
||||||
my $last_timestamp = $self->read_statefile_key(key => 'last_timestamp');
|
my $last_timestamp = $self->read_statefile_key(key => 'last_timestamp');
|
||||||
my $timespan = 300;
|
my $timespan = 300;
|
||||||
$timespan = time() - $last_timestamp if (defined($last_timestamp));
|
$timespan = time() - $last_timestamp if (defined($last_timestamp));
|
||||||
|
|
||||||
my $cache_networks = $options{custom}->get_cache_networks();
|
|
||||||
my $connections = $options{custom}->get_networks_connection_stats(timespan => $timespan, filter_name => $self->{option_results}->{filter_network_name});
|
|
||||||
my $clients = $options{custom}->get_networks_clients(timespan => $timespan, filter_name => $self->{option_results}->{filter_network_name});
|
|
||||||
|
|
||||||
$self->{networks} = {};
|
$self->{networks} = {};
|
||||||
foreach my $id (keys %$connections) {
|
|
||||||
$self->{networks}->{$id} = {
|
my $cache_networks = $options{custom}->get_cache_networks();
|
||||||
display => $cache_networks->{$id}->{name},
|
foreach (values %$cache_networks) {
|
||||||
assoc => defined($connections->{$id}->{assoc}) ? $connections->{$id}->{assoc} : 0,
|
if (defined($self->{option_results}->{filter_network_name}) && $self->{option_results}->{filter_network_name} ne '' &&
|
||||||
auth => defined($connections->{$id}->{auth}) ? $connections->{$id}->{auth} : 0,
|
$_->{name} !~ /$self->{option_results}->{filter_device_name}/) {
|
||||||
dhcp => defined($connections->{$id}->{dhcp}) ? $connections->{$id}->{dhcp} : 0,
|
$self->{output}->output_add(long_msg => "skipping network '" . $_->{name} . "': no matching filter.", debug => 1);
|
||||||
dns => defined($connections->{$id}->{dns}) ? $connections->{$id}->{dns} : 0,
|
next;
|
||||||
success => defined($connections->{$id}->{success}) ? $connections->{$id}->{success} : 0,
|
}
|
||||||
|
|
||||||
|
my $organization = $options{custom}->get_organization(network_id => $_->{id});
|
||||||
|
if (defined($self->{option_results}->{filter_organization_id}) && $self->{option_results}->{filter_organization_id} ne '' &&
|
||||||
|
$organization->{id} !~ /$self->{option_results}->{filter_organization_id}/) {
|
||||||
|
$self->{output}->output_add(long_msg => "skipping device '" . $_->{name} . "': no matching filter.", debug => 1);
|
||||||
|
next;
|
||||||
|
}
|
||||||
|
if (defined($self->{option_results}->{filter_organization_name}) && $self->{option_results}->{filter_organization_name} ne '' &&
|
||||||
|
$organization->{name} !~ /$self->{option_results}->{filter_organization_name}/) {
|
||||||
|
$self->{output}->output_add(long_msg => "skipping device '" . $_->{name} . "': no matching filter.", debug => 1);
|
||||||
|
next;
|
||||||
|
}
|
||||||
|
|
||||||
|
my $connections = $options{custom}->get_networks_connection_stats(
|
||||||
|
timespan => $timespan,
|
||||||
|
network_id => $_->{id}
|
||||||
|
);
|
||||||
|
my $clients = $options{custom}->get_networks_clients(
|
||||||
|
timespan => $timespan,
|
||||||
|
network_id => $_->{id}
|
||||||
|
);
|
||||||
|
|
||||||
|
$self->{networks}->{ $_->{id} } = {
|
||||||
|
display => $_->{name},
|
||||||
|
assoc => defined($connections->{assoc}) ? $connections->{assoc} : 0,
|
||||||
|
auth => defined($connections->{auth}) ? $connections->{auth} : 0,
|
||||||
|
dhcp => defined($connections->{dhcp}) ? $connections->{dhcp} : 0,
|
||||||
|
dns => defined($connections->{dns}) ? $connections->{dns} : 0,
|
||||||
|
success => defined($connections->{success}) ? $connections->{success} : 0,
|
||||||
traffic_in => 0, traffic_out => 0
|
traffic_in => 0, traffic_out => 0
|
||||||
};
|
};
|
||||||
|
|
||||||
if (defined($clients->{$id})) {
|
if (defined($clients)) {
|
||||||
foreach (@{$clients->{$id}}) {
|
foreach my $client (@$clients) {
|
||||||
$self->{networks}->{$id}->{traffic_in} += $_->{usage}->{recv} * 8;
|
$self->{networks}->{ $_->{id} }->{traffic_in} += $client->{usage}->{recv} * 8;
|
||||||
$self->{networks}->{$id}->{traffic_out} += $_->{usage}->{sent} * 8;
|
$self->{networks}->{ $_->{id} }->{traffic_out} += $client->{usage}->{sent} * 8;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (scalar(keys %{$self->{networks}}) <= 0) {
|
if (scalar(keys %{$self->{networks}}) <= 0) {
|
||||||
$self->{output}->add_option_msg(short_msg => "No networks found.");
|
$self->{output}->add_option_msg(short_msg => 'No networks found.');
|
||||||
$self->{output}->option_exit();
|
$self->{output}->option_exit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -167,6 +196,14 @@ Check networks.
|
||||||
|
|
||||||
Filter network name (Can be a regexp).
|
Filter network name (Can be a regexp).
|
||||||
|
|
||||||
|
=item B<--filter-organization-id>
|
||||||
|
|
||||||
|
Filter networks by organization id (Can be a regexp).
|
||||||
|
|
||||||
|
=item B<--filter-organization-name>
|
||||||
|
|
||||||
|
Filter networks by organization name (Can be a regexp).
|
||||||
|
|
||||||
=item B<--warning-*> B<--critical-*>
|
=item B<--warning-*> B<--critical-*>
|
||||||
|
|
||||||
Thresholds.
|
Thresholds.
|
||||||
|
|
Loading…
Reference in New Issue