Monitoring CPU Usage
1. Top Command
The top
command provides a real-time view of system resource usage, including CPU usage.
top
- Press 1 within
top
to view the usage per CPU core (if you have multiple cores).
2. htop (Interactive Version of top)
htop
is a more user-friendly, colorized version of top
. You may need to install it first:
sudo apt update sudo apt install htop
Once installed, you can run:
htop
- You can use F6 to sort by CPU, memory, or other metrics.
3. mpstat (CPU Usage by Core)
To monitor the CPU usage on a per-core basis, you can use mpstat
from the sysstat
package.
First, install sysstat
if you haven’t already:
sudo apt update sudo apt install sysstat
Then, you can check CPU statistics:
mpstat -P ALL 1
- This command shows CPU usage for all cores every 1 second.
4. sar (Historical CPU Data)
sar
can also give you historical CPU data if sysstat
is installed.
To see CPU usage history:
sar -u 1 3
- This will show CPU utilization every second for 3 times.
5. Using pidstat
If you need to monitor CPU usage by process:
pidstat -u 1
This will show the CPU usage by process every 1 second.
Monitoring Disk Usage
1. df – Disk Usage
The df
command reports the amount of disk space used and available on the file system:
df -h
- The
-h
flag makes the output human-readable (e.g., GB, MB).
2. du – Disk Usage of Specific Directory
The du
command shows disk usage for files and directories. To view disk usage of a directory (and its subdirectories):
du -sh /path/to/directory
- The
-s
flag provides a summary for the specified directory. - The
-h
flag makes the output human-readable.
3. iostat (Disk I/O Statistics)
The iostat
command shows CPU and disk I/O statistics. You need the sysstat
package installed for this.
sudo apt install sysstat
Then, run:
iostat -xz 1
- This command provides detailed I/O statistics for each device, updating every 1 second.
4. smartctl (SMART Disk Health)
The smartctl
command can give you detailed health information about your disks using SMART (Self-Monitoring, Analysis, and Reporting Technology).
First, install smartmontools
:
sudo apt install smartmontools
Then, run:
sudo smartctl -a /dev/sda
- Replace
/dev/sda
with your actual disk identifier.
5. dstat (Real-time System Resource Monitoring)
dstat
is a versatile tool that shows various system resources in real-time, including disk and CPU usage.
Install it with:
sudo apt install dstat
Run it with:
dstat -cd
- This will show both CPU and disk usage in real-time.
Other Useful Disk & CPU Monitoring Tools
1. Monitoring Disk Performance with vmstat
You can use vmstat
to check system performance, including CPU and disk stats.
vmstat 1
- This will update every second, giving you an overview of CPU, memory, and I/O activity.
2. Monitoring Disk Usage with lsblk
The lsblk
command provides information about block devices and their mount points:
lsblk
- This will show a tree of all your disks, partitions, and their mount points.
Example: Monitoring CPU & Disk Together
You can also monitor both CPU and disk in real time using dstat
:
dstat -cd --disk-usage
This will show both CPU usage and disk usage in a real-time stream.
These are some of the basic and commonly used commands to monitor CPU and disk usage on an Ubuntu 22.04 server. You can use them individually or combine them depending on what you need to check (real-time vs. historical data).
Leave a Reply