docs-v2/content/v2.0/monitor-alert/task-failure.md

1.8 KiB

title seotitle description menu weight v2.0/tags
Alert on failed tasks Alert on failed tasks. Create an alert when a task fails.
v2_0
parent
Monitor & alert
201
tasks

Use a task to receive alerts when a task fails.

Example alert task script

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 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 and Create notification rules. {{% /note %}}