Linux sar Command
1. What is the sar command
sar (System Activity Reporter) is a powerful performance monitoring tool on Linux systems and is part of the sysstat toolkit. It can collect, report, and save various system activity information, including:
- CPU usage
- Memory utilization
- I/O activity
- Network statistics
- Process activity
- Device load, etc.
1.1 Advantages of the sar command
- Historical data analysis: View system status at any point in the past
- Comprehensive monitoring: Covers all key performance indicators of the system
- Low overhead: Data collection has minimal impact on system performance
- Automation: Can be configured to collect data automatically on a regular basis
2. Installation and basic configuration
2.1 Install the sysstat package
In most Linux distributions, the sar command needs to be obtained by installing the sysstat package:
# Ubuntu/Debian
sudo apt-get install sysstat
# CentOS/RHEL
sudo yum install sysstat
# Fedora
sudo dnf install sysstat
2.2 Enable data collection
After installation, you need to enable the data collection service:
Example
Edit the configuration file
sudo
vi
/
etc
/
default
/
sysstat
Change ENABLED="false" to
ENABLED
=
"true"
Restart the service
sudo
systemctl restart sysstat
By default, sar collects data every 10 minutes and saves it in the /var/log/sysstat/ directory.
3. Basic syntax and common parameters
3.1 Basic syntax format
sar [选项] [间隔时间] [次数]
3.2 Description of common parameters
| Parameters | Description |
|---|---|
| -A | Display all reports |
| -u | Display CPU utilization |
| -r | Show memory usage |
| -b | Display I/O and transfer rate statistics |
| -n DEV | Display network device statistics |
| -q | Display system load and queue length |
| -d | Display disk activity |
| -P ALL | Display statistics for each CPU |
| -s | Specify the start time |
| -e | Specify the end time |
| -f | Read data from the specified file |
4. Practical application examples
4.1 Monitor CPU usage in real time
Example
Refresh every 2 seconds, 5 times in total
sar
-u
2
5
Example output:
Linux 5.4.0-91-generic (hostname) 03/15/2023 _x86_64_ (4 CPU)
10:30:01 AM CPU %user %nice %system %iowait %steal %idle
10:30:03 AM all 5.12 0.00 1.02 0.51 0.00 93.35
10:30:05 AM all 6.23 0.00 1.34 0.23 0.00 92.20
4.2 View historical memory usage
Example
View today's memory usage
sar
-r
View data for a specified date (requires specifying a file)
sar
-r
-f
/
var
/
log
/
sysstat
/
sa15
Data for the 15th
4.3 Monitor disk I/O activity
Example
Monitor disk activity, refreshing every 1 second, 10 times in total
sar
-d
1
10
4.4 View network interface statistics
Example
Monitor network interface activity
sar
-n
DEV
1
5
5. Advanced usage and tips
5.1 Combine monitoring of multiple metrics
Example
Monitor CPU, memory, and disk at the same time
sar
-urdb
1
5
5.2 Generate reports for a specific time period
Example
View CPU usage from 9 AM to 10 AM
sar
-u
-s
09:00:00
-e
10
:00:00
5.3 Save output to a file
Example
Save monitoring results to a file
sar
-A
1
10
system_report.log
5.4 Monitor a specific CPU core
Example
Monitor CPU0 usage
sar
-P
0
1
5
6. Data interpretation guide
6.1 Interpreting CPU metrics
| Pointer | Meaning | Healthy range |
|---|---|---|
| %user | User-space CPU usage | <70% |
| %system | Kernel-space CPU usage | <30% |
| %iowait | CPU time waiting for I/O | <5% |
| %idle | CPU idle time | >20% |
6.2 Interpreting memory metrics
| Pointer | Meaning |
|---|---|
| kbmemfree | Free physical memory (KB) |
| kbmemused | Used physical memory (KB) |
| %memused | Memory usage rate |
| kbbuffers | Memory used by buffers (KB) |
| kbcached | Memory used by cache (KB) |
6.3 Interpreting disk metrics
| Pointer | Meaning |
|---|---|
| tps | Transfers per second |
| rd_sec/s | Sectors read per second |
| wr_sec/s | Sectors written per second |
| %util | Device utilization |
7. Common troubleshooting
7.1 Identifying CPU bottlenecks
If both the %user or %system Persistently above 80% may indicate:
- Compute-intensive applications
- Too many system calls
- Need to optimize code or add CPU resources
7.2 Determining insufficient memory
When the following occur at the same time, memory may be insufficient:
%memusedPersistently above 90%kbcachedValue is very low- Swap partition (
kbswpused) usage is high
7.3 Identifying I/O bottlenecks
%iowait high and disk %util high indicates:
- Disk I/O has become a bottleneck
- You may need a faster storage device
- or optimize I/O-intensive operations
Linux Command Reference