Skip to content

Commit

Permalink
Adds template option for advanced custom message formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
Octavian Larion authored and majormoses committed Nov 12, 2017
1 parent f7a3d7b commit e7c623e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
4 changes: 1 addition & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachangelog.com/)

## [Unreleased]

## [3.1.0] - 2017-11-07
### Added
- Added `-f, --format` parameter to be able to specify a custom message format
- Added optional `-t, --template` parameter to specify the location of a ERB template file
- Added possibility to specify custom timeout
- Some cleanup and minor refactorings

Expand Down
22 changes: 10 additions & 12 deletions bin/handler-opsgenie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
require 'net/https'
require 'uri'
require 'json'
require 'erb'

class Opsgenie < Sensu::Handler
attr_reader :json_config, :message_format
attr_reader :json_config, :message_template

OPSGENIE_URL = 'https://api.opsgenie.com/v1/json/alert'.freeze

Expand All @@ -20,10 +21,10 @@ class Opsgenie < Sensu::Handler
long: '--json <config-name>',
default: 'opsgenie'

option :message_format,
description: "Custom message format, eg: 'Custom: <<check_output>>:<<check_name>>:<<client_name>>'",
short: '-f <format>',
long: '--format <format>',
option :message_template,
description: 'Location of custom erb template for advanced message formatting',
short: '-t <file_path>',
long: '--template <file_path>',
default: nil

def handle
Expand All @@ -34,8 +35,8 @@ def handle
private

def init
@json_config = settings[config[:json_config]] || {}
@message_format = config[:message_format]
@json_config = settings[config[:json_config]] || {}
@message_template = config[:message_template]
# allow config to be changed by the check
json_config.merge!(@event['check']['opsgenie']) if @event['check']['opsgenie']
end
Expand All @@ -61,17 +62,14 @@ def process

def message
return @event['notification'] unless @event['notification'].nil?
return default_message if message_format.nil?
return default_message if message_template.nil? || !File.exist?(message_template)
custom_message
rescue StandardError
default_message
end

def custom_message
message_format.gsub(/\<{2}\w+\>{2}/) do |value|
keys = value[2...-2].split('_')
@event[keys[0]][keys[1]]
end
ERB.new(File.read(message_template)).result(binding)
end

def default_message
Expand Down

0 comments on commit e7c623e

Please sign in to comment.