bin/handler-opsgenie.rb in sensu-plugins-opsgenie-0.0.2 vs bin/handler-opsgenie.rb in sensu-plugins-opsgenie-0.0.3

- old
+ new

@@ -16,10 +16,14 @@ long: '--json JSONCONFIG', default: 'opsgenie.json' def handle @json_config = JSON.parse(File.open(config[:json_config]).read) + # allow config to be changed by the check + if @event['check']['opsgenie'] + @json_config['opsgenie'].merge!(@event['check']['opsgenie']) + end description = @event['notification'] || [@event['client']['name'], @event['check']['name'], @event['check']['output'].chomp].join(' : ') begin timeout(30) do response = case @event['action'] @@ -49,26 +53,35 @@ def close_alert post_to_opsgenie(:close, alias: event_id) end + def event_tags + @event['client']['tags'] + end + def create_alert(description) tags = [] tags << @json_config['opsgenie']['tags'] if @json_config['opsgenie']['tags'] tags << 'OverwriteQuietHours' if event_status == 2 && @json_config['opsgenie']['overwrite_quiet_hours'] == true tags << 'unknown' if event_status >= 3 tags << 'critical' if event_status == 2 tags << 'warning' if event_status == 1 + unless event_tags.nil? + event_tags.each { |tag, value| tags << "#{tag}_#{value}" } + end - post_to_opsgenie(:create, alias: event_id, message: description, tags: tags.join(',')) + recipients = @json_config['opsgenie']['recipients'] if @json_config['opsgenie']['recipients'] + teams = @json_config['opsgenie']['teams'] if @json_config['opsgenie']['teams'] + + post_to_opsgenie(:create, alias: event_id, message: description, tags: tags.join(','), recipients: recipients, teams: teams) end def post_to_opsgenie(action = :create, params = {}) params['customerKey'] = @json_config['opsgenie']['customerKey'] - params['recipients'] = @json_config['opsgenie']['recipients'] # override source if specified, default is ip - params['source'] = @json_config['source'] if @json_config['source'] + params['source'] = @json_config['opsgenie']['source'] if @json_config['opsgenie']['source'] uripath = (action == :create) ? '' : 'close' uri = URI.parse("https://api.opsgenie.com/v1/json/alert/#{uripath}") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true