Linux dstat command
dstat is a powerful Linux system monitoring tool that displays system resource usage in real time, including key metrics such as CPU, memory, disk, and network. dstat is designed to replace traditional tools such as vmstat, iostat, and netstat, providing a unified monitoring interface.
Main Features of dstat
- Multi-function Integration: integrates the functions of multiple system monitoring tools
- Real-time Display: can continuously refresh and display system status
- Customizability: allows users to select specific metrics to monitor
- Color Output: uses color display by default, making it easy to identify abnormal values
- CSV Export: supports exporting monitoring data to CSV format
dstat Basic Syntax
The basic command format of dstat is as follows:
dstat [选项] [间隔时间] [执行次数]
Common parameters
| Parameters | Description |
|---|---|
-c |
Show CPU usage |
-d |
Show disk read/write activity |
-m |
Show memory usage |
-n |
Show network status |
-l |
Show system load |
-p |
Show process status |
-s |
Show swap usage |
-t |
Show timestamps |
--output |
Save output to a CSV file |
--top-cpu |
Show the processes consuming the most CPU |
--top-mem |
Show the processes consuming the most memory |
Common Monitoring Scenario Examples
1. Basic System Monitoring
dstat -cdlmnpsy
This command displays:
- CPU usage
- Disk read/write
- System load
- Memory Usage
- Network status
- Process status
- Swap usage
2. Monitor Specific Resources
Example
graph TD
A[Start monitoring] --> B{Select monitoring item}
B -->|CPU| C[dstat -c]
B -->|Memory| D[dstat -m]
B -->|Disk| E[dstat -d]
B -->|Network| F[dstat -n]
3. Monitoring with Timestamps
dstat -t -cdm
Example output:
----system---- ----total-cpu-usage---- -dsk/total- ---memory-usage----
time |usr sys idl wai hiq siq| read writ| used buff cach free
12-05 14:30:01| 2 1 96 0 0 1| 12k 25k| 3.2G 1.1G 5.6G 2.4G
12-05 14:30:02| 3 1 95 0 0 1| 24k 18k| 3.2G 1.1G 5.6G 2.4G
4. Save Monitoring Results to a File
dstat -cdm --output /tmp/dstat_output.csv 5 10
This command will:
- Monitor CPU, disk, and memory
- Refresh every 5 seconds
- Execute 10 times in total
- Save the results to /tmp/dstat_output.csv
Advanced usage
1. Monitor a Specific Process
dstat --top-cpu --top-mem
2. Customize Monitoring Items
dstat -c -d -n -N eth0,total
This command monitors:
- CPU usage
- Disk activity
- Network traffic (specific interface eth0 and total)
3. Combine Plugins
dstat supports a variety of plugins, which can be separated by commas:
dstat --time,proc,disk,net,tcp,load,sys
Frequently Asked Questions
1. What is the difference between dstat and the top command?
| Feature | dstat | top |
|---|---|---|
| Display Mode | Table form | List form |
| Monitoring Scope | System-wide | Mainly processes |
| Refresh Mode | Customizable interval | Fixed interval |
| Data export | Supports CSV | Not supported |
2. How to install dstat?
On most Linux distributions, it can be installed via the package manager:
Example
Ubuntu/Debian
sudo
apt-get install
dstat
CentOS/RHEL
sudo
yum install
dstat
Fedora
sudo
dnf
install
dstat
3. How to interpret dstat's output?
- CPU section: usr (user space), sys (system space), idl (idle), wai (IO wait)
- Memory section: used, buff (buffers), cach (cache), free
- Disk section: read, writ (write), usually in KB/s
Best Practice Recommendations
- Long-term Monitoring: for servers, it is recommended to use the
--outputparameter to save the monitoring data - Problem diagnosis: when performance problems occur, use
dstat -tcdmnto quickly view the usage of each resource - Benchmarking: before and after system tuning, use dstat to record performance data for comparison
- Custom Views: create your own dstat monitoring combinations based on actual needs and save them as aliases for convenience
Summary
dstat is a powerful tool for Linux system administrators and developers; it provides comprehensive system monitoring capabilities and is highly customizable. By mastering dstat, you can:
- Quickly identify system performance bottlenecks
- Monitor usage trends of key resources
- Collect performance data for analysis
- Replace multiple single-function monitoring tools
Readers are encouraged to try different combinations of parameters in real work and find the monitoring solution best suited to their work scenarios.
Linux Command Reference