Created
November 11, 2014 22:03
-
-
Save mzupan/362bbead61cb5063ce3c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"mailer": { | |
"admin_gui": "http://monitor.domain.com/", | |
"mail_from": "[email protected]", | |
"mail_to": "[email protected]", | |
"smtp_domain": "domain.com" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/opt/sensu/embedded/bin/ruby | |
# | |
# Sensu Extension: mailer | |
# | |
# This mailer extension was pretty much copy/pasted from the following authors work | |
# It got moved to an extension so the sensu doesn't fork bomb itself if you have a lot | |
# of alerts | |
# | |
# Author (sort of) Mike Zupan (http://zcentric.com) @mikezupan | |
# Taken from Pal-Kristian Hamre (https://github.com/pkhamre | http://twitter.com/pkhamre) | |
# | |
# Released under the same terms as Sensu (the MIT license); see LICENSE | |
# for details. | |
gem 'mail', '~> 2.5.4' | |
require 'mail' | |
require 'timeout' | |
# patch to fix Exim delivery_method: https://github.com/mikel/mail/pull/546 | |
module ::Mail | |
class Exim < Sendmail | |
def self.call(path, arguments, destinations, encoded_message) | |
popen "#{path} #{arguments}" do |io| | |
io.puts encoded_message.to_lf | |
io.flush | |
end | |
end | |
end | |
end | |
module Sensu::Extension | |
class Mailer < Handler | |
def post_init | |
end | |
def definition | |
{ | |
type: 'extension', | |
name: 'mailer', | |
mutator: 'ruby_hash' | |
} | |
end | |
def name | |
definition[:name] | |
end | |
def description | |
'A Mailer handler that will not fork' | |
end | |
# convert the status to a string | |
def status_to_string | |
case @event['check']['status'] | |
when 0 | |
'OK' | |
when 1 | |
'WARNING' | |
when 2 | |
'CRITICAL' | |
else | |
'UNKNOWN' | |
end | |
end | |
def short_name | |
@event['client']['name'] + '/' + @event['check']['name'] | |
end | |
def action_to_string | |
@event['action'].eql?('resolve') ? "RESOLVED" : "ALERT" | |
end | |
def run(event) | |
# Is this event a resolution? | |
resolved = event[:action].eql?(:resolve) | |
if (resolved || [1, 2, 3].include?(event[:check][:status])) | |
check = event[:check] | |
admin_gui = @settings["mailer"]["gui"] || 'http://localhost:8080/' | |
mail_to = @settings["mailer"]["mail_to"] | |
mail_from = @settings["mailer"]["mail_from"] | |
playbook = "Playbook: #{@event['check']['playbook']}" if @event['check']['playbook'] | |
body = <<-BODY.gsub(/^\s+/, '') | |
#{@event['check']['output']} | |
Admin GUI: #{admin_gui}/#/client/Manage/#{@event['client']['name']} | |
Host: #{@event['client']['name']} | |
Timestamp: #{Time.at(@event['check']['issued'])} | |
Address: #{@event['client']['address']} | |
Check Name: #{@event['check']['name']} | |
Command: #{@event['check']['command']} | |
Status: #{status_to_string} | |
Occurrences: #{@event['occurrences']} | |
#{playbook} | |
BODY | |
subject = "#{action_to_string} - #{short_name}: #{status_to_string}" | |
begin | |
timeout 10 do | |
Mail.deliver do | |
to mail_to | |
from mail_from | |
subject subject | |
body body | |
end | |
yield('mail -- sent alert for ' + short_name + ' to ' + mail_to.to_s, 0) | |
end | |
rescue Timeout::Error | |
yield('mail -- timed out while attempting to ' + @event['action'] + ' an incident -- ' + short_name, 2) | |
end | |
else | |
yield("Mailer not handling", 2) | |
end | |
end | |
def stop | |
yield | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment