sensu-plugins/sensu-plugins-dashing

View on GitHub
bin/handler-dashing.rb

Summary

Maintainability
A
0 mins
Test Coverage
#!/usr/bin/env ruby
#
# Sensu Dashing(http://shopify.github.io/dashing/) dashboard notifier
#
# This handler sends event information to the dashing api.
#
# The handler pushes event output to widgets named following a convention of:
#  clientname-checkname
#
# For example, if you have a client named mysqlserver and a check named check_diskusage,
# it will send events to a widget with the data-id = mysqlserver-check_diskusage
#
# The event output will be pushed to the moreinfo data-bind of a widget.
#
# This works with a text dashing widget and any others that have a moreinfo data-binding.
#
# Two settings are required in dashing.json
#   auth_token  :  The shared token from you dashing instance.
#   host        :  The ip and port of the dashing instance.
#
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details

require 'sensu-handler'
require 'net/http'

#
# Dashign Notifier
#
class DashingNotifier < Sensu::Handler
  def widget
    @event['client']['name'] + '-' + @event['check']['name']
  end

  def handle
    token = settings['dashing']['auth_token']
    data = @event['check']['output']
    payload = { 'auth_token' => token.to_s, 'moreinfo' => data.to_s }.to_json

    uri = URI.parse(settings['dashing']['host'])
    http = Net::HTTP.new(uri.host, uri.port)
    http.post("/widgets/#{widget}", payload)
  rescue => e
    puts "Exception occured in DashingNotifier: #{e.message}", e.backtrace
  end
end