Nested variables in Grafana

Quite often you want to use one variable of Grafana as input in query to derive next variable value set. Think of a typical example, you have 10 host machines and each host has several network interfaces. And you want to display stats of each interface of selected host.

Now, when you select “host” variable on dashboard, you want “interface” variable values to be populated dynamically.

Here is the solution with an example to address this problem.

My influxdb, hostnwdata measurement, has host and interface tags and ifin and ifout fields.


> select * from hostnwdata
name: hostnwdata
time                           host        ifin ifout interface
----                           ----        ---- ----- ---------
2020-06-09T07:11:41.190329303Z 192.168.0.2 1    4     eth0
2020-06-09T07:12:04.23347367Z  192.168.0.2 0.4  1.5   eth1
2020-06-09T07:12:23.064462936Z 192.168.0.5 0.4  1.5   eth3
2020-06-09T07:12:36.719960026Z 192.168.0.5 0.6  3.5   eth4
2020-06-09T07:12:54.47216988Z  192.168.0.1 0.8  1.5   eth1
2020-06-09T07:13:24.670095494Z 192.168.0.5 0.6  5     eth1

To create “$HostIP” variable for dashboard, query is:


SHOW TAG VALUES  WITH KEY = "host"

Next, to create another variable “$Interface” based on “$HostIP”, query is:


SHOW TAG VALUES  WITH KEY = "interface" WHERE host =~ /^$HostIP$/

Now, whenever you select a host, you will see interfaces of selected host getting populated in “$Interface” variable on dashboard.