Git Product home page Git Product logo

sensu-email-handler's Introduction

Bonsai Asset Badge

Sensu Email Handler

Overview

The Sensu Go Email Handler is a Sensu Event Handler for sending incident notification emails.

Usage examples

Help output

The Sensu Email handler sends an email notifications

Usage:
  sensu-email-handler [flags]

Flags:
  -a, --authMethod string         The SMTP authentication method, one of 'none', 'plain', or 'login' (default "plain")
  -T, --bodyTemplateFile string   A template file to use for the body
  -l, --enableLoginAuth           [deprecated] Use "login auth" mechanisim
  -f, --fromEmail string          The 'from' email address
  -h, --help                      help for sensu-email-handler
  -H, --hookout                   Include output from check hook(s)
  -i, --insecure                  [deprecated] Use an insecure connection (unauthenticated on port 25)
  -s, --smtpHost string           The SMTP host to use to send to send email
  -p, --smtpPassword string       The SMTP password, if not in env SMTP_PASSWORD
  -P, --smtpPort uint             The SMTP server port (default 587)
  -u, --smtpUsername string       The SMTP username, if not in env SMTP_USERNAME
  -S, --subjectTemplate string    A template to use for the subject (default "Sensu Alert - {{.Entity.Name}}/{{.Check.Name}}: {{.Check.State}}")
  -k, --tlsSkipVerify             Do not verify TLS certificates
  -t, --toEmail string            The 'to' email address (accepts comma delimited and/or multiple flags)

Environment variables

Argument Environment Variable
--smtpUsername SMTP_USERNAME
--smtpPassword SMTP_PASSWORD

Security Note: Care should be taken to not expose the password for this handler by specifying it on the command line or by directly setting the environment variable in the handler definition. It is suggested to make use of secrets management to surface it as an environment variable. The handler definition above references it as a secret. Below is an example secrets definition that make use of the built-in env secrets provider.

---
type: Secret
api_version: secrets/v1
metadata:
  name: smtp_password
spec:
  provider: env
  id: SMTP_PASSWORD

Annotations

All of the above command line arguments can be overridden by check or entity annotations. The annotation consists of the key formed by appending the "long" argument specification to the string sensu.io/plugins/email/config (e.g. sensu.io/plugins/email/config/toEmail).

For example, having the following in an agent.yml file will create an entity annotation such that emails generated by events on this entity will go to [email protected] instead of the recipient defined in the handler. And the subject would be something to the effect of 'failing - webserver01/check-nginx'.

namespace: "default"
subscriptions:
  - linux
backend-url:
  - "ws://127.0.0.1:8081"
annotations:
  sensu.io/plugins/email/config/toEmail: "[email protected]"
  sensu.io/plugins/email/config/subjectTemplate: "{{.Check.State}} - {{.Entity.Name}}/{{.Check.Name}}",

Note: When using template expansion for annotations in checks, the token delimiters {{ and }} have to be escaped with {{` and `}}, respectively. Yes, this is ugly, but it is because checks are ran through token expansion before being ran. This is not needed for annotations in entities.

Example:

"sensu.io/plugins/email/config/subjectTemplate": "{{`{{ .Check.State }}`}} - {{`{{ .Entity.Name }}`}}/{{`{{ .Check.Name }}`}}",

Templates

The plugin provides an option to use a template file for the body of the email and is capable of using HTML for formatting the email. This template file would need to be available on all backends on which this handler may run. An example is provided below:

/etc/sensu/email_template

<html>
Greetings,

<h3>Informational Details</h3>
<b>Check</b>: {{ .Check.Name }}<br>
<b>Entity</b>: {{ .Entity.Name }}<br>
<b>State</b>: {{ .Check.State }}<br>
<b>Occurrences</b>: {{ .Check.Occurrences }}<br>
<b>Playbook</b>: https://example.com/monitoring/wiki/playbook
<h3>Check Output Details</h3>
<b>Check Output</b>:<br>{{range $element := StringLines .Check.Output}}{{$element}}<br>{{end}} 
<h4>Check Hook(s)</h4>
{{range .Check.Hooks}}<b>Hook Name</b>:  {{.Name}}<br>
<b>Hook Command</b>:  {{.Command}}<br>
<b>Hook Output</b>: {{.Output}}<br>
{{end}}<br>
<br>
<br>
#monitoringlove,<br>
<br>
Sensu<br>
</html>

Note that this uses tokens to populate the values provided by the event. More information on template syntax and format can be found in the documentation

Also note that line breaks in your template and any text surfaced by token substitution are replaced with the HTML <br> tag.

At the time of this example, check hooks and templates are not able to be used together via the -H and -T flags. However, you may include the hook output as part of the template via the following:

{{range .Check.Hooks}}
Hook Name:  {{.Name}}
Hook Command:  {{.Command}}
{{.Output}}

Formatting Timestamps in Templates

A Sensu Go event contains multiple timestamps (e.g. .Check.Issued, .Check.Executed, .Check.LastOk) that are presented in UNIX timestamp format. A function named UnixTime is provided to print these values in a customizable human readable format as part of a template. To customize the output format of the timestamp, use the same format as specified by Golang's Time.Format. Additional examples can be found here.

