lib/flapjack/gateways/pagerduty.rb in flapjack-1.1.0 vs lib/flapjack/gateways/pagerduty.rb in flapjack-1.2.0rc1

- old
+ new

@@ -2,12 +2,10 @@ require 'em-hiredis' require 'em-synchrony' require 'em-synchrony/em-http' -require 'oj' - require 'flapjack/data/entity_check' require 'flapjack/data/alert' require 'flapjack/redis_pool' require 'flapjack/utility' @@ -38,11 +36,11 @@ @should_quit = true redis_uri = @redis_config[:path] || "redis://#{@redis_config[:host] || '127.0.0.1'}:#{@redis_config[:port] || '6379'}/#{@redis_config[:db] || '0'}" shutdown_redis = EM::Hiredis.connect(redis_uri) - shutdown_redis.rpush(@config['queue'], Oj.dump('notification_type' => 'shutdown')) + shutdown_redis.rpush(@config['queue'], Flapjack.dump_json('notification_type' => 'shutdown')) end def start @logger.info("starting") while not test_pagerduty_connection and not @should_quit do @@ -65,11 +63,11 @@ @logger.debug("pagerduty gateway is going into blpop mode on #{queue}") events[queue] = @redis.blpop(queue, 0) event_json = events[queue][1] begin - event = Oj.load(event_json) + event = Flapjack.load_json(event_json) @logger.debug("pagerduty notification event received: " + event.inspect) if 'shutdown'.eql?(event['notification_type']) @logger.debug("@should_quit: #{@should_quit}") next @@ -108,14 +106,17 @@ 'resolve' when 'test' 'trigger' end + # Setting the HOSTNAME and the SERVICE makes them visible in the Pagerduty UI pagerduty_event = { 'service_key' => alert.address, 'incident_key' => alert.event_id, 'event_type' => pagerduty_type, - 'description' => message } + 'description' => message, + 'details' => {'HOSTNAME' => alert.entity, + 'SERVICE' => alert.check}} send_pagerduty_event(pagerduty_event) alert.record_send_success! rescue => e @logger.error "Error generating or dispatching pagerduty message: #{e.class}: #{e.message}\n" + @@ -161,13 +162,13 @@ @logger.error "Error: test_pagerduty_connection: API returned #{code.to_s} #{results.inspect}" false end def send_pagerduty_event(event) - options = { :body => Oj.dump(event) } + options = { :body => Flapjack.dump_json(event) } http = EM::HttpRequest.new(PAGERDUTY_EVENTS_API_URL).post(options) - response = Oj.load(http.response) + response = Flapjack.load_json(http.response) status = http.response_header.status @logger.debug "send_pagerduty_event got a return code of #{status.to_s} - #{response.inspect}" unless status == 200 raise "Error sending event to pagerduty: status: #{status.to_s} - #{response.inspect}" + " posted data: #{options[:body]}" @@ -258,10 +259,10 @@ @logger.debug("pagerduty_acknowledged?: query: #{query.inspect}") @logger.debug("pagerduty_acknowledged?: auth: #{options[:head].inspect}") http = EM::HttpRequest.new(url).get(options) begin - response = Oj.load(http.response) + response = Flapjack.load_json(http.response) rescue Oj::Error @logger.error("failed to parse json from a post to #{url} ... response headers and body follows...") return nil end status = http.response_header.status