Send alert email
Send an alert email using a third-party service, such as SendGrid, Amazon Simple Email Service (SES), Mailjet, or Mailgun. To send an alert email, complete the following steps:
- Create a check to identify the data to monitor and the status to alert on.
- Set up your preferred email service (sign up, retrieve API credentials, and send test email):
- SendGrid: See Getting Started With the SendGrid API
- AWS Simple Email Service (SES): See Using the Amazon SES API. Your AWS SES request, including the
url
(endpoint), authentication, and the structure of the request may vary. For more information, see Amazon SES API requests and Authenticating requests to the Amazon SES API. - Mailjet: See Getting Started with Mailjet
- Mailgun: See Mailgun Signup
- Create an alert email task to call your email service and send an alert email.
In the procedure below, we use the Task page in the InfluxDB UI (user interface) to create a task. Explore other ways to create a task.
Create an alert email task
In the InfluxDB UI, select Tasks in the navigation menu on the left.
Click Create Task.
In the Name field, enter a descriptive name, for example, Send alert email, and then enter how often to run the task in the Every field, for example,
10m
. For more detail, such as using cron syntax or including an offset, see Task configuration options.In the right panel, enter the following detail in your task script (see examples below):
- Import the Flux HTTP package.
- (Optional) Store your API key as a secret for reuse. First, add your API key as a secret, and then import the Flux InfluxDB Secrets package.
- Query the
statuses
measurement in the_monitoring
bucket to retrieve all statuses generated by your check. - Set the time range to monitor; use the same interval that the task is scheduled to run. For example,
range (start: -task.every)
. - Set the
_level
to alert on, for example,crit
,warn
,info
, orok
. - Use the
map()
function to evaluate the criteria to send an alert usinghttp.post()
. - Specify your email service
url
(endpoint), include applicable requestheaders
, and verify your requestdata
format follows the format specified for your email service.
Examples
The example below uses the Mailjet Send API to send an alert email when more than 3 critical statuses occur since the last task run.
To view your Mailjet API credentials, sign in to Mailjet and open the API Key Management page.
import "http"
import "json"
// Import the Secrets package if you store your API keys as secrets.
// For detail on how to do this, see Step 4 above.
import "influxdata/influxdb/secrets"
// Retrieve the secrets if applicable. Otherwise, skip this line
// and add the API keys as Basic credentials in the Authorization header.
MAILJET_APIKEY = secrets.get(key: "MAILJET_APIKEY")
MAILJET_SECRET_APIKEY = secrets.get(key: "MAILJET_SECRET_APIKEY")
numberOfCrits = from(bucket: "_monitoring")
|> range(start: -task.every)
|> filter(fn: (r) => r.measurement == "statuses" and "r.level" == "crit")
|> count()
numberOfCrits
|> map(
fn: (r) => if r._value > 3 then
{r with
_value: http.post(
url: "https://api.mailjet.com/v3.1/send",
headers: {
"Content-type": "application/json",
"Authorization": "Basic ${MAILJET_APIKEY}:${MAILJET_SECRET_APIKEY}"
},
data: json.encode(
v: {
"Messages": [
{
"From": {"Email": "jane.doe@example.com"},
"To": [{"Email": "john.doe@example.com"}],
"Subject": "InfluxDB critical alert",
"TextPart": "There have been ${r._value} critical statuses.",
"HTMLPart": "<h3>${r._value} critical statuses</h3><p>There have been ${r._value} critical statuses.",
},
],
},
),
),
}
else
{r with _value: 0},
)
Was this page helpful?
Thank you for your feedback!
Support and feedback
Thank you for being part of our community! We welcome and encourage your feedback and bug reports for InfluxDB and this documentation. To find support, use the following resources:
Customers with an annual or support contract can contact InfluxData Support. Customers using a trial license can email trial@influxdata.com for assistance.