Create custom checks

In the UI, you can create two kinds of checks: threshold and deadman.

Using a Flux task, you can create a custom check that provides a couple advantages:

  • Customize and transform the data you would like to use for the check.
  • Set up custom criteria for your alert (other than threshold and deadman).

Create a task

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

    Tasks

    Tasks

  2. Click Create Task, and then select New Task.

  3. In the Name field, enter a descriptive name, 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. Enter the Flux script for your custom check, including the monitor.check function.

Use the the API endpoint /checks/{checkID}/query to see the Flux code for a check built in the UI. This can be useful for constructing custom checks.

Example: Monitor failed tasks

The script below is fairly complex, and can be used as a framework for similar tasks. It does the following:

  • Import the necessary influxdata/influxdb/monitor package, and other packages for data processing.
  • Query the _tasks bucket to retrieve all statuses generated by your check.
  • Set the _level to alert on, for example, crit, warn, info, or ok.
  • Create a check object that specifies an ID, name, and type for the check.
  • Define the ok and crit statuses.
  • Execute the monitor function on the check using the task_data.

Example alert task script

  1. import "strings"
  2. import "regexp"
  3. import "influxdata/influxdb/monitor"
  4. import "influxdata/influxdb/v1"
  5. option task = {name: "Failed Tasks Check", every: 1h, offset: 4m}
  6. task_data = from(bucket: "_tasks")
  7. |> range(start: -task.every)
  8. |> filter(fn: (r) =>
  9. (r["_measurement"] == "runs"))
  10. |> filter(fn: (r) =>
  11. (r["_field"] == "logs"))
  12. |> map(fn: (r) => ({ r with name: strings.split(v: regexp.findString(r: /option task = \{([^\}]+)/, v: r._value), t: "\\\\\\\"")[1] }))
  13. |> drop(columns: ["_value", "_start", "_stop"])
  14. |> group(columns: ["name", "taskID", "status", "_measurement"])
  15. |> map(fn: (r) =>
  16. ({r with _value: if r.status == "failed" then 1 else 0}))
  17. |> last()
  18. check = {
  19. _check_id: "0000000000000001", // 16 characters, alphanumeric
  20. _check_name: "Failed Tasks Check", // string
  21. _type: "custom", // can also use "threashold" or "deadman"
  22. tags: {},
  23. }
  24. ok = (r) =>
  25. (r["logs"] == 0)
  26. crit = (r) =>
  27. (r["logs"] == 1)
  28. messageFn = (r) =>
  29. ("The task: ${r.taskID} - ${r.name} has a status of ${r.status}")
  30. task_data
  31. |> v1["fieldsAsCols"]()
  32. |> monitor["check"](
  33. data: check,
  34. messageFn: messageFn,
  35. ok: ok,
  36. crit: crit,
  37. )

Creating a custom check does not send a notification email. For information on how to create notification emails, see Create notification endpoints, Create notification rules, and Send alert email