# Alerting in InfluxDB 2.0 using Flux

**URL:** <https://community.influxdata.com/t/alerting-in-influxdb-2-0-using-flux/20060>\
**Category:** InfluxDB 2\
**Created:** [May 25, 2021, 2:14pm UTC](https://community.influxdata.com/t/alerting-in-influxdb-2-0-using-flux/20060 "2021-05-25T14:14:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mert](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/mert/32/5152_2.png) [@Mert](https://community.influxdata.com/u/Mert)\
**Post date:** [May 25, 2021, 2:14pm UTC](https://community.influxdata.com/t/alerting-in-influxdb-2-0-using-flux/20060/1 "2021-05-25T14:14:32Z")

</div>

Is there a way to write a little more advance alerting task using Flux in Influx 2, something similar to Kapacitor? As far as I understand, we can only set a constant threshold using the UI.

Edit: I have a time series, and I want to know if series A exceeds B? I don’t have a constant threshold. How can I handle this?

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [May 25, 2021, 8:02pm UTC](https://community.influxdata.com/t/alerting-in-influxdb-2-0-using-flux/20060/2 "2021-05-25T20:02:57Z")

</div>

Hello @Mert,  
Absolutely you can craft custom tasks, checks, and notification rules. Have you seen [InfluxDB's Checks and Notifications System | InfluxData](https://www.influxdata.com/blog/influxdbs-checks-and-notifications-system/)? Please give it a read and let me know if you still have any questions.

To write a check see whether field A exceeds field B would look something like:

```auto
package main 
import "influxdata/influxdb/monitor"
import "influxdata/influxdb/v1"

data = from(bucket: "telegraf")
|> range(start: -5s)
|> filter(fn: (r) =>(r["_measurement"] == "my_measurement"))
|> filter(fn: (r) =>(r["tag"] == "my_tag"))
|> filter(fn: (r) =>(r["_field"] == "A" and "r["_field"] == "B"))

option task = {name: "Comparison Check", every: 5s, offset: 0s}
check = {_check_id: "074bba32a48c3000", 
  _check_name: "Comparison Check",
  _type: "threshold",
 tags: {},}

crit = (r) =>r["A"] > r["B"]
messageFn = (r) =>("Check: ${ r._check_name } is: ${ r._level }")

data
|> v1["fieldsAsCols"]()
|> monitor["check"](data: check, messageFn: messageFn, crit: crit)

```

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [May 25, 2021, 8:03pm UTC](https://community.influxdata.com/t/alerting-in-influxdb-2-0-using-flux/20060/3 "2021-05-25T20:03:43Z")

</div>

@Mert Please let me know if that meets your needs. Or if you have trouble with it, but also please consider giving that blog post a look. Thank you.

---

<div class="post-metadata">

**Author:** ![Mert](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/mert/32/5152_2.png) [@Mert](https://community.influxdata.com/u/Mert)\
**Post date:** [May 25, 2021, 9:36pm UTC](https://community.influxdata.com/t/alerting-in-influxdb-2-0-using-flux/20060/4 "2021-05-25T21:36:49Z")

</div>

@Anaisdg Thanks,

Your example works fine generally. I’ve some questions if you don’t mind.

- Is there a difference between `monitor["check"]()` and `monitor.check()`? Both seems working?
- In your example, we didn’t use `|> last()` or `|> mean()`, so there may be more than one observation.  
How is the `crit` function works in that case? I assume it’ll return the result for the last row?

Edit: I read the blog post. I wish I had found the blog post this morning. It’s would be a good start. 😃

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [May 26, 2021, 7:06pm UTC](https://community.influxdata.com/t/alerting-in-influxdb-2-0-using-flux/20060/5 "2021-05-26T19:06:14Z")

</div>

Hello @Mert,  
No that’s just different syntax for the same thing.  
So the predicate functions messagfn() and crit(), ok(), info(), etc depending on what the user inclues are passed into the monitor.check() function. The monitor.check function takes the input and assigns a level and message for each row using the predicate functions.

I’m glad the blog was useful. Please let me know if there are points of confusion or anything I need to append/explain further.
