Grafana Flux Query

Hello All,

am using Grafana 11.4.0 and InfluxDB v2.7 with Telegraf. am trying to get interface information from Cisco IOS XR devices by using variables queries. I can get the source devices by using device variable but am not able to get interface information from devices. the snapshot is mentioned below for reference . thanks for your feedback.

Working query:

from**(bucket: "telegraf")
  |> range(start: -2m)
  // normalizing timestamp
  |> aggregateWindow(every: 1m, fn: last)
  |> filter(fn: (r) => r._measurement == "Cisco-IOS-XR-pfi-im-cmd-oper:interfaces/interface-xr/interface" )
  |> filter(fn: (r) => r._field == "state" )
  |> group()
  |> distinct(column: "source")

Not working query

import "regexp"
state=from(bucket: "telegraf")
  |> range(start: v.timeRangeStart, stop:v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "Cisco-IOS-XR-pfi-im-cmd-oper:interfaces/interface-xr/interface")
  |> filter(fn: (r) => r["_field"] == "state")
  |> filter(fn: (r) => r["source"] =="${device}")
  |> drop(columns: ["_measurement", "source"])
  |> last()

descriptions = from(bucket: "telegraf")
  |> range(start: v.timeRangeStart, stop:v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "Cisco-IOS-XR-pfi-im-cmd-oper:interfaces/interface-xr/interface")
  |> filter(fn: (r) => r["_field"] == "description")
  |> filter(fn: (r) => r["source"] =="${device}")
  |> drop(columns: ["_measurement", "source"])
  |> last()

union(tables:[state,descriptions])
   |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
   |> filter(fn: (r) => exists r["description"])
  // |> filter(fn: (r) => r["description"] =~ /.*VLAN.*/)
  |> drop(columns: ["description"])
  |> pivot(rowKey: ["_stop"], columnKey: ["interface_name"], valueColumn: "state")
|> drop(columns: ["_start","_stop","host","path","subscription"])
|> columns()

Regards


Is the query working when you use it in a regular panel? I find the the query panel in the variable page lacking feedback. I normally work in a regular panel until I get the output I expect before I try to use it in the variable page.

And bonus tip, you can reference one other variables when building a new variable. So you can avoid union by using Grafana variables as a “function call” to another flux query. Some instances that makes my queries a lot simpler and faster, and you can mix and match data sources that way. Mysql lookups etc.