Hi guys,
I am a new developer and also new to influxDB. I am working on alerts/checks and notifications, after following the steps from the document, I am able to see the alert from InfluxDB UI. However, I realised that I could only select ONE field to check for ONE sensor. What should I do if I have multiple sensors and fields that need to be checked? How can I configure it?
Thank you for reading.
Hello @vinh_lee,
So I don’t recommend using the UI to make alerts. Instead use tasks.
This way you can use any custom logic you want using Flux:
import "array"
import "slack"
option task = { name: "Event Alert", every: 1h0m0s, offset: 5m0s }
alert = (eventValue, threshold) =>
(if eventValue >= threshold then slack.message(
url: "https://hooks.slack.com/services/####/####/####",
text: "An alert event has occurred! The number of field values= \"${string(v: eventValue)}\ ". and The sensor is= \"${string(v: sensorName)}\ ". ",
color: "warning",
) else 0)
data = from(bucket: "bucket1")
|> range(start: -task.every, stop: now())
|> filter(fn: (r) =>
(r._measurement == "measurement1" and r._field == "field1" or r._field == "field2))
|> group()
|> filter(fn: (r) => r._value > threshold, onEmpty: "keep")
events = data
|> findRecord(fn: (key) =>
(true), idx: 0)
eventTotal = events._value
sensorName = events.<SensorTagKey>
data_0
|> yield(name: "ignore")
alert(eventValue: eventTotal, threshold: <your threshold value>)
Alternatively I really recommend using Grafana for alerting lol.
something like that though.
PS what are you doing with influxDB? I like to learn about community users use cases. Thank you!
Hi @Anaisdg. Thank you so much for your reply, It is just for my team project when we want to check our sensor. I already solved the issue using custom tasks as you suggested. However, I have another question about custom tasks or notifications. I wonder what happens if I intentionally
set up “every” one second for my task to show the error immediately. Do you reckon it is a good practice? if not, how do we send the message?
Thanks
Setting it up to run every second really depends on how much data you’re querying each time. It’s not necessarily a bad idea if you’re querying a small amount of data.
However you could also consider using one task for the processing where you then write the alert status to a new measurement or bucket and a second task for the alerting that runs every second. The second task would query that new measurement and bucket and return an alert if there is a value present.
Hi @Anaisdg ,
Thank you so much for your response,
Since my sensor will write its measurement every one second to a bucket, I’m not going to query big data, probably within -10 or 15 seconds.
However, to check its status, I will need a task, which will run every one second, to store the status. Do you think it duplicates the data, for instance, 1 bucket for real database and another bucket just for status ? Let’s say in 24 hours, there are no changes in the status.
Apologize for asking so many things. I really want to know your point of view. Not only InfluxDb but also the Database concept.
Regards,
@vinh_lee,
Of course
Yah I’d probably put the results of a check in another bucket with a short retention policy so you’re not keeping that data around forever. Unless of course you want it for some reason.
Thank you so much.