Note: the predefined format constants are not available.

The example below embellishes the prior example template to include two timestamps.

[...]
<h3>Check Output Details</h3>
<b>Executed</b>: {{(UnixTime .Check.Executed).Format "2 Jan 2006 15:04:05"}}<br>
<b>Last OK</b>: {{(UnixTime .Check.LastOK).Format "2 Jan 2006 15:04:05"}}<br>
<b>Check Output</b>: {{.Check.Output}}
[...]

Formatting the Event ID in Templates

Each Sensu Go event contains a unique event ID (UUID) that is presented to this handler in a non-printable format. To properly print this value as part of a template, the function UUIDFromBytes is provided. The example below shows its use:

[...]
<b>Event ID</b>: {{UUIDFromBytes .ID}}
<h3>Check Output Details</h3>
<b>Check Output</b>: {{.Check.Output}}

Additional formatting via sprig module

Additional functions have been added via the sprig module and examples can be found here.

This example splits a fully qualified entity name and provides the host. We also pull out the interval for the fatigue check if it exists. (index is a built-in golang template function) The div sprig function allows us to translate seconds into hours or minutes.

[...]
{{ $host := split "." .Entity.Name }}
Frequency: {{ div .Check.Interval 60 }} minutes
{{ if index .Check.Annotations "fatigue_check/interval" }}<b>Email Every</b>:  {{ div (index .Check.Annotations "fatigue_check/interval") 3600}} hour(s){{ end }}
<b>Check Output</b>:<br>{{range $element := StringLines .Check.Output}}{{$element}}<br>{{end}} 
[...]

Exmaple of adding the first line of the output to subject.

-S {{ $out := split "\n" .Check.Output -}}Sensu Alert - {{.Entity.Name}}/{{.Check.Name}}: {{ $out._0 }}

Configuration

Asset registration

Assets are the best way to make use of this handler. If you're not using an asset, please consider doing so! If you're using sensuctl 5.13 or later, you can use the following command to add the asset:

sensuctl asset add sensu/sensu-email-handler

If you're using an earlier version of sensuctl, you can download the asset definition from this project's Bonsai Asset Index page.

Asset definition

---
type: Asset
api_version: core/v2
metadata:
  name: sensu-email-handler_linux_amd64
  labels:
  annotations:
    io.sensu.bonsai.url: https://bonsai.sensu.io/assets/sensu/sensu-email-handler
    io.sensu.bonsai.api_url: https://bonsai.sensu.io/api/v1/assets/sensu/sensu-email-handler
    io.sensu.bonsai.tier: Supported
    io.sensu.bonsai.version: 0.2.0
    io.sensu.bonsai.namespace: sensu
    io.sensu.bonsai.name: sensu-email-handler
    io.sensu.bonsai.tags: handler
spec:
  url: https://assets.bonsai.sensu.io/45eaac0851501a19475a94016a4f8f9688a280f6/sensu-email-handler_0.2.0_linux_amd64.tar.gz
  sha512: d69df76612b74acd64aef8eed2ae10d985f6073f9b014c8115b7896ed86786128c20249fd370f30672bf9a11b041a99adb05e3a23342d3ad80d0c346ec23a946
  filters:
  - entity.system.os == 'linux'
  - entity.system.arch == 'amd64'

Handler definition

---
api_version: core/v2
type: Handler
metadata:
  namespace: default
  name: email
spec:
  type: pipe
  command: sensu-email-handler -f [email protected] -t [email protected] -t "[email protected], [email protected]" -s smtp.example.com
    -u user -p password
  timeout: 10
  filters:
  - is_incident
  - not_silenced
  runtime_assets:
  - sensu/sensu-email-handler

Debugging

It can be helpful to run from the command line to debug issues such as authentication. For this you will need two things. First you'll need to have the sensu-email-handler binary and sensuctl utility available locally. Second you will need a JSON representation of a Sensu event. You can obtain the JSON event representation using the sensuctl commandline utility. Here is a generalized example you can use to test with:

sensuctl event info sensu-entity keepalive --format=json | \
  sensu-email-handler -f [email protected] \
  -t [email protected] -s smtp.example.com \
  -u smtp_username -p smtp_password -S 'testing'

You will need to ensure the details in the command are correct for your environment. Specifically you'll want to replace sensu-entity with the name of a known Sensu entity valid for your environment (Note: sensuctl entity list is helpful)

Installing from source and contributing

Download the latest version of the sensu-email-handler from releases, or create an executable from this source.

From the local path of the sensu-email-handler repository:

go build -o /usr/local/bin/sensu-email-handler main.go

For additional instructions, see CONTRIBUTING

sensu-email-handler's People

Contributors

amdprophet avatar asachs01 avatar bluekeyes avatar ccressent avatar dependabot[bot] avatar echlebek avatar fguimond avatar ghoneycutt avatar hurrycaine avatar jspaleta avatar nixwiz avatar portertech avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sensu-email-handler's Issues

Plugin mentions template file, but no examples present

