13.3 Host metrics — CPU, RAM, disk
Why: the problem
An Infosys project runs its billing app on three EC2 servers. One morning the app is slow but there are no errors. The developer says "the code is fine". The DBA says "the database is fine". Who is right? The answer is in host metrics: the numbers of the machine itself. CPU, memory, disk and network. If CPU is 100%, the machine is too busy. If memory is full, Linux starts swapping to disk and everything becomes slow. If the disk is 100% full, the app cannot write files and crashes. If disk IO wait is high, the disk is too slow. These four resources are like the four tyres of a car. One flat tyre and the whole car is slow. Before you blame code or the database, you check the host. That is why host metrics are the first thing every DevOps engineer reads.
What we will build
You will read host metrics in three ways. First, with the classic Linux commands top, vmstat, free, df and iostat, and you will learn what each column means. Second, you will create load on purpose with stress-ng and watch the numbers change, so you can recognise a busy CPU, full memory and a slow disk by face. Third, you will install the Amazon CloudWatch agent so that memory and disk, which EC2 does not send by default, also appear in CloudWatch.
What: every term in plain English
- Host — One machine: a laptop, a VM or an EC2 instance.
- CPU utilization — Percent of time the CPU is busy. 100% means no free time.
- Load average — How many tasks want the CPU, averaged over 1, 5 and 15 minutes. Compare it with the number of CPUs.
- Memory available — RAM that apps can still use. Linux uses free RAM as cache, so look at "available", not "free".
- Swap — Disk space used as slow extra RAM. Swap in use means memory pressure.
- Disk usage — Percent of the disk space used. At 100% writes fail.
- IO wait (wa) — Percent of time CPU waits for the disk. High wa means a slow or busy disk.
- Inodes — Slots for files. A disk can be "full" of tiny files even when space is free.
How: step by step
-
See live processes
top -b -n 1 | head -15What it does: One snapshot of CPU, memory and top processes.
Output
%Cpu(s): 3.1 us, 1.0 sy, 0.0 ni, 95.6 id, 0.3 wa MiB Mem : 3910.0 total, 820.4 free, 1210.2 used, 1879.4 buff/cache -
See CPU, memory and IO every second
vmstat 1 5What it does: Five lines, one per second.
Output
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 1 0 0 840120 60212 1860300 0 0 2 10 210 380 3 1 96 0 0 -
Count CPUs
nprocWhat it does: Load average must be compared with this number.
Output
2 -
See disk space and inodes
df -h / && df -i /What it does: Space and file slots on the root disk.
Output
/dev/xvda1 20G 14G 6.0G 70% / /dev/xvda1 1.3M 210K 1.1M 17% / -
See disk speed
sudo apt-get install -y sysstat && iostat -xz 1 3What it does: Per-disk utilization and wait time.
Output
Device r/s w/s r_await w_await %util xvda 0.4 3.1 0.52 1.10 0.6 -
See network
ip -s link show eth0What it does: Bytes and errors in and out.
Output
RX: bytes packets errors dropped 812345678 612345 0 0
Example 1: Read a healthy server with the classic commands
Stack. Ubuntu 24.04 or Amazon Linux 2023
Goal. Read a healthy server with the classic commands.
Commands
uptime
nproc
free -m
df -h /
vmstat 1 3
ps aux --sort=-%cpu | head -5
ps aux --sort=-%mem | head -5
What each line does
uptime— Load average for 1, 5, 15 minutes.nproc— Number of CPUs. Load higher than this means waiting.free -m— Memory in MB. Read the "available" column.df -h /— Disk used percent of the root disk.vmstat 1 3— Three lines. Read us, sy, id, wa on the right.ps aux --sort=-%cpu— The processes using most CPU first.
Green output
21:10:01 up 3 days, load average: 0.20, 0.25, 0.30
2
total used free shared buff/cache available
Mem: 3910 1210 820 12 1880 2440
/dev/xvda1 20G 14G 6.0G 70% /
r b swpd free ... us sy id wa st
0 0 0 840120 ... 2 1 97 0 0
USER PID %CPU %MEM COMMAND
mysql 812 2.1 12.0 /usr/sbin/mysqld
ec2-user 1530 1.0 3.2 node server.js
Expected result. Load 0.20 on 2 CPUs, 2.4 GB available, disk 70%, CPU 97% idle. This server is healthy. Save this as your "normal" picture.
Example 2: Create CPU, memory and disk load with stress-ng and watch it
Stack. Ubuntu 24.04, stress-ng, vmstat
Goal. Create CPU, memory and disk load with stress-ng and watch it.
Terminal 1: make load
sudo apt-get install -y stress-ng
# CPU: keep 2 CPUs busy for 60 seconds
stress-ng --cpu 2 --timeout 60s
# Memory: use 1 GB for 60 seconds
stress-ng --vm 1 --vm-bytes 1G --timeout 60s
# Disk: heavy writes for 60 seconds
stress-ng --hdd 1 --timeout 60s
Terminal 2: watch
vmstat 2
What each line does
--cpu 2— Starts 2 workers that keep the CPU busy.--vm 1 --vm-bytes 1G— One worker that grabs 1 GB of RAM.--hdd 1— One worker that writes and deletes files fast.--timeout 60s— Stops by itself after 60 seconds. Safe for labs.vmstat 2— Prints a new line every 2 seconds so you watch the change.
Green output
r b swpd free ... us sy id wa
0 0 0 840120 ... 2 1 97 0 normal
2 0 0 838900 ... 99 1 0 0 CPU test: us=99, id=0
1 0 0 210300 ... 30 10 60 0 memory test: free drops by 1 GB
1 2 0 205100 ... 5 20 40 35 disk test: wa=35, b=2 blocked
Expected result. Now you know each problem by face. CPU busy: us high, id near 0. Memory: free and available drop. Disk: wa and b go up.
Example 3: Send memory and disk metrics to CloudWatch with the CloudWatch agent
Stack. Amazon Linux 2023, CloudWatch agent
Goal. Send memory and disk metrics to CloudWatch with the CloudWatch agent.
Install on Amazon Linux 2023
sudo dnf install -y amazon-cloudwatch-agent
/opt/aws/amazon-cloudwatch-agent/etc/config.json
{
"agent": { "metrics_collection_interval": 60 },
"metrics": {
"namespace": "CWAgent",
"append_dimensions": { "InstanceId": "${aws:InstanceId}" },
"metrics_collected": {
"mem": { "measurement": ["mem_used_percent"] },
"disk": { "measurement": ["used_percent"], "resources": ["/"] },
"swap": { "measurement": ["swap_used_percent"] }
}
}
}
Start the agent
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl \
-a fetch-config -m ec2 -s \
-c file:/opt/aws/amazon-cloudwatch-agent/etc/config.json
sudo systemctl status amazon-cloudwatch-agent --no-pager
What each line does
metrics_collection_interval: 60— Collect every 60 seconds."namespace": "CWAgent"— The group name where your metrics appear in CloudWatch.${aws:InstanceId}— The agent fills in the instance id, so each server is separate.mem_used_percent— RAM used percent. EC2 does not send this without the agent."resources": ["/"]— Watch only the root disk.-a fetch-config -m ec2 -s— Load the config, in EC2 mode, and start the agent.
Green output
Active: active (running) since Sun 2026-10-25 21:20:03 IST
$ aws cloudwatch list-metrics --namespace CWAgent --output text
METRICS mem_used_percent CWAgent
DIMENSIONS InstanceId i-0abc1234def567890
METRICS disk_used_percent CWAgent
DIMENSIONS path /
Expected result. After two minutes memory and disk appear in CloudWatch under CWAgent. The instance needs an IAM role with the CloudWatchAgentServerPolicy.
Verify: did it work?
-
Is load okay?
echo "load $(cut -d" " -f1 /proc/loadavg) cpus $(nproc)"What it does: Compare the two numbers.
Output
load 0.20 cpus 2 -
Is anything swapping?
vmstat 1 3 | tail -1What it does: si and so columns must be 0.
Output
0 0 0 840120 60212 1860300 0 0 2 10 210 380 3 1 96 0 0
When it is red 1
No space left on device (but df -h shows 40% used)
Cause. The disk is out of inodes. Millions of tiny files, often old sessions or cache.
Fix, step by step
- Run df -i / and look at IUse%.
- Find the folder: sudo du --inodes -x / 2>/dev/null | sort -n | tail -5
- Delete the old files and add a cleanup job.
When it is red 2
stress-ng: command not found
Cause. The package is not installed.
Fix, step by step
- Ubuntu: sudo apt-get install -y stress-ng
- Amazon Linux: sudo dnf install -y stress-ng
- Run the test again.
When it is red 3
AccessDeniedException ... cloudwatch:PutMetricData in agent log
Cause. The EC2 role does not allow the agent to send metrics.
Fix, step by step
- Open IAM, find the instance role.
- Attach CloudWatchAgentServerPolicy.
- Restart: sudo systemctl restart amazon-cloudwatch-agent
Cleanup
-
Stop any running stress test
pkill stress-ngWhat it does: Ends the load test.
Output
(no output) -
Stop the agent on a lab box
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a stopWhat it does: Stops sending custom metrics, so no cost.
Output
(no output)
Ravindra Bagale's Tip
Before you blame the code, check the machine: CPU, RAM, disk, network. Four wheels — one puncture and the car is slow.
Code ला दोष देण्याआधी machine तपासा: CPU, RAM, disk, network. गाडीची चार चाके, एक जरी puncture असला तरी गाडी slow.
Code को दोष देने से पहले machine जाँचो: CPU, RAM, disk, network. गाड़ी के चार पहिये — एक भी पंचर हो तो गाड़ी slow.
Practice tasks
Easy Save your normal picture
Why: You can only see "bad" if you know "normal".
What to do: Save the output of Example 1 to normal.txt on each lab server.
- Run the commands.
- Redirect to a file with today's date.
Check: You have one baseline file per server.
Medium Find the top memory process
Why: During memory alerts you must find the culprit fast.
What to do: Write one command that prints the top 3 memory users.
- ps aux --sort=-%mem | head -4
Check: You see the biggest process first.
Hard CloudWatch alarm for memory
Why: Memory is a common silent killer on EC2.
What to do: Create an alarm on mem_used_percent above 85 for 10 minutes.
- Use the CWAgent namespace.
- Period 300, evaluation periods 2.
- Send to an SNS topic with your email.
Check: The alarm goes to ALARM during a stress-ng --vm test.
Easy Say host metrics in two lines
Why: If you can say it simply, you understand it. Juniors and interviewers both want the short version first.
What to do: Write two short lines about host metrics for a fresher who knows only Linux, Git and Docker basics.
- Write the problem it solves.
- Write the one command, query or config line you use for it.
- Read it aloud to a friend.
Check: A fresher understands it without asking a question.
Easy Keep a throwaway monitoring lab
Why: Practice needs a lab you can break. If it gets messy, you delete the containers and start again in one minute.
What to do: Run Node Exporter in Docker and read its metrics page.
- docker run -d --name ne -p 9100:9100 prom/node-exporter
- curl -s localhost:9100/metrics | grep node_load1
- docker rm -f ne
Check: You see a line that starts with node_load1 and a number.
Medium Do it on a second stack
Why: Monitoring ideas must work on Prometheus and on CloudWatch. Only the tool changes, not the thinking.
What to do: Repeat this lesson on a different stack than the one you used first.
- If you used Prometheus, try the CloudWatch console or AWS CLI.
- Keep the same question: is it up, is it fast, is it failing, is it full?
- Compare the two graphs side by side.
Check: Both tools answer the same question with a number and a graph.
Medium Break it on purpose
Why: You must know the red error by face. During an outage there is no time to read slowly.
What to do: Cause one error from the "When it is red" part of this lesson and fix it.
- Follow the error section.
- Copy the exact message into your notes.
- Write the fix in one line.
Check: You fixed it using only the steps, without searching the web.
Medium Write a one-page runbook
Why: At 2 a.m. nobody remembers PromQL. A short page saves the on-call person.
What to do: Write a one-page runbook for this lesson.
- Title, when the alert fires, the graph to open, the exact commands, how to check, how to undo.
Check: A teammate can follow it alone, without calling you.
Hard Explain it like an interview
Why: Interviewers want real stories, not definitions.
What to do: Record a 60 second answer about this lesson.
- Say the problem, the metric you watched, the number you saw and the fix.
Check: Your answer has a real metric name and a real number.
Hard Draw the flow top to bottom
Why: A drawing shows where a number is born and where it ends up.
What to do: Draw this lesson as boxes from top to bottom.
- Start at the server or app, go through the exporter, Prometheus or CloudWatch, then Grafana, then the alert on your phone.
Check: Each box matches a real command or file in this lesson.
Homework: try at home
Try at home
At home, run vmstat 2 on your laptop or a lab VM. Then open 20 browser tabs and close them. Write down how free memory and CPU idle changed. That is reading host metrics from real life.
Recap
- Host metrics are CPU, memory, disk and network.
- Compare load average with nproc.
- Read "available" memory, not "free".
- High wa means the disk is the problem.
- EC2 sends CPU free, but memory and disk need the CloudWatch agent.
FAQ
What is a normal CPU percent?
Steady 40 to 70% is fine for most servers. Long 90%+ means you need more CPU or less work.
Is buff/cache bad?
No. Linux uses spare RAM as cache and gives it back when apps need it.
Why does my server get slow when swap is used?
Disk is thousands of times slower than RAM.
What is steal time (st)?
On cloud VMs, time the hypervisor gave your CPU to another VM. Common on t2/t3 burst instances.
Do I need iostat?
Only when you suspect the disk. It shows await and %util per disk.
Interview questions
-
How do you check why a Linux server is slow?
uptime and nproc for load, top for the busy process, free for memory, df for disk, vmstat for wa and swap, iostat for disk.
-
What is load average?
The average number of tasks running or waiting for CPU over 1, 5 and 15 minutes.
-
What is the difference between free and available memory?
Free is unused. Available includes cache that Linux can give back to apps.
-
How do you send memory metrics from EC2?
Install the CloudWatch agent with a config that collects mem_used_percent and attach CloudWatchAgentServerPolicy.
-
What are inodes?
Slots for file metadata. When they run out, you cannot create files even with free space.
Quick quiz
Q1. Which column in vmstat shows disk wait?
wa.
Q2. Load average is 4 on a 2 CPU server. Is it okay?
No. Tasks are waiting for CPU.
Q3. Which free -m column should you read?
available.
Q4. Why can a disk be full at 40% used?
It is out of inodes.
Q5. Does EC2 send memory to CloudWatch by default?
No. You need the CloudWatch agent.
Did you understand? Let us try it once more.
Revision card
Interview card