An example for how to send alerts to Discord with InfluxDB v2 Alerts. Written by Craig, thanks Craig!
import "json"
import "strings"
import "influxdata/influxdb/secrets"
//Discord webhook - this value can be stored in the secret-store()
endpoint = secrets.get(key: "DISCORD")
headers = {
"Content-Type": "application/json",
"Accept": "application/json"
}
//username - overrides the current username of the webhook.
//content - simple message, the message contains (up to 2000 characters)
discord = (a) => {
data = {"username": "influxdb", "content": "🔥 Alert: Disk Space >90% on host- " + a }
HostStatus = http.post(url: endpoint, headers: headers, data: json.encode(v: data))
return string(v: HostStatus)
}
monitorCheck = from(bucket: "telegraf")
|> range(start: -5m)
|> filter(fn: (r) => r["_measurement"] == "disk")
|> filter(fn: (r) => r["_field"] == "used_percent")
|> filter(fn: (r) => r["path"] == "/")
|> last()
|> map(fn: (r) => { return {HostStatus: if r._value >= 60.0 then "${discord(a: r.host)}" else "Diskspace is fine 👍 on host - " + r.host}})
|> yield(name: "discord")