Add to template (#3886)

* added TICK script example

* add toTemplate info and example
pull/3905/head
lwandzura 2022-03-28 15:11:20 -05:00 committed by GitHub
parent 6cabe1d9cd
commit b6bc0647cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 37 additions and 1 deletions

View File

@ -75,8 +75,9 @@ The following Email event handler options can be set in a
`.email()` in a TICKscript.
| Name | Type | Description |
| ---- | ---- | ----------- |
| ---- | --------------- | ------------------------ |
| to | list of strings | List of email addresses. |
| toTemplate(s) | string template| Derived email addresses. |
### Example: handler file
```yaml
@ -187,3 +188,38 @@ Add the handler:
```bash
kapacitor define-topic-handler email_cpu_handler.yaml
```
### Send email alerts using the toTemplate option
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.
```js
stream
|from()
.measurement('cpu')
.where(lambda: "host" == 'serverA')
.groupBy('host')
|window()
.period(10s)
.every(10s)
|count('value')
|default()
.field('extraemail','bob@example.com')
.tag('tagemail','bob2@example.com')
|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('user1@example.com', 'user2@example.com')
.toTemplates('{{ index .Fields "extraemail" }}')
```