sensu-plugins / sensu-plugins-mailer

This plugin is an email handler for Sensu.
http://sensu-plugins.io
MIT License
17 stars 37 forks source link

Mailer breaks when you specify an ERB template #63

Closed techietubby closed 6 years ago

techietubby commented 6 years ago

My email template was working until recently when I upgraded my plugins to the latest versions:

mail (2.7.0, 2.6.3) mailgun-ruby (1.1.8, 1.0.3) sensu-plugins-mailer (2.0.0)

Now if I try to specify an ERB file I see these errors in the log and get no mail:

cat /etc/sensu/conf.d/handlers/mailer.json | /opt/sensu/embedded/bin/handler-mailer.rb warning: event filtering in sensu-plugin is deprecated, see http://bit.ly/sensu-plugin /opt/sensu/embedded/lib/ruby/gems/2.4.0/gems/sensu-plugin-1.4.5/lib/sensu-handler.rb:202:in +': no implicit conversion of nil into String (TypeError) from /opt/sensu/embedded/lib/ruby/gems/2.4.0/gems/sensu-plugin-1.4.5/lib/sensu-handler.rb:202:infilter_silenced' from /opt/sensu/embedded/lib/ruby/gems/2.4.0/gems/sensu-plugin-1.4.5/lib/sensu-handler.rb:36:in filter' from /opt/sensu/embedded/lib/ruby/gems/2.4.0/gems/sensu-plugin-1.4.5/lib/sensu-handler.rb:98:inblock in '

majormoses commented 6 years ago

Does it still break if you accept the default template or is this specific to specifying a template?

majormoses commented 6 years ago

looking at the code it would suggest that your event does not have a client name: https://github.com/sensu-plugins/sensu-plugin/blob/v1.4.5/lib/sensu-handler.rb#L202 and therefore you can not add a string with nil.

majormoses commented 6 years ago

cat /etc/sensu/conf.d/handlers/mailer.json | /opt/sensu/embedded/bin/handler-mailer.rb

This is not really a valid test, you need to cat an example event and then pipe that to it. You can specify via an ENV var the config dirs like this SENSU_CONFIG_FILES=$PATH_TO_SENU_DIR cat $PATH_TO_EVENT | $PATH_TO_HANDLER

majormoses commented 6 years ago

I'd need to see more about your redacted handler config, template, previous version installed to further troubleshoot.

techietubby commented 6 years ago

I only did the quick test to try to see if Ruby would throw-out an error message that would give me a clue as to what was broken/missing. I did try a really basic template which consisted of the first line of the standard ERB file just to see if that made a different. I have also specifying the template using "-t" and "template" : "/etc/sensu/conf.d/mail-template.erb".

techietubby commented 6 years ago
$ cat /etc/sensu/conf.d/handlers/mailer.json
{
  "mailer": {
      "admin_gui"   : "https://admin.test.com",
      "mail_from"   : "sensu@test.com",
      "mail_to"     : "OS_Linux@test.com",
      "smtp_address": "localhost",
      "smtp_port"   : "25",
      "smtp_domain" : "localhost",
      "template"    : "/etc/sensu/conf.d/mail-template.erb"
  },

  "handlers": {
     "mailer": {
        "type": "pipe",
        "command": "/opt/sensu/embedded/bin/handler-mailer.rb",
        "handle_flapping": true,
        "severities": [ "warning", "critical", "unknown" ]
      }
   }
}
majormoses commented 6 years ago

That config looks legit doing a quick scan.

Can you show me the log indicating where it is failing? As in are you getting the exact same error in the log that you got when running a handler with no event?

techietubby commented 6 years ago

