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:

  1. Create a check to identify the data to monitor and the status to alert on.
  2. Set up your preferred email service (sign up, retrieve API credentials, and send test email):
  3. 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

  1. In the InfluxDB UI, select Tasks in the navigation menu on the left.

    Tasks

  2. Click Create Task.

  3. 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.

  4. 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, or ok.
    • Use the map() function to evaluate the criteria to send an alert using http.post().
    • Specify your email service url (endpoint), include applicable request headers, and verify your request data format follows the format specified for your email service.

Examples

SendGrid AWS SES Mailjet Mailgun

The example below uses the SendGrid API to send an alert email when more than 3 critical statuses occur since the previous task run.

  1. import "http"
  2. import "json"
  3. // Import the Secrets package if you store your API key as a secret.
  4. // For detail on how to do this, see Step 4 above.
  5. import "influxdata/influxdb/secrets"
  6. // Retrieve the secret if applicable. Otherwise, skip this line
  7. // and add the API key as the Bearer token in the Authorization header.
  8. SENDGRID_APIKEY = secrets.get(key: "SENDGRID_APIKEY")
  9. numberOfCrits = from(bucket: "_monitoring")
  10. |> range(start: -task.every)
  11. |> filter(fn: (r) => r._measurement == "statuses" and r._level == "crit")
  12. |> count()
  13. numberOfCrits
  14. |> map(
  15. fn: (r) => if r._value > 3 then
  16. {r with _value: http.post(
  17. url: "https://api.sendgrid.com/v3/mail/send",
  18. headers: {"Content-Type": "application/json", "Authorization": "Bearer ${SENDGRID_APIKEY}"},
  19. data: json.encode(
  20. v: {
  21. "personalizations": [
  22. {
  23. "to": [
  24. {
  25. "email": "jane.doe@example.com"
  26. }
  27. ]
  28. }
  29. ],
  30. "from": {
  31. "email": "john.doe@example.com"
  32. },
  33. "subject": "InfluxDB critical alert",
  34. "content": [
  35. {
  36. "type": "text/plain",
  37. "value": "There have been ${r._value} critical statuses."
  38. }
  39. ]
  40. }
  41. )
  42. )}
  43. else
  44. {r with _value: 0},
  45. )

The example below uses the AWS SES API v2 to send an alert email when more than 3 critical statuses occur since the last task run.

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. We recommend signing your AWS API requests using the Signature Version 4 signing process.

  1. import "http"
  2. import "json"
  3. // Import the Secrets package if you store your API credentials as secrets.
  4. // For detail on how to do this, see Step 4 above.
  5. import "influxdata/influxdb/secrets"
  6. // Retrieve the secrets if applicable. Otherwise, skip this line
  7. // and add the API key as the Bearer token in the Authorization header.
  8. AWS_AUTH_ALGORITHM = secrets.get(key: "AWS_AUTH_ALGORITHM")
  9. AWS_CREDENTIAL = secrets.get(key: "AWS_CREDENTIAL")
  10. AWS_SIGNED_HEADERS = secrets.get(key: "AWS_SIGNED_HEADERS")
  11. AWS_CALCULATED_SIGNATURE = secrets.get(key: "AWS_CALCULATED_SIGNATURE")
  12. numberOfCrits = from(bucket: "_monitoring")
  13. |> range(start: -task.every)
  14. |> filter(fn: (r) => r.measurement == "statuses" and r._level == "crit")
  15. |> count()
  16. numberOfCrits
  17. |> map(
  18. fn: (r) => if r._value > 3 then
  19. {r with _value: http.post(
  20. url: "https://email.your-aws-region.amazonaws.com/sendemail/v2/email/outbound-emails",
  21. headers: {
  22. "Content-Type": "application/json",
  23. "Authorization": "Bearer ${AWS_AUTH_ALGORITHM}${AWS_CREDENTIAL}${AWS_SIGNED_HEADERS}${AWS_CALCULATED_SIGNATURE}"},
  24. data: json.encode(v: {
  25. "Content": {
  26. "Simple": {
  27. "Body": {
  28. "Text": {
  29. "Charset": "UTF-8",
  30. "Data": "There have been ${r._value} critical statuses."
  31. }
  32. },
  33. "Subject": {
  34. "Charset": "UTF-8",
  35. "Data": "InfluxDB critical alert"
  36. }
  37. }
  38. },
  39. "Destination": {
  40. "ToAddresses": [
  41. "john.doe@example.com"
  42. ]
  43. }
  44. }
  45. )
  46. )}
  47. else
  48. {r with _value: 0},
  49. )

