tl;dr:
Handlers have no idea what sensu handler “name” they were called as. They can’t lookup their settings based on how they were called. Can’t have 2 of the same handler command with different settings with most community plugins, nor is there a way to have a setting change based on which handler name was called. Should we add a handler hash to the event output?
(All code below main body, for readability.)
Let’s assume I have a memory check( #1) :
I’d like to notify Ops (mailer, #2) if there’s any systems !OK state, and Eng (mail_eng, #3) if there’s any critical. No problem - just use a different handler, w/ different to:’s.
However, if you want to use most of the community plugins, you can’t actually use 2 different sets of config settings, because they hard code the setting key they use to typically their name.
From https://github.com/sensu/sensu-community-plugins/blob/master/handlers/notification/mailer.rb#L41 :
mail_to = settings[‘mailer’]['mail_to’]
If you don’t define the handler name as mailer.rb, it won’t use the settings.
This is an issue for pretty much every community handler plugin, as far as I can see.
Sensu doesn’t emit the name of the handler being called as part of the json event output to pipe commands.
I’d like to propose that we add a handler section to an event, as it’s emitted.
( Probably best in: https://github.com/sensu/sensu/blob/master/lib/sensu/server.rb#L296 )
We could put the handler settings there, so that handlers could be simplified, and not have to look up their values from the settings
I’ve heard suggestions to use a custom field on the check, but that still doesn’t let me change which group gets alerted per severity.
Open to other ideas as to how to deal w/ this, obviously… but this just seems like a nice programatic way to deal w/ these kinds of issues.
I’ve been able to hack around this in a fashion for right now by using a (sym)link for the mailer.rb file to mail_eng.rb, and changing the line above to be :
basename=File.basename($0,File.extname($0))
mail_to = settings[basename][‘mail_to’]
Which sets basename to be name of the script called (mail_eng, in this case.)
#1 : Check Def:
{
“checks”: {
“check_mem”: {
“command”: “/etc/sensu/plugins/check_mem.sh -w 90 -c 95”,
“occurrences”: 2,
“handlers”: [
“mailer",
“mail_eng”
],
“standalone”: false,
“subscribers”: [
“allhosts"
],
“interval”: 60
}
}
}
#2: mailer Handler:
{
“mailer": {
“mail_to”: “mbarr@example.com”,
“mail_from”: “sensu@example.com”,
},
“handlers”: {
“mailer”: {
“severities”: [
“warning”,
“critical”,
“unknown”
],
“command”: “/etc/sensu/handlers/mailer.rb”,
“type”: “pipe”
}
}
}
#3: mail_eng Handler:
{
“mail_eng”: {
“mail_to”: “eng-alerts@example.com”,
“mail_from”: “sensu@example.com”,
},
“handlers”: {
“mail_eng”: {
“severities”: [
“critical”
],
“type”: “pipe”,
“command”: “/etc/sensu/handlers/mail_eng.rb”
}
}
}