diff --git a/cloud/prometheus/direct/nginxingresscontroller/mode/connections.pm b/cloud/prometheus/direct/nginxingresscontroller/mode/connections.pm new file mode 100644 index 000000000..ddb9185d8 --- /dev/null +++ b/cloud/prometheus/direct/nginxingresscontroller/mode/connections.pm @@ -0,0 +1,195 @@ +# +# Copyright 2019 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 cloud::prometheus::direct::nginxingresscontroller::mode::connections; + +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 => 'global', type => 0, cb_prefix_output => 'prefix_containers_output' }, + ]; + + $self->{maps_counters}->{global} = [ + { label => 'reading', set => { + key_values => [ { name => 'reading' } ], + output_template => 'Reading: %d', + perfdatas => [ + { label => 'reading', value => 'reading_absolute', template => '%d', + min => 0, unit => 'connections' }, + ], + } + }, + { label => 'waiting', set => { + key_values => [ { name => 'waiting' } ], + output_template => 'Waiting: %d', + perfdatas => [ + { label => 'waiting', value => 'waiting_absolute', template => '%d', + min => 0, unit => 'connections' }, + ], + } + }, + { label => 'writing', set => { + key_values => [ { name => 'writing' } ], + output_template => 'Writing: %d', + perfdatas => [ + { label => 'writing', value => 'writing_absolute', template => '%d', + min => 0, unit => 'connections' }, + ], + } + }, + { label => 'active', set => { + key_values => [ { name => 'active' } ], + output_template => 'Active: %d', + perfdatas => [ + { label => 'active', value => 'active_absolute', template => '%d', + min => 0, unit => 'connections' }, + ], + } + }, + { label => 'accepted', set => { + key_values => [ { name => 'accepted', diff => 1 } ], + output_template => 'Accepted: %.2f/s', + per_second => 1, + perfdatas => [ + { label => 'accepted', value => 'accepted_per_second', template => '%.2f', + min => 0, unit => 'connections/s' }, + ], + } + }, + { label => 'handled', set => { + key_values => [ { name => 'handled', diff => 1 } ], + output_template => 'Handled: %.2f/s', + per_second => 1, + perfdatas => [ + { label => 'handled', value => 'handled_per_second', template => '%.2f', + min => 0, unit => 'connections/s' }, + ], + } + }, + ]; +} + +sub prefix_containers_output { + my ($self, %options) = @_; + + return "Connections "; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => { + "filter-counters:s" => { name => 'filter_counters' }, + "extra-filter:s@" => { name => 'extra_filter' }, + "metric-overload:s@" => { name => 'metric_overload' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $self->{metrics} = { + 'connections' => '^nginx_ingress_controller_nginx_process_connections$', + 'connections_total' => '^nginx_ingress_controller_nginx_process_connections_total$', + }; + foreach my $metric (@{$self->{option_results}->{metric_overload}}) { + next if ($metric !~ /(.*),(.*)/); + $self->{metrics}->{$1} = $2 if (defined($self->{metrics}->{$1})); + } + + $self->{extra_filter} = ''; + foreach my $filter (@{$self->{option_results}->{extra_filter}}) { + $self->{extra_filter} .= ',' . $filter; + } +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{cache_name} = "prometheus_nginxingresscontroller_" . md5_hex($options{custom}->get_connection_info()) . '_' . $self->{mode} . '_' . + (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')); + + $self->{global} = {}; + + my $results = $options{custom}->query(queries => [ 'label_replace({__name__=~"' . $self->{metrics}->{connections} . '",' . + $self->{extra_filter} . '}, "__name__", "connections", "", "")', + 'label_replace({__name__=~"' . $self->{metrics}->{connections_total} . '",' . + $self->{extra_filter} . '}, "__name__", "connections_total", "", "")' ]); + + foreach my $result (@{$results}) { + $self->{global}->{$result->{metric}->{state}} = ${$result->{value}}[1]; + } +} + +1; + +__END__ + +=head1 MODE + +Check connections number. + +=over 8 + +=item B<--warning-*> + +Threshold warning. +Can be: 'reading', 'waiting', 'writing', 'active', +'accepted', 'handled'. + +=item B<--critical-*> + +Threshold critical. +Can be: 'reading', 'waiting', 'writing', 'active', +'accepted', 'handled'. + +=item B<--extra-filter> + +Add a PromQL filter (Can be multiple) + +Example : --extra-filter='name=~".*pretty.*"' + +=item B<--metric-overload> + +Overload default metrics name (Can be multiple) + +Example : --metric-overload='metric,^my_metric_name$' + +Default : + + - connections: ^nginx_ingress_controller_nginx_process_connections$ + - connections_total: ^nginx_ingress_controller_nginx_process_connections_total$ + +=back + +=cut diff --git a/cloud/prometheus/direct/nginxingresscontroller/mode/requests.pm b/cloud/prometheus/direct/nginxingresscontroller/mode/requests.pm new file mode 100644 index 000000000..8203ecd08 --- /dev/null +++ b/cloud/prometheus/direct/nginxingresscontroller/mode/requests.pm @@ -0,0 +1,204 @@ +# +# Copyright 2019 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 cloud::prometheus::direct::nginxingresscontroller::mode::requests; + +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 => 'global', type => 0 }, + { name => 'namespaces', type => 1, cb_prefix_output => 'prefix_namespaces_output', + message_multiple => 'All namespaces request metrics are ok', skipped_code => { -10 => 1 } }, + ]; + + $self->{maps_counters}->{global} = [ + { label => 'requests', set => { + key_values => [ { name => 'requests', diff => 1 } ], + output_template => 'Requests: %.2f/s', + per_second => 1, + perfdatas => [ + { label => 'requests', value => 'requests_per_second', template => '%.2f', + min => 0, unit => 'requests/s' }, + ], + } + }, + ]; + $self->{maps_counters}->{namespaces} = [ + { label => 'requests-2xx', set => { + key_values => [ { name => 'requests_2xx', diff => 1 } ], + output_template => 'Requests 2xx: %.2f/s', + per_second => 1, + perfdatas => [ + { label => 'requests_2xx', value => 'requests_2xx_per_second', template => '%.2f', unit => 'requests/s', + min => 0, label_extra_instance => 1 }, + ], + } + }, + { label => 'requests-3xx', set => { + key_values => [ { name => 'requests_3xx', diff => 1 } ], + output_template => 'Requests 3xx: %.2f/s', + per_second => 1, + perfdatas => [ + { label => 'requests_3xx', value => 'requests_3xx_per_second', template => '%.2f', unit => 'requests/s', + min => 0, label_extra_instance => 1 }, + ], + } + }, + { label => 'requests-4xx', set => { + key_values => [ { name => 'requests_4xx', diff => 1 } ], + output_template => 'Requests 4xx: %.2f/s', + per_second => 1, + perfdatas => [ + { label => 'requests_4xx', value => 'requests_4xx_per_second', template => '%.2f', unit => 'requests/s', + min => 0, label_extra_instance => 1 }, + ], + } + }, + { label => 'requests-5xx', set => { + key_values => [ { name => 'requests_5xx', diff => 1 } ], + output_template => 'Requests 5xx: %.2f/s', + per_second => 1, + perfdatas => [ + { label => 'requests_5xx', value => 'requests_5xx_per_second', template => '%.2f', unit => 'requests/s', + min => 0, label_extra_instance => 1 }, + ], + } + }, + ]; +} + +sub prefix_namespaces_output { + my ($self, %options) = @_; + + return "Namespace '" . $options{instance_value}->{display} . "' "; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => { + "filter-counters:s" => { name => 'filter_counters' }, + "extra-filter:s@" => { name => 'extra_filter' }, + "metric-overload:s@" => { name => 'metric_overload' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $self->{metrics} = { + 'requests_total' => '^nginx_ingress_controller_nginx_process_requests_total$', + 'requests' => '^nginx_ingress_controller_requests$', + }; + foreach my $metric (@{$self->{option_results}->{metric_overload}}) { + next if ($metric !~ /(.*),(.*)/); + $self->{metrics}->{$1} = $2 if (defined($self->{metrics}->{$1})); + } + + $self->{extra_filter} = ''; + foreach my $filter (@{$self->{option_results}->{extra_filter}}) { + $self->{extra_filter} .= ',' . $filter; + } +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{cache_name} = "prometheus_nginxingresscontroller_" . md5_hex($options{custom}->get_connection_info()) . '_' . $self->{mode} . '_' . + (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')); + + $self->{global} = {}; + + my $results = $options{custom}->query(queries => [ 'label_replace({__name__=~"' . $self->{metrics}->{requests_total} . '",' . + $self->{extra_filter} . '}, "__name__", "requests_total", "", "")' ]); + + foreach my $result (@{$results}) { + $self->{global}->{requests} = ${$result->{value}}[1]; + } + + $self->{namespaces} = {}; + + $results = $options{custom}->query(queries => [ 'label_replace({__name__=~"' . $self->{metrics}->{requests} . '",' . + $self->{extra_filter} . '}, "__name__", "requests", "", "")' ]); + + foreach my $result (@{$results}) { + $self->{namespaces}->{$result->{metric}->{exported_namespace}}->{display} = $result->{metric}->{exported_namespace}; + $self->{namespaces}->{$result->{metric}->{exported_namespace}}->{requests_2xx} += ${$result->{value}}[1] if ($result->{metric}->{status} =~ /^2/); + $self->{namespaces}->{$result->{metric}->{exported_namespace}}->{requests_3xx} += ${$result->{value}}[1] if ($result->{metric}->{status} =~ /^3/); + $self->{namespaces}->{$result->{metric}->{exported_namespace}}->{requests_4xx} += ${$result->{value}}[1] if ($result->{metric}->{status} =~ /^4/); + $self->{namespaces}->{$result->{metric}->{exported_namespace}}->{requests_5xx} += ${$result->{value}}[1] if ($result->{metric}->{status} =~ /^5/); + } +} + +1; + +__END__ + +=head1 MODE + +Check requests number. + +=over 8 + +=item B<--warning-*> + +Threshold warning. +Can be: 'requests', 'requests-2xx', 'requests-3xx', +'requests-4xx', 'requests-5xx'. + +=item B<--critical-*> + +Threshold critical. +Can be: 'requests', 'requests-2xx', 'requests-3xx', +'requests-4xx', 'requests-5xx'. + +=item B<--extra-filter> + +Add a PromQL filter (Can be multiple) + +Example : --extra-filter='name=~".*pretty.*"' + +=item B<--metric-overload> + +Overload default metrics name (Can be multiple) + +Example : --metric-overload='metric,^my_metric_name$' + +Default : + + - requests_total: ^nginx_ingress_controller_nginx_process_requests_total$ + - requests: ^nginx_ingress_controller_requests$ + +=back + +=cut diff --git a/cloud/prometheus/direct/nginxingresscontroller/plugin.pm b/cloud/prometheus/direct/nginxingresscontroller/plugin.pm new file mode 100644 index 000000000..c65565c3d --- /dev/null +++ b/cloud/prometheus/direct/nginxingresscontroller/plugin.pm @@ -0,0 +1,56 @@ +# +# Copyright 2019 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 cloud::prometheus::direct::nginxingresscontroller::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' => 'cloud::prometheus::direct::nginxingresscontroller::mode::connections', + 'requests' => 'cloud::prometheus::direct::nginxingresscontroller::mode::requests', + ); + + $self->{custom_modes}{api} = 'cloud::prometheus::restapi::custom::api'; + return $self; +} + +sub init { + my ( $self, %options ) = @_; + + $self->SUPER::init(%options); +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check Nginx Ingress Controller metrics through Prometheus server. + +=cut