For details on the request syntax, see SendEmail API v2 reference.

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.

  1. import "http"
  2. import "json"
  3. // Import the Secrets package if you store your API keys as secrets.
  4. // For detail on how to do this, see Step 4 above.
  5. import "influxdata/influxdb/secrets"
  6. // Retrieve the secrets if applicable. Otherwise, skip this line
  7. // and add the API keys as Basic credentials in the Authorization header.
  8. MAILJET_APIKEY = secrets.get(key: "MAILJET_APIKEY")
  9. MAILJET_SECRET_APIKEY = secrets.get(key: "MAILJET_SECRET_APIKEY")
  10. numberOfCrits = from(bucket: "_monitoring")
  11. |> range(start: -task.every)
  12. |> filter(fn: (r) => r.measurement == "statuses" and "r.level" == "crit")
  13. |> count()
  14. numberOfCrits
  15. |> map(
  16. fn: (r) => if r._value > 3 then
  17. {r with
  18. _value: http.post(
  19. url: "https://api.mailjet.com/v3.1/send",
  20. headers: {
  21. "Content-type": "application/json",
  22. "Authorization": "Basic ${MAILJET_APIKEY}:${MAILJET_SECRET_APIKEY}"
  23. },
  24. data: json.encode(
  25. v: {
  26. "Messages": [
  27. {
  28. "From": {"Email": "jane.doe@example.com"},
  29. "To": [{"Email": "john.doe@example.com"}],
  30. "Subject": "InfluxDB critical alert",
  31. "TextPart": "There have been ${r._value} critical statuses.",
  32. "HTMLPart": "<h3>${r._value} critical statuses</h3><p>There have been ${r._value} critical statuses.",
  33. },
  34. ],
  35. },
  36. ),
  37. ),
  38. }
  39. else
  40. {r with _value: 0},
  41. )

The example below uses the Mailgun API to send an alert email when more than 3 critical statuses occur since the last task run.

To view your Mailgun API keys, sign in to Mailjet and open Account Security - API security. Mailgun requires that a domain be specified via Mailgun. A domain is automatically created for you when you first set up your account. You must include this domain in your url endpoint (for example, https://api.mailgun.net/v3/YOUR_DOMAIN or https://api.eu.mailgun.net/v3/YOUR_DOMAIN. If you’re using a free version of Mailgun, you can set up a maximum of five authorized recipients (to receive email alerts) for your domain. To view your Mailgun domains, sign in to Mailgun and view the Domains page.

  1. import "http"
  2. import "json"
  3. // Import the Secrets package if you store your API key as a secret.
  4. // For detail on how to do this, see Step 4 above.
  5. import "influxdata/influxdb/secrets"
  6. // Retrieve the secret if applicable. Otherwise, skip this line
  7. // and add the API key as the Bearer token in the Authorization header.
  8. MAILGUN_APIKEY = secrets.get(key: "MAILGUN_APIKEY")
  9. numberOfCrits = from(bucket: "_monitoring")
  10. |> range(start: -task.every)
  11. |> filter(fn: (r) => r["_measurement"] == "statuses")
  12. |> filter(fn: (r) => r["_level"] == "crit")
  13. |> count()
  14. numberOfCrits
  15. |> map(
  16. fn: (r) => if r._value > 1 then
  17. {r with _value: http.post(
  18. url: "https://api.mailgun.net/v3/YOUR_DOMAIN/messages",
  19. headers: {
  20. "Content-type": "application/json",
  21. "Authorization": "Basic api:${MAILGUN_APIKEY}"
  22. },
  23. data: json.encode(v: {
  24. "from": "Username <mailgun@YOUR_DOMAIN_NAME>",
  25. "to": "email@example.com",
  26. "subject": "InfluxDB critical alert",
  27. "text": "There have been ${r._value} critical statuses."
  28. }
  29. )
  30. )}
  31. else
  32. {r with _value: 0},
  33. )

alert email notifications check