ties":["warning","critical","unknown"],"name":"mailer"},"event":{"id":"920932af-5372-443e-a888-09fa8cf69a15"},"output":["(erubis:14:in build_body': undefined method[]' for nil:NilClass (NoMethodError)\n\tfrom /opt/sensu/embedded/lib/ruby/gems/2.4.0/gems/erubis-2.7.0/lib/erubis/evaluator.rb:65:in eval'\n\tfrom /opt/sensu/embedded/lib/ruby/gems/2.4.0/gems/erubis-2.7.0/lib/erubis/evaluator.rb:65:inresult'\n\tfrom /opt/sensu/embedded/lib/ruby/gems/2.4.0/gems/sensu-plugins-mailer-2.0.0/bin/handler-mailer.rb:204:in build_body'\n\tfrom /opt/sensu/embedded/lib/ruby/gems/2.4.0/gems/sensu-plugins-mailer-2.0.0/bin/handler-mailer.rb:208:inhandle'\n\tfrom /opt/sensu/embedded/lib/ruby/gems/2.4.0/gems/sensu-plugin-1.4.5/lib/sensu-handler.rb:99:in block in <class:Handler>'\nwarning: event filtering in sensu-plugin is deprecated, see http://bit.ly/sensu-plugin\nwarning: occurrence filtering in sensu-plugin is deprecated, see http://bit.ly/sensu-plugin\n"]} {"timestamp":"2017-12-14T09:45:05.593877+0100","level":"error","message":"handler output","handler":{"type":"pipe","command":"/opt/sensu/embedded/bin/ruby /opt/sensu/embedded/bin/handler-mailer.rb -t /etc/sensu/conf.d/mail-template.erb","filters":["postqueue","state_change_only","handle_repeats"],"handle_flapping":true,"severities":["warning","critical","unknown"],"name":"mailer"},"event":{"id":"45dd067f-8c34-44c9-af0b-ce6dfe80dfb3"},"output":["(erubis:14:inbuild_body': undefined method []' for nil:NilClass (NoMethodError)\n\tfrom /opt/sensu/embedded/lib/ruby/gems/2.4.0/gems/erubis-2.7.0/lib/erubis/evaluator.rb:65:ineval'\n\tfrom /opt/sensu/embedded/lib/ruby/gems/2.4.0/gems/erubis-2.7.0/lib/erubis/evaluator.rb:65:in result'\n\tfrom /opt/sensu/embedded/lib/ruby/gems/2.4.0/gems/sensu-plugins-mailer-2.0.0/bin/handler-mailer.rb:204:inbuild_body'\n\tfrom /opt/sensu/embedded/lib/ruby/gems/2.4.0/gems/sensu-plugins-mailer-2.0.0/bin/handler-mailer.rb:208:in handle'\n\tfrom /opt/sensu/embedded/lib/ruby/gems/2.4.0/gems/sensu-plugin-1.4.5/lib/sensu-handler.rb:99:inblock in '\nwarning: event filtering in sensu-plugin is deprecated, see http://bit.ly/sensu-plugin\nwarning: occurrence filtering in sensu-plugin is deprecated, see http://bit.ly/sensu-plugin\n"]}

This is a snippet from sensu-server.log

absolutejam commented 6 years ago

Is it possible that your ERB template is bad? Perhaps some fields you've defined are missing in the event, or a syntax error?

I've just tried it with the following template, which I know will succeed:

Client name: <%= @event['client']['name'] %>

and had no issues getting that email.

My mailer config:

{
  "handlers": {
    "mailer_itteam": {
      "command": "/opt/sensu/embedded/bin/handler-mailer.rb -j mailer_itteam",
      "filters": [
        "workhours_itteam",
        "change_only",
        "occurrences"
      ],
      "type": "pipe"
    }
  },
  "mailer_itteam": {
    "admin_gui": "http://uchiwa.redacted",
    "mail_from": "Monitoring@redacted",
    "mail_to": [
      "ServerEventNotifications@redacted"
    ],
    "smtp_address": "exchange.redacted",
    "smtp_domain": "redacted",
    "smtp_port": 25,
    "template": "/etc/sensu/handlers/mail-template.erb"
  }
}

And my gem versions:

mail (2.6.3)
mailgun-ruby (1.0.3)
...
sensu (1.2.0)
sensu-extension (1.5.1)
sensu-extensions (1.9.0)
sensu-extensions-check-dependencies (1.0.1)
sensu-extensions-debug (1.0.0)
sensu-extensions-influxdb2 (0.0.2)
sensu-extensions-json (1.0.0)
sensu-extensions-occurrences (1.2.0)
sensu-extensions-only-check-output (1.0.0)
sensu-extensions-ruby-hash (1.0.0)
sensu-extensions-snmp-trap (0.0.33)
sensu-json (2.1.0)
sensu-logger (1.2.1)
sensu-plugin (2.0.1, 1.4.5)
sensu-plugins-mailer (2.0.0)
sensu-plugins-pushover (1.0.0)
sensu-redis (2.2.0)
sensu-settings (10.12.0)
sensu-spawn (2.2.1)

Any chance you can share your ERB template?

majormoses commented 6 years ago

Yup this is definitely an issue with your ERB template:

"output":["(erubis:14:inbuild_body': undefined method []' for nil:NilClass (NoMethodError)\n\tfrom /opt/sensu/embedded/lib/ruby/gems/2.4.0/gems/erubis-2.7.0/lib/erubis/evaluator.rb:65

In the future please include the error messages in issues so we don't lose troubleshooting time running off in a different direction?

I need to see your ERB template to go any further.

techietubby commented 6 years ago

Hi, I now have the configuration working. I can only think that I have accidentally edited the ERB template and not noticed the difference as it was all working until I had to rebuild my Ruby GEMS. thanks for all your help. Andrew