In the community version of the Core handler and in Sensu Enterprise, it's possible to use a template file for the subject and body of the email. While there is a flag for this in the handler, it's not really documented at all. Without diving into source, I have no idea if I need an erb template, or if there's some other templating language at play.

Location of the email template file

For Sensu Go, there might be a base Docker location where we run sensuctl commands, a sensu-backend, and a sensu-frontend. They can all have /etc/sensu/email_template.

Where is the file supposed to go? /etc/sensu/email_template is just too vague a description of the location.

Support HTML linebreaks where linebreaks present in check output

When a check output contains linebreaks, these characters are not converted to HTML linebreaks in HTML email templates. Given this can't be managed by a user in the template itself, and we can infer that the intention of the check is to break the line at specified positions, it would be good to replace linebreaks in the check output data with HTML linebreak tags when writing HTML emails.

Include option for including check hook output in email body

My use case for email alerts includes being able to capture check hook output (like top/ps output for CPU or memory alerts).

CheckCPU TOTAL CRITICAL: total=100.0 user=44.8 nice=0.0 system=55.2 idle=0.0 iowait=0.0 irq=0.0 softirq=0.0 steal=0.0 guest=0.0 guest_nice=0.0


Hook Name:  linux-process-list-cpu-hook
Hook Command Ran:  /usr/bin/top -b -n 1 -o '%CPU' | head -20

top - 12:25:08 up 13 days,  4:44,  2 users,  load average: 4.21, 1.48, 0.53
Tasks: 153 total,   6 running, 147 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.6 us,  0.2 sy,  0.0 ni, 99.2 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1014488 total,   317980 free,   550848 used,   145660 buff/cache
KiB Swap:        0 total,        0 free,        0 used.   289728 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
26760 a-todd    20   0  269628 157108    872 R 26.7 15.5   0:19.67 stress
26756 a-todd    20   0    7480     88      0 R 20.0  0.0   0:19.66 stress
26757 a-todd    20   0    7480     88      0 R 20.0  0.0   0:19.65 stress
26759 a-todd    20   0    7480     88      0 R 20.0  0.0   0:19.67 stress
26758 a-todd    20   0  269628  99664    872 R 13.3  9.8   0:19.66 stress
    1 root      20   0  119912   4732   2676 S  0.0  0.5   0:36.06 systemd
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd
    3 root      20   0       0      0      0 S  0.0  0.0   0:15.12 ksoftirqd/0
    5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H
    7 root      20   0       0      0      0 S  0.0  0.0   0:20.57 rcu_sched
    8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh
    9 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0
   10 root      rt   0       0      0      0 S  0.0  0.0   0:04.98 watchdog/0

Support use of envvars for configuration

Expected behavior

Plugin follows convention of supporting environment variables to provide configuration, as established in other handlers, e.g. pagerduty and influxdb.

Current behavior

Configuration must be passed as command line argument

Context

Command line arguments are considered insecure as they may be viewed from process table (e.g. using ps or top commands). This may lead to disclosure of sensitive configuration values, i.e. username and password.

toEmail annotation array and quotation handling

I'm using ansible and the sensu-go-ansible playbooks to configure checks. As a result if I pass an array to the toEmail annotation it ends up being configured as ['[email protected]', '[email protected]'] which sensu-email-handler accepts, and then doesnt actually send email.

A quick test of variations of quoting below:

No quotes in the array

curl -X POST -H 'Content-Type: application/json' -d '{
  "check": {
    "metadata": {
      "name": "test-event",
      "annotations": {
        "sensu.io/plugins/email/config/toEmail": "[[email protected]]"
      }
    },
    "status": 2,
    "output": "no quote test",
    "handlers": [ "email" ]
  }
}' http://127.0.0.1:3031/events

Results: no email

Single quote in the array

curl -X POST -H 'Content-Type: application/json' -d '{
  "check": {
    "metadata": {
      "name": "test-event",
      "annotations": {
        "sensu.io/plugins/email/config/toEmail": "['[email protected]']"
      }
    },
    "status": 2,
    "output": "single quote test",
    "handlers": [ "email" ]
  }
}' http://127.0.0.1:3031/events

Results: no email

Escaped double quotes

curl -X POST -H 'Content-Type: application/json' -d '{
  "check": {
    "metadata": {
      "name": "test-event",
      "annotations": {
        "sensu.io/plugins/email/config/toEmail": "[\"[email protected]\"]"
      }
    },
    "status": 2,
    "output": "escaped double quotes test",
    "handlers": [ "email" ]
  }
}' http://127.0.0.1:3031/events

Results: email actually sent

Any advice here? I'm not entirely sure if this is a bug here, or something to be taken up with sensu-go-ansible

Plugin always opens with `EHLO localhost` and the email headers contain `Received: from localhost` at the smtp server it hits. This causes SpamAssassin to mark message from Sensu as spam.

Despite server hostname being set correctly and with a valid /etc/hosts, sensu-email-handler sends EHLO localhost and the email header has Received: from localhost which trips up SpamAssassin's HELO_LOCALHOST anti-spam rule, occasionally marking messages from sensu as spam combined with other minor flags.

/etc/hosts looks like: 127.0.0.1 sensu.domain.com sensu-go localhost localhost.localdomain
/etc/hostname looks like: sensu.domain.com

