bin/handler-opsgenie.rb in sensu-plugins-opsgenie-4.1.2 vs bin/handler-opsgenie.rb in sensu-plugins-opsgenie-4.2.0
- old
+ new
@@ -9,11 +9,11 @@
require 'uri'
require 'json'
require 'erb'
class Opsgenie < Sensu::Handler
- attr_reader :json_config, :message_template
+ attr_reader :json_config, :message_template, :verbose
OPSGENIE_URL = 'https://api.opsgenie.com/v2/alerts'.freeze
option :json_config,
description: 'Configuration name',
@@ -25,10 +25,16 @@
description: 'Location of custom erb template for advanced message formatting',
short: '-t <file_path>',
long: '--template <file_path>',
default: nil
+ option :verbose,
+ description: 'Enable verbose/debugging output',
+ short: '-v',
+ long: '--verbose',
+ default: false
+
def handle
init
process
end
@@ -64,10 +70,15 @@
end
rescue Timeout::Error
puts "opsgenie -- timed out while attempting to #{@event['action']} a incident -- #{event_id}"
end
+ def description
+ return json_config['description'] unless json_config['description'].nil?
+ @event['check']['output'].chomp
+ end
+
def message
return @event['notification'] unless @event['notification'].nil?
return default_message if message_template.nil? || !File.exist?(message_template)
custom_message
rescue StandardError
@@ -77,11 +88,11 @@
def custom_message
ERB.new(File.read(message_template)).result(binding)
end
def default_message
- [@event['client']['name'], @event['check']['name'], @event['check']['output'].chomp].join(' : ')
+ [@event['client']['name'], @event['check']['name']].join(' : ')
end
def event_id
return @event['check']['opsgenie']['alias'] unless @event['check']['opsgenie'].nil? || @event['check']['opsgenie']['alias'].nil?
@@ -109,11 +120,11 @@
def create_alert
post_to_opsgenie(:create,
alias: event_id,
message: message,
- description: json_config['description'],
+ description: description,
entity: client_name,
tags: tags,
recipients: json_config['recipients'],
teams: json_config['teams'])
end
@@ -143,9 +154,22 @@
''
else
"#{encoded_alias}/close?identifierType=alias"
end
uri = URI.parse("#{OPSGENIE_URL}/#{uripath}")
+
+ if config[:verbose]
+ # Note that the ordering of these lines roughly follows the order
+ # alert fields are displayed in the OpsGenie web UI.
+ puts "URL: #{uri}"
+ puts "Message: #{params[:message]}"
+ puts "Tags: #{params[:tags]}"
+ puts "Entity: #{params[:entity]}"
+ puts "Teams: #{params[:teams]}"
+ puts "Alias: #{params[:alias]}"
+ puts "Description: #{params[:description]}"
+ end
+
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.request_uri, 'Authorization' => "GenieKey #{json_config['customerKey']}", 'Content-Type' => 'application/json')
request.body = params.to_json