Grafana and InfluxDB: Using template variable as query field (if variable, then..)

I’m currently implementing a new Icinga2 installation and am now using Grafana on top of InfluxDB to show the graphs (replacing the outdated PNP4Naigos and Nagiosgraph). So far everything works. The dashboard is based on the Icinga2 template (https://grafana.com/dashboards/381).

I went one step further and wanted to add a custom template variable to dynamically Enable/Disable threshold viewing. I figured I add another custom variable (in “Templating”), naming the variable $thresholds and give two choices as values: “No,Yes”.

The variable selection shows up as I needed it to on top of the dashboard.

A threshold query (e.g. warning) in the metrics of a graph looks like this:

SELECT mean("warn") FROM "nrpe_check_load" WHERE ("metric" = 'load1' AND "hostname" =~ /^$hostname$/) AND $timeFilter GROUP BY time($interval) fill(null)

Now I want to use the $thresholds variable in this query. To be precise: I only want the query to be true (executed) when the $thresholds value is set to “Yes”.

I tried it this way:

SELECT mean("warn") FROM "nrpe_check_load" WHERE ("metric" = 'load1' AND "hostname" =~ /^$hostname$/) AND $thresholds = "Yes" AND $timeFilter GROUP BY time($interval) fill(null)

But without success. Is it even possible to use a variable as a field? Or maybe there’s a different approach to achieve this?

I went through the documentation but couldn’t find examples using variables in the query not being used as values (unless I missed it).

Thx in advance for any suggestions.

I have the same issue here… but I also need to insert this variable in the middle of the word. eg:

SELECT mean(“M1_U1_Speed”) -> works

but how can I use a variable instead of U1:
SELECT mean("M1_Motor_Speed") (for example) I also tried [[Motor]] and /^Motor/ and some other combinations.

PS.: InfluxDB

[UPDATE]
Solved it with the sintax: /^M1_[[Motor]]_Speed$/

You might be able to use a kludge similar to what I’m using, but I’d really like the Grafana UI for InfluxQL to be able to make a metric be conditional directly on a variable value. That should be easy enough; presumably any quasi-tag-name enclosed in single quotes would be not a field, but a constant, so additional quotes wouldn’t be wrapped around it.

The kludge that I’m using involves picking an impossible value (here shown by a variable, where typically a constant like “foobar” would be clearer) for one of the tags. For tag “mytag”, assert that it is not equal to (getting the single quotes right): [[ImpossibleValue]]’ AND ‘[[ShowMyTag]]’ = 'true