Yet sensu-email-handler still uses localhost as its Extended HELO.

Feature Request: additional template functions

In Golang there are minimal built in functions to manipulate values (being resolved in the template). For more complex things you can create your own custom template functions or methods attached to the data you execute. Even common string manipulations are not in the built in functions.

I have an example where I need to split the entity name on a hyphen to get an abbreviated name for the subject line. I could write a custom split function to add to handler but after a quick search there is a robust package called sprig. Probably more then we need and if anything is missing then the new function could be added to sprig.

github
func docs
godoc

If sounds good, I can put in a PR.

Templates should be capable of being assets

This is probably a larger discussion that needs to happen in the Sensu Go project, but given that we have a template file that has to live on disk, it seems to run counter to the workflow established in Sensu Go, which is that everything is an api call/lives in the datastore. Ideally a template would be its own sort of primitive and could be reused across multiple handlers, but that may be a pipe dream. In the case of the email handler, and example would be:

type: TemplateConfig
api_version: core/v2
metadata:
  annotations: null
  labels: null
  name: email_template
  namespace: default
spec:
  subject: "Sensu Alert - {{.Entity.Name}}/{{.Check.Name}}: {{.Check.State}}"
  body: 
    '[
        "salutation": "Greetings"
        "contact": {{ Event.Contact }}
        "info": "The check {{ Check.Name }} is in a {{ Check.State }}"
        "occurrences": "{{ Event.Occurrences }}"
        "playbook": "http://URL/To/My/Playbook"
    ]'

The syntax here is probably not according to how things would actually work, but it gets the idea across.

Revisit fix for HTML linebreaks

As commented here, the changes in #37 are impacting users by introducing unwanted linebreaks.

Additional commentary via support conversation:

Remember, newlines are ignored in html in the rendering. Since this is an HTML template, the handler should not be modifying the template by adding more line break tags. The user that is in control of the template HTML code can add line breaks where they need them.

Is there a way we can satisfy the requested behavior in #36 without replacing all newlines in the template?

sensu-email-handler not working with postfix 3.3.0

Expected Behavior

Be able to use sensu-email-handler with postfix relay in version 3.3.0

Current Behavior

The handler isn't compliant with postfix 3.3.0 which implemente new feature on Header "From" :
http://www.postfix.org/announcements/postfix-3.3.0.html

