Sub topic 12
Building a server dashboard
This chapter turns the PromQL queries from earlier chapters into a working Grafana dashboard for your Linux servers. It assumes the following:
- Grafana can reach Prometheus through a data source.
- Prometheus scrapes at least one
nodejob, and theupmetric shows those targets as healthy. - You can read the
rate()andavg byqueries from the previous chapter.
Dashboards, panels, and queries
A dashboard is a saved page of panels. A panel is one visualization backed by one or more queries. A query is a PromQL expression that Grafana sends to the Prometheus data source.
Grafana does not store metric data. Each time a panel renders, Grafana asks Prometheus for the data over the dashboard's time range. A panel that shows "No data" is therefore usually a query problem or a data source problem. Prometheus is rarely at fault.
For a time series panel, Grafana calls the Prometheus range query API. It passes a start time, an end time, and a step, and the step depends on the panel width and time range. Stat and gauge panels usually need only the latest value, so they use an instant query.
Decide what the dashboard answers
Start with questions an operator asks during an incident. Then pick a panel for each one.
| Question | Panel type | Unit |
|---|---|---|
| Is every server up? | Stat | none |
| How busy is each CPU? | Time series | Percent (0-100) |
| How much memory is in use? | Gauge or time series | Percent (0-100) |
| Which filesystems are filling up? | Bar gauge | Percent (0-100) |
| How long has each server been running? | Stat | Duration (seconds) |
| How much network traffic flows? | Time series | Bytes/sec |
Create the CPU panel
- Create a dashboard and choose Add visualization.
- Select the Prometheus data source.
- Switch the query editor to Code mode and enter the query.
- Choose the Time series visualization.
100 * (1 - avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[$__rate_interval])))The query matches the one from the previous chapter, with one change. It uses $__rate_interval instead of a fixed [5m]. Grafana computes this variable from the panel's step and the data source scrape interval, and it is never shorter than four scrape intervals. rate() needs at least two samples in its window. A fixed short window gives gaps when you zoom in, and a fixed long window hides spikes when you zoom out.
Next, set these options in the panel editor:
- Legend, Custom:
{{instance}}gives one readable name per server instead of the full label set. - Standard options, Unit: Percent (0-100). The query already multiplies by 100. If you drop the
100 *, choose Percent (0.0-1.0) instead. - Standard options, Min and Max: 0 and 100, so the axis does not rescale and make a quiet server look busy.
Memory, disk, uptime, and network panels
Add one panel per query. Use these expressions.
# Memory in use, in percent (Gauge or Time series)
100 * (1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes)
# Disk used per filesystem, in percent (Bar gauge)
100 * (1 - node_filesystem_avail_bytes{fstype!~"tmpfs|overlay"} / node_filesystem_size_bytes{fstype!~"tmpfs|overlay"})
# Uptime in seconds (Stat, unit Duration)
time() - node_boot_time_seconds
# Network receive throughput per server (Time series, unit Bytes/sec)
sum by (instance) (rate(node_network_receive_bytes_total{device!="lo"}[$__rate_interval]))
# Servers that are up (Stat)
count(up{job="node"} == 1)Some details in these queries matter:
MemAvailablecounts memory the kernel can reclaim, such as page cache. Using it avoids the false alarm you get from "free" memory on a healthy Linux host.- The disk query returns one series per mountpoint per server. Set the legend to
{{instance}} {{mountpoint}}so the bars can be told apart. device!="lo"excludes the loopback interface, which would otherwise inflate traffic totals.sum by (instance)adds all network devices on one server. Without it, you get one line per interface.count(up{job="node"} == 1)returns nothing when every target is down, because the filtered vector is empty. Grafana shows "No data" in that case. Use the Stat panel's Value mappings to display "0" or "ALL DOWN" when the value is missing.
Thresholds and colors
Thresholds change the color of a value when it crosses a limit. For the disk and memory panels, set a base color of green, yellow at 80, and red at 90. Choose Show thresholds as lines or filled regions so the limit is visible on time series panels. Thresholds only affect display. They do not send notifications. Alerting rules in Prometheus do that, and they come later in the course.
Instant versus range queries
Each query has a Type option under Query options: Range, Instant, or Both.
- Range returns a series of points and suits time series panels.
- Instant returns one value at the end of the time range. Use it for stat, gauge, bar gauge, and table panels.
- A stat panel on a range query still works, because it reduces the series with a Calculation such as Last or Mean. This transfers more data than needed.
For the bar gauge and the uptime stat, switch the query to Instant and set the calculation to Last.
Keep the dashboard in version control
A dashboard built by hand lives in Grafana's database. Export it as JSON from Dashboard settings, JSON Model, and commit it to a repository. Then provision it with a file provider, the same way you provisioned the data source.
apiVersion: 1
providers:
- name: servers
type: file
disableDeletion: false
updateIntervalSeconds: 30
allowUiUpdates: false
options:
path: /var/lib/grafana/dashboardsSave this file in /etc/grafana/provisioning/dashboards/, and put the exported JSON files in /var/lib/grafana/dashboards. Grafana rescans the folder every 30 seconds. With allowUiUpdates: false, edits made in the browser cannot be saved over the provisioned copy, so the file stays the source of truth.
Common problems
| Symptom | Likely cause | Fix |
|---|---|---|
| Panel shows "No data" | Wrong data source, or metric name typo | Run the same query in the Prometheus web UI. Check the data source with **Save & test**. |
| CPU shows 10000% or 1% | Unit and `100 *` do not match | Pick Percent (0-100) when you multiply by 100. |
| Lines have gaps when zoomed in | A fixed `rate()` window that is too short | Use `$__rate_interval`. |
| Dozens of legend entries | Query has no aggregation | Add `by (instance)` or filter with label matchers. |
| Graph is flat at 0 or 100 | Axis min and max are too tight, or the series is absent | Check the raw values in the **Table view** toggle. |
| Panel loads slowly | Wide time range with an expensive query | Save the query as a recording rule and query that instead. |
Exercise
Your memory panel uses the query 100 * (1 - nodememoryMemAvailablebytes / nodememoryMemTotalbytes), and the Y axis reads "9000%". The query itself is correct. What setting did you most likely get wrong, and how do you fix it?
Show answer
The panel unit is set to a fraction-based unit, Percent (0.0-1.0), but the query already multiplies by 100. Grafana multiplies by 100 again for display, so 90 becomes 9000%. Change the unit to Percent (0-100), or remove the "100 *" from the query and keep the 0.0-1.0 unit.
Exercise
Write a query for a bar gauge showing the percentage of disk space used on each real filesystem, and say which query type you would choose and why.
Show answer
Use 100 * (1 - nodefilesystemavailbytes{fstype!~"tmpfs|overlay"} / nodefilesystemsizebytes{fstype!~"tmpfs|overlay"}). Choose Instant. A bar gauge shows one current value per series, so a range query would send many points that are then reduced to one anyway.