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 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..7984026 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].freeze + DEFAULT_PRIORITY = 'P3'.freeze option :json_config, description: 'Configuration name', @@ -129,6 +131,19 @@ def create_alert teams: json_config['teams']) end + def event_priority + return DEFAULT_PRIORITY unless json_config['priority'] + priority = json_config['priority'] + + canonical_priority = priority.upcase + unless 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 +156,13 @@ 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. + 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 # 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..0d7993c 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. @@ -43,4 +46,13 @@ 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 + 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