I am still quite new to influx so I am still trying to get my head around the concepts and think the right way about writing scripts. I want to write a deadman check for my endpoints (servers). Looking through the forums and tutorials, I came up with this script which catches 1 or n servers that have not responded in the past hour window
import "influxdata/influxdb/monitor"
import "experimental"
import "slack"
option v = {bucket: "telegraf"}
option task = {name: "Manual Deadman Check", every: 2m}
mydata =
from(bucket: "telegraf")
|> range(start: -60m)
|> filter(fn: (r) => r._measurement == "system" and r._field == "uptime")
// ignore hosts that come and go
|> filter(fn: (r) => r["host"] !~ /^ps.+/)
|> monitor.deadman(t: experimental.subDuration(from: now(), d: 2m))
|> filter(fn: (r) => r["dead"] == true)
|> yield(name: "myvar")
|> findRecord(fn: (key) => true, idx: 0)
if exists mydata.host then
slack.message(
url: "https://mattermost..blah.blah.blah",
token: "",
channel: "",
text: "Danger: Deadman Check last checkin from ${mydata.host} was ${mydata._time}",
color: "danger",
)
else
0
small detail, is there a better format to ignore an else condition than just a zero ?
my main problem I cant quite figure out is how to list ALL the hosts that are marked dead in the webhook call. Right now, this script will just list the last one in the resultant table.
I know there are no loops per se, I am thinking I need a map statement perhaps and then accumulate into a single string maybe ? But I dont see anyway to just append to a string the various hosts names ? Or am I thinking about this the wrong way ?
Thank you so much @scott ! I will review and experiment with it. Like I said, I am still trying to get my head around the “right” way to process my data vs thinking in SQL terms.
One quick question, why the underscore in variable names. I take it, it is purely convention as being just local in scope ?
Thanks very much @scott that indeed works! I really appreciate the code. I am at that stage where I am still struggling to “think the influx way”, but its starting to get just a little be more sensical to me.