when sensu send an email to our postfix relay smtp in version 3.3.0 this one is returning :
Feb 1 16:03:21 tech-smtp postfix/qmgr[25813]: 3917BBC446: from=sensu@XXXXX, size=410, nrcpt=1 (queue active)
Feb 1 16:03:21 tech-smtp postfix/smtpd[9968]: disconnect from ip-XX.XX.XX.XX[X.X.X.X] ehlo=1 mail=1 rcpt=1 data=1 quit=1 commands=5
Feb 1 16:03:21 tech-smtp postfix/relay/smtp[9965]: Untrusted TLS connection established to aspmx.l.google.com[74.125.193.27]:25: TLSv1.2 with cipher ECDHE-RSA-CHACHA20-POLY1305 (256/256 bits)
Feb 1 16:03:21 tech-smtp postfix/relay/smtp[9965]: 3917BBC446: to=My_EMAIL@XXXXX, relay=aspmx.l.google.com[74.125.193.27]:25, delay=0.39, delays=0.01/0/0.14/0.24, dsn=5.7.1, status=bounced (host aspmx.l.google.com[74.125.193.27] said: 550-5.7.1 [X.X.X.X 11] Our system has detected that this message is 550-5.7.1 not RFC 5322 compliant: 550-5.7.1 'From' header is missing. 550-5.7.1 To reduce the amount of spam sent to Gmail, this message has been 550-5.7.1 blocked. Please visit 550-5.7.1 https://support.google.com/mail/?p=RfcMessageNonCompliant 550 5.7.1 and review RFC 5322 specifications for more information. r9-v6si3788066ejp.209 - gsmtp (in reply to end of DATA command))

Possible Solution or workaround

Use postfix version 3.1.0 which is working with sensu handler :
Feb 1 14:46:19 imail-prod postfix/smtpd[16000]: connect from unknown[X.X.X.X]
Feb 1 14:46:19 imail-prod postfix/smtpd[16000]: 2308B23E00: client=unknown[X.X.X.X]
Feb 1 14:46:19 imail-prod postfix/cleanup[16795]: 2308B23E00: message-id=<>
Feb 1 14:46:19 imail-prod postfix/qmgr[385]: 2308B23E00: from=sensu@XXXXXXX, size=368, nrcpt=1 (queue active)
Feb 1 14:46:19 imail-prod postfix/smtpd[16000]: disconnect from unknown[X.X.X.X] ehlo=1 mail=1 rcpt=1 data=1 quit=1 commands=5
Feb 1 14:46:19 imail-prod postfix/smtp[16801]: Untrusted TLS connection established to aspmx.l.google.com[64.233.167.26]:25: TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
Feb 1 14:46:19imail-prod postfix/smtp[16801]: 2308B23E00: to=MY_MAIL@XXXXX, relay=aspmx.l.google.com[64.233.167.26]:25, delay=0.34, delays=0.05/0/0.09/0.2, dsn=2.0.0, status=sent (250 2.0.0 OK 1549028779 n17si436556wro.438 - gsmtp)
Feb 1 14:46:19 imail-prod postfix/qmgr[385]: 2308B23E00: removed

Steps to Reproduce (for bugs)

execute email handler through a smtp server postfix 3.3.0

Your Environment

Backend which exec the handler are in v5.1.1 under ubuntu 18.04

Anonymous mail with STARTTLS not supported by insecure flag

The --insecure flag currently combines several settings that are valid independently into a single option:

  • It forces the port to 25
  • It disables STARTTLS handling (with the intent of ignoring invalid TLS certificates)
  • It disables authentication

In my case, our internal mail server allows anonymous access, but requires TLS (and uses valid certificates). With the current code, using the --insecure flag causes this error:

error executing handler: EOF

I believe this is because the server closes the connection on port 25 when StartTLS is not called. Using smtp.SendMail(...) with a nil auth implementation works as expected (tested with a local build of the handler.)

I propose the following changes:

  • Remove the --insecure flag
  • Add a new --tls-skip-verify flag to disable TLS certificate checks while still supporting TLS connections.
  • Add a new --auth-method flag that can take values none, plain, or login with a default value of plain. This would also remove the enableLoginAuth flag. As an alternative, a new --anonymous flag could be added to disable the username/password check.
  • Require users to provide the existing --smptPort flag to use a non-default port

I've started implementing this, but wanted to file an issue for discussion as well since it involves breaking the current interface.

Error: failed to send email: 504 5.7.4 Unrecognized authentication type

Hi,
I'm trying to send emails using smtp.office365.com but I get this error:

Error: failed to send email: 504 5.7.4 Unrecognized authentication type [DBBPR09CA0015.eurprd09.prod.outlook.com]

I'm using the same credential/settings successfully used by other applications (e.g. grafana).

Thanks

Feature Request: Send Single Email to Multiple Recipients

Issue #22 touched on this briefly, but was closed, as another user suggested utilizing separate handlers for each contact. However, this workaround still sends individualized emails to recipients, which doesn't work very well for everyone. It's nice to know who received the alert, and an email chain can easily get started based on the remediation of that alert.

The ability to have multiple recipients in the to and cc fields (and maybe even bcc while we're at it) would be beneficial. I don't really understand why this wasn't a feature from the start.

On top of it, the ability to allow users to "subscribe" to all alerts from a host (when they normally only go to another team), by CCing them through annotations on the host, would be great.

annotations:
  sensu.io/plugins/email/config/ccEmail: [email protected]

Bump dependencies

The following dependencies need to be bumped. Either we let dependabot recreate the PRs or we bump them manually.

  • golang.org/x/net 0.7.0
  • golang.org/x/sys 0.1.0
  • golang.org/grpc 1.56.3

Feature Request: Apply line breaks from Sensu Attributes in Templates

The output format of any Sensu Attribute {{ .Check.Name }}, {{ .Check.Output }} etc.) loses line breaks once formatted through html. This presents a readability issue compared to the default handler.

Default Handler no template (Output example):
ERROR:
Backups
Failed last run!

Same output using template:
ERROR: Backups Failed last run!

Request:
Handler should add <br> for \n

Is it able to parse a html or erb templates?

Hi Team,

I have template as mentioned below
Sensu Alert - {{.Entity.Name}}

I am able to parse above template and I have received email

Is this script able to parse html or erb template as mentioned below

</style> Check Name: {{.Check.Name}}

As I checked, I am not able to parse it
How Can I present the mail in a tabular format

Support a template file for email body

One very useful feature I've see is that https://github.com/sensu-plugins/sensu-plugins-mailer supports parsing an ERB template for the email body. Reviewing this handler, it appears that the body is mostly hardcoded to the check definition.

I think a major improvement would be to support passing in a file that can be used as a template for the body itself. I'd be willing to spend some free time on the side to put something together for this feature.

Does the Sensu team have thoughts on a preferred templating solution? If I remember correctly, Go templates didn't work perfectly for the Sensu Go filters and it's now javascript based?

Lemme know if you think this is a worthwhile feature!

Can't send email with 0.5.1

sensu-backend 5.19.1
sensu-email-handler 0.5.1

Handler configured with command as "sensu-email-handler -f [email protected] -s smtphost -P 25 -a none"

If I submit a test event to it with

curl -X POST -H 'Content-Type: application/json' -d '{
  "check": {
    "metadata": {
      "name": "test-event",
      "annotations": {
        "sensu.io/plugins/email/config/toEmail": "[email protected]"
      }
    },
    "status": 2,
    "output": "this is a test event targeting the email_ops handler",
    "handlers": [ "email" ]
  }
}' http://127.0.0.1:3031/events

I get no email, and the sensu-backend spits out:

