HI I can’t get either ponymailer nor mailer to send an email out. I have sendmail installed and was able to send an email with mail -s. Below is what I have confgured for ponymailer. Any guideline is welcome.
Thanks,
Robert
/opt/sensu/embedded/bin/gem list
*** LOCAL GEMS ***
amq-protocol (1.9.2)
amqp (1.5.0)
async_sinatra (1.2.0)
bigdecimal (1.2.0)
childprocess (0.5.3)
daemons (1.2.2)
em-redis-unified (1.0.0)
em-worker (0.0.2)
eventmachine (1.0.3)
ffi (1.9.8)
io-console (0.4.2)
ipaddress (0.8.0)
json (1.7.7)
mail (2.5.4)
mime-types (2.6.1, 1.25.1)
minitest (4.3.2)
mixlib-cli (1.5.0)
mixlib-config (2.2.1)
mixlib-log (1.6.0)
mixlib-shellout (2.1.0)
multi_json (1.11.0)
ohai (6.16.0)
polyglot (0.3.5)
pony (1.11)
psych (2.0.0)
rack (1.6.1)
rack-protection (1.5.3)
rake (0.9.6)
rdoc (4.0.0)
sensu (0.19.2)
sensu-em (2.5.2)
sensu-extension (1.1.2)
sensu-extensions (1.2.0)
sensu-logger (1.0.0)
sensu-plugin (1.1.0)
sensu-settings (1.9.0)
sensu-spawn (1.2.0)
sensu-transport (3.0.0)
sinatra (1.4.6)
systemu (2.6.5, 2.5.2)
test-unit (2.0.0.0)
thin (1.6.3)
tilt (2.0.1)
treetop (1.4.15)
uuidtools (2.1.5)
yajl-ruby (1.2.1)
cat /etc/sensu/handlers/ponymailer.rb
#!/usr/bin/env ruby
···
Sensu Handler: ponymailer
This handler formats alerts as mails and sends them off to a pre-defined recipient.
Copyright 2012 Panagiotis Papadomitsos pj@ezgr.net
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 ‘timeout’
require ‘pony’
require ‘date’
class PonyMailer < Sensu::Handler
STATUSES = {
0 => ‘OK’,
1 => ‘WARNING’,
2 => ‘CRITICAL’
}
def short_name
@event[‘client’][‘name’] + ‘/’ + @event[‘check’][‘name’]
end
def action_to_string
@event[‘action’].eql?(‘resolve’) ? ‘RESOLVED’ : ‘ALERT’
end
def handle
if settings[‘ponymailer’][‘only_send_on_change’]
last_status = @event[‘check’][‘history’][-2].to_i
if last_status == @event[‘check’][‘status’]
return
end
end
mail_options = {
subject: "Sensu Monitoring Alert: #{action_to_string} :: #{short_name}",
from: "#{settings['ponymailer']['fromname']} <#{settings['ponymailer']['from']}>",
via: :smtp,
via_options: {
address: settings['ponymailer']['hostname'],
port: settings['ponymailer']['port'],
enable_starttls_auto: settings['ponymailer']['tls']
},
charset: 'utf-8',
sender: settings['ponymailer']['from']
}
mail_options.merge!(via_options: {
address: settings['ponymailer']['hostname'],
port: settings['ponymailer']['port'],
enable_starttls_auto: settings['ponymailer']['tls'],
user_name: settings['ponymailer']['username'],
password: settings['ponymailer']['password'],
authentication: :plain
}) if settings['ponymailer']['authenticate']
mail_options[:body] = %(Sensu has detected a failed check. Event analysis follows:
Event Timestamp: #{Time.at(@event[‘check’][‘issued’].to_i)}
Check That Failed: #{@event[‘check’][‘name’]}
Check Command: #{@event[‘check’][‘command’]}
Check Flapping: #{@event[‘check’][‘flapping’]}
Check Occurrences: #{@event[‘occurrences’]}
Check History: #{@event[‘check’][‘history’].map { |h| STATUSES[h.to_i] }.join(’ => ‘)}
Node Name: #{@event[‘client’][‘name’]}
Node IP Address: #{@event[‘client’][‘address’]}
Node LPOL: #{Time.at(@event[‘client’][‘timestamp’].to_i)}
Node Subscriptions: #{@event[‘client’][‘subscriptions’].join(’, ')}
=== Check Output ===
#{@event[‘check’][‘output’]}
)
Pony.options = mail_options
# #YELLOW
unless settings['ponymailer']['recipients'].empty? # rubocop:disable GuardClause
settings['ponymailer']['recipients'].each do |to|
begin
Timeout.timeout 10 do
Pony.mail(to: to)
puts 'mail -- sent alert for ' + short_name + ' to ' + to
end
rescue Timeout::Error
puts 'mail -- timed out while attempting to ' + @event['action'] + ' an incident -- ' + short_name
end
end
end
end
end
/etc/sensu/conf.d/ponymailer.json
{
“ponymailer”: {
“authenticate”:true,
“only_send_on_change”:false,
“username”:"xxx@gmail.com",
“tls”:true,
“port”:“587”,
“fromname”:“Operations”,
“hostname”:“smtp.gmail.com”,
“password”:“xxxx”,
“from”:"ops@xxxx.net",
“recipients”:[
"xxxx@gmail.com"
]
}
}
/etc/sensu/conf.d/handler_ponymailer.json
{
“handlers”: {
“ponymailer”: {
“type”: “pipe”,
“command”: “/opt/sensu/embedded/bin/ruby /etc/sensu/handlers/ponymailer.rb”
}
}
}
{“timestamp”:“2015-06-20T04:57:00.233492+0000”,“level”:“debug”,“message”:“received result”,“result”:{“client”:“pdx01.infra.domain.net”,“check”:{“name”:“memory”,“issued”:1434776220,“command”:"/etc/sensu/plugins/check-mem.sh -w 303800 -c 303800",“handlers”:[“ponymailer”],“interval”:10,“subscribers”:[“common”],“executed”:1434776220,“duration”:0.006,“output”:“MEM CRITICAL - free system memory: 672 MB\n”,“status”:2}}}
{“timestamp”:“2015-06-20T04:57:00.233610+0000”,“level”:“debug”,“message”:“processing result”,“result”:{“client”:“pdx01.infra.domain.net”,“check”:{“name”:“memory”,“issued”:1434776220,“command”:"/etc/sensu/plugins/check-mem.sh -w 303800 -c 303800",“handlers”:[“ponymailer”],“interval”:10,“subscribers”:[“common”],“executed”:1434776220,“duration”:0.006,“output”:“MEM CRITICAL - free system memory: 672 MB\n”,“status”:2}}}
{“timestamp”:“2015-06-20T04:57:00.234173+0000”,“level”:“debug”,“message”:“storing check result”,“check”:{“handlers”:[“ponymailer”],“command”:"/etc/sensu/plugins/check-mem.sh -w 303800 -c 303800",“interval”:10,“subscribers”:[“common”],“name”:“memory”,“issued”:1434776220,“executed”:1434776220,“duration”:0.006,“output”:“MEM CRITICAL - free system memory: 672 MB\n”,“status”:2}}
{“timestamp”:“2015-06-20T04:57:00.235571+0000”,“level”:“info”,“message”:“processing event”,“event”:{“id”:“b67033bb-9a78-4fd6-a98b-3a525f7bf552”,“client”:{“name”:“pdx01.infra.domain.net”,“address”:“localhost”,“subscriptions”:[“common”],“version”:“0.19.2”,“timestamp”:1434776206},“check”:{“handlers”:[“ponymailer”],“command”:"/etc/sensu/plugins/check-mem.sh -w 303800 -c 303800",“interval”:10,“subscribers”:[“common”],“name”:“memory”,“issued”:1434776220,“executed”:1434776220,“duration”:0.006,“output”:“MEM CRITICAL - free system memory: 672 MB\n”,“status”:2,“history”:[“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”],“total_state_change”:0},“occurrences”:723,“action”:“create”}}
{“timestamp”:“2015-06-20T04:57:00.235927+0000”,“level”:“debug”,“message”:“handling event”,“event_data”:"{“id”:“b67033bb-9a78-4fd6-a98b-3a525f7bf552”,“client”:{“name”:“pdx01.infra.domain.net”,“address”:“localhost”,“subscriptions”:[“common”],“version”:“0.19.2”,“timestamp”:1434776206},“check”:{“handlers”:[“ponymailer”],“command”:"/etc/sensu/plugins/check-mem.sh -w 303800 -c 303800",“interval”:10,“subscribers”:[“common”],“name”:“memory”,“issued”:1434776220,“executed”:1434776220,“duration”:0.006,“output”:“MEM CRITICAL - free system memory: 672 MB\n”,“status”:2,“history”:[“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”,“2”],“total_state_change”:0},“occurrences”:723,“action”:“create”}",“handler”:{“type”:“pipe”,“command”:"/opt/sensu/embedded/bin/ruby /etc/sensu/handlers/ponymailer.rb",“name”:“ponymailer”}}
{“timestamp”:“2015-06-20T04:57:00.805423+0000”,“level”:“info”,“message”:“handler output”,“handler”:{“type”:“pipe”,“command”:"/opt/sensu/embedded/bin/ruby /etc/sensu/handlers/ponymailer.rb",“name”:“ponymailer”},“output”:[“only handling every 180 occurrences: pdx01.infra.domain.net/memory\n”]}