From 7ec5a421cbd7bd3ea6d8ac20a5a0220d76431eaf Mon Sep 17 00:00:00 2001 From: TJ Saunders Date: Sun, 21 Jan 2018 08:45:21 -0800 Subject: [PATCH 1/6] Support setting the OpsGenie alert priority on a per-check basis. --- CHANGELOG.md | 2 ++ README.md | 26 +++++++++++++++++++ bin/handler-opsgenie.rb | 19 +++++++++++++- test/fixtures/create-alert-with-priority.json | 1 + .../fixtures/resolve-alert-with-priority.json | 1 + .../helpers/serverspec/handler-shared_spec.rb | 11 ++++++++ 6 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/create-alert-with-priority.json create mode 100644 test/fixtures/resolve-alert-with-priority.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 96a9de7..bf71afe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ 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] +### Added +- Added possibility to specify priority per check (@Castaglia) ## [4.2.0] - 2018-02-20 ### Changed diff --git a/README.md b/README.md index fa8e601..4e70a07 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,10 @@ assign the default priority of "P3" to the alert. ## Configuration Notes +### Per Check Attributes + +#### `alias` + If the check definition uses the custom `alias` attribute, _e.g._: ``` { @@ -182,3 +186,25 @@ We can define a custom `alias` attribute in this check: And with this, running on multiple clients, any alerts would be generated with the same event ID of `mysqldb`, by using that `alias` attribute as the event ID. + +#### `priority` + +By default, an OpsGenie alert is created with a default [priority](https://docs.opsgenie.com/docs/priority-settings) value of "P3". The priority for a specific +check can be explicitly set using the custom `priority` attribute, _e.g._: +``` +{ + "checks": { + "check_mysql_access": { + "opsgenie": { + "priority": "P1", + +``` +The list of valid values, per [OpsGenie alert docs](https://docs.opsgenie.com/docs/alert-api#section-create-alert), are: + +* P1 +* P2 +* P3 +* P4 +* P5 + +Any value other than these will be ignored. diff --git a/bin/handler-opsgenie.rb b/bin/handler-opsgenie.rb index 9927426..3c10c7b 100755 --- a/bin/handler-opsgenie.rb +++ b/bin/handler-opsgenie.rb @@ -14,6 +14,8 @@ class Opsgenie < Sensu::Handler attr_reader :json_config, :message_template, :verbose OPSGENIE_URL = 'https://api.opsgenie.com/v2/alerts'.freeze + PRIORITIES = %w[P1 P2 P3 P4 P5] + DEFAULT_PRIORITY = 'P3'.freeze option :json_config, description: 'Configuration name', @@ -129,6 +131,18 @@ def create_alert teams: json_config['teams']) end + def priority + priority = json_config['priority'] + + canonical_priority = priority.upcase + if !PRIORITIES.include? canonical_priority + puts "opsgenie -- ignoring unsupported priority '#{priority}'" + canonical_priority = DEFAULT_PRIORITY + end + + canonical_priority + end + def tags tags = [] tags += json_config['tags'] if json_config['tags'] @@ -141,9 +155,12 @@ def tags end def post_to_opsgenie(action = :create, params = {}) - # override source if specified, default is ip + # Override source if specified, default is ip params['source'] = json_config['source'] if json_config['source'] + # Override priority, if specified. + params['priority'] = priority if json_config['priority'] + encoded_alias = URI.escape(params[:alias]) # TODO: come back and asses if this logic is correct, I left it functionally # as it was originally written but I suspect we should have at least three diff --git a/test/fixtures/create-alert-with-priority.json b/test/fixtures/create-alert-with-priority.json new file mode 100644 index 0000000..60df18a --- /dev/null +++ b/test/fixtures/create-alert-with-priority.json @@ -0,0 +1 @@ +{"client":{"name":"test01","address":"127.0.0.1","subscriptions":["all"],"timestamp":1326390159},"check":{"name":"some.fake.check.name","issued":1326390169,"output":"CRITICAL: text\n","status":2,"notification":"check failed","command":"/path/to/some/stuff/here -A do_smoke","subscribers":["all"],"interval":60,"handlers":["default","opsgenie"],"history":["0","0","2"],"flapping":false,"opsgenie":{"priority":"bad_priority_value"}},"occurrences":1,"action":"create"} diff --git a/test/fixtures/resolve-alert-with-priority.json b/test/fixtures/resolve-alert-with-priority.json new file mode 100644 index 0000000..52ebff7 --- /dev/null +++ b/test/fixtures/resolve-alert-with-priority.json @@ -0,0 +1 @@ +{"client":{"name":"test01","address":"127.0.0.1","subscriptions":["all"],"timestamp":1326390159},"check":{"name":"some.fake.check.name","issued":1326390169,"output":"CRITICAL: text\n","status":2,"notification":"check failed","command":"/path/to/some/stuff/here -A do_smoke","subscribers":["all"],"interval":60,"handlers":["default","opsgenie"],"history":["0","0","2"],"flapping":false,"opsgenie":{"priority":"bad_priority_value"}},"occurrences":1,"action":"resolve"} diff --git a/test/integration/helpers/serverspec/handler-shared_spec.rb b/test/integration/helpers/serverspec/handler-shared_spec.rb index 51fcd2f..ac24363 100644 --- a/test/integration/helpers/serverspec/handler-shared_spec.rb +++ b/test/integration/helpers/serverspec/handler-shared_spec.rb @@ -14,6 +14,9 @@ create_alert_with_description_file = '/tmp/kitchen/data/test/fixtures/create-alert-with-description.json' +create_alert_with_priority_file = '/tmp/kitchen/data/test/fixtures/create-alert-with-priority.json' +resolve_alert_with_priority_file = '/tmp/kitchen/data/test/fixtures/resolve-alert-with-priority.json' + # These tests would require a valid OpsGenie API key and heartbeat name # configured in order to succeed. Thus for now, we limit ourselves to the # expected failure cases. @@ -42,5 +45,13 @@ describe command("#{handler} -v < #{create_alert_with_description_file}") do its(:stdout) { should_not match(/Description:.*CRITICAL.*text/) } its(:stdout) { should match(/Description:.*#{custom_description_pattern}/) } + + custom_priority_pattern = 'bad_priority_value' + describe command("#{handler} < #{create_alert_with_priority_file}") do + its(:stdout) { should match(/ignoring unsupported priority.*#{custom_priority_pattern}/) } + end + + describe command("#{handler} < #{resolve_alert_with_priority_file}") do + its(:stdout) { should match(/ignoring unsupported priority.*#{custom_priority_pattern}/) } end end From 272a74193fede833cfc98448cec56b6628076c95 Mon Sep 17 00:00:00 2001 From: TJ Saunders Date: Sun, 21 Jan 2018 09:38:25 -0800 Subject: [PATCH 2/6] Attempting to appease rubocop. --- bin/handler-opsgenie.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/handler-opsgenie.rb b/bin/handler-opsgenie.rb index 3c10c7b..b74e6f3 100755 --- a/bin/handler-opsgenie.rb +++ b/bin/handler-opsgenie.rb @@ -14,7 +14,7 @@ class Opsgenie < Sensu::Handler attr_reader :json_config, :message_template, :verbose OPSGENIE_URL = 'https://api.opsgenie.com/v2/alerts'.freeze - PRIORITIES = %w[P1 P2 P3 P4 P5] + PRIORITIES = %w[P1 P2 P3 P4 P5].freeze DEFAULT_PRIORITY = 'P3'.freeze option :json_config, @@ -135,7 +135,7 @@ def priority priority = json_config['priority'] canonical_priority = priority.upcase - if !PRIORITIES.include? canonical_priority + unless PRIORITIES.include? canonical_priority puts "opsgenie -- ignoring unsupported priority '#{priority}'" canonical_priority = DEFAULT_PRIORITY end From 230e8e3d30525a3347655e8424c8c942daf2acbf Mon Sep 17 00:00:00 2001 From: TJ Saunders Date: Sun, 21 Jan 2018 10:23:28 -0800 Subject: [PATCH 3/6] Move the check for configuration existence into the method, so that the code reads more consistently with other similar use cases. --- bin/handler-opsgenie.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bin/handler-opsgenie.rb b/bin/handler-opsgenie.rb index b74e6f3..c20c0ae 100755 --- a/bin/handler-opsgenie.rb +++ b/bin/handler-opsgenie.rb @@ -131,7 +131,8 @@ def create_alert teams: json_config['teams']) end - def priority + def event_priority + return nil unless json_config['priority'] priority = json_config['priority'] canonical_priority = priority.upcase @@ -159,7 +160,8 @@ def post_to_opsgenie(action = :create, params = {}) params['source'] = json_config['source'] if json_config['source'] # Override priority, if specified. - params['priority'] = priority if json_config['priority'] + priority = event_priority + params['priority'] = priority if priority encoded_alias = URI.escape(params[:alias]) # TODO: come back and asses if this logic is correct, I left it functionally From de137596119909b7c9a3c1331bd04f5b31f5cfec Mon Sep 17 00:00:00 2001 From: TJ Saunders Date: Wed, 21 Mar 2018 20:28:56 -0700 Subject: [PATCH 4/6] Excellent review suggestion! --- bin/handler-opsgenie.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/handler-opsgenie.rb b/bin/handler-opsgenie.rb index c20c0ae..7984026 100755 --- a/bin/handler-opsgenie.rb +++ b/bin/handler-opsgenie.rb @@ -132,7 +132,7 @@ def create_alert end def event_priority - return nil unless json_config['priority'] + return DEFAULT_PRIORITY unless json_config['priority'] priority = json_config['priority'] canonical_priority = priority.upcase From 4e8c18ab9a97a136db477d25e2a7777d8b21cd87 Mon Sep 17 00:00:00 2001 From: TJ Saunders Date: Wed, 21 Mar 2018 20:35:47 -0700 Subject: [PATCH 5/6] Missing `end`. --- test/integration/helpers/serverspec/handler-shared_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/test/integration/helpers/serverspec/handler-shared_spec.rb b/test/integration/helpers/serverspec/handler-shared_spec.rb index ac24363..0d7993c 100644 --- a/test/integration/helpers/serverspec/handler-shared_spec.rb +++ b/test/integration/helpers/serverspec/handler-shared_spec.rb @@ -45,6 +45,7 @@ describe command("#{handler} -v < #{create_alert_with_description_file}") do its(:stdout) { should_not match(/Description:.*CRITICAL.*text/) } its(:stdout) { should match(/Description:.*#{custom_description_pattern}/) } + end custom_priority_pattern = 'bad_priority_value' describe command("#{handler} < #{create_alert_with_priority_file}") do From 94052ccd11e98f34482c268d6a00a4b6df052d8c Mon Sep 17 00:00:00 2001 From: TJ Saunders Date: Wed, 21 Mar 2018 20:40:45 -0700 Subject: [PATCH 6/6] IMHO, Rubocop's defaults a little too strict. --- .rubocop.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index 680bd41..e6ae882 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,4 +1,7 @@ +BlockLength: + Max: 30 + MethodLength: Max: 200