diff --git a/network/cisco/meraki/cloudcontroller/restapi/custom/api.pm b/network/cisco/meraki/cloudcontroller/restapi/custom/api.pm index bfc2a979e..f846f6d12 100644 --- a/network/cisco/meraki/cloudcontroller/restapi/custom/api.pm +++ b/network/cisco/meraki/cloudcontroller/restapi/custom/api.pm @@ -116,10 +116,17 @@ sub get_token { sub get_cache_networks { my ($self, %options) = @_; - $self->cache_networks_organizations(); + $self->cache_meraki_entities(); return $self->{cache_networks}; } +sub get_cache_devices { + my ($self, %options) = @_; + + $self->cache_meraki_entities(); + return $self->{cache_devices}; +} + sub build_options_for_httplib { my ($self, %options) = @_; @@ -171,7 +178,7 @@ sub request_api { } while (1); } -sub cache_networks_organizations { +sub cache_meraki_entities { my ($self, %options) = @_; return if ($self->{cache_checked} == 1); @@ -181,16 +188,19 @@ sub cache_networks_organizations { my $timestamp_cache = $self->{cache}->get(name => 'last_timestamp'); $self->{cache_organizations} = $self->{cache}->get(name => 'organizations'); $self->{cache_networks} = $self->{cache}->get(name => 'networks'); + $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); $self->{cache_networks} = $self->get_networks(organizations => [keys %{$self->{cache_organizations}}], disable_cache => 1); + $self->{cache_devices} = $self->get_devices(organizations => [keys %{$self->{cache_organizations}}], disable_cache => 1); $self->{cache}->write(data => { last_timestamp => time(), organizations => $self->{cache_organizations}, - networks => $self->{cache_networks} + networks => $self->{cache_networks}, + devices => $self->{cache_devices} }); } } @@ -198,7 +208,7 @@ sub cache_networks_organizations { sub get_organizations { my ($self, %options) = @_; - $self->cache_networks_organizations(); + $self->cache_meraki_entities(); return $self->{cache_organizations} if (!defined($options{disable_cache}) || $options{disable_cache} == 0); my $datas = $self->request_api(endpoint => '/organizations'); my $results = {}; @@ -210,7 +220,7 @@ sub get_organizations { sub get_networks { my ($self, %options) = @_; - $self->cache_networks_organizations(); + $self->cache_meraki_entities(); return $self->{cache_networks} if (!defined($options{disable_cache}) || $options{disable_cache} == 0); my $results = {}; @@ -222,6 +232,21 @@ sub get_networks { return $results; } +sub get_devices { + my ($self, %options) = @_; + + $self->cache_meraki_entities(); + return $self->{cache_devices} if (!defined($options{disable_cache}) || $options{disable_cache} == 0); + + my $results = {}; + foreach my $id (keys %{$self->{cache_organizations}}) { + my $datas = $self->request_api(endpoint => '/organizations/' . $id . '/devices'); + $results->{$_->{serial}} = $_ foreach (@$datas); + } + + return $results; +} + sub filter_networks { my ($self, %options) = @_; @@ -242,10 +267,25 @@ sub filter_networks { return $network_ids; } +sub filter_organizations { + my ($self, %options) = @_; + + my $organization_ids = []; + foreach (values %{$self->{cache_organizations}}) { + if (!defined($options{filter_name}) || $options{filter_name} eq '') { + push @$organization_ids, $_->{id}; + } elsif ($_->{name} =~ /$options{filter_name}/) { + push @$organization_ids, $_->{id}; + } + } + + return $organization_ids; +} + sub get_networks_connection_stats { my ($self, %options) = @_; - $self->cache_networks_organizations(); + $self->cache_meraki_entities(); my $network_ids = $self->filter_networks(filter_name => $options{filter_name}); my $timespan = defined($options{timespan}) ? $options{timespan} : 300; @@ -262,7 +302,7 @@ sub get_networks_connection_stats { sub get_networks_clients { my ($self, %options) = @_; - $self->cache_networks_organizations(); + $self->cache_meraki_entities(); my $network_ids = $self->filter_networks(filter_name => $options{filter_name}); my $timespan = defined($options{timespan}) ? $options{timespan} : 300; @@ -276,8 +316,82 @@ sub get_networks_clients { return $results; } -sub get_device_statuses { +sub get_organization_device_statuses { my ($self, %options) = @_; + + $self->cache_meraki_entities(); + my $organization_ids = $self->filter_organizations(filter_name => $options{filter_name}); + my $results = {}; + foreach my $id (@$organization_ids) { + my $datas = $self->request_api(endpoint => '/organizations/' . $id . '/deviceStatuses'); + foreach (@$datas) { + $results->{$_->{serial}} = $_; + $results->{organizationId} = $id; + } + } + + return $results; +} + +sub get_network_device_connection_stats { + my ($self, %options) = @_; + + if (scalar(keys %{$options{devices}}) > 5) { + $self->{output}->add_option_msg(short_msg => 'cannot check more than 5 devices at once (api rate limit)'); + $self->{output}->option_exit(); + } + + $self->cache_meraki_entities(); + my $timespan = defined($options{timespan}) ? $options{timespan} : 300; + $timespan = 1 if ($timespan <= 0); + + my $results = {}; + foreach (keys %{$options{devices}}) { + my $data = $self->request_api(endpoint => '/networks/' . $options{devices}->{$_} . '/devices/' . $_ . '/connectionStats?timespan=' . $options{timespan}); + $results->{$_} = $data; + } + + return $results; +} + +sub get_network_device_uplink { + my ($self, %options) = @_; + + if (scalar(keys %{$options{devices}}) > 5) { + $self->{output}->add_option_msg(short_msg => 'cannot check more than 5 devices at once (api rate limit)'); + $self->{output}->option_exit(); + } + + $self->cache_meraki_entities(); + + my $results = {}; + foreach (keys %{$options{devices}}) { + my $data = $self->request_api(endpoint => '/networks/' . $options{devices}->{$_} . '/devices/' . $_ . '/uplink'); + $results->{$_} = $data; + } + + return $results; +} + +sub get_device_clients { + my ($self, %options) = @_; + + if (scalar(keys %{$options{devices}}) > 5) { + $self->{output}->add_option_msg(short_msg => 'cannot check more than 5 devices at once (api rate limit)'); + $self->{output}->option_exit(); + } + + $self->cache_meraki_entities(); + my $timespan = defined($options{timespan}) ? $options{timespan} : 300; + $timespan = 1 if ($timespan <= 0); + + my $results = {}; + foreach (keys %{$options{devices}}) { + my $data = $self->request_api(endpoint => '/devices/' . $_ . '/clients?timespan=' . $options{timespan}); + $results->{$_} = $data; + } + + return $results; } 1; diff --git a/network/cisco/meraki/cloudcontroller/restapi/mode/devices.pm b/network/cisco/meraki/cloudcontroller/restapi/mode/devices.pm new file mode 100644 index 000000000..26e46067d --- /dev/null +++ b/network/cisco/meraki/cloudcontroller/restapi/mode/devices.pm @@ -0,0 +1,372 @@ +# +# Copyright 2020 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package network::cisco::meraki::cloudcontroller::restapi::mode::devices; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold catalog_status_calc); +use Digest::MD5 qw(md5_hex); + +sub custom_status_output { + my ($self, %options) = @_; + + return 'status: ' . $self->{result_values}->{status}; +} + +sub custom_link_status_output { + my ($self, %options) = @_; + + return 'status: ' . $self->{result_values}->{link_status}; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output', skipped_code => { -10 => 1 } }, + { name => 'devices', type => 3, cb_prefix_output => 'prefix_device_output', cb_long_output => 'device_long_output', indent_long_output => ' ', message_multiple => 'All devices are ok', + group => [ + { name => 'device_status', type => 0, skipped_code => { -10 => 1 } }, + { name => 'device_connections', type => 0, cb_prefix_output => 'prefix_connection_output', skipped_code => { -10 => 1 } }, + { name => 'device_traffic', type => 0, cb_prefix_output => 'prefix_traffic_output', skipped_code => { -10 => 1, -11 => 1 } }, + { name => 'device_links', display_long => 1, cb_prefix_output => 'prefix_link_output', message_multiple => 'All links are ok', type => 1, skipped_code => { -10 => 1 } }, + ] + } + ]; + + $self->{maps_counters}->{global} = [ + { label => 'total-online', nlabel => 'devices.total.online.count', display_ok => 0, set => { + key_values => [ { name => 'online'}, { name => 'total'} ], + output_template => 'online: %s', + perfdatas => [ + { value => 'online_absolute', template => '%s', min => 0, max => 'total_absolute' } + ] + } + }, + { label => 'total-offline', nlabel => 'devices.total.offline.count', display_ok => 0, set => { + key_values => [ { name => 'offline'}, { name => 'total'} ], + output_template => 'offline: %s', + perfdatas => [ + { value => 'offline_absolute', template => '%s', min => 0, max => 'total_absolute' } + ] + } + }, + { label => 'total-alerting', nlabel => 'devices.total.alerting.count', display_ok => 0, set => { + key_values => [ { name => 'alerting'}, { name => 'total'} ], + output_template => 'alerting: %s', + perfdatas => [ + { value => 'alerting_absolute', template => '%s', min => 0, max => 'total_absolute' } + ] + } + }, + ]; + + $self->{maps_counters}->{device_status} = [ + { label => 'status', threshold => 0, set => { + key_values => [ { name => 'status' }, { name => 'display' } ], + closure_custom_calc => \&catalog_status_calc, + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold + } + } + ]; + + $self->{maps_counters}->{device_connections} = [ + { label => 'connections-success', nlabel => 'device.connections.success.count', set => { + key_values => [ { name => 'assoc' }, { name => 'display' } ], + output_template => 'success: %s', + perfdatas => [ + { value => 'assoc_absolute', + template => '%d', min => 0, label_extra_instance => 1, instance_use => 'display_absolute' } + ] + } + }, + { label => 'connections-auth', nlabel => 'device.connections.auth.count', display_ok => 0, set => { + key_values => [ { name => 'auth' }, { name => 'display' } ], + output_template => 'auth: %s', + perfdatas => [ + { value => 'auth_absolute', + template => '%d', min => 0, label_extra_instance => 1, instance_use => 'display_absolute' } + ] + } + }, + { label => 'connections-assoc', nlabel => 'device.connections.assoc.count', display_ok => 0, set => { + key_values => [ { name => 'assoc' }, { name => 'display' } ], + output_template => 'assoc: %s', + perfdatas => [ + { value => 'assoc_absolute', + template => '%d', min => 0, label_extra_instance => 1, instance_use => 'display_absolute' } + ] + } + }, + { label => 'connections-dhcp', nlabel => 'device.connections.dhcp.count', display_ok => 0, set => { + key_values => [ { name => 'dhcp' }, { name => 'display' } ], + output_template => 'dhcp: %s', + perfdatas => [ + { value => 'dhcp_absolute', + template => '%d', min => 0, label_extra_instance => 1, instance_use => 'display_absolute' } + ] + } + }, + { label => 'connections-dns', nlabel => 'device.connections.dns.count', display_ok => 0, set => { + key_values => [ { name => 'dns' }, { name => 'display' } ], + output_template => 'dns: %s', + perfdatas => [ + { value => 'dns_absolute', + template => '%d', min => 0, label_extra_instance => 1, instance_use => 'display_absolute' } + ] + } + } + ]; + + $self->{maps_counters}->{device_traffic} = [ + { label => 'traffic-in', nlabel => 'device.traffic.in.bitspersecond', set => { + key_values => [ { name => 'traffic_in', diff => 1 }, { name => 'display' } ], + output_template => 'in: %s %s/s', + per_second => 1, output_change_bytes => 2, + perfdatas => [ + { value => 'traffic_in_per_second', template => '%s', + min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'display' } + ] + } + }, + { label => 'traffic-out', nlabel => 'device.traffic.out.bitspersecond', set => { + key_values => [ { name => 'traffic_out', diff => 1 }, { name => 'display' } ], + output_template => 'out: %s %s/s', + per_second => 1, output_change_bytes => 2, + perfdatas => [ + { value => 'traffic_out_per_second', template => '%s', + min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'display' } + ] + } + } + ]; + + $self->{maps_counters}->{device_links} = [ + { label => 'link-status', threshold => 0, set => { + key_values => [ { name => 'link_status' }, { name => 'display' } ], + closure_custom_calc => \&catalog_status_calc, + closure_custom_output => $self->can('custom_link_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold + } + } + ]; +} + +sub device_long_output { + my ($self, %options) = @_; + + return "checking device '" . $options{instance_value}->{display} . "'"; +} + +sub prefix_device_output { + my ($self, %options) = @_; + + return "Device '" . $options{instance_value}->{display} . "' "; +} + +sub prefix_global_output { + my ($self, %options) = @_; + + return 'Devices '; +} + +sub prefix_connection_output { + my ($self, %options) = @_; + + return 'connection '; +} + +sub prefix_traffic_output { + my ($self, %options) = @_; + + return 'traffic '; +} + +sub prefix_link_output { + my ($self, %options) = @_; + + return "link '" . $options{instance_value}->{display} . "' "; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'filter-name:s' => { name => 'filter_name' }, + 'unknown-status:s' => { name => 'unknown_status', default => '' }, + 'warning-status:s' => { name => 'warning_status', default => '' }, + 'critical-status:s' => { name => 'critical_status', default => '%{status} =~ /alerting/i' }, + 'unknown-link-status:s' => { name => 'unknown_link_status', default => '' }, + 'warning-link-status:s' => { name => 'warning_link_status', default => '' }, + 'critical-link-status:s' => { name => 'critical_link_status', default => '%{link_status} =~ /failed/i' } + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $self->change_macros(macros => [ + 'unknown_status', 'warning_status', 'critical_status', + 'unknown_link_status', 'warning_link_status', 'critical_link_status' + ]); +} + +sub manage_selection { + my ($self, %options) = @_; + + $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_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all')); + my $last_timestamp = $self->read_statefile_key(key => 'last_timestamp'); + my $timespan = 300; + $timespan = time() - $last_timestamp if (defined($last_timestamp)); + + my $cache_devices = $options{custom}->get_cache_devices(); + my $devices = {}; + foreach (values %$cache_devices) { + if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && + $_->{name} !~ /$self->{option_results}->{filter_name}/) { + $self->{output}->output_add(long_msg => "skipping device '" . $_->{name} . "': no matching filter.", debug => 1); + next; + } + + $devices->{$_->{serial}} = $_->{networkId}; + } + + my $device_statuses = $options{custom}->get_organization_device_statuses(); + my $connections = $options{custom}->get_network_device_connection_stats(timespan => $timespan, devices => $devices); + my $clients = $options{custom}->get_device_clients(timespan => $timespan, devices => $devices); + my $links = $options{custom}->get_network_device_uplink(devices => $devices); + + $self->{global} = { total => 0, online => 0, offline => 0, alerting => 0 }; + $self->{devices} = {}; + foreach my $serial (keys %$devices) { + $self->{devices}->{$serial} = { + display => $cache_devices->{$serial}->{name}, + device_status => { + display => $cache_devices->{$serial}->{name}, + status => $device_statuses->{$serial}->{status} + }, + device_connections => { + display => $cache_devices->{$serial}->{name}, + assoc => defined($connections->{$serial}->{assoc}) ? $connections->{$serial}->{assoc} : 0, + auth => defined($connections->{$serial}->{auth}) ? $connections->{$serial}->{auth} : 0, + dhcp => defined($connections->{$serial}->{dhcp}) ? $connections->{$serial}->{dhcp} : 0, + dns => defined($connections->{$serial}->{dns}) ? $connections->{$serial}->{dns} : 0, + success => defined($connections->{$serial}->{assoc}) ? $connections->{$serial}->{success} : 0, + }, + device_traffic => { + display => $cache_devices->{$serial}->{name}, + traffic_in => 0, + traffic_out => 0 + }, + device_links => {} + }; + + if (defined($clients->{$serial})) { + foreach (@{$clients->{$serial}}) { + $self->{devices}->{$serial}->{device_traffic}->{traffic_in} += $_->{usage}->{recv} * 8; + $self->{devices}->{$serial}->{device_traffic}->{traffic_out} += $_->{usage}->{sent} * 8; + } + } + + if (defined($links->{$serial})) { + foreach (@{$links->{$serial}}) { + $self->{devices}->{$serial}->{device_links}->{$_->{interface}} = { + display => $_->{interface}, + link_status => lc($_->{status}) + }; + } + } + + $self->{global}->{total}++; + $self->{global}->{ lc($device_statuses->{$serial}->{status}) }++ + if (!defined($self->{global}->{ lc($device_statuses->{$serial}->{status}) })) + } + + if (scalar(keys %{$self->{devices}}) <= 0) { + $self->{output}->output_add(short_msg => 'no devices found'); + } +} + +1; + +__END__ + +=head1 MODE + +Check devices. + +=over 8 + +=item B<--filter-name> + +Filter device name (Can be a regexp). + +=item B<--unknown-status> + +Set unknown threshold for status. +Can used special variables like: %{status}, %{display} + +=item B<--warning-status> + +Set warning threshold for status. +Can used special variables like: %{status}, %{display} + +=item B<--critical-status> + +Set critical threshold for status (Default: '%{status} =~ /alerting/i'). +Can used special variables like: %{status}, %{display} + +=item B<--unknown-link-status> + +Set unknown threshold for status. +Can used special variables like: %{link_status}, %{display} + +=item B<--warning-link-status> + +Set warning threshold for status. +Can used special variables like: %{link_status}, %{display} + +=item B<--critical-link-status> + +Set critical threshold for status (Default: '%{link_status} =~ /failed/i'). +Can used special variables like: %{link_status}, %{display} + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'connections-success', 'connections-auth', 'connections-assoc', +'connections-dhcp', 'connections-dns', 'traffic-in', 'traffic-out'. + +=back + +=cut diff --git a/network/cisco/meraki/cloudcontroller/restapi/mode/networks.pm b/network/cisco/meraki/cloudcontroller/restapi/mode/networks.pm index ce53e1e7a..4960262ba 100644 --- a/network/cisco/meraki/cloudcontroller/restapi/mode/networks.pm +++ b/network/cisco/meraki/cloudcontroller/restapi/mode/networks.pm @@ -139,10 +139,10 @@ sub manage_selection { $self->{networks}->{$id} = { display => $cache_networks->{$id}->{name}, assoc => defined($connections->{$id}->{assoc}) ? $connections->{$id}->{assoc} : 0, - auth => defined($connections->{$id}->{assoc}) ? $connections->{$id}->{auth} : 0, - dhcp => defined($connections->{$id}->{assoc}) ? $connections->{$id}->{assoc} : 0, - dns => defined($connections->{$id}->{assoc}) ? $connections->{$id}->{dhcp} : 0, - success => defined($connections->{$id}->{assoc}) ? $connections->{$id}->{success} : 0, + auth => defined($connections->{$id}->{auth}) ? $connections->{$id}->{auth} : 0, + dhcp => defined($connections->{$id}->{dhcp}) ? $connections->{$id}->{dhcp} : 0, + dns => defined($connections->{$id}->{dns}) ? $connections->{$id}->{dns} : 0, + success => defined($connections->{$id}->{success}) ? $connections->{$id}->{success} : 0, traffic_in => 0, traffic_out => 0 };