Hi
. I’m trying to do a custom check with the tasks in Flux (InfluxDB 2.x) I’m taking a field from a measurement and grouping by tag. I want that if the value is greater than X alert by Slack (with webhook). Replicate the typical Kapacitor critical and warning alert with Flux. I have looked for information on the internet but I can’t do it. Does anyone have a simple model for this? Thanks.
@delapuentem Something like this should work:
import "influxata/influxdb/tasks"
import "influxata/influxdb/slack"
toSlack = slack.endpoint(
url: "https://slack.com/api/chat.postMessage",
token: secrets.get(key: "YOUR_SLACK_TOKEN")
)
threshold = 90.0
from(bucket: "example")
|> range(start: tasks.lastSuccess(orTime: -task.every))
|> filter(fn: (r) => r._measurement == "example-measurement" and r._field == "example-field")
|> group(columns: ["example-tag"])
|> max()
|> filter(fn: (r) => r._value >= threshold)
|> toSlack(
mapFn: (r) => { r with
channel: "anomalies",
text: "${r.example-tag} has crossed the threshold!",
color: "danger"
}
)()
This takes the max of each group and checks to see if it exceeds the defined threshold.
Hi @scott . Thank you very much for your help and your answer. Maybe I’m doing something wrong but it gives me a syntax error in this part because of the “:” characters.
|> toSlack(
mapFn: (r) => { r with
channel: "anomalies",
text: "${r.example-tag} has crossed the threshold!",
color: "danger"
})
The error: Failed to create new task: invalid AST: loc 19:14-19:15: invalid statement @19:14-19:15: :
@delapuentem Sorry, I had a few errors in there . Try this:
import "influxdata/influxdb/tasks"
import "slack"
toSlack = slack.endpoint(
url: "https://slack.com/api/chat.postMessage",
token: "YOUR_SLACK_TOKEN"
)
threshold = 90.0
from(bucket: "default")
|> range(start: tasks.lastSuccess(orTime: -task.every))
|> filter(fn: (r) => r._measurement == "exampleMeasurement" and r._field == "exampleField")
|> group(columns: ["exampleTag"])
|> max()
|> filter(fn: (r) => r._value >= threshold)
|> toSlack(
mapFn: (r) => ({ r with
channel: "anomalies",
text: "${r.exampleTag} has crossed the threshold!",
color: "danger"
})
)()
1 Like