Monitoring CPU, Memory, and Disk Usage with Prometheus and Grafana
This guide explains how to use Prometheus' irate function and Grafana to calculate and visualize CPU, memory, and disk usage percentages for each host, including the necessary PromQL queries and formulas in a monitoring setup.
1. CPU Usage
Start by calculating the per‑second rate for each CPU mode using PromQL's irate function on the node_cpu_seconds_total metric. Example query:
irate(node_cpu_seconds_total{job="node"}[5m])
To get an average per instance, wrap the irate call in an avg aggregation with a by (instance) clause:
avg(irate(node_cpu_seconds_total{job="node"}[5m])) by (instance)
To focus on idle time, add the mode="idle" label and compute the idle percentage:
avg(irate(node_cpu_seconds_total{job="node",mode="idle"}[5m])) by (instance) * 100
Subtract this from 100 to obtain the active CPU usage percentage:
100 - avg(irate(node_cpu_seconds_total{job="node",mode="idle"}[5m])) by (instance) * 100
The result is a metric per host showing the average CPU usage over a 5‑minute window.
2. Memory Usage
Identify the relevant node_memory metrics:
node_memory_MemTotal_bytes – total memory on the host
node_memory_MemFree_bytes – free memory
node_memory_Buffers_bytes_bytes – memory in buffers
node_memory_Cached_bytes_bytes – memory in page cache
All values are in bytes. Compute memory usage percentage with the following expression:
(node_memory_MemTotal_bytes-(node_memory_MemFree_bytes+node_memory_Cached_bytes+node_memory_Buffers_bytes)) / node_memory_MemTotal_bytes * 100
3. Disk Usage
For disk monitoring, use the node_filesystem metrics. The size metric node_filesystem_size_bytes reports the total capacity of each mounted filesystem. To calculate the used percentage for the root filesystem ( / ), apply a similar formula as for memory, adding a mountpoint="/" label:
(node_filesystem_size_bytes{mountpoint="/"} - node_filesystem_free_bytes{mountpoint="/"}) / node_filesystem_size_bytes{mountpoint="/"} * 100
Additional Notes
Grafana can be localized to Chinese and supports importing monitoring templates for services such as MySQL and Redis.
Related Articles
Two Ways to Deploy Prometheus
Deploy Prometheus + Grafana
Prometheus + Alertmanager Email Alert Configuration
Please share and follow the DevOps Operations Group
Practical DevOps Architecture
Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.