Apr 22 03:49:57 certmon sensu-backend[16358]: {"check_name":"test-event","check_namespace":"default","component":"pipelined","entity_name":"certmon","entity_namespace":"default","filter":"not_silenced","handler":"email","level":"debug","msg":"allowing event","time":"2020-04-22T03:49:57Z","uuid":"1fc5dc0f-4e8d-4698-9d3d-63f124af2914"}
Apr 22 03:49:57 certmon sensu-backend[16358]: {"check_name":"test-event","check_namespace":"default","component":"pipelined","entity_name":"certmon","entity_namespace":"default","handler":"email","level":"info","msg":"sending event to handler","time":"2020-04-22T03:49:57Z","uuid":"1fc5dc0f-4e8d-4698-9d3d-63f124af2914"}
Apr 22 03:49:57 certmon sensu-backend[16358]: {"assets":["sensu/sensu-email-handler"],"check":"test-event","component":"pipelined","entity":"certmon","event_uuid":"1fc5dc0f-4e8d-4698-9d3d-63f124af2914","handler":"email","level":"debug","msg":"fetching assets for handler","namespace":"default","time":"2020-04-22T03:49:57Z"}
Apr 22 03:49:57 certmon sensu-backend[16358]: {"asset":"sensu/sensu-email-handler","component":"asset-manager","entity":"certmon","level":"info","msg":"asset includes builds, using builds instead of asset","time":"2020-04-22T03:49:57Z"}
Apr 22 03:49:57 certmon sensu-backend[16358]: {"asset":"sensu/sensu-email-handler","component":"asset-manager","entity":"certmon","filter":["entity.system.os == 'darwin'","entity.system.arch == 'amd64'"],"level":"debug","msg":"entity not filtered, not installing asset build","time":"2020-04-22T03:49:57Z"}
Apr 22 03:49:57 certmon sensu-backend[16358]: {"asset":"sensu/sensu-email-handler","component":"asset-manager","entity":"certmon","filter":["entity.system.os == 'darwin'","entity.system.arch == '386'"],"level":"debug","msg":"entity not filtered, not installing asset build","time":"2020-04-22T03:49:57Z"}
Apr 22 03:49:57 certmon sensu-backend[16358]: {"asset":"sensu/sensu-email-handler","component":"asset-manager","entity":"certmon","filter":["entity.system.os == 'darwin'","entity.system.arch == 'amd64'"],"level":"debug","msg":"entity not filtered, not installing asset build","time":"2020-04-22T03:49:57Z"}
Apr 22 03:49:57 certmon sensu-backend[16358]: {"asset":"sensu/sensu-email-handler","component":"asset-manager","entity":"certmon","filter":["entity.system.os == 'linux'","entity.system.arch == 'armv7'"],"level":"debug","msg":"entity not filtered, not installing asset build","time":"2020-04-22T03:49:57Z"}
Apr 22 03:49:57 certmon sensu-backend[16358]: {"asset":"sensu/sensu-email-handler","component":"asset-manager","entity":"certmon","filter":["entity.system.os == 'linux'","entity.system.arch == 'arm64'"],"level":"debug","msg":"entity not filtered, not installing asset build","time":"2020-04-22T03:49:57Z"}
Apr 22 03:49:57 certmon sensu-backend[16358]: {"asset":"sensu/sensu-email-handler","component":"asset-manager","entity":"certmon","filter":["entity.system.os == 'linux'","entity.system.arch == '386'"],"level":"debug","msg":"entity not filtered, not installing asset build","time":"2020-04-22T03:49:57Z"}
Apr 22 03:49:57 certmon sensu-backend[16358]: {"asset":"sensu/sensu-email-handler","component":"asset-manager","entity":"certmon","filter":["entity.system.os == 'linux'","entity.system.arch == 'amd64'"],"level":"debug","msg":"entity filtered, installing asset build","time":"2020-04-22T03:49:57Z"}
Apr 22 03:49:57 certmon sensu-backend[16358]: {"assets":["sensu/sensu-email-handler"],"check":"test-event","component":"pipelined","entity":"certmon","event_uuid":"1fc5dc0f-4e8d-4698-9d3d-63f124af2914","handler":"email","level":"info","msg":"event pipe handler executed","namespace":"default","output":"Usage:\n  sensu-email-handler [flags]\n  sensu-email-handler [command]\n\nAvailable Commands:\n  help        Help about any command\n  version     Print the version number of this plugin\n\nFlags:\n  -a, --authMethod string         The SMTP authentication method, one of 'none', 'plain', or 'login' (default \"plain\")\n  -T, --bodyTemplateFile string   A template file to use for the body\n  -l, --enableLoginAuth           [deprecated] Use \"login auth\" mechanisim\n  -f, --fromEmail string          The 'from' email address\n  -h, --help                      help for sensu-email-handler\n  -H, --hookout                   Include output from check hook(s)\n  -i, --insecure                  [deprecated] Use an insecure connection (unauthenticated on port 25)\n  -s, --smtpHost string           The SMTP host to use to send to send email\n  -p, --smtpPassword string       The SMTP password, if not in env SMTP_PASSWORD\n  -P, --smtpPort uint             The SMTP server port (default 587)\n  -u, --smtpUsername string       The SMTP username, if not in env SMTP_USERNAME\n  -S, --subjectTemplate string    A template to use for the subject (default \"Sensu Alert - {{.Entity.Name}}/{{.Check.Name}}: {{.Check.State}}\")\n  -k, --tlsSkipVerify             Do not verify TLS certificates\n  -t, --toEmail strings           The 'to' email address (accepts comma delimited and/or multiple flags)\n\nUse \"sensu-email-handler [command] --help\" for more information about a command.\n\nError executing sensu-email-handler: invalid character 'E' looking for beginning of value\n","status":1,"time":"2020-04-22T03:49:57Z"}

