How to Filter measurement metrics by Hostname

Hello guys,

I’m trying to perform the filtering of the “System_Up_Time” metric by Hostname, within the variables I have this configuration:

Up to this point I have the correct result, all hosts appear, but the problem is that I am not able to obtain the results according to the filtering of the selected host:

When I enter the hostname, I have the correct data, I would like to know if it is possible to make it work, when changing the Host, return the metric related to it.

Thank you

Try this:

r.host == $host

$host is the variable that you created before

Hello Fadjar

I tested your tip but it didn’t work, anyway I thank you for trying to help, I leave a capture of how I inserted the variable

inserting filter(fn: ® => r.host == “$host” does not generate errors but I believe that the query searches for the host with the name “$host” and the result is null

any other ideas are welcome

just to add, I am using the version Grafana v7.2.1 (72a6c64532) + Influxdb2 Version 2.0.0

Maybe you can check this example:

I didn’t much have experience using Influxdb

Unfortunately https://play.grafana.org/ for InfluxDB is created and linked with a database of Influx 1.5 or less if I am not mistaken, the queries are totally different, in any case I am still investigating, thank you again. :+1:

Hi @marcelrissardi, Have you fixed this issue? I have the same problem with this thread and I tried long time to fixed it but all failed.
Below is my step. ( Grafana7.3+Influxdb2.0-rc3)

from(bucket: "flower")
  |> range(start: v.timeRangeStart, stop:v.timeRangeStop)
  |> filter(fn: (r) =>
    r._measurement == "mem" and
    r._field == "used_percent"
  )

This query will show all serials of all hosts no matter I select one or more hosts because the query cmd doesn’t filter the host tag.

When I add a tag filter in the query cmd (add 'r.host == "${host}"'), the grafana has the right action. No matter which one host I selecte, it shows the right host data. But if I select more than one host, it shows no data (no error shows).

from(bucket: “flower”)
|> range(start: v.timeRangeStart, stop:v.timeRangeStop)
|> filter(fn: ® =>
r._measurement == “mem” and
r.host == “${host}” and
r._field == “used_percent”
)

If you have fixed your problem, can you share your steps ? Thank you very much.

The variable works in influxdb dashboard but doesn’t work in grafana dashboard.

Try this:

'r.host =~ "${host}"'

Because you use several values, it’s line IN in SQL.
If you use exact single value, not multiple value, you can use “==” ,for safety, better use “=~” instead

Hi @fadjar340,
Thank you for your reply. I also tried this .
When I add r.host =~ "${host}" the error msg shows "invalid: type error @5:15-5:30: expected regexp but found string".
When i add r.host =~ ${host}" the error msg shows "invalid: error @5:22-5:27: undefined identifier host2"

I know the syntax…

try this:

r.host =~ /^$host$/

Because it’s need regex…

The solution:

In the Grafana query to get the tag values ​​for the variable:

import "influxdata/influxdb/schema"
schema.tagValues(bucket: "flower", tag: "host")

In Grafana’s query:

from(bucket: "flower")
|> range(start: v.timeRangeStart, stop:v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "mem")
|> filter(fn: (r) => r["_field"] == "used_percent")
|> filter(fn: (r) => contains(value: r["host"], set: ${host:json}))

Where set: ${ host:json} is the name of the variable. Adding attribute :json you can select multiple values ​​in variable.

Regards !! :wave:

1 Like

Yes. contains works. Thank you.

hello is there any other option? I did as your recommendation and presents me with the following error: