Chapter 11 · Lesson 6 of 10
Monitoring and logs

Grafana dashboards for CPU, memory and disk

Grafana draws Prometheus data as graphs: add Prometheus as a data source, write one PromQL query per panel, add an instance variable and keep Grafana behind HTTPS with a changed admin password.

By the end you will

Build a dashboard with CPU, memory, disk and network panels, and lock Grafana down behind HTTPS.

Time
30 min
Level
Intermediate
Check
4 questions
You need
  • Prometheus scraping node_exporter
  • Grafana installed on a server or in Docker

Grafana turns Prometheus queries into graphs. Add Prometheus as a data source, create one panel per PromQL query (CPU busy percent, memory available, disk free percent, network traffic), add an instance variable so one dashboard serves every server, then change the default admin password and serve Grafana through a reverse proxy with HTTPS.

How it works#

Grafana stores no metrics. It sends your PromQL to Prometheus each time a panel refreshes and draws the result. A dashboard is a saved set of panels with a time range and optional variables. The flow is exporter, Prometheus, Grafana, as in Prometheus and node exporter basics. That means Grafana can be rebuilt freely as long as you keep the dashboard JSON, and that any panel is only as good as its query.

Step 1: Add the Prometheus data source#

Run Grafana on a private port first. With Docker Compose, bind it to localhost, as in centralize logs with Loki or rsyslog:

yaml
services:
  grafana:
    image: grafana/grafana:latest
    ports:
      - "127.0.0.1:3000:3000"
    volumes:
      - grafana-data:/var/lib/grafana
volumes:
  grafana-data:

Pin the image to a specific version for real use. Open Grafana, log in, and go to Connections, then Data sources, then Add data source, and choose Prometheus. Set the URL (http://127.0.0.1:9090 on the same host, or http://prometheus:9090 by Compose service name) and click Save and test.

On the first login Grafana asks for the admin password. Change it right away to a long unique one, and never leave the default. See also environment variables and secrets for passing it in without writing it in a file you commit.

Step 2: Create the panels#

Create a new dashboard and add a panel for each query. Set the visualization to Time series, and the unit shown in brackets.

CPU busy percent (unit Percent 0-100):

output
100 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)

Memory available percent:

output
node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes * 100

Disk free percent for the root filesystem:

output
node_filesystem_avail_bytes{mountpoint="/",fstype!~"tmpfs|overlay"}
  / node_filesystem_size_bytes{mountpoint="/",fstype!~"tmpfs|overlay"} * 100

Network received and sent, in bits per second (unit bits/sec):

output
rate(node_network_receive_bytes_total{device!~"lo|veth.*|docker.*"}[5m]) * 8
rate(node_network_transmit_bytes_total{device!~"lo|veth.*|docker.*"}[5m]) * 8

Set the panel title to what it answers, for example “Disk free %”, and add a threshold color so a low value is red. Memory available matters more than “free” memory, because Linux uses spare memory for cache, as explained in check disk, memory and CPU usage.

Step 3: Add an instance variable#

Open Dashboard settings, then Variables, and add a Query variable named instance with the query:

output
label_values(node_uname_info, instance)

Tick multi-value and include all. Then change every panel query to filter on it, for example:

output
node_memory_MemAvailable_bytes{instance=~"$instance"}
  / node_memory_MemTotal_bytes{instance=~"$instance"} * 100

Now one dashboard works for every server in your scrape list, and a drop-down at the top selects which one.

Step 4: Import a community dashboard#

Grafana has a public dashboard library, and node exporter dashboards are among the most shared. Use Dashboards, then New, then Import, enter the dashboard ID or paste its JSON, and pick your Prometheus data source. Treat it as a starting point. Open the panels, read the queries, and delete anything that confuses you. A dashboard with 40 panels you never look at hides the 4 that matter.

Step 5: Put Grafana behind HTTPS#

Do not publish port 3000 to the internet. Place a reverse proxy in front, terminate HTTPS there, and proxy to 127.0.0.1:3000. The proxy part is covered in nginx reverse proxy with certbot. Validate nginx with sudo nginx -t before reloading. Also consider limiting access by IP or a VPN, since a Grafana login page is a target for password guessing.

Check it worked#

  • The data source page says the query succeeded after Save and test.
  • Each panel shows a line, not “No data”.
  • CPU, memory and disk values match top, free -h and df -h on the server within a few percent.
  • The instance drop-down lists your servers and the panels change when you pick one.
  • The login page loads only over HTTPS and the default password no longer works.

Edge cases and gotchas#

  • Wrong memory metric. MemFree looks low on healthy servers. Use MemAvailable.
  • Pseudo filesystems pollute disk graphs. Exclude tmpfs, overlay and similar with fstype!~, or you will see dozens of lines.
  • Docker network devices. Virtual interfaces like veth... and docker0 double count traffic. Filter them out.
  • Time range and rate window. Zooming out to months with a 5 minute rate window works, but queries get heavier. Keep ranges reasonable.
  • Dashboards are not backed up automatically. The Grafana volume holds them. Export JSON and keep it in git, without secrets.
  • Anonymous access. Check that anonymous viewing is off unless you chose it, since graphs reveal hostnames and usage.

Troubleshooting#

SymptomLikely causeFix
Post "http://localhost:9090/api/v1/query": dial tcp: connect: connection refusedGrafana in a container cannot reach host localhostUse the Compose service name or the host’s private IP
Panel shows No dataQuery label or metric wrongRun the query in Prometheus first and fix names
Disk panel has many linesPseudo filesystems includedAdd `fstype!~“tmpfs
CPU always near 0 or 100Wrong mode labelUse mode="idle" and the subtraction form
Variable list emptyMetric absentUse label_values(up, instance) instead
Login loop behind the proxyWrong root URL or headersSet the root URL in Grafana’s config and forward headers
502 Bad Gateway from the proxyGrafana not runningSee fix 502 bad gateway
Dashboard lost after container recreationNo volumeMount a volume at /var/lib/grafana

Quick reference#

output
CPU busy %:   100 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
Memory avail: node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes * 100
Disk free %:  node_filesystem_avail_bytes{mountpoint="/"} / node_filesystem_size_bytes{mountpoint="/"} * 100
Variable:     label_values(node_uname_info, instance)

Next#

Turn these numbers into notifications in alerting without alert fatigue.

Check what you learned4 questions
1Which memory metric best shows memory you can still use?
2What does a dashboard variable named instance let you do?
3What should you do with the default Grafana admin login right after install?
4Why compute disk free as a percentage?

Frequently asked questions

How do I connect Grafana to Prometheus?

In Grafana open Connections, then Data sources, add Prometheus and enter its URL, for example http://127.0.0.1:9090 or the Compose service name. Save and test, and Grafana reports whether it can query.

What PromQL shows CPU busy percent?

100 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100). It subtracts the idle share from 100 per instance.

Should I build dashboards or import one?

Importing a community node exporter dashboard is a fast start. Read what each panel queries and remove what you do not need, then build your own for the checks you actually alert on.

How do I secure Grafana?

Change the default admin password on first login, put Grafana behind a reverse proxy with HTTPS, and do not publish port 3000 directly to the internet.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios