diff --git a/content/influxdb/v2.0/monitor-alert/custom-checks.md b/content/influxdb/v2.0/monitor-alert/custom-checks.md new file mode 100644 index 000000000..71b33e03d --- /dev/null +++ b/content/influxdb/v2.0/monitor-alert/custom-checks.md @@ -0,0 +1,102 @@ +--- +title: Create custom checks +seotitle: Custom checks +description: > + Create custom checks with a Flux task. +menu: + influxdb_2_0: + parent: Monitor & alert +weight: 201 +v2.0/tags: [alerts, checks, tasks, Flux] +--- + +In the UI, you can create two kinds of [checks](/influxdb/v2.0/reference/glossary/#check): +[`threshold`](/influxdb/v2.0/monitor-alert/checks/create/#threshold-check) and +[`deadman`](/influxdb/v2.0/monitor-alert/checks/create/#deadman-check). + +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. + + {{< nav-icon "tasks" >}} + +2. Click **{{< icon "plus" >}} 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](/influxdb/v2.0/process-data/task-options/). +4. Enter the Flux script for your custom check, including the [`monitor.check`](/influxdb/v2.0/reference/flux/stdlib/monitor/check/) function. + +{{% note %}} +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. +{{% /note %}} + +### 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 + +```js +import "strings" +import "regexp" +import "influxdata/influxdb/monitor" +import "influxdata/influxdb/v1" + +option task = {name: "Failed Tasks Check", every: 1h, offset: 4m} + +task_data = from(bucket: "_tasks") + |> range(start: -task.every) + |> filter(fn: (r) => + (r["_measurement"] == "runs")) + |> filter(fn: (r) => + (r["_field"] == "logs")) + |> map(fn: (r) => ({ r with name: strings.split(v: regexp.findString(r: /option task = \{([^\}]+)/, v: r._value), t: "\\\\\\\"")[1] })) + |> drop(columns: ["_value", "_start", "_stop"]) + |> group(columns: ["name", "taskID", "status", "_measurement"]) + |> map(fn: (r) => + ({r with _value: if r.status == "failed" then 1 else 0})) + |> last() +check = { + _check_id: "0000000000000001", // 16 characters, alphanumeric + _check_name: "Failed Tasks Check", // string + _type: "custom", // can also use "threashold" or "deadman" + tags: {}, +} +ok = (r) => + (r["logs"] == 0) +crit = (r) => + (r["logs"] == 1) +messageFn = (r) => + ("The task: ${r.taskID} - ${r.name} has a status of ${r.status}") + +task_data + |> v1["fieldsAsCols"]() + |> monitor["check"]( + data: check, + messageFn: messageFn, + ok: ok, + crit: crit, + ) +``` + +{{% note %}} +Creating a custom check does not send a notification email. +For information on how to create notification emails, see +[Create notification endpoints](/influxdb/v2.0/monitor-alert/notification-endpoints/create), +[Create notification rules](/influxdb/v2.0/monitor-alert/notification-rules/create), +and [Send alert email](/influxdb/v2.0/monitor-alert/send-email/) +{{% /note %}} diff --git a/content/influxdb/v2.0/monitor-alert/task-failure.md b/content/influxdb/v2.0/monitor-alert/task-failure.md deleted file mode 100644 index b88630dca..000000000 --- a/content/influxdb/v2.0/monitor-alert/task-failure.md +++ /dev/null @@ -1,69 +0,0 @@ ---- -title: Alert on failed tasks -seotitle: Alert on failed tasks. -description: > - Create an alert when a task fails. -menu: - influxdb_2_0: - parent: Monitor & alert -weight: 201 -influxdb/v2.0/tags: [tasks] ---- - -Use a task to receive alerts when a task fails. - -## Example alert task script - -```js -import "strings" - import "regexp" -import "influxdata/influxdb/monitor" -import "influxdata/influxdb/v1" - -option task = {name: "Failed Tasks Check", every: 1h, offset: 4m} - -task_data = from(bucket: "_tasks") - |> range(start: -1h, stop: now()) - |> filter(fn: (r) => - (r["_measurement"] == "runs")) - |> filter(fn: (r) => - (r["_field"] == "logs")) - |> map(fn: (r) => ({ r with name: strings.split(v: regexp.findString(r: /option task = \{([^\}]+)/, v: r._value), t: "\\\\\\\"")[1] })) - |> drop(columns: ["_value", "_start", "_stop"]) - |> group(columns: ["name", "taskID", "status", "_measurement"]) - |> map(fn: (r) => - ({r with _value: if r.status == "failed" then 1 else 0})) - |> last() -check = { - _check_id: "0000000deadbeef1", - _check_name: "Failed Tasks Check", - _type: "threshold", - tags: {}, -} -ok = (r) => - (r["logs"] == 0) -crit = (r) => - (r["logs"] == 1) -messageFn = (r) => - ("The task: ${r.taskID} - ${r.name} has a status of ${r.status}") - -task_data - |> v1["fieldsAsCols"]() - |> monitor["check"]( - data: check, - messageFn: messageFn, - ok: ok, - crit: crit, - ) -``` - -## Add your task - -Once your task is ready, see [Create a task](/influxdb/v2.0/process-data/manage-tasks/create-task) for information about adding it to InfluxDB. - -{{% note %}} -This script does not send an email alert. -For information on how to create notification emails, see -[Create notification endpoints](/influxdb/v2.0/monitor-alert/notification-endpoints/create) -and [Create notification rules](/influxdb/v2.0/monitor-alert/notification-rules/create). -{{% /note %}}