From 8928f46fff61bdd39b75cff3060fad493590fd2a Mon Sep 17 00:00:00 2001 From: Arturo Penen Date: Tue, 11 Jun 2019 16:50:03 +0200 Subject: [PATCH 1/6] Added aggr type/name config to aggregations query Changed query-average to fill that config --- bin/check-es-query-average.rb | 40 ++++++++++++------- .../elasticsearch-query.rb | 2 +- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/bin/check-es-query-average.rb b/bin/check-es-query-average.rb index c5cfa6d..ce6c2f8 100755 --- a/bin/check-es-query-average.rb +++ b/bin/check-es-query-average.rb @@ -149,6 +149,16 @@ class ESQueryAverage < Sensu::Plugin::Check::CLI required: false, default: true + option :aggr_type, + description: 'Elasticsearch query aggr type (avg)', + long: '--aggr_type', + default: 'avg' + + option :aggr_name, + description: 'Elasticsearch query aggr name (average)', + long: '--aggr_name', + default: 'average' + option :aggr_field, description: 'Elasticsearch query field to aggregate and average from', short: '-a FIELD', @@ -257,28 +267,28 @@ def kibana_info def run response = client.search(build_request_options) if config[:invert] - if response['aggregations']['average']['value'] < config[:crit] - critical "Query average (#{response['aggregations']['average']['value']}) was below critical threshold. #{kibana_info}" - elsif response['aggregations']['average']['value'] < config[:warn] - warning "Query average (#{response['aggregations']['average']['value']}) was below warning threshold. #{kibana_info}" + if response['aggregations'][config[:aggr_name]]['value'] < config[:crit] + critical "Query average (#{response['aggregations'][config[:aggr_name]]['value']}) was below critical threshold. #{kibana_info}" + elsif response['aggregations'][config[:aggr_name]]['value'] < config[:warn] + warning "Query average (#{response['aggregations'][config[:aggr_name]]['value']}) was below warning threshold. #{kibana_info}" else - ok "Query average (#{response['aggregations']['average']['value']}) was ok" + ok "Query average (#{response['aggregations'][config[:aggr_name]]['value']}) was ok" end - elsif response['aggregations']['average']['value'] > config[:crit] - critical "Query average (#{response['aggregations']['average']['value']}) was above critical threshold. #{kibana_info}" - elsif response['aggregations']['average']['value'] > config[:warn] - warning "Query average (#{response['aggregations']['average']['value']}) was above warning threshold. #{kibana_info}" + elsif response['aggregations'][config[:aggr_name]]['value'] > config[:crit] + critical "Query average (#{response['aggregations'][config[:aggr_name]]['value']}) was above critical threshold. #{kibana_info}" + elsif response['aggregations'][config[:aggr_name]]['value'] > config[:warn] + warning "Query average (#{response['aggregations'][config[:aggr_name]]['value']}) was above warning threshold. #{kibana_info}" else - ok "Query average (#{response['aggregations']['average']['value']}) was ok" + ok "Query average (#{response['aggregations'][config[:aggr_name]]['value']}) was ok" end rescue Elasticsearch::Transport::Transport::Errors::NotFound if config[:invert] - if response['aggregations']['average']['value'] < config[:crit] - critical "Query average (#{response['aggregations']['average']['value']}) was below critical threshold. #{kibana_info}" - elsif response['aggregations']['average']['value'] < config[:warn] - warning "Query average (#{response['aggregations']['average']['value']}) was below warning threshold. #{kibana_info}" + if response['aggregations'][config[:aggr_name]]['value'] < config[:crit] + critical "Query average (#{response['aggregations'][config[:aggr_name]]['value']}) was below critical threshold. #{kibana_info}" + elsif response['aggregations'][config[:aggr_name]]['value'] < config[:warn] + warning "Query average (#{response['aggregations'][config[:aggr_name]]['value']}) was below warning threshold. #{kibana_info}" else - ok "Query average (#{response['aggregations']['average']['value']}) was ok" + ok "Query average (#{response['aggregations'][config[:aggr_name]]['value']}) was ok" end else ok 'No results found, average was below thresholds' diff --git a/lib/sensu-plugins-elasticsearch/elasticsearch-query.rb b/lib/sensu-plugins-elasticsearch/elasticsearch-query.rb index a5e3f72..40d2edc 100644 --- a/lib/sensu-plugins-elasticsearch/elasticsearch-query.rb +++ b/lib/sensu-plugins-elasticsearch/elasticsearch-query.rb @@ -100,7 +100,7 @@ def build_request_options } }, 'aggregations' => { - 'average' => { 'avg' => { 'field' => config[:aggr_field] } } + config[:aggr_name] => { config[:aggr_type] => { 'field' => config[:aggr_field] } } } } else From 500ad3f104f9c550a1a88f19914de03c0a5c4f56 Mon Sep 17 00:00:00 2001 From: Arturo Penen Date: Thu, 13 Jun 2019 11:47:32 +0200 Subject: [PATCH 2/6] Modify changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85dc4ee..285a7de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](http://semver.org/). This CHANGELOG follows the format listed [here](https://github.com/sensu-plugins/community/blob/master/HOW_WE_CHANGELOG.md) ## [Unreleased] +### Changed +- elasticsearch-query.rb: changed literal aggregation name and 'type' to config vars +- check-es-query-average.rb: changed literal aggregation name and 'type' to use the new config vars ## [4.0.0] - 2019-05-07 ### Breaking Changes From ca50cc0ff373056d5bc90cfe77b5902d04ffcc33 Mon Sep 17 00:00:00 2001 From: Arturo Penen Date: Thu, 13 Jun 2019 11:50:45 +0200 Subject: [PATCH 3/6] corrected changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 285a7de..f39e5d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ This CHANGELOG follows the format listed [here](https://github.com/sensu-plugins ## [Unreleased] ### Changed - elasticsearch-query.rb: changed literal aggregation name and 'type' to config vars -- check-es-query-average.rb: changed literal aggregation name and 'type' to use the new config vars +- check-es-query-average.rb: changed literal aggregation name average and type avg to use the new config vars ## [4.0.0] - 2019-05-07 ### Breaking Changes From 1de6d13fdf24a2a1aeaf8505ad5fba2f446ce790 Mon Sep 17 00:00:00 2001 From: Arturo Penen Date: Mon, 17 Jun 2019 11:00:30 +0200 Subject: [PATCH 4/6] corrected param name --- bin/check-es-query-average.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/check-es-query-average.rb b/bin/check-es-query-average.rb index ce6c2f8..a922d00 100755 --- a/bin/check-es-query-average.rb +++ b/bin/check-es-query-average.rb @@ -151,12 +151,12 @@ class ESQueryAverage < Sensu::Plugin::Check::CLI option :aggr_type, description: 'Elasticsearch query aggr type (avg)', - long: '--aggr_type', + long: '--aggr-type', default: 'avg' option :aggr_name, description: 'Elasticsearch query aggr name (average)', - long: '--aggr_name', + long: '--aggr-name', default: 'average' option :aggr_field, From 6c1310679d6d25f1a5fd83e227e86d47c897d1c7 Mon Sep 17 00:00:00 2001 From: Arturo Penen Date: Wed, 19 Jun 2019 09:54:44 +0200 Subject: [PATCH 5/6] corrected long description --- bin/check-es-query-average.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/check-es-query-average.rb b/bin/check-es-query-average.rb index a922d00..3384331 100755 --- a/bin/check-es-query-average.rb +++ b/bin/check-es-query-average.rb @@ -151,12 +151,12 @@ class ESQueryAverage < Sensu::Plugin::Check::CLI option :aggr_type, description: 'Elasticsearch query aggr type (avg)', - long: '--aggr-type', + long: '--aggr-type TYPE', default: 'avg' option :aggr_name, description: 'Elasticsearch query aggr name (average)', - long: '--aggr-name', + long: '--aggr-name NAME', default: 'average' option :aggr_field, From b0ea8bd8b5c75ac771ae9775da971b4c7c59464f Mon Sep 17 00:00:00 2001 From: Arturo Penen Date: Wed, 19 Jun 2019 17:19:33 +0200 Subject: [PATCH 6/6] The parameter does not need to be variable in this ruby --- bin/check-es-query-average.rb | 42 ++++++++++++++--------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/bin/check-es-query-average.rb b/bin/check-es-query-average.rb index 3384331..5b0db32 100755 --- a/bin/check-es-query-average.rb +++ b/bin/check-es-query-average.rb @@ -149,16 +149,6 @@ class ESQueryAverage < Sensu::Plugin::Check::CLI required: false, default: true - option :aggr_type, - description: 'Elasticsearch query aggr type (avg)', - long: '--aggr-type TYPE', - default: 'avg' - - option :aggr_name, - description: 'Elasticsearch query aggr name (average)', - long: '--aggr-name NAME', - default: 'average' - option :aggr_field, description: 'Elasticsearch query field to aggregate and average from', short: '-a FIELD', @@ -265,30 +255,32 @@ def kibana_info end def run + config[:aggr_name] = 'average' + config[:aggr_type] = 'avg' response = client.search(build_request_options) if config[:invert] - if response['aggregations'][config[:aggr_name]]['value'] < config[:crit] - critical "Query average (#{response['aggregations'][config[:aggr_name]]['value']}) was below critical threshold. #{kibana_info}" - elsif response['aggregations'][config[:aggr_name]]['value'] < config[:warn] - warning "Query average (#{response['aggregations'][config[:aggr_name]]['value']}) was below warning threshold. #{kibana_info}" + if response['aggregations']['average']['value'] < config[:crit] + critical "Query average (#{response['aggregations']['average']['value']}) was below critical threshold. #{kibana_info}" + elsif response['aggregations']['average']['value'] < config[:warn] + warning "Query average (#{response['aggregations']['average']['value']}) was below warning threshold. #{kibana_info}" else - ok "Query average (#{response['aggregations'][config[:aggr_name]]['value']}) was ok" + ok "Query average (#{response['aggregations']['average']['value']}) was ok" end - elsif response['aggregations'][config[:aggr_name]]['value'] > config[:crit] - critical "Query average (#{response['aggregations'][config[:aggr_name]]['value']}) was above critical threshold. #{kibana_info}" - elsif response['aggregations'][config[:aggr_name]]['value'] > config[:warn] - warning "Query average (#{response['aggregations'][config[:aggr_name]]['value']}) was above warning threshold. #{kibana_info}" + elsif response['aggregations']['average']['value'] > config[:crit] + critical "Query average (#{response['aggregations']['average']['value']}) was above critical threshold. #{kibana_info}" + elsif response['aggregations']['average']['value'] > config[:warn] + warning "Query average (#{response['aggregations']['average']['value']}) was above warning threshold. #{kibana_info}" else - ok "Query average (#{response['aggregations'][config[:aggr_name]]['value']}) was ok" + ok "Query average (#{response['aggregations']['average']['value']}) was ok" end rescue Elasticsearch::Transport::Transport::Errors::NotFound if config[:invert] - if response['aggregations'][config[:aggr_name]]['value'] < config[:crit] - critical "Query average (#{response['aggregations'][config[:aggr_name]]['value']}) was below critical threshold. #{kibana_info}" - elsif response['aggregations'][config[:aggr_name]]['value'] < config[:warn] - warning "Query average (#{response['aggregations'][config[:aggr_name]]['value']}) was below warning threshold. #{kibana_info}" + if response['aggregations']['average']['value'] < config[:crit] + critical "Query average (#{response['aggregations']['average']['value']}) was below critical threshold. #{kibana_info}" + elsif response['aggregations']['average']['value'] < config[:warn] + warning "Query average (#{response['aggregations']['average']['value']}) was below warning threshold. #{kibana_info}" else - ok "Query average (#{response['aggregations'][config[:aggr_name]]['value']}) was ok" + ok "Query average (#{response['aggregations']['average']['value']}) was ok" end else ok 'No results found, average was below thresholds'