From 001dabcca24fda227f43b4356f047a9b5212a51d Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Wed, 1 Apr 2020 13:42:24 +0200 Subject: [PATCH] wip: add cisco meraki api requests --- .../cloudcontroller/restapi/custom/api.pm | 23 +++ .../restapi/mode/apirequests.pm | 136 ++++++++++++++++++ .../meraki/cloudcontroller/restapi/plugin.pm | 5 +- 3 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 centreon-plugins/network/cisco/meraki/cloudcontroller/restapi/mode/apirequests.pm diff --git a/centreon-plugins/network/cisco/meraki/cloudcontroller/restapi/custom/api.pm b/centreon-plugins/network/cisco/meraki/cloudcontroller/restapi/custom/api.pm index f846f6d12..6e866a2f6 100644 --- a/centreon-plugins/network/cisco/meraki/cloudcontroller/restapi/custom/api.pm +++ b/centreon-plugins/network/cisco/meraki/cloudcontroller/restapi/custom/api.pm @@ -113,6 +113,13 @@ sub get_token { return md5_hex($self->{api_token}); } +sub get_cache_organizations { + my ($self, %options) = @_; + + $self->cache_meraki_entities(); + return $self->{cache_organizations}; +} + sub get_cache_networks { my ($self, %options) = @_; @@ -333,6 +340,22 @@ sub get_organization_device_statuses { return $results; } +sub get_organization_api_requests_overview { + my ($self, %options) = @_; + + $self->cache_meraki_entities(); + my $organization_ids = $self->filter_organizations(filter_name => $options{filter_name}); + my $timespan = defined($options{timespan}) ? $options{timespan} : 300; + $timespan = 1 if ($timespan <= 0); + + my $results = {}; + foreach my $id (@$organization_ids) { + $results->{$id} = $self->request_api(endpoint => '/organizations/' . $id . '/apiRequests/overview?timespan=' . $options{timespan}); + } + + return $results; +} + sub get_network_device_connection_stats { my ($self, %options) = @_; diff --git a/centreon-plugins/network/cisco/meraki/cloudcontroller/restapi/mode/apirequests.pm b/centreon-plugins/network/cisco/meraki/cloudcontroller/restapi/mode/apirequests.pm new file mode 100644 index 000000000..b738ea9db --- /dev/null +++ b/centreon-plugins/network/cisco/meraki/cloudcontroller/restapi/mode/apirequests.pm @@ -0,0 +1,136 @@ +# +# 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::apirequests; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use Digest::MD5 qw(md5_hex); + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'organizations', type => 1, cb_prefix_output => 'prefix_organization_output', message_multiple => 'All organizations are ok' } + ]; + + $self->{maps_counters}->{organizations} = [ + { label => 'api-requests-200', nlabel => 'organization.api.requests.200.count', set => { + key_values => [ { name => 'requests_200' }, { name => 'display' } ], + output_template => 'code 200: %s', + perfdatas => [ + { value => 'requests_200_absolute', + template => '%d', min => 0, label_extra_instance => 1, instance_use => 'display_absolute' } + ] + } + }, + { label => 'api-requests-404', nlabel => 'organization.api.requests.404.count', set => { + key_values => [ { name => 'requests_404' }, { name => 'display' } ], + output_template => 'code 404: %s', + perfdatas => [ + { value => 'requests_404_absolute', + template => '%d', min => 0, label_extra_instance => 1, instance_use => 'display_absolute' } + ] + } + }, + { label => 'api-requests-429', nlabel => 'organization.api.requests.429.count', set => { + key_values => [ { name => 'requests_429' }, { name => 'display' } ], + output_template => 'code 429: %s', + perfdatas => [ + { value => 'requests_429_absolute', + template => '%d', min => 0, label_extra_instance => 1, instance_use => 'display_absolute' } + ] + } + } + ]; +} + +sub prefix_organization_output { + my ($self, %options) = @_; + + return "Organization '" . $options{instance_value}->{display} . "' requests "; +} + +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' } + }); + + return $self; +} + +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_organizations = $options{custom}->get_cache_organizations(); + my $api_requests = $options{custom}->get_organization_api_requests_overview(timespan => $timespan, filter_name => $self->{option_results}->{filter_name}); + + $self->{organizations} = {}; + foreach my $id (keys %$api_requests) { + $self->{organizations}->{$id} = { + display => $cache_organizations->{$id}->{name}, + requests_200 => defined($api_requests->{$id}->{responseCodeCounts}->{200}) ? $api_requests->{$id}->{responseCodeCounts}->{200} : 0, + requests_404 => defined($api_requests->{$id}->{responseCodeCounts}->{404}) ? $api_requests->{$id}->{responseCodeCounts}->{404} : 0, + requests_429 => defined($api_requests->{$id}->{responseCodeCounts}->{429}) ? $api_requests->{$id}->{responseCodeCounts}->{429} : 0, + }; + } + + if (scalar(keys %{$self->{organizations}}) <= 0) { + $self->{output}->add_option_msg(short_msg => "No organizations found."); + $self->{output}->option_exit(); + } +} + +1; + +__END__ + +=head1 MODE + +Check api requests. + +=over 8 + +=item B<--filter-name> + +Filter organization name (Can be a regexp). + +=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/centreon-plugins/network/cisco/meraki/cloudcontroller/restapi/plugin.pm b/centreon-plugins/network/cisco/meraki/cloudcontroller/restapi/plugin.pm index ed31ce585..136e24225 100644 --- a/centreon-plugins/network/cisco/meraki/cloudcontroller/restapi/plugin.pm +++ b/centreon-plugins/network/cisco/meraki/cloudcontroller/restapi/plugin.pm @@ -31,8 +31,9 @@ sub new { $self->{version} = '1.0'; %{$self->{modes}} = ( - 'devices' => 'network::cisco::meraki::cloudcontroller::restapi::mode::devices', - 'networks' => 'network::cisco::meraki::cloudcontroller::restapi::mode::networks' + 'api-requests' => 'network::cisco::meraki::cloudcontroller::restapi::mode::apirequests', + 'devices' => 'network::cisco::meraki::cloudcontroller::restapi::mode::devices', + 'networks' => 'network::cisco::meraki::cloudcontroller::restapi::mode::networks' ); $self->{custom_modes}{api} = 'network::cisco::meraki::cloudcontroller::restapi::custom::api';