Sub topic 13
Using Variables to Switch Between Servers
This chapter assumes you can create Grafana panels, use PromQL selectors, and connect Grafana to a Prometheus data source. You also need several servers with an instance label, such as server1.example.com:9100.
A Grafana variable is a named value that viewers can change. Use a variable for the server selector so one dashboard can display different servers instead of requiring one dashboard per server.
Create an instance variable
Open the dashboard, select Dashboard settings, then Variables, and choose Add variable. Configure these values:
- Name:
instance - Type:
Query - Data source: Prometheus
- Query:
label_values(node_cpu_seconds_total, instance) - Multi-value: disabled at first
- Include All option: disabled at first
The query asks Prometheus for every distinct value of the instance label on the selected metric. Grafana places the results in a drop-down list.
Use the variable in a panel query with a label matcher:
100 * (
1 - avg by (instance) (
rate(node_cpu_seconds_total{mode="idle", instance="$instance"}[5m])
)
)When a viewer selects server1.example.com:9100, Grafana substitutes that value for $instance before sending the query to Prometheus.
Support multiple servers
Enable Multi-value when viewers need to compare servers. Enable Include All option when they should also be able to select every server.
A multi-value variable must use a regular-expression matcher, written with =~, rather than an exact matcher, written with =:
100 * (
1 - avg by (instance) (
rate(node_cpu_seconds_total{
mode="idle",
instance=~"$instance"
}[5m])
)
)Grafana expands a selection such as server1 and server2 into a regular expression that matches both values. The All choice expands to a pattern matching every available instance.
Use the same variable in memory and disk panels:
100 * (
1 - node_memory_MemAvailable_bytes{instance=~"$instance"}
/ node_memory_MemTotal_bytes{instance=~"$instance"}
)For disk usage, match both sides of the division with the same filter. Otherwise Prometheus may divide unrelated series or return no result.
100 * (
1 -
node_filesystem_avail_bytes{
instance=~"$instance",
fstype!~"tmpfs|overlay"
}
/
node_filesystem_size_bytes{
instance=~"$instance",
fstype!~"tmpfs|overlay"
}
)Common errors
An empty panel often means the variable value does not match the label value. Check the variable's preview values and inspect the query in Grafana's query inspector.
A query using $instance with multi-selection usually returns no data because = expects one exact value. Change it to =~.
Avoid variables with very broad queries on high-cardinality labels. A high-cardinality label has many distinct values, which increases dashboard query cost. Prefer stable labels such as instance, job, or environment.
Exercise
Why must a panel use instance=~"$instance" when the dashboard variable supports multiple server selections?
Show answer
The =~ matcher accepts the regular expression Grafana generates for multiple selected values or the All option. The = matcher expects one exact value.