From b08b3d0d5b35cf35c29b3f9b67d3724e4fce2799 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Wed, 15 Jul 2020 16:39:48 +0200 Subject: [PATCH] Fix #1897 --- .../nginx/nginxplus/restapi/custom/api.pm | 203 ++++++++++++++++++ .../nginxplus/restapi/mode/connections.pm | 127 +++++++++++ .../nginx/nginxplus/restapi/mode/httpzones.pm | 194 +++++++++++++++++ .../apps/nginx/nginxplus/restapi/mode/ssl.pm | 119 ++++++++++ .../apps/nginx/nginxplus/restapi/plugin.pm | 51 +++++ 5 files changed, 694 insertions(+) create mode 100644 centreon-plugins/apps/nginx/nginxplus/restapi/custom/api.pm create mode 100644 centreon-plugins/apps/nginx/nginxplus/restapi/mode/connections.pm create mode 100644 centreon-plugins/apps/nginx/nginxplus/restapi/mode/httpzones.pm create mode 100644 centreon-plugins/apps/nginx/nginxplus/restapi/mode/ssl.pm create mode 100644 centreon-plugins/apps/nginx/nginxplus/restapi/plugin.pm diff --git a/centreon-plugins/apps/nginx/nginxplus/restapi/custom/api.pm b/centreon-plugins/apps/nginx/nginxplus/restapi/custom/api.pm new file mode 100644 index 000000000..c8c0798f4 --- /dev/null +++ b/centreon-plugins/apps/nginx/nginxplus/restapi/custom/api.pm @@ -0,0 +1,203 @@ +# +# 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::nginx::nginxplus::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' }, + 'hostname:s' => { name => 'hostname' }, + 'port:s' => { name => 'port' }, + 'proto:s' => { name => 'proto' }, + 'timeout:s' => { name => 'timeout' }, + 'api-path:s' => { name => 'api_path' }, + '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->{http} = centreon::plugins::http->new(%options); + + return $self; +} + +sub set_options { + my ($self, %options) = @_; + + $self->{option_results} = $options{option_results}; +} + +sub set_defaults {} + +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} : 443; + $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https'; + $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 30; + $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_path} = (defined($self->{option_results}->{api_path})) ? $self->{option_results}->{api_path} : '/api/6'; + $self->{unknown_http_status} = (defined($self->{option_results}->{unknown_http_status})) ? $self->{option_results}->{unknown_http_status} : '(%{http_code} < 200 or %{http_code} >= 300) and %{http_code} != 424'; + $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(); + } + + 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}; + if ($self->{api_username} ne '') { + $self->{option_results}->{credentials} = 1; + $self->{option_results}->{basic} = 1; + $self->{option_results}->{username} = $self->{api_username}; + $self->{option_results}->{password} = $self->{api_password}; + } +} + +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}->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 $content = $self->{http}->request( + url_path => $self->{api_path} . $options{endpoint}, + unknown_status => $self->{unknown_http_status}, + warning_status => $self->{warning_http_status}, + critical_status => $self->{critical_http_status} + ); + + 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; +} + +1; + +__END__ + +=head1 NAME + +Nginx Plus Rest API + +=head1 REST API OPTIONS + +Nginx Plus Rest API + +=over 8 + +=item B<--hostname> + +Nginx hostname. + +=item B<--port> + +Port used (Default: 443) + +=item B<--proto> + +Specify https if needed (Default: 'https') + +=item B<--api-username> + +Nginx basic username. + +=item B<--api-password> + +Nginx basic password. + +=item B<--api-path> + +Specify api path (Default: '/api/6') + +=item B<--timeout> + +Set timeout in seconds (Default: 30). + +=back + +=head1 DESCRIPTION + +B. + +=cut diff --git a/centreon-plugins/apps/nginx/nginxplus/restapi/mode/connections.pm b/centreon-plugins/apps/nginx/nginxplus/restapi/mode/connections.pm new file mode 100644 index 000000000..70cb33d4b --- /dev/null +++ b/centreon-plugins/apps/nginx/nginxplus/restapi/mode/connections.pm @@ -0,0 +1,127 @@ +# +# 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::nginx::nginxplus::restapi::mode::connections; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use Digest::MD5 qw(md5_hex); + +sub prefix_connection_output { + my ($self, %options) = @_; + + return 'Connections '; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'connection', type => 0, cb_prefix_output => 'prefix_connection_output' } + ]; + + $self->{maps_counters}->{connection} = [ + { label => 'active', nlabel => 'connections.active.count', set => { + key_values => [ { name => 'active' } ], + output_template => 'active: %s', + perfdatas => [ + { template => '%s', min => 0 } + ] + } + }, + { label => 'idle', nlabel => 'connections.idle.count', set => { + key_values => [ { name => 'idle' } ], + output_template => 'idle: %s', + perfdatas => [ + { template => '%s', min => 0 } + ] + } + }, + { label => 'accepted', nlabel => 'connections.accepted.count', display_ok => 0, set => { + key_values => [ { name => 'accepted', diff => 1 } ], + output_template => 'accepted: %s', + perfdatas => [ + { template => '%s', min => 0 } + ] + } + }, + { label => 'dropped', nlabel => 'connections.dropped.count', display_ok => 0, set => { + key_values => [ { name => 'dropped', diff => 1 } ], + output_template => 'dropped: %s', + perfdatas => [ + { template => '%s', min => 0 } + ] + } + } + ]; +} + +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 => { + }); + + return $self; +} + +sub manage_selection { + my ($self, %options) = @_; + + my $result = $options{custom}->request_api( + endpoint => '/connections' + ); + + $self->{connection} = {}; + foreach (keys %$result) { + $self->{connection}->{$_} = $result->{$_}; + } + + $self->{cache_name} = 'nginx_nginxplus_' . $options{custom}->get_hostname() . '_' . $self->{mode} . '_' . + (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')); +} + +1; + +__END__ + +=head1 MODE + +Check connections. + +=over 8 + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='accepted' + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'active', 'idle', 'accepted', 'dropped'. + +=back + +=cut diff --git a/centreon-plugins/apps/nginx/nginxplus/restapi/mode/httpzones.pm b/centreon-plugins/apps/nginx/nginxplus/restapi/mode/httpzones.pm new file mode 100644 index 000000000..ccde104a6 --- /dev/null +++ b/centreon-plugins/apps/nginx/nginxplus/restapi/mode/httpzones.pm @@ -0,0 +1,194 @@ +# +# 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::nginx::nginxplus::restapi::mode::httpzones; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use Digest::MD5 qw(md5_hex); + +sub prefix_server_zone_output { + my ($self, %options) = @_; + + return "Server zone '" . $options{instance_value}->{display} . "' "; +} + +sub prefix_location_zone_output { + my ($self, %options) = @_; + + return "Location zone '" . $options{instance_value}->{display} . "' "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'server_zones', type => 1, cb_prefix_output => 'prefix_server_zone_output', message_multiple => 'All server zones are ok', skipped_code => { -10 => 1 } }, + { name => 'location_zones', type => 1, cb_prefix_output => 'prefix_location_zone_output', message_multiple => 'All location zones are ok', skipped_code => { -10 => 1 } } + ]; + + foreach my $name (('server', 'location')) { + $self->{maps_counters}->{$name . '_zones'} = [ + { label => $name . 'zone-requests', nlabel => 'http.' . $name . 'zone.requests.persecond', set => { + key_values => [ { name => 'requests', per_second => 1 } ], + output_template => 'requests: %.2f/s', + perfdatas => [ + { template => '%.2f', min => 0, unit => '/s', label_extra_instance => 1 } + ] + } + }, + { label => $name . 'zone-requests-discarded', nlabel => 'http.' . $name . 'zone.requests.discarded.count', set => { + key_values => [ { name => 'discarded' } ], + output_template => 'discarded: %s', + perfdatas => [ + { template => '%s', min => 0, label_extra_instance => 1 } + ] + } + }, + { label => $name . 'zone-traffic-in', nlabel => 'http.' . $name . 'zone.traffic.in.bitspersecond', set => { + key_values => [ { name => 'received', per_second => 1 } ], + output_template => 'traffic in: %s %s/s', + output_change_bytes => 2, + perfdatas => [ + { template => '%s', min => 0, unit => 'b/s', label_extra_instance => 1 } + ] + } + }, + { label => $name . 'zone-traffic-out', nlabel => 'http.' . $name . 'zone.traffic.out.bitspersecond', set => { + key_values => [ { name => 'sent', per_second => 1 } ], + output_template => 'traffic out: %s %s/s', + output_change_bytes => 2, + perfdatas => [ + { template => '%s', min => 0, unit => 'b/s', label_extra_instance => 1 } + ] + } + }, + { label => $name . 'zone-responses-total', nlabel => 'http.' . $name . 'zone.responses.total.count', set => { + key_values => [ { name => 'responses_total', diff => 1 } ], + output_template => 'responses total: %s', + perfdatas => [ + { template => '%s', min => 0, label_extra_instance => 1 } + ] + } + } + ]; + + foreach (('1xx', '2xx', '3xx', '4xx', '5xx')) { + push @{$self->{maps_counters}->{$name . '_zones'}}, { + label => $name . 'zone-responses-' . $_, nlabel => 'http.' . $name . 'zone.responses.' . $_ . '.count', display_ok => 0, set => { + key_values => [ { name => 'responses_' . $_, diff => 1 } ], + output_template => 'responses ' . $_ . ': %s', + perfdatas => [ + { template => '%s', min => 0, 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-server-name:s' => { name => 'filter_server_name' }, + 'filter-location-name:s' => { name => 'filter_location_name' } + }); + + return $self; +} + +sub manage_selection { + my ($self, %options) = @_; + + foreach my $name (('server', 'location')) { + my $results = $options{custom}->request_api( + endpoint => '/http/' . $name . '_zones/' + ); + + $self->{$name . '_zones'} = {}; + foreach (keys %$results) { + if (defined($self->{option_results}->{'filter_' . $name . '_name'}) && $self->{option_results}->{'filter_' . $name . '_name'} ne '' && + $_ !~ /$self->{option_results}->{'filter_' . $name . '_name'}/) { + $self->{output}->output_add(long_msg => "skipping $name zone '" . $_ . "': no matching filter.", debug => 1); + next; + } + + $self->{$name . '_zones'}->{$_} = { + display => $_, + discarded => $results->{ $_ }->{discarded}, + requests => $results->{ $_ }->{requests}, + received => $results->{ $_ }->{received} * 8, + sent => $results->{ $_ }->{sent} * 8, + responses_total => $results->{ $_ }->{responses}->{total}, + responses_1xx => $results->{ $_ }->{responses}->{'1xx'}, + responses_2xx => $results->{ $_ }->{responses}->{'2xx'}, + responses_3xx => $results->{ $_ }->{responses}->{'3xx'}, + responses_4xx => $results->{ $_ }->{responses}->{'4xx'}, + responses_5xx => $results->{ $_ }->{responses}->{'5xx'} + }; + } + } + + $self->{cache_name} = 'nginx_nginxplus_' . $options{custom}->get_hostname() . '_' . $self->{mode} . '_' . + (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' . + (defined($self->{option_results}->{filter_server_name}) ? md5_hex($self->{option_results}->{filter_server_name}) : md5_hex('all')); +} + +1; + +__END__ + +=head1 MODE + +Check http zones. + +=over 8 + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='serverzone' + +=item B<--filter-server-name> + +Filter server zone name (can be a regexp). + +=item B<--filter-location-name> + +Filter location zone name (can be a regexp). + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'serverzone-requests', 'serverzone-requests-discarded', 'serverzone-traffic-in', 'serverzone-traffic-out', +'serverzone-responses-total', 'serverzone-responses-1xx', 'serverzone-responses-2xx', 'serverzone-responses-3xx', +'serverzone-responses-4xx', 'serverzone-responses-5xx', +'locationzone-requests', 'locationzone-requests-discarded', 'locationzone-traffic-in', 'locationzone-traffic-out', +'locationzone-responses-total', 'locationzone-responses-1xx', 'locationzone-responses-2xx', 'locationzone-responses-3xx', +'locationzone-responses-4xx', 'locationzone-responses-5xx'. + +=back + +=cut diff --git a/centreon-plugins/apps/nginx/nginxplus/restapi/mode/ssl.pm b/centreon-plugins/apps/nginx/nginxplus/restapi/mode/ssl.pm new file mode 100644 index 000000000..6eed740be --- /dev/null +++ b/centreon-plugins/apps/nginx/nginxplus/restapi/mode/ssl.pm @@ -0,0 +1,119 @@ +# +# 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::nginx::nginxplus::restapi::mode::ssl; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use Digest::MD5 qw(md5_hex); + +sub prefix_ssl_output { + my ($self, %options) = @_; + + return 'Ssl '; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'ssl', type => 0, cb_prefix_output => 'prefix_ssl_output' } + ]; + + $self->{maps_counters}->{ssl} = [ + { label => 'handshakes-succeeded', nlabel => 'ssl.handshakes.succeeded.count', set => { + key_values => [ { name => 'handshakes', diff => 1 } ], + output_template => 'handshakes succeeded: %s', + perfdatas => [ + { template => '%s', min => 0 } + ] + } + }, + { label => 'handshakes-failed', nlabel => 'ssl.handshakes.failed.count', set => { + key_values => [ { name => 'handshakes_failed', diff => 1 } ], + output_template => 'handshakes failed: %s', + perfdatas => [ + { template => '%s', min => 0 } + ] + } + }, + { label => 'sessions-reuses', nlabel => 'ssl.sessions.reuses.count', set => { + key_values => [ { name => 'session_reuses', diff => 1 } ], + output_template => 'session reuses: %s', + perfdatas => [ + { template => '%s', min => 0 } + ] + } + } + ]; +} + +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 => { + }); + + return $self; +} + +sub manage_selection { + my ($self, %options) = @_; + + my $result = $options{custom}->request_api( + endpoint => '/ssl' + ); + + $self->{ssl} = {}; + foreach (keys %$result) { + $self->{ssl}->{$_} = $result->{$_}; + } + + $self->{cache_name} = 'nginx_nginxplus_' . $options{custom}->get_hostname() . '_' . $self->{mode} . '_' . + (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')); +} + +1; + +__END__ + +=head1 MODE + +Check ssl statistics. + +=over 8 + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='failed' + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'handshakes-succeeded', 'handshakes-failed', 'sessions-reuses'. + +=back + +=cut diff --git a/centreon-plugins/apps/nginx/nginxplus/restapi/plugin.pm b/centreon-plugins/apps/nginx/nginxplus/restapi/plugin.pm new file mode 100644 index 000000000..ccf5a91ce --- /dev/null +++ b/centreon-plugins/apps/nginx/nginxplus/restapi/plugin.pm @@ -0,0 +1,51 @@ +# +# 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::nginx::nginxplus::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} = { + 'connections' => 'apps::nginx::nginxplus::restapi::mode::connections', + 'http-zones' => 'apps::nginx::nginxplus::restapi::mode::httpzones', + 'ssl' => 'apps::nginx::nginxplus::restapi::mode::ssl' + }; + + $self->{custom_modes}->{api} = 'apps::nginx::nginxplus::restapi::custom::api'; + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check Nginx Web Servers through Nginx Plus Rest API. + +=cut