Skip to content

Commit

Permalink
Support setting the OpsGenie alert priority on a per-check basis. (#27)
Browse files Browse the repository at this point in the history
* Support setting the OpsGenie alert priority on a per-check basis.

* Attempting to appease rubocop.

* Move the check for configuration existence into the method, so that the code
reads more consistently with other similar use cases.

* Excellent review suggestion!

* Missing `end`.

* IMHO, Rubocop's defaults a little too strict.
  • Loading branch information
Castaglia authored and majormoses committed Mar 22, 2018
1 parent b7a4bdf commit 27e12f5
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

BlockLength:
Max: 30

MethodLength:
Max: 200

Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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._:
```
{
Expand Down Expand Up @@ -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.
21 changes: 20 additions & 1 deletion bin/handler-opsgenie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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']
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/create-alert-with-priority.json
Original file line number Diff line number Diff line change
@@ -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"}
1 change: 1 addition & 0 deletions test/fixtures/resolve-alert-with-priority.json
Original file line number Diff line number Diff line change
@@ -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"}
12 changes: 12 additions & 0 deletions test/integration/helpers/serverspec/handler-shared_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

0 comments on commit 27e12f5

Please sign in to comment.