Creating a task/check in Influx 2.0

I’m trying to write a threshold check in influx v2. The Monitoring & Alerting section appears to allow basic checks where you pick one field, one aggregate function, and set some thresholds. I can’t seem to pull up a script editor in this workflow. So instead i’m hoping i can create a monitor in the tasks section with the code below. The code as it sits throws this error “An internal error has occurred”

I’d like to alert if i haven’t received a point for a series after some amount of time.

import "experimental"
import "influxdata/influxdb/monitor"

t1 = from(bucket: "telegraf")
  |> range(start: -30m)
  |> filter(fn: (r) => r._measurement == "processes")
  |> filter(fn: (r) => r._field == "dead")
  |> last()

t2 = from(bucket: "telegraf")
  |> range(start: -30m)
  |> filter(fn: (r) => r._measurement == "processes")
  |> filter(fn: (r) => r._field == "dead")
  |> last()
  |> map(fn: (r) => ({
  r with
  _time: experimental.addDuration(d: 0h, to: now()),

}))

curr_elapsed = union(tables: [t1, t2])
  |> sort(columns: ["_time"], desc: false)
  |> elapsed()

avg_elapsed = from(bucket: "telegraf")
  |> range(start: -30m)
  |> filter(fn: (r) => r._measurement == "processes")
  |> filter(fn: (r) => r._field == "dead")
  |> elapsed()
  |> mean(column: "elapsed")

  
data = {
_check_id: "000000000000000a",
_check_name: "threshold check",
_type: "threshold",
_measurement: "telegraf",
tags: {aaa: "vaaa", bbb: "vbbb"}
}
  
join(tables: {current: curr_elapsed, avg: avg_elapsed}, on: ["host"])
|> rename(columns: {_measurement_avg: "_measurement", _field_avg: "_field", })
|> monitor.check(
crit: (r) => r.elapsed_current > r.elapsed_avg ,
messageFn: (r) => "TEST",
data: data
)

i used this a a reference: check_test.flux

If i comment out monitor.check it yields results as i’d expect it to.

#group FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE TRUE
#datatype string long string string string string dateTime:RFC3339 dateTime:RFC3339 dateTime:RFC3339 dateTime:RFC3339 dateTime:RFC3339 long double long string
#default _result
result table _field _field_current _measurement _measurement_current _start_avg _start_current _stop_avg _stop_current _time _value elapsed_avg elapsed_current host
0 dead dead processes processes 2019-12-09T18:11:30.909468818Z 2019-12-09T18:11:30.909468818Z 2019-12-09T18:41:30.909468818Z 2019-12-09T18:41:30.909468818Z 2019-12-09T18:41:30.909468818Z 0 10 10 host1
1 dead dead processes processes 2019-12-09T18:11:30.909468818Z 2019-12-09T18:11:30.909468818Z 2019-12-09T18:41:30.909468818Z 2019-12-09T18:41:30.909468818Z 2019-12-09T18:41:30.909468818Z 0 10 0 host2

Any idea on how to make the monitor.check work?

Thanks for looking

I added a yield() before monitor.check() and received some useful feedback

1st issue: found column “_field” in the group key; experimental.to() expects pivoted data
2nd issue:group key column “_start_avg” has type time; type string is required

I was able to get monitor.check to execute by removing/dropping these columns.

join(tables: {current: curr_elapsed, avg: avg_elapsed}, on: ["host"])
|> rename(columns: {_measurement_avg: "_measurement"})
|> drop(columns: ["_start_avg", "_start_current", "_stop_avg", "_stop_current"])
|> monitor.check(
    crit: (r) => r.elapsed_current > r.elapsed_avg ,
    messageFn: (r) => "Blah",
    data: data
)
1 Like

Thank you for the feedback. We’re definitely working to add the script editor to the workflow.

Is there a github issue tracking adding this feature in the UI? I didn’t seem to find one.