Sensu and Mailer Alert Handler

Below is a working mailer handler configuration for Sensu. The mailer settings go to “/etc/sensu/conf.d/mailer.json” and then update the handler file “/etc/sensu/conf.d/handlers.json”.

shanker@mon1:~$ cat /etc/sensu/conf.d/mailer.json
{
 "mailer": {
   "type": "pipe",
   "command": "handler-mailer.rb",
   "admin_gui": "https://XXX/",
   "mail_from": “alerts@XXXX",
   "mail_to": [
     “alerts@XXX" ],
   "subscriptions": {
     “group:XXXX_oncall": {
       "mail_to": “sde@XXX"
     },
     "admins": {
       "mail_to": “shanker.balan@XXXX"
     }
   },
   "smtp_address": "localhost",
   "smtp_port": "25",
   "smtp_domain": “XXX"
 }
}
shanker@mon1:~$ cat /etc/sensu/conf.d/handlers.json
{
 "handlers": {
   "default": {
     "type": "set",
     "handlers": [
       "logstash",
       "debug"
     ]
   },
   "mailer": {
     "type": "pipe",
     "command": "handler-mailer.rb",
     "filters": [
       "filter_warn_hourly"
     ],
     "severities": [
       "ok",
       "warning",
       "critical",
       "unknown"
     ]
   },
   "sms": {
     "type": "pipe",
     "command": "handler-sms.rb",
     "severities": [
       "critical"
     ]
   },
   "logstash": {
     "type": "pipe",
     "command": "handler-logstash.rb",
     "severities": [
       "ok",
       "warning",
       "unknown",
       "critical"
     ]
   }
 }
}

You can test by creating a dummy event and firing the handler manually.

Sample “event.json”

shanker@mon1:~$ cat event.json
{"timestamp":"2016-09-13T07:22:45.392029+0000","level":"info","message":"processing event","event":{"client":{"name":"spam2.aws.example.com","address":"unknown","subscriptions":[],"keepalives":false,"version":"0.25.3"},"check":{"standalone":true,"interval":180,"refresh":300,"source":"spam2.aws.example.com","command":"/usr/lib/nagios/plugins/check_snmp -H spam2.aws.example.com -C gujmail-public -o 1.3.6.1.4.1.20632.2.4 -w 50 -c 60 -l deferredQueueSize -t 10","occurrences":3,"handlers":["default","mailer"],"name":"deferredQueueSize-spam2.aws.example.com","issued":1473751365,"executed":1473751365,"duration":0.005,"output":"SNMP CRITICAL - deferredQueueSize *260* | deferredQueueSize=260 \n","status":2,"type":"standard","origin":"mon1.aws.example.com","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":1384,"action":"create","timestamp":1473751365,"id":"fafcc224-9776-43c9-aab1-1e522ffd1a9b","last_state_change":1473502425,"last_ok":null}}
shanker@mon1:~$ cat event.json | jq .event | /opt/sensu/embedded/bin/handler-mailer.rb warning: event filtering in sensu-plugin is deprecated, see http://bit.ly/sensu-plugin
warning: occurrence filtering in sensu-plugin is deprecated, see http://bit.ly/sensu-plugin
mail -- sent alert for spam2.aws.example.com/deferredQueueSize-spam2.aws.example.com to ["alerts@XXXX"]
shanker@mon1:~$

Shanker Balan

Shanker Balan is a devops and infrastructure freelancer with over 14 years of industry experience in large scale Internet systems. He is available for both short term and long term projects on contract. Please use the Contact Form for any enquiry.

More Posts - Website

Follow Me:
TwitterLinkedIn