Sha256: d0b97d63696f890748f559ff81eb8b72f0bdafbd20d7779a32731c28c9de42bb

Contents?: true

Size: 1.9 KB

Versions: 3

Compression:

Stored size: 1.9 KB

Contents

#!/usr/bin/env ruby
#
# Sensu - Graphite Event Handler
#
# This handler takes events and POSTs them to a Graphite events URI.
#
# For configuration see: graphite_event.json
#
# See here for more details:
#
# * https://code.launchpad.net/~lucio.torre/graphite/add-events/+merge/69142
#
# Author: Rob Wilson <roobert@gmail.com>
#
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details

require 'sensu-handler'
require 'net/http'
require 'net/https'
require 'uri'
require 'json'

class GraphiteEvent < Sensu::Handler
  option :tags_as_array,
         description: 'send tags as array - for post graphite 1.0 compatibility.',
         short: '-t',
         long: '--tags-as-array',
         boolean: true,
         required: false,
         default: false

  def post_event(uri, body)
    uri          = URI.parse(uri)
    req          = Net::HTTP::Post.new(uri.path)
    sock         = Net::HTTP.new(uri.host, uri.port)
    sock.use_ssl = uri.scheme == 'https'
    req.body     = body

    req.basic_auth(uri.user, uri.password) if uri.user

    sock.start { |http| http.request(req) }
  end

  def event_status
    case @event['check']['status']
    when 0
      'ok'
    when 1
      'warning'
    when 2
      'critical'
    else
      'unknown'
    end
  end

  def handle
    tags = [
      'sensu',
      'event',
      event_status,
      @event['client']['name'],
      @event['check']['name']
    ]

    tags += settings['graphite_event']['tags'] if settings['graphite_event']['tags']

    body = {
      'what' => 'sensu_event',
      'tags' => config[:tags_as_array] ? [tags.join(',')] : tags.join(','),
      'data' => event_status,
      'when' => Time.now.to_i
    }

    uri = settings['graphite_event']['server_uri']

    begin
      post_event(uri, body.to_json)
    rescue => e
      bail "failed to send event to #{uri}: #{e}"
    end

    puts "sent event to Graphite: #{body.to_json}"
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
sensu-plugins-graphite-boutetnico-1.0.0 bin/handler-graphite-event.rb
sensu-plugins-graphite-3.1.1 bin/handler-graphite-event.rb
sensu-plugins-graphite-3.1.0 bin/handler-graphite-event.rb