Skip to content

Instantly share code, notes, and snippets.

@sarguru
Created November 26, 2012 14:55
Show Gist options
  • Save sarguru/4148616 to your computer and use it in GitHub Desktop.
Save sarguru/4148616 to your computer and use it in GitHub Desktop.
mailer
"mailer": {
"type": "pipe",
"command": "/etc/sensu/handlers/mailer.rb"
}
#path /etc/sensu/conf.d/mailer.json
{
"mailer": {
"mail_from": "[email protected]",
"mail_to": "[email protected]",
"smtp_address": "192.168.1.98",
"smtp_port": "25",
"smtp_domain": "deeproot.in"
}
}
#!/usr/bin/env ruby
#path: /etc/sensu/handlers/mailer.rb
# Sensu Handler: mailer
#
# This handler formats alerts as mails and sends them off to a pre-defined recipient.
#
# Copyright 2012 Pål-Kristian Hamre (https://github.com/pkhamre | http://twitter.com/pkhamre)
#
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.
require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'sensu-handler'
require 'mail'
require 'timeout'
class Mailer < Sensu::Handler
def short_name
@event['client']['name'] + '/' + @event['check']['name']
end
def action_to_string
@event['action'].eql?('resolve') ? "RESOLVED" : "ALERT"
end
def handle
smtp_address = settings['mailer']['smtp_address'] || 'localhost'
smtp_port = settings['mailer']['smtp_port'] || '25'
smtp_domain = settings['mailer']['smtp_domain'] || 'localhost.localdomain'
params = {
:mail_to => settings['mailer']['mail_to'],
:mail_from => settings['mailer']['mail_from'],
:smtp_addr => smtp_address,
:smtp_port => smtp_port,
:smtp_domain => smtp_domain
}
body = "#{@event['check']['output']}"
subject = "#{action_to_string} - #{short_name}: #{@event['check']['notification']}"
Mail.defaults do
delivery_method :smtp, {
:address => params[:smtp_addr],
:port => params[:smtp_port],
:domain => params[:smtp_domain],
:openssl_verify_mode => 'none'
}
end
begin
timeout 10 do
Mail.deliver do
to params[:mail_to]
from params[:mail_from]
subject subject
body body
end
puts 'mail -- sent alert for ' + short_name + ' to ' + params[:mail_to]
end
rescue Timeout::Error
puts 'mail -- timed out while attempting to ' + @event['action'] + ' an incident -- ' + short_name
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment