Hello,
Sharing my solution to a problem I spent hours searching the internet for.
My use case - I want to alert on a count of something. If all is ok, the query returns “no results”. In fact, there is no data after the range statement, so this solution doesn’t work:
Because monitor.check sees “no results” instead of 0, I never get an “ok” status written to _monitoring and because my notifications are set to stateChanges, I never get a notification; there is no ok → warn or ok → crit change.
Here is the sudo code that doesn’t work:
import "influxdata/influxdb/monitor"
data = {_check_id: "0000....etc"}
from(bucket: "somebucket")
|> range(start: -2h)
|> filter(fn: (r) => r["_measurement"] == "some_measurement")
... if all is ok, there is already "no results" here
... a bunch more flux
|> count()
|> monitor.check:
ok: (r) => r._value < 1
warn: (r) => r._value >= 1,
crit: (r) => r._value >= 10,
messageFn: (r) =>
if r._level == "crit" then
"some crit message"
else if r._level == "warn" then
"some warn message"
else
"ok",
data: data,
)
Here is the sudo code that does work:
import "influxdata/influxdb/monitor"
import "csv"
data = {_check_id: "0000....etc"}
csvData =
"
_value
0
"
mydata = from(bucket: "somebucket")
|> range(start: -2h)
|> filter(fn: (r) => r["_measurement"] == "some_measurement")
... a bunch of flux
|> count()
dummy = csv.from(csv: csvData, mode: "raw")
|> map(fn: (r) => ({r with _value: int(v: r._value)}))
union(tables:[mydata,dummy])
//get the highest value, either 0 from dummy or actual count from mydata
|> max()
// add _measurement or monitor.check fails
|> set(key: "_measurement", value: "some_measurement")
// add _measurement to group key or monitor.check fails
|> group(columns: ["_measurement"])
|> monitor.check:
ok: (r) => r._value < 1
warn: (r) => r._value >= 1,
crit: (r) => r._value >= 10,
messageFn: (r) =>
if r._level == "crit" then
"some crit message"
else if r._level == "warn" then
"some warn message"
else
"ok",
data: data,
)
Maybe this will help someone else!
Thanks,
Tom