Debug info?

I have setup the handler, but I am not certain it is doing anything. Is there a way to get debug info assuming it is firing or is is logging somewhere?

Hook output & body template are mutually exclusive

Aug 30 19:12:50 ip-10-0-0-63.us-west-2.compute.internal sensu-backend[7837]: {"assets":["sensu-email-handler"],"component":"pipelined","handler":"email","level":"info","msg":"event pipe handler executed","namespace":"default","output":"Error executing plugin: error validating input: --hookout (-H) and --bodyTemplateFile (-T) are mutually exclusiveError: error validating input: --hookout (-H) and --bodyTemplateFile (-T) are mutually exclusive\nUsage:\n  sensu-email-handler [flags]\n\nFlags:\n  -T, --bodyTemplateFile string   A template file to use for the body\n  -l, --enableLoginAuth           Use \"login auth\" mechanisim\n  -f, --fromEmail string          The 'from' email address\n  -h, --help                      help for sensu-email-handler\n  -H, --hookout                   Include output from check hook(s)\n  -i, --insecure                  Use an insecure connection (unauthenticated on port 25)\n  -s, --smtpHost string           The SMTP host to use to send to send email\n  -p, --smtpPassword string       The SMTP password, if not in env SMTP_PASSWORD (default \"\")\n  -P, --smtpPort uint             The SMTP server port (default 587)\n  -u, --smtpUsername string       The SMTP username, if not in env SMTP_USERNAME (default \"\")\n  -S, --subjectTemplate string    A template to use for the subject (default \"Sensu Alert - {{.Entity.Name}}/{{.Check.Name}}: {{.Check.State}}\")\n  -t, --toEmail string            The 'to' email address\n\nError executing [sensu-email-handler error validating input: --hookout (-H) and --bodyTemplateFile (-T) are mutually exclusive]: %!v(MISSING)\n","status":1,"time":"2019-08-30T19:12:50Z"}

It seems odd to me that hook output would not be appended to a message. In my head, it would make sense to have some sort of template that I'm using (e.g., to send playbook info, etc) AND still have hook output in the handler.

Email handler is not able to format nested HTML output

I am using email handler plugin and installed using below command.

sensuctl asset add sensu/sensu-email-handler -r email-handler

below is the check definition
{
“api_version”:“core/v2”,
“type”:“Check”,
“metadata”:{
“namespace”:“default”,
“name”:“app-gw-health-check”,
“labels”:{}
},
“spec”:{
“command”:“python3.6 /etc/sensu/plugins/app-gw-health-check.py”,
“subscriptions”:[
“roundrobin:worker”
],
“publish”:true,
“round_robin”:true,
“interval”:3600,
“handlers”:[
“alert_handler”,
“resolve_handler”
],
“proxy_entity_name”:“proxyclient”,
“timeout”:590
}
}

Output is not getting formatted while loading using {{.Check.Output}}

image

image

I have updated on sensu go forum as well.
https://discourse.sensu.io/t/html-output-for-mail-body-is-rendering-in-webui/2308/3

Q1'21 handler maintenance

Repository Updates

Module Updates

  • Update SDK, core, and all other modules with 'go get -u' (note: This may require some manual cleanup)
  • Cleanup with 'go mod tidy'

