From abed5a9169a7efd58035201b15235211c1939af7 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Mon, 25 May 2020 11:50:16 +0200 Subject: [PATCH 1/5] enhance curl backend to be faster. keep the same connection --- apps/iis/restapi/custom/api.pm | 279 ++++++++++++++++++ apps/iis/restapi/mode/applicationpools.pm | 155 ++++++++++ apps/iis/restapi/mode/listapplicationpools.pm | 104 +++++++ apps/iis/restapi/mode/listwebsites.pm | 102 +++++++ apps/iis/restapi/mode/websites.pm | 191 ++++++++++++ apps/iis/restapi/plugin.pm | 52 ++++ centreon/plugins/backend/http/curl.pm | 12 +- centreon/plugins/backend/http/lwp.pm | 9 +- centreon/plugins/backend/http/useragent.pm | 2 +- 9 files changed, 899 insertions(+), 7 deletions(-) create mode 100644 apps/iis/restapi/custom/api.pm create mode 100644 apps/iis/restapi/mode/applicationpools.pm create mode 100644 apps/iis/restapi/mode/listapplicationpools.pm create mode 100644 apps/iis/restapi/mode/listwebsites.pm create mode 100644 apps/iis/restapi/mode/websites.pm create mode 100644 apps/iis/restapi/plugin.pm diff --git a/apps/iis/restapi/custom/api.pm b/apps/iis/restapi/custom/api.pm new file mode 100644 index 000000000..815b09db1 --- /dev/null +++ b/apps/iis/restapi/custom/api.pm @@ -0,0 +1,279 @@ +# +# 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 apps::iis::restapi::custom::api; + +use strict; +use warnings; +use centreon::plugins::http; +use JSON::XS; + +sub new { + my ($class, %options) = @_; + my $self = {}; + bless $self, $class; + + if (!defined($options{output})) { + print "Class Custom: Need to specify 'output' argument.\n"; + exit 3; + } + if (!defined($options{options})) { + $options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument."); + $options{output}->option_exit(); + } + + if (!defined($options{noptions})) { + $options{options}->add_options(arguments => { + 'api-username:s' => { name => 'api_username' }, + 'api-password:s' => { name => 'api_password' }, + 'api-token:s' => { name => 'api_token' }, + 'hostname:s' => { name => 'hostname' }, + 'port:s' => { name => 'port' }, + 'proto:s' => { name => 'proto' }, + 'timeout:s' => { name => 'timeout' }, + 'unknown-http-status:s' => { name => 'unknown_http_status' }, + 'warning-http-status:s' => { name => 'warning_http_status' }, + 'critical-http-status:s' => { name => 'critical_http_status' } + }); + } + $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1); + + $self->{output} = $options{output}; + $self->{mode} = $options{mode}; + $self->{http} = centreon::plugins::http->new(%options); + + return $self; +} + +sub set_options { + my ($self, %options) = @_; + + $self->{option_results} = $options{option_results}; +} + +sub set_defaults { + my ($self, %options) = @_; + + foreach (keys %{$options{default}}) { + if ($_ eq $self->{mode}) { + for (my $i = 0; $i < scalar(@{$options{default}->{$_}}); $i++) { + foreach my $opt (keys %{$options{default}->{$_}[$i]}) { + if (!defined($self->{option_results}->{$opt}[$i])) { + $self->{option_results}->{$opt}[$i] = $options{default}->{$_}[$i]->{$opt}; + } + } + } + } + } +} + +sub check_options { + my ($self, %options) = @_; + + $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : ''; + $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 55539; + $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https'; + $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10; + $self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : ''; + $self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : ''; + $self->{api_token} = (defined($self->{option_results}->{api_token})) ? $self->{option_results}->{api_token} : ''; + $self->{unknown_http_status} = (defined($self->{option_results}->{unknown_http_status})) ? $self->{option_results}->{unknown_http_status} : '%{http_code} < 200 or %{http_code} >= 300'; + $self->{warning_http_status} = (defined($self->{option_results}->{warning_http_status})) ? $self->{option_results}->{warning_http_status} : ''; + $self->{critical_http_status} = (defined($self->{option_results}->{critical_http_status})) ? $self->{option_results}->{critical_http_status} : ''; + + if ($self->{hostname} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --hostname option."); + $self->{output}->option_exit(); + } + if ($self->{api_username} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --api-username option."); + $self->{output}->option_exit(); + } + if ($self->{api_password} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --api-password option."); + $self->{output}->option_exit(); + } + if ($self->{api_token} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --api-token option."); + $self->{output}->option_exit(); + } + + return 0; +} + +sub build_options_for_httplib { + my ($self, %options) = @_; + + $self->{option_results}->{hostname} = $self->{hostname}; + $self->{option_results}->{timeout} = $self->{timeout}; + $self->{option_results}->{port} = $self->{port}; + $self->{option_results}->{proto} = $self->{proto}; + $self->{option_results}->{timeout} = $self->{timeout}; +} + +sub settings { + my ($self, %options) = @_; + + return if (defined($self->{settings_done})); + $self->build_options_for_httplib(); + $self->{http}->add_header(key => 'Accept', value => 'application/json'); + $self->{http}->add_header(key => 'Content-Type', value => 'application/json'); + $self->{http}->add_header(key => 'Access-Token', value => 'Bearer ' . $self->{api_token}); + $self->{http}->set_options(%{$self->{option_results}}); + $self->{settings_done} = 1; +} + +sub get_hostname { + my ($self, %options) = @_; + + return $self->{hostname}; +} + +sub request_api { + my ($self, %options) = @_; + + $self->settings(); + my $login = {}; + if (!defined($self->{login_done})) { + $login = { credentials => 1, ntlmv2 => 1, username => $self->{api_username}, password => $self->{api_password} }; + $self->{login_done} = 1; + } + my $content = $self->{http}->request( + url_path => $options{endpoint}, + unknown_status => $self->{unknown_http_status}, + warning_status => $self->{warning_http_status}, + critical_status => $self->{critical_http_status}, + %$login + ); + + if (!defined($content) || $content eq '') { + $self->{output}->add_option_msg(short_msg => "API returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']"); + $self->{output}->option_exit(); + } + + my $decoded; + eval { + $decoded = JSON::XS->new->utf8->decode($content); + }; + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)"); + $self->{output}->option_exit(); + } + + return $decoded; +} + +sub get_application_pools { + my ($self, %options) = @_; + + my $results = {}; + my $pools = $self->request_api(endpoint => '/api/webserver/application-pools'); + foreach (@{$pools->{app_pools}}) { + if (defined($options{filter_name}) && $options{filter_name} ne '' && + $_->{name} !~ /$options{filter_name}/) { + $self->{output}->output_add(long_msg => "skipping pool '" . $_->{name} . "': no matching filter.", debug => 1); + next; + } + + $results->{ $_->{name} } = $_; + my $detail = $self->request_api(endpoint => '/api/webserver/application-pools/' . $_->{id}); + $results->{ $_->{name} }->{auto_start} = $detail->{auto_start}; + + next if (defined($options{no_monitoring})); + + my $monitor = $self->request_api(endpoint => '/api/webserver/application-pools/monitoring/' . $_->{id}); + $results->{ $_->{name} }->{requests} = $monitor->{requests}; + } + + return $results; +} + +sub get_websites { + my ($self, %options) = @_; + + my $results = {}; + my $websites = $self->request_api(endpoint => '/api/webserver/websites'); + foreach (@{$websites->{websites}}) { + if (defined($options{filter_name}) && $options{filter_name} ne '' && + $_->{name} !~ /$options{filter_name}/) { + $self->{output}->output_add(long_msg => "skipping website '" . $_->{name} . "': no matching filter.", debug => 1); + next; + } + + $results->{ $_->{name} } = $_; + + next if (defined($options{no_monitoring})); + + my $monitor = $self->request_api(endpoint => '/api/webserver/websites/monitoring/' . $_->{id}); + $results->{ $_->{name} }->{network} = $monitor->{network}; + } + + return $results; +} + +1; + +__END__ + +=head1 NAME + +IIS Rest API + +=head1 REST API OPTIONS + +IIS Rest API + +=over 8 + +=item B<--hostname> + +IIS hostname. + +=item B<--port> + +Port used (Default: 55539) + +=item B<--proto> + +Specify https if needed (Default: 'https') + +=item B<--api-username> + +IIS API username. + +=item B<--api-password> + +IIS API password. + +=item B<--api-token> + +IIS API token. + +=item B<--timeout> + +Set timeout in seconds (Default: 10). + +=back + +=head1 DESCRIPTION + +B. + +=cut diff --git a/apps/iis/restapi/mode/applicationpools.pm b/apps/iis/restapi/mode/applicationpools.pm new file mode 100644 index 000000000..8cbe040e5 --- /dev/null +++ b/apps/iis/restapi/mode/applicationpools.pm @@ -0,0 +1,155 @@ +# +# 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 apps::iis::restapi::mode::applicationpools; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); +use Digest::MD5 qw(md5_hex); + +sub custom_status_output { + my ($self, %options) = @_; + + return sprintf( + 'status: %s [auto start: %s]', + $self->{result_values}->{status}, + $self->{result_values}->{auto_start} + ); +} + +sub prefix_pool_output { + my ($self, %options) = @_; + + return "Application pool '" . $options{instance_value}->{display} . "' "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'pools', type => 1, cb_prefix_output => 'prefix_pool_output', message_multiple => 'All application pools are ok', skipped_code => { -10 => 1 } } + ]; + + $self->{maps_counters}->{pools} = [ + { label => 'status', threshold => 0, set => { + key_values => [ { name => 'status' }, { name => 'auto_start' }, { name => 'display' } ], + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold + } + }, + { label => 'requests', nlabel => 'applicationpool.requests.persecond', set => { + key_values => [ { name => 'requests_total', per_second => 1 }, { name => 'display' } ], + output_template => 'requests: %.2f/s', + perfdatas => [ + { template => '%.2f', min => 0, unit => '/s', label_extra_instance => 1 } + ] + } + } + ]; +} + +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 => '%{auto_start} eq "true" and %{status} !~ /starting|started/' } + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $self->change_macros(macros => ['warning_status', 'critical_status', 'unknown_status']); +} + +sub manage_selection { + my ($self, %options) = @_; + + my $results = $options{custom}->get_application_pools(filter_name => $self->{option_results}->{filter_name}); + + $self->{pools} = {}; + foreach (values %$results) { + $self->{pools}->{$_->{name}} = { + display => $_->{name}, + status => $_->{status}, + auto_start => $_->{auto_start} ? 'true' : 'false', + requests_total => $_->{requests}->{total} + }; + } + + $self->{cache_name} = 'iis_' . $self->{mode} . '_' . $options{custom}->get_hostname() . '_' . + (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')); +} + +1; + +__END__ + +=head1 MODE + +Check application pools. + +=over 8 + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='status' + +=item B<--filter-name> + +Filter application pool name (can be a regexp). + +=item B<--unknown-status> + +Set unknown threshold for status. +Can used special variables like: %{status}, %{auto_start}, %{display} + +=item B<--warning-status> + +Set warning threshold for status. +Can used special variables like: %{status}, %{auto_start}, %{display} + +=item B<--critical-status> + +Set critical threshold for status (Default: '%{auto_start} eq "true" and %{status} !~ /starting|started/'). +Can used special variables like: %{status}, %{auto_start}, %{display} + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'usage' (B), 'usage-free' (B), 'usage-prct' (%). + +=back + +=cut diff --git a/apps/iis/restapi/mode/listapplicationpools.pm b/apps/iis/restapi/mode/listapplicationpools.pm new file mode 100644 index 000000000..44c287576 --- /dev/null +++ b/apps/iis/restapi/mode/listapplicationpools.pm @@ -0,0 +1,104 @@ +# +# 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 apps::iis::restapi::mode::listapplicationpools; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $options{options}->add_options(arguments => { + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); +} + +sub manage_selection { + my ($self, %options) = @_; + + return $options{custom}->get_application_pools(no_monitoring => 1); +} + +sub run { + my ($self, %options) = @_; + + my $results = $self->manage_selection(custom => $options{custom}); + foreach (sort(keys %$results)) { + $self->{output}->output_add( + long_msg => sprintf( + '[name: %s] [status: %s] [auto_start: %s]', + $results->{$_}->{name}, + $results->{$_}->{status}, + $results->{$_}->{auto_start} ? 'true' : 'false' + ) + ); + } + + $self->{output}->output_add( + severity => 'OK', + short_msg => 'List application pools:' + ); + $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); + $self->{output}->exit(); +} + +sub disco_format { + my ($self, %options) = @_; + + $self->{output}->add_disco_format(elements => ['name', 'auto_start', 'status']); +} + +sub disco_show { + my ($self, %options) = @_; + + my $results = $self->manage_selection(custom => $options{custom}); + foreach (sort(keys %$results)) { + $self->{output}->add_disco_entry( + name => $results->{$_}->{name}, + status => $results->{$_}->{status}, + auto_start => $results->{$_}->{auto_start} ? 'true' : 'false' + ); + } +} + +1; + +__END__ + +=head1 MODE + +List application pools. + +=over 8 + +=back + +=cut diff --git a/apps/iis/restapi/mode/listwebsites.pm b/apps/iis/restapi/mode/listwebsites.pm new file mode 100644 index 000000000..151d21809 --- /dev/null +++ b/apps/iis/restapi/mode/listwebsites.pm @@ -0,0 +1,102 @@ +# +# 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 apps::iis::restapi::mode::listwebsites; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $options{options}->add_options(arguments => { + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); +} + +sub manage_selection { + my ($self, %options) = @_; + + return $options{custom}->get_websites(no_monitoring => 1); +} + +sub run { + my ($self, %options) = @_; + + my $results = $self->manage_selection(custom => $options{custom}); + foreach (sort(keys %$results)) { + $self->{output}->output_add( + long_msg => sprintf( + '[name: %s] [status: %s]', + $results->{$_}->{name}, + $results->{$_}->{status} + ) + ); + } + + $self->{output}->output_add( + severity => 'OK', + short_msg => 'List websites:' + ); + $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); + $self->{output}->exit(); +} + +sub disco_format { + my ($self, %options) = @_; + + $self->{output}->add_disco_format(elements => ['name', 'status']); +} + +sub disco_show { + my ($self, %options) = @_; + + my $results = $self->manage_selection(custom => $options{custom}); + foreach (sort(keys %$results)) { + $self->{output}->add_disco_entry( + name => $results->{$_}->{name}, + status => $results->{$_}->{status} + ); + } +} + +1; + +__END__ + +=head1 MODE + +List websites. + +=over 8 + +=back + +=cut diff --git a/apps/iis/restapi/mode/websites.pm b/apps/iis/restapi/mode/websites.pm new file mode 100644 index 000000000..df9dba128 --- /dev/null +++ b/apps/iis/restapi/mode/websites.pm @@ -0,0 +1,191 @@ +# +# 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 apps::iis::restapi::mode::websites; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); +use Digest::MD5 qw(md5_hex); + +sub custom_status_output { + my ($self, %options) = @_; + + return sprintf( + 'status: %s', + $self->{result_values}->{status} + ); +} + +sub prefix_website_output { + my ($self, %options) = @_; + + return "Website '" . $options{instance_value}->{display} . "' "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'websites', type => 1, cb_prefix_output => 'prefix_website_output', message_multiple => 'All websites are ok', skipped_code => { -10 => 1 } } + ]; + + $self->{maps_counters}->{websites} = [ + { label => 'status', threshold => 0, set => { + key_values => [ { name => 'status' }, { name => 'auto_start' }, { name => 'display' } ], + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold + } + }, + { label => 'requests', nlabel => 'website.requests.persecond', set => { + key_values => [ { name => 'requests_total', per_second => 1 }, { name => 'display' } ], + output_template => 'requests: %.2f/s', + perfdatas => [ + { template => '%.2f', min => 0, unit => '/s', label_extra_instance => 1 } + ] + } + }, + { label => 'traffic-in', nlabel => 'website.traffic.in.bitspersecond', set => { + key_values => [ { name => 'traffic_in', per_second => 1 }, { name => 'display' } ], + output_template => 'traffic in: %s %s/s', + output_change_bytes => 2, + perfdatas => [ + { template => '%s', min => 0, unit => 'b/s', label_extra_instance => 1 } + ] + } + }, + { label => 'traffic-out', nlabel => 'website.traffic.out.bitspersecond', set => { + key_values => [ { name => 'traffic_out', per_second => 1 }, { name => 'display' } ], + output_template => 'traffic out: %s %s/s', + output_change_bytes => 2, + perfdatas => [ + { template => '%s', min => 0, unit => 'b/s', label_extra_instance => 1 } + ] + } + }, + { label => 'connections-current', nlabel => 'website.connections.current.count', set => { + key_values => [ { name => 'current_connections' }, { name => 'display' } ], + output_template => 'current connections: %s', + perfdatas => [ + { template => '%s', min => 0, label_extra_instance => 1 } + ] + } + }, + { label => 'connections-total', nlabel => 'website.connections.total.persecond', set => { + key_values => [ { name => 'total_connections', per_second => 1 }, { name => 'display' } ], + output_template => 'total connections: %.2f/s', + perfdatas => [ + { template => '%.2f', min => 0, unit => '/s', label_extra_instance => 1 } + ] + } + } + ]; +} + +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} !~ /starting|started/' } + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $self->change_macros(macros => ['warning_status', 'critical_status', 'unknown_status']); +} + +sub manage_selection { + my ($self, %options) = @_; + + my $results = $options{custom}->get_websites(filter_name => $self->{option_results}->{filter_name}); + + $self->{websites} = {}; + foreach (values %$results) { + $self->{websites}->{$_->{name}} = { + display => $_->{name}, + status => $_->{status}, + requests_total => $_->{requests}->{total}, + traffic_in => $_->{network}->{total_bytes_recv} * 8, + traffic_out => $_->{network}->{total_bytes_sent} * 8, + current_connections => $_->{network}->{current_connections}, + total_connections => $_->{network}->{total_connection_attempts} + }; + } + + $self->{cache_name} = 'iis_' . $self->{mode} . '_' . $options{custom}->get_hostname() . '_' . + (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')); +} + +1; + +__END__ + +=head1 MODE + +Check websites. + +=over 8 + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='status' + +=item B<--filter-name> + +Filter website 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} !~ /starting|started/'). +Can used special variables like: %{status}, %{display} + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'usage' (B), 'usage-free' (B), 'usage-prct' (%). + +=back + +=cut diff --git a/apps/iis/restapi/plugin.pm b/apps/iis/restapi/plugin.pm new file mode 100644 index 000000000..f9bd7b047 --- /dev/null +++ b/apps/iis/restapi/plugin.pm @@ -0,0 +1,52 @@ +# +# 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 apps::iis::restapi::plugin; + +use strict; +use warnings; +use base qw(centreon::plugins::script_custom); + +sub new { + my ( $class, %options ) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $self->{version} = '0.1'; + $self->{modes} = { + 'application-pools' => 'apps::iis::restapi::mode::applicationpools', + 'list-application-pools' => 'apps::iis::restapi::mode::listapplicationpools', + 'list-websites' => 'apps::iis::restapi::mode::listwebsites', + 'websites' => 'apps::iis::restapi::mode::websites' + }; + + $self->{custom_modes}{api} = 'apps::iis::restapi::custom::api'; + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check Windows IIS in Rest API. + +=cut diff --git a/centreon/plugins/backend/http/curl.pm b/centreon/plugins/backend/http/curl.pm index 39bdc6c83..af14dbe5c 100644 --- a/centreon/plugins/backend/http/curl.pm +++ b/centreon/plugins/backend/http/curl.pm @@ -263,7 +263,9 @@ sub cb_get_header { sub request { my ($self, %options) = @_; - $self->{curl_easy} = Net::Curl::Easy->new(); + if (!defined($self->{curl_easy})) { + $self->{curl_easy} = Net::Curl::Easy->new(); + } if ($self->{output}->is_debug()) { $self->curl_setopt(option => $self->{constant_cb}->(name => 'CURLOPT_DEBUGFUNCTION'), parameter => \&cb_debug); @@ -294,8 +296,10 @@ sub request { if (defined($options{request}->{http_peer_addr}) && $options{request}->{http_peer_addr} ne '') { $url =~ /^(?:http|https):\/\/(.*?)(\/|\:|$)/; - $self->{curl_easy}->pushopt($self->{constant_cb}->(name => 'CURLOPT_RESOLVE'), - [$1 . ':' . $options{request}->{port_force} . ':' . $options{request}->{http_peer_addr}]); + $self->{curl_easy}->setopt( + $self->{constant_cb}->(name => 'CURLOPT_RESOLVE'), + [$1 . ':' . $options{request}->{port_force} . ':' . $options{request}->{http_peer_addr}] + ); } my $uri = URI->new($url); @@ -317,7 +321,7 @@ sub request { $self->set_method(%options, content_type_forced => $content_type_forced, headers => $headers); if (scalar(@$headers) > 0) { - $self->{curl_easy}->pushopt($self->{constant_cb}->(name => 'CURLOPT_HTTPHEADER'), $headers); + $self->{curl_easy}->setopt($self->{constant_cb}->(name => 'CURLOPT_HTTPHEADER'), $headers); } if (defined($options{request}->{cacert_file}) && $options{request}->{cacert_file} ne '') { diff --git a/centreon/plugins/backend/http/lwp.pm b/centreon/plugins/backend/http/lwp.pm index 93d93ee7a..6169080d6 100644 --- a/centreon/plugins/backend/http/lwp.pm +++ b/centreon/plugins/backend/http/lwp.pm @@ -123,8 +123,13 @@ sub request { my $request_options = $options{request}; if (!defined($self->{ua})) { $self->{ua} = centreon::plugins::backend::http::useragent->new( - keep_alive => 1, protocols_allowed => ['http', 'https'], timeout => $request_options->{timeout}, - credentials => $request_options->{credentials}, username => $request_options->{username}, password => $request_options->{password}); + keep_alive => 1, + protocols_allowed => ['http', 'https'], + timeout => $request_options->{timeout}, + credentials => $request_options->{credentials}, + username => $request_options->{username}, + password => $request_options->{password} + ); if (defined($request_options->{cookies_file})) { centreon::plugins::misc::mymodule_load( output => $self->{output}, diff --git a/centreon/plugins/backend/http/useragent.pm b/centreon/plugins/backend/http/useragent.pm index 0b467c66e..042e034e9 100644 --- a/centreon/plugins/backend/http/useragent.pm +++ b/centreon/plugins/backend/http/useragent.pm @@ -43,7 +43,7 @@ sub get_basic_credentials { my($self, $realm, $uri, $proxy) = @_; return if $proxy; return $self->{username}, $self->{password} if $self->{credentials} and wantarray; - return $self->{username}.":".$self->{password} if $self->{credentials}; + return $self->{username} . ':' . $self->{password} if $self->{credentials}; return undef; } From 967acb849ee1658acd0e7c33cead823710cef6cd Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Mon, 25 May 2020 13:32:00 +0200 Subject: [PATCH 2/5] fix help --- apps/iis/restapi/mode/applicationpools.pm | 2 +- apps/iis/restapi/mode/websites.pm | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/apps/iis/restapi/mode/applicationpools.pm b/apps/iis/restapi/mode/applicationpools.pm index 8cbe040e5..bb5becc53 100644 --- a/apps/iis/restapi/mode/applicationpools.pm +++ b/apps/iis/restapi/mode/applicationpools.pm @@ -148,7 +148,7 @@ Can used special variables like: %{status}, %{auto_start}, %{display} =item B<--warning-*> B<--critical-*> Thresholds. -Can be: 'usage' (B), 'usage-free' (B), 'usage-prct' (%). +Can be: 'requests'. =back diff --git a/apps/iis/restapi/mode/websites.pm b/apps/iis/restapi/mode/websites.pm index df9dba128..dbd5f71c7 100644 --- a/apps/iis/restapi/mode/websites.pm +++ b/apps/iis/restapi/mode/websites.pm @@ -184,7 +184,8 @@ Can used special variables like: %{status}, %{display} =item B<--warning-*> B<--critical-*> Thresholds. -Can be: 'usage' (B), 'usage-free' (B), 'usage-prct' (%). +Can be: 'requests', 'traffic-in', 'traffic-out', +'connections-current', 'connections-total'. =back From b6df54e1cef2eb7089e851ded5edea64b9436d3a Mon Sep 17 00:00:00 2001 From: Fabien THEPAUT Date: Mon, 25 May 2020 11:34:33 -0400 Subject: [PATCH 3/5] Remove unexisting qtype-any threshold options from help --- apps/bind9/web/mode/zoneusage.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/bind9/web/mode/zoneusage.pm b/apps/bind9/web/mode/zoneusage.pm index 4325d6b50..7979a756f 100644 --- a/apps/bind9/web/mode/zoneusage.pm +++ b/apps/bind9/web/mode/zoneusage.pm @@ -142,7 +142,7 @@ Filter zone name (can be a regexp). =item B<--warning-*> <--critical-*> Thresholds. -Can be: qtype-a', 'qtype-cname', 'qtype-mx', 'qtype-txt', 'qtype-soa', 'qtype-ptr', 'qtype-ns', 'qtype-any', +Can be: qtype-a', 'qtype-cname', 'qtype-mx', 'qtype-txt', 'qtype-soa', 'qtype-ptr', 'qtype-ns', 'nsstat-requestv4', 'nsstat-requestv6'. =back From 7003d934e0767a956ebb6e7fba6cfa8fa801f075 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Mon, 25 May 2020 18:43:14 +0200 Subject: [PATCH 4/5] add iplabel datametrie --- .../iplabel/datametrie/restapi/custom/api.pm | 307 ++++++++++++++++++ .../iplabel/datametrie/restapi/mode/kpi.pm | 191 +++++++++++ .../datametrie/restapi/mode/listkpi.pm | 115 +++++++ .../iplabel/datametrie/restapi/plugin.pm | 50 +++ 4 files changed, 663 insertions(+) create mode 100644 apps/monitoring/iplabel/datametrie/restapi/custom/api.pm create mode 100644 apps/monitoring/iplabel/datametrie/restapi/mode/kpi.pm create mode 100644 apps/monitoring/iplabel/datametrie/restapi/mode/listkpi.pm create mode 100644 apps/monitoring/iplabel/datametrie/restapi/plugin.pm diff --git a/apps/monitoring/iplabel/datametrie/restapi/custom/api.pm b/apps/monitoring/iplabel/datametrie/restapi/custom/api.pm new file mode 100644 index 000000000..d048dffa0 --- /dev/null +++ b/apps/monitoring/iplabel/datametrie/restapi/custom/api.pm @@ -0,0 +1,307 @@ +# +# 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 apps::monitoring::iplabel::datametrie::restapi::custom::api; + +use strict; +use warnings; +use centreon::plugins::http; +use centreon::plugins::statefile; +use JSON::XS; +use Digest::MD5 qw(md5_hex); + +sub new { + my ($class, %options) = @_; + my $self = {}; + bless $self, $class; + + if (!defined($options{output})) { + print "Class Custom: Need to specify 'output' argument.\n"; + exit 3; + } + if (!defined($options{options})) { + $options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument."); + $options{output}->option_exit(); + } + + if (!defined($options{noptions})) { + $options{options}->add_options(arguments => { + 'api-username:s' => { name => 'api_username' }, + 'api-password:s' => { name => 'api_password' }, + 'hostname:s' => { name => 'hostname' }, + 'port:s' => { name => 'port' }, + 'proto:s' => { name => 'proto' }, + 'timeout:s' => { name => 'timeout' }, + 'unknown-http-status:s' => { name => 'unknown_http_status' }, + 'warning-http-status:s' => { name => 'warning_http_status' }, + 'critical-http-status:s' => { name => 'critical_http_status' } + }); + } + $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1); + + $self->{output} = $options{output}; + $self->{mode} = $options{mode}; + $self->{http} = centreon::plugins::http->new(%options); + $self->{cache} = centreon::plugins::statefile->new(%options); + + return $self; +} + +sub set_options { + my ($self, %options) = @_; + + $self->{option_results} = $options{option_results}; +} + +sub set_defaults { + my ($self, %options) = @_; + + foreach (keys %{$options{default}}) { + if ($_ eq $self->{mode}) { + for (my $i = 0; $i < scalar(@{$options{default}->{$_}}); $i++) { + foreach my $opt (keys %{$options{default}->{$_}[$i]}) { + if (!defined($self->{option_results}->{$opt}[$i])) { + $self->{option_results}->{$opt}[$i] = $options{default}->{$_}[$i]->{$opt}; + } + } + } + } + } +} + +sub check_options { + my ($self, %options) = @_; + + $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : 'api.ip-label.net'; + $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443; + $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https'; + $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10; + $self->{unknown_http_status} = (defined($self->{option_results}->{unknown_http_status})) ? $self->{option_results}->{unknown_http_status} : '%{http_code} < 200 or %{http_code} >= 300'; + $self->{warning_http_status} = (defined($self->{option_results}->{warning_http_status})) ? $self->{option_results}->{warning_http_status} : ''; + $self->{critical_http_status} = (defined($self->{option_results}->{critical_http_status})) ? $self->{option_results}->{critical_http_status} : ''; + $self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : undef; + $self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : undef; + + if (!defined($self->{hostname}) || $self->{hostname} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --hostname option."); + $self->{output}->option_exit(); + } + if (!defined($self->{api_username}) || $self->{api_username} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --api-username option."); + $self->{output}->option_exit(); + } + if (!defined($self->{api_password}) || $self->{api_password} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --api-password option."); + $self->{output}->option_exit(); + } + + $self->{cache}->check_options(option_results => $self->{option_results}); + + return 0; +} + +sub get_hostname { + my ($self, %options) = @_; + + return $self->{hostname}; +} + +sub get_port { + my ($self, %options) = @_; + + return $self->{port}; +} + +sub build_options_for_httplib { + my ($self, %options) = @_; + + $self->{option_results}->{hostname} = $self->{hostname}; + $self->{option_results}->{port} = $self->{port}; + $self->{option_results}->{proto} = $self->{proto}; + $self->{option_results}->{timeout} = $self->{timeout}; +} + +sub settings { + my ($self, %options) = @_; + + $self->build_options_for_httplib(); + $self->{http}->add_header(key => 'Content-Type', value => 'application/json;charset=UTF-8'); + $self->{http}->add_header(key => 'Accept', value => 'application/json;charset=UTF-8'); + $self->{http}->set_options(%{$self->{option_results}}); +} + +sub json_decode { + my ($self, %options) = @_; + + my $decoded; + eval { + $decoded = JSON::XS->new->utf8->decode($options{content}); + }; + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); + $self->{output}->option_exit(); + } + + return $decoded; +} + +sub clean_session_token { + my ($self, %options) = @_; + + my $datas = { last_timestamp => time() }; + $options{statefile}->write(data => $datas); + $self->{session_token} = undef; + $self->{http}->add_header(key => 'Ipln-Auth', value => undef); +} + +sub authenticate { + my ($self, %options) = @_; + + my $has_cache_file = $options{statefile}->read(statefile => 'iplabel_datametrie_api_' . md5_hex($self->{option_results}->{hostname}) . '_' . md5_hex($self->{option_results}->{api_username})); + my $session_token = $options{statefile}->get(name => 'session_token'); + + if ($has_cache_file == 0 || !defined($session_token)) { + my $content = $self->{http}->request( + url_path => '/REST/Get_Token/', + credentials => 1, + basic => 1, + username => $self->{api_username}, + password => $self->{api_password}, + warning_status => '', + unknown_status => '', + critical_status => '' + ); + if ($self->{http}->get_code() != 200) { + $self->{output}->add_option_msg(short_msg => "login error [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']"); + $self->{output}->option_exit(); + } + + my $decoded = $self->json_decode(content => $content); + + if (!defined($decoded) || !defined($decoded->{Ipln_WS_REST_datametrie}->{Get_Token}->{status}) || + $decoded->{Ipln_WS_REST_datametrie}->{Get_Token}->{status} ne 'success') { + $self->{output}->add_option_msg(short_msg => 'error retrieving session_token'); + $self->{output}->option_exit(); + } + + $session_token = $decoded->{Ipln_WS_REST_datametrie}->{Get_Token}->{response}->{TOKEN}; + + my $datas = { last_timestamp => time(), session_token => $session_token }; + $options{statefile}->write(data => $datas); + } + + $self->{session_token} = $session_token; + $self->{http}->add_header(key => 'Ipln-Auth', value => $self->{session_token}); +} + +sub request_api { + my ($self, %options) = @_; + + $self->settings(); + if (!defined($self->{session_token})) { + $self->authenticate(statefile => $self->{cache}); + } + + my $content = $self->{http}->request( + url_path => '/REST' . $options{endpoint}, + get_param => $options{get_param}, + unknown_status => $self->{unknown_http_status}, + warning_status => $self->{warning_http_status}, + critical_status => $self->{critical_http_status} + ); + + # wrong token is 200 + my $decoded = $self->json_decode(content => $content); + if (!defined($decoded) || !defined($decoded->{Ipln_WS_REST_datametrie}->{ $options{label} }->{status})) { + $self->{output}->add_option_msg(short_msg => 'error while retrieving data (add --debug option for detailed message)'); + $self->{output}->option_exit(); + } + + if ($decoded->{Ipln_WS_REST_datametrie}->{ $options{label} }->{status} ne 'success') { + $self->clean_session_token(statefile => $self->{cache}); + $self->authenticate(statefile => $self->{cache}); + $content = $self->{http}->request( + url_path => '/REST' . $options{endpoint}, + get_param => $options{get_param}, + unknown_status => $self->{unknown_http_status}, + warning_status => $self->{warning_http_status}, + critical_status => $self->{critical_http_status} + ); + + $decoded = $self->json_decode(content => $content); + if (!defined($decoded) || !defined($decoded->{Ipln_WS_REST_datametrie}->{ $options{label} }->{status})) { + $self->{output}->add_option_msg(short_msg => 'error while retrieving data (add --debug option for detailed message)'); + $self->{output}->option_exit(); + } + } + + if ($decoded->{Ipln_WS_REST_datametrie}->{ $options{label} }->{status} ne 'success') { + $self->{output}->add_option_msg(short_msg => 'method failed'); + $self->{output}->option_exit(); + } + + return $decoded->{Ipln_WS_REST_datametrie}->{ $options{label} }->{response}; +} + +1; + +__END__ + +=head1 NAME + +Ip-Label Datametrie Rest API + +=head1 REST API OPTIONS + +Ip-Label Datametrie Rest API + +=over 8 + +=item B<--hostname> + +Set hostname (Default: 'api.ip-label.net'). + +=item B<--port> + +Port used (Default: 443) + +=item B<--proto> + +Specify https if needed (Default: 'https') + +=item B<--api-username> + +Set username. + +=item B<--api-password> + +Set password. + +=item B<--timeout> + +Set timeout in seconds (Default: 10). + +=back + +=head1 DESCRIPTION + +B. + +=cut diff --git a/apps/monitoring/iplabel/datametrie/restapi/mode/kpi.pm b/apps/monitoring/iplabel/datametrie/restapi/mode/kpi.pm new file mode 100644 index 000000000..5a828d2b1 --- /dev/null +++ b/apps/monitoring/iplabel/datametrie/restapi/mode/kpi.pm @@ -0,0 +1,191 @@ +# +# 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 apps::monitoring::iplabel::datametrie::restapi::mode::kpi; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use POSIX; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); + +sub custom_status_output { + my ($self, %options) = @_; + + return sprintf('status: %s', $self->{result_values}->{status}); +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'kpi', type => 1, cb_prefix_output => 'prefix_kpi_output', message_multiple => 'All KPI are ok', skipped_code => { -10 => 1 } }, + ]; + + $self->{maps_counters}->{kpi} = [ + { label => 'status', threshold => 0, set => { + key_values => [ { name => 'status' }, { name => 'display' } ], + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold + } + }, + { label => 'success-rate', nlabel => 'kpi.success.rate.percentage', set => { + key_values => [ { name => 'success_rate', no_value => -1 }, { name => 'display' } ], + output_template => 'success rate: %.2f %%', + perfdatas => [ + { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1 } + ] + } + }, + { label => 'sla-availability', nlabel => 'kpi.sla.availability.percentage', set => { + key_values => [ { name => 'sla_availability', no_value => -1 }, { name => 'display' } ], + output_template => 'sla availability: %.2f %%', + perfdatas => [ + { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1 } + ] + } + }, + { label => 'performance', nlabel => 'kpi.performance.count', set => { + key_values => [ { name => 'performance', no_value => -1 }, { name => 'display' } ], + output_template => 'performance: %s', + perfdatas => [ + { template => '%s', min => 0, label_extra_instance => 1 } + ] + } + } + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, 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 => '' } + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $self->change_macros(macros => ['unknown_status', 'warning_status', 'critical_status', ]); +} + +sub prefix_kpi_output { + my ($self, %options) = @_; + + return "KPI '" . $options{instance_value}->{display} . "' "; +} + +sub manage_selection { + my ($self, %options) = @_; + + my $results = $options{custom}->request_api( + endpoint => '/Get_Monitors/', + label => 'Get_Monitors' + ); + + $self->{kpi} = {}; + my $time = time(); + my $end_date = POSIX::strftime('%d/%m/%Y %H:%M:%S', gmtime()); + foreach (@$results) { + if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && + $_->{MONITOR_NAME} !~ /$self->{option_results}->{filter_name}/) { + $self->{output}->output_add(long_msg => "skipping monitor '" . $_->{MONITOR_NAME} . "': no matching filter.", debug => 1); + next; + } + + my $start_date = POSIX::strftime('%d/%m/%Y %H:%M:%S', gmtime($time - ($_->{PERIODICITY} * 60 * 2))); + my $kpi_detail = $options{custom}->request_api( + endpoint => '/GMT/Get_KPI/', + label => 'Get_KPI', + get_param => [ + 'monitor_id=' . $_->{MONITOR_ID}, + 'date_value1=' . $start_date, + 'date_value2=' . $end_date + ] + ); + + $self->{kpi}->{ $_->{MONITOR_NAME} } = { + display => $_->{MONITOR_NAME}, + status => $_->{MONITOR_STATUS}, + success_rate => $kpi_detail->{SUCCESS_RATE}, + performance => $kpi_detail->{PERFORMANCE}, + sla_availability => $kpi_detail->{SLA_AVAILABILITY}, + }; + } + + if (scalar(keys %{$self->{kpi}}) <= 0) { + $self->{output}->add_option_msg(short_msg => "No monitor found"); + $self->{output}->option_exit(); + } +} + +1; + +__END__ + +=head1 MODE + +Check KPI. + +=over 8 + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='status' + +=item B<--filter-name> + +Filter monitor 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. +Can used special variables like: %{status}, %{display} + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'success-rate' (%) 'sla-availability' (%), 'performance'. + +=back + +=cut diff --git a/apps/monitoring/iplabel/datametrie/restapi/mode/listkpi.pm b/apps/monitoring/iplabel/datametrie/restapi/mode/listkpi.pm new file mode 100644 index 000000000..9ea38d036 --- /dev/null +++ b/apps/monitoring/iplabel/datametrie/restapi/mode/listkpi.pm @@ -0,0 +1,115 @@ +# +# 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 apps::monitoring::iplabel::datametrie::restapi::mode::listkpi; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $options{options}->add_options(arguments => { + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); +} + +sub manage_selection { + my ($self, %options) = @_; + + my $api_results = $options{custom}->request_api( + endpoint => '/Get_Monitors/', + label => 'Get_Monitors' + ); + my $results = {}; + foreach (@$api_results) { + $results->{$_->{MONITOR_ID}} = { + id => $_->{MONITOR_ID}, + name => $_->{MONITOR_NAME}, + client_id => $_->{CLIENT_ID}, + status => $_->{MONITOR_STATUS} + }; + } + + return $results; +} + +sub run { + my ($self, %options) = @_; + + my $results = $self->manage_selection(%options); + foreach (sort {lc $a->{name} cmp lc $b->{name}} values %$results) { + $self->{output}->output_add( + long_msg => sprintf( + '[name = %s][id = %s][client id = %s][status = %s]', + $_->{name}, + $_->{id}, + $_->{client_id}, + $_->{status} + ) + ); + } + + $self->{output}->output_add( + severity => 'OK', + short_msg => 'List kpi:' + ); + $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); + $self->{output}->exit(); +} + +sub disco_format { + my ($self, %options) = @_; + + $self->{output}->add_disco_format(elements => ['id', 'name', 'client_id', 'status']); +} + +sub disco_show { + my ($self, %options) = @_; + + my $scenarios = $self->manage_selection(%options); + foreach (sort {lc $a->{name} cmp lc $b->{name}} values %$scenarios) { + $self->{output}->add_disco_entry(%$_); + } +} + +1; + +__END__ + +=head1 MODE + +List KPI. + +=over 8 + +=back + +=cut diff --git a/apps/monitoring/iplabel/datametrie/restapi/plugin.pm b/apps/monitoring/iplabel/datametrie/restapi/plugin.pm new file mode 100644 index 000000000..48b29d62c --- /dev/null +++ b/apps/monitoring/iplabel/datametrie/restapi/plugin.pm @@ -0,0 +1,50 @@ +# +# 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 apps::monitoring::iplabel::datametrie::restapi::plugin; + +use strict; +use warnings; +use base qw(centreon::plugins::script_custom); + +sub new { + my ( $class, %options ) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $self->{version} = '0.1'; + $self->{modes} = { + 'list-kpi' => 'apps::monitoring::iplabel::datametrie::restapi::mode::listkpi', + 'kpi' => 'apps::monitoring::iplabel::datametrie::restapi::mode::kpi' + }; + + $self->{custom_modes}{api} = 'apps::monitoring::iplabel::datametrie::restapi::custom::api'; + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check IP-Label Datametrie using Rest API. + +=cut From adfe6a05f0f0c1376b691d16081c64cc6fc650d3 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Tue, 26 May 2020 09:21:26 +0200 Subject: [PATCH 5/5] fix help --- network/ruckus/smartzone/snmp/mode/accesspoints.pm | 4 +--- network/ruckus/zonedirector/snmp/mode/accesspoints.pm | 6 +++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/network/ruckus/smartzone/snmp/mode/accesspoints.pm b/network/ruckus/smartzone/snmp/mode/accesspoints.pm index d67ab5b67..88ce5132b 100644 --- a/network/ruckus/smartzone/snmp/mode/accesspoints.pm +++ b/network/ruckus/smartzone/snmp/mode/accesspoints.pm @@ -236,9 +236,7 @@ Can used special variables like: %{config_status}, %{connection_status}, %{regi =item B<--warning-*> B<--critical-*> Thresholds. -Can be: 'memory-usage', 'usage-free', 'usage-prct', 'traffic-in', 'traffic-out', -'cpu-utilization', 'connection-accesspoints', 'connection-client-devices-authorized', -'connection-rogue-devices'. +Can be: 'traffic-in', 'traffic-out', 'connection-client-devices-authorized'. =back diff --git a/network/ruckus/zonedirector/snmp/mode/accesspoints.pm b/network/ruckus/zonedirector/snmp/mode/accesspoints.pm index ff5f71460..183c1acf8 100644 --- a/network/ruckus/zonedirector/snmp/mode/accesspoints.pm +++ b/network/ruckus/zonedirector/snmp/mode/accesspoints.pm @@ -107,7 +107,7 @@ sub set_counters { ] } }, - { label => 'usage-free', display_ok => 0, nlabel => 'accesspoint.memory.free.bytes', set => { + { label => 'memory-usage-free', display_ok => 0, nlabel => 'accesspoint.memory.free.bytes', set => { key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, { name => 'display' } ], closure_custom_output => $self->can('custom_usage_output'), perfdatas => [ @@ -115,7 +115,7 @@ sub set_counters { ] } }, - { label => 'usage-prct', display_ok => 0, nlabel => 'accesspoint.memory.usage.percentage', set => { + { label => 'memory-usage-prct', display_ok => 0, nlabel => 'accesspoint.memory.usage.percentage', set => { key_values => [ { name => 'prct_used' }, { name => 'display' } ], output_template => 'ram used: %.2f %%', perfdatas => [ @@ -318,7 +318,7 @@ Can used special variables like: %{zd_connection_status} =item B<--warning-*> B<--critical-*> Thresholds. -Can be: 'memory-usage', 'usage-free', 'usage-prct', 'traffic-in', 'traffic-out', +Can be: 'memory-usage', 'memory-usage-free', 'memory-usage-prct', 'traffic-in', 'traffic-out', 'cpu-utilization', 'connection-accesspoints', 'connection-client-devices-authorized', 'connection-rogue-devices'.