content/kapacitor/v1/reference/event_handlers/email.md
The Email event handler sends alert messages via SMTP/email.
Configuration as well as default option values for the Email event
handler are set in the [smtp] section of your kapacitor.conf.
Below is an example configuration:
[smtp]
enabled = true
host = "localhost"
port = 25
username = "username"
password = "passw0rd"
from = "[email protected]"
to = ["[email protected]", "[email protected]"]
no-verify = false
idle-timeout = "30s"
global = false
state-changes-only = false
enabledSet to true to enable the SMTP event handler.
hostThe SMTP host.
portThe SMTP port.
usernameYour SMTP username.
passwordYour SMTP password.
fromThe "From" address for outgoing mail.
toList of default "To" addresses.
no-verifySkip TLS certificate verification when connecting to the SMTP server.
idle-timeoutThe time after which idle connections are closed.
globalIf true, all alerts will be sent via Email without explicitly specifying the
SMTP handler in the TICKscript.
state-changes-onlySets all alerts in state-changes-only mode, meaning alerts will only be sent if
the alert state changes.
Only applies if global is true.
The following Email event handler options can be set in a
handler file or when using
.email() in a TICKscript.
| Name | Type | Description |
|---|---|---|
| to | list of strings | List of email addresses. |
| toTemplate(s) | string template | Derived email addresses. |
id: handler-id
topic: topic-name
kind: smtp
options:
to:
- [email protected]
- [email protected]
|alert()
// ...
.email()
.to('[email protected]')
.to('[email protected]')
// OR
.email('[email protected]')
.to('[email protected]')
The Email event handler can be used in both TICKscripts and handler files to email alerts. The email subject is the AlertNode.Message property. The email body is the AlertNode.Details property. The emails are sent as HTML emails so the body can contain html markup.
SMTP settings in kapacitor.conf
[smtp]
enabled = true
host = "smtp.myserver.com"
port = 25
username = "username"
password = "passw0rd"
from = "[email protected]"
to = ["[email protected]"]
no-verify = false
idle-timeout = "30s"
global = false
state-changes-only = false
The following TICKscript uses the .email() event handler to send out emails
whenever idle CPU usage drops below 10%.
email-cpu-alert.tick
stream
|from()
.measurement('cpu')
|alert()
.crit(lambda: "usage_idle" < 10)
.message('Hey, check your CPU')
.email()
.to('[email protected]')
.to('[email protected]')
The following setup sends an alert to the cpu topic with the message, "Hey,
check your CPU". An email handler is added that subscribes to the cpu topic
and emails all alerts.
Create a TICKscript that publishes alert messages to a topic.
The TICKscript below sends an alert message to the cpu topic any time idle
CPU usage drops below 10%.
cpu_alert.tick
stream
|from()
.measurement('cpu')
|alert()
.crit(lambda: "usage_idle" < 10)
.message('Hey, check your CPU')
.topic('cpu')
Add and enable the TICKscript:
kapacitor define cpu_alert -tick cpu_alert.tick
kapacitor enable cpu_alert
Create a handler file that subscribes to the cpu topic and uses the email or smtp
event handler to email alerts.
email_cpu_handler.yaml
id: email-cpu-alert
topic: cpu
kind: smtp
options:
to:
- [email protected]
- [email protected]
Add the handler:
kapacitor define-topic-handler email_cpu_handler.yaml
You can use toTemplate to derive email addresses directly from data instead of hardcoding them individually.
In the example below, we are using both the to option and toTemplates option in order to derive email addresses from a dataset and send email alerts directly to recipients.
Like the to option, the toTemplates option can be used more than once in a TICKscript.
You can combine the to and toTemplates options or use them individually depending on your use case.
stream
|from()
.measurement('cpu')
.where(lambda: "host" == 'serverA')
.groupBy('host')
|window()
.period(10s)
.every(10s)
|count('value')
|default()
.field('extraemail','[email protected]')
.tag('tagemail','[email protected]')
|alert()
.id('kapacitor.{{ .Name }}.{{ index .Tags "host" }}')
.details('''
<b>{{.Message}}</b>
Value: {{ index .Fields "count" }}
<a href="http://graphs.example.com/host/{{index .Tags "host"}}">Details</a>
''')
.info(lambda: "count" > 6.0)
.warn(lambda: "count" > 7.0)
.crit(lambda: "count" > 8.0)
.email()
.to('[email protected]', '[email protected]')
.toTemplates('{{ index .Fields "extraemail" }}')