I’m trying to make alerting tasks, more specifically to run threshold checks as tasks and send alerts to my webhook. I tried a couple of different approaches but so far nothing has worked as it should. I tried with http.post which sends alerts to webhook but it doesn’t send it on status change but rather continues to send it every time the task runs. I also tried it with monitor.notify and http.endpoint which fails to send alerts altogether. Here’s my code including the monitor.notify method.
I tried putting the limit() function with http.post but it still sends alerts every time task runs, I also tried it with r.elapsed but it still doesn’t work and is not optimal for us.
Also, my notification rule is configured correctly and it should work.
I just need an alert sent to my webhook when status changes from any to critical.
import "http"
import "influxdata/influxdb/schema"
import "influxdata/influxdb/tasks"
import "influxdata/influxdb/monitor"
import "influxdata/influxdb/v1"
import "influxdata/influxdb/secrets"
import "math"
import "date"
option task = {name: "task_test", every: 1m}
notification = {
_notification_rule_id: "0ab59d6d1b228000",
_notification_rule_name: "dummyNOKnew",
_notification_endpoint_id: "0a577a5381943000",
_notification_endpoint_name: "WebhookDummy",
}
data =
from(bucket: "grafana_linux/autogen")
|> range(start: tasks.lastSuccess(orTime: -task.every))
|> filter(fn: (r) => r["_measurement"] == "disk")
|> filter(fn: (r) => r["host"] == "lvpgrafanasrv01.local")
|> filter(fn: (r) => r["path"] == "/disk_1")
|> filter(fn: (r) => r["_field"] == "used_percent")
|> map(
fn: (r) => ({r with _level:
if r._value <= 75.0 then
"ok"
else if r._value <= 80.0 and r._value > 75.0 then
"noalert"
else if r._value > 80.0 then
"crit"
else
"noalert",
}),
)
|> filter(fn: (r) => r._level == "ok" or r._level == "crit")
|> yield(name: "alert")
data
|> schema["fieldsAsCols"]()
|> filter(fn: (r) => r["_measurement"] == "disk")
|> filter(fn: (r) => r["host"] == "lvpgrafanasrv01.local")
|> filter(fn: (r) => r["path"] == "/disk_1")
|> filter(fn: (r) => r["_field"] == "used_percent")
|> monitor["notify"](
data: notification,
endpoint:
http.endpoint(url: "http://***endpoint**** ")(
mapFn: (r) => {
body = r._message
return {
headers: {"Content-Type": "application/x-www-form-urlencoded"},
data: bytes(v: "value is ${r._value}" )
}
},
),
)