Removing a specific firmware type from a query/ Grafana alert

Hi. I am using influx db 2 and coding in flux to make an alert in Grafana.
I am trying to remove certain devices with certain firmwares from the alert. The firmware I need to remove has a -p at the end.

I have tried so many things that don’t work.

Start Code:
from(bucket: “BB”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)

|> filter(fn: (r) => r._measurement == “d” or r._measurement == “o”)
|> filter(fn: (r) => r._field == “dis” or r._field == “fw”)

//Exclude partner firmware, things I have tried:

|> filter(fn: (r) => r[“fw”] !~ /-p/) // Exclude devices where fw contains “-p” using regex
//|> filter(fn: (r) => r._field != “fw” or r._value !~ /-p/)
//|> filter(fn: (r) => not (r._field == “fw” and r._value =~ /-p$/))


(The rest of the code works great to give the alert that is managed by the variable dis. I just want to take out the devices with firmware -p.)

End Code.

Thanks.

Hi @Celeste

Maybe this?

import "regexp"  // not totally sure this is needed, but whatever

from(bucket: "BB")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r._measurement == "d" or r._measurement == "o")
|> filter(fn: (r) => r._field == "dis" or r._field == "fw")
|> filter(fn: (r) => 
    if r._field == "fw" then
        not regexp.matchRegexpString(r: /-p$/, v: r._value)
    else
        true
)

where

  • r: the regex pattern /-p$
  • v: the value to test (r._value)

Hi Grant, Thank you!
I get this error: [sse.dataQueryError] failed to execute query [A]: invalid: runtime error @11:6-16:4: filter: type conflict: string != float

fw is a string

Hi @Celeste

How about this?

import "regexp"

from(bucket: "BB")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "d" or r._measurement == "o")
  |> filter(fn: (r) => r._field == "dis" or r._field == "fw")
  |> filter(fn: (r) => 
    if r._field == "fw" then 
      not regexp.matchRegexpString(r: /-p$/, v: string(v: r._value))
    else 
      true
  )

Ooo. I think it is working. If I find otherwise I’ll let you know.
Thank you Grant!

1 Like

Hi Grant! This doesn’t work actually. Is there something different about the way flux functions inside a Grafana alert that I am missing? What is strange is I use “contains” to remove device id’s like this below and it works.

    // Exclude specific device_ids
    |> filter(fn: (r) => not contains(value: r.device_id, set: [
      "1",
      "2",
      "3",

But when I use contains to remove firmware versions, or other imported fields it doesn’t apply the filter.

Can you share the full query that works (uses “contains” to remove device IDs) and also the full query that does not work (uses “contains” to remove firmware versions)? For the latter, does Grafana or InfluxDB give you an error?

I presume the 2nd query does not work in Influx Data Explorer either? Usually if the query works in Influx Data Explorer, it will work in Grafana. I have found that putting the same query into Grafana Alerting sometimes presents some challenges.