README Updates

  • Add note on templates and link to docs, when needed
  • Ensure help output is current
  • Caveat on using check annotations (ref https://github.com/sensu/sensu-pagerduty-handler/blob/master/README.md)
  • Make sure annotations are explained as overrides
  • Add table of environment variables available
  • Make sure layout matches current standard
  • Remove empty sections (e.g. Files, Additional Notes)

Golang version updates

  • Update go.mod to use 1.14
  • Update GitHub Actions (release and test) to use 1.14

GitHub Actions Updates

  • All have lint, test, and release actions
  • Add PR to lint and test actions on OSS handlers

Secrets update

  • For any password/token arguments, ensure 'Secret: true' is set

Code updates

  • Output supporting templates where it makes sense to (make sure to document in README)
  • Replace import of types with api/core/v2 and change *types.Event references to *corev2.Event
  • Run 'gofmt' on all source files

Output logging

  • Output logging information (e.g. any incident or job # created)

Release

  • Make sure Bonsai and GoReleaser configs are in sync and functioning Remove 32 bit macOS from Bonsai
  • Add any changes to CHANGELOG
  • Submit PR
  • After merge, cut a new release
  • In bonsai make sure the handler has the right tier (supported, enterprise)

Update curation(s)

  • Update (or add new) curations to use new version/assets (add in template args if available)

Needs support for annotations

This handler should include support for annotations similar to the Slack handler, at least for items like the Subject and Body templates. This could also serve as an alternative to the template file that was added prior.

SMTP connection fails

I am trying to use this handler with a sensu cluster and our E-Mail provider https://www.jpberlin.de/ using smtp.
When I am trying to send an E-Mail directly from one of the sensu backend machines like this:

sensuctl event info entitiy keepalive --format=json|./sensu-email-handler -f [email protected] -t [email protected] -s mail.jpberlin.de -u [email protected] -p <redacted>

I get:

Error executing sensu-email-handler: error executing handler: 535 5.7.8 Error: authentication failed:

We can send E-Mails using smtp using this provider from other applications without problems.

JPBerlin states that they support STARTTLS and both ports 25 and 587, I tried both ports with the same result:

Zugang per TLS/STARTTLS
IMAP: 143
POP3: 110
SMTP: 25 bzw. 587

https://www.jpberlin.de/hilfe/e-mail/wie-kann-ich-meine-e-mails-mit-meinem-mailprogramm-abrufen/

I am pretty much out of ideas here. Any help would be appreciated.

Unable to send HTML emails

  • sensu-backend version: sensu-backend version 6.7.2+ee, enterprise edition, build e0d4063f2671a83ef64938c0ac7d2f3bd30e0a8d, built 2022-05-11T21:42:19Z, built with go1.17.6
  • sensu-agent version: sensu-backend version 6.7.2+ee, enterprise edition, build e0d4063f2671a83ef64938c0ac7d2f3bd30e0a8d, built 2022-05-11T21:42:19Z, built with go1.17.6
  • sensu-email-handler version: 1.2.2

sensu-backend is running on Ubuntu 20.04
sensu-agent is running on Windows Server 2019


I tried some simple HTML content, like so.

<html><head><style>p {color: red;}</style></head><body><h1>This was a triump.</h1><p>I'm making a note here: Huge success.</p></body></html>

It successfully uses the template, but it is sent as plaintext.

I also tried copy/pasting the template that is available in the documentation, and that also resulted in just plain text.

The following is an example of what I'm receiving as an email.

failed to validate event entity is invalid entity name must not be empty

I'm new in Sensu, I'm trying to use sensu-email-handler with check-disk-usage (check) in a docker container but when the event schedule the sensu-email-handler I obtain this error:

Error: failed to validate event: entity is invalid: entity name must not be empty

This is my configuration:

~ # sensuctl check info check-disk-usage
=== check-disk-usage
Name: check-disk-usage
Interval: 300
Command: check-disk-usage.rb -w 65 -c 90
Cron:
Timeout: 0
TTL: 0
Subscriptions: linux
Handlers: email
Runtime Assets: sensu-plugins-disk-checks
Hooks:
Publish?: true
Stdin?: false
Proxy Entity ID:
Organization:
Environment: default
Metric Format: nagios_perfdata
Metric Handlers:

~ # sensuctl handler info email
=== email
Name: email
Type: pipe
Timeout: 10
Filters: is_incident, not_silenced
Mutator:
Execute: RUN:  sensu-email-handler -f [email protected] -t [email protected] -s smtp.gmail.com -u [email protected] -p password -P 587
Environment Variables:

What is wrong? Thank you

Is it possible to send to multiple recipients?

Hi,

I'm having trouble figuring out how to send alerts to more than one recipient. Would I need multiple handlers or can I somehow pass in more than one email address?

Looking at https://golang.org/pkg/net/smtp/#pkg-examples the underlying code seems to take a string array but the variable in this handler asset seems to only handle string and I can't quite figure out if it supports multiple recipients or not. I've tried a lot of different types of syntaxes with no luck.

//Sebastian Mildgrim

Feature Request: Check and Host Annotations Support

The ability to add in recipients to a single email by appending them onto the To, CC, or BCC field, would be beneficial. Utilizing check or host annotations would probably be a good way to do this. This would allow teams and users to "subscribe" to the handlers in a more flexible way, and would cut down on the number of separate handlers that may, otherwise, be required.

annotations:
  sensu.io.plugins.email.config.ccEmail: [email protected]
  sensu.io.plugins.email.config.toEmail: [email protected]
  sensu.io.plugins.email.config.bccEmail: [email protected]

Example: Disk checks normally go to the server team, but teamWidget would like to be CCed onto disk check alerts for their servers only. Their servers could have client annotations set up, without requiring separate checks and handlers to be configured.

The appending portion of this feature request this may conflict with the override behavior currently exhibited in filtering. Regardless, annotation support of any sort would be a nice feature.

The downside to this approach is in the event you have 2+ email handlers already configured for a check. Both handlers would look at the annotation configuration above, and the annotated recipients may receive two or more email notifications, instead of one. This could be avoided by having a sensu.io.plugins.email.config.handlerName configuration available in the annotations, but then this approaches another complex way of setting up the same thing normal handlers are designed for.

This is a good discussion piece, if nothing else :)

Use annotations and labels on templates

Hi, I'm trying to include some labels in the notification email but I can't find a way for the template to take the label that I indicate.

For example, if the entity has the label "country": "DEU", put the following in the template:

Country: {{.Entity.ObjectMeta.Labels["country"]}}

But I can't find a way to make it work

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.