Sub topic 3
Building a Server Dashboard
This chapter assumes you have Prometheus scraping node_exporter, Grafana connected to Prometheus as a data source, and working knowledge of PromQL selectors and rate().
Panels and variables
Each Grafana panel runs a PromQL query. This query gives CPU use per server in percent:
100 * (1 - avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])))A dashboard variable is a dropdown that fills placeholders in your queries. Create a variable named server, type Query, with this query:
label_values(node_cpu_seconds_total, instance)Then filter panels with $server. The =~ operator matches a regex, so it also works when the dropdown allows multiple values:
100 * (1 - avg by (instance) (rate(node_cpu_seconds_total{instance=~"$server", mode="idle"}[5m])))Recording rules
A recording rule is a query that Prometheus evaluates on a schedule and stores as a new series. Dashboards and alerts then read the stored result instead of recalculating it.
groups:
- name: node-recording
rules:
- record: instance:node_cpu_utilisation:rate5m
expr: 1 - avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m]))Alerting rules
An alerting rule fires when its expression stays true for the for duration.
groups:
- name: node-alerts
rules:
- alert: InstanceDown
expr: up{job="node"} == 0
for: 5m
labels:
severity: critical
annotations:
summary: "{{ $labels.instance }} is down"Alertmanager routing
Alertmanager receives firing alerts from Prometheus. It groups, deduplicates, and sends them to receivers.
route:
receiver: team
group_by: ['alertname', 'instance']
group_wait: 30s
group_interval: 5m
repeat_interval: 4h
receivers:
- name: team
webhook_configs:
- url: http://localhost:5001/alertsStorage and retention
Prometheus stores data on local disk. Set how long it keeps data with a startup flag:
prometheus --storage.tsdb.retention.time=30dPlan disk size with this estimate: samples per second × retention seconds × about 2 bytes per sample. Add a safety margin of 20 to 30 percent.
Securing and scaling
Prometheus has no built-in user authentication. Bind it to localhost or place it behind a reverse proxy that enforces TLS and basic auth, and enable login on Grafana. To scale, split scrape targets across several Prometheus instances, then use federation or remote write for long-term storage.
Exercise
Exercise
A server has 200 samples per second and you want 30 days of retention. Using about 2 bytes per sample, how many gigabytes of disk do you need before the safety margin?
Show answer
200 × 2,592,000 seconds × 2 bytes ≈ 1,036,800,000 bytes, which is about 1.04 GB. Before the 20 to 30 percent margin, plan for roughly 1 GB.