R / Richie全部文章 ↑

Linux · 2 分钟阅读

dstat:全能资源统计

dstat 把 vmstat、iostat、ifstat 的功能糅在一起,输出整齐的彩色表格。top / sar 也能做类似的事,但 dstat 胜在一行看到所有维度。

2026 视角:dstat 维护已停更(最后一次大版本 2016 年),新项目建议用 htop + nmon + iostat -dx 组合,或直接上 Prometheus + Grafana。dstat 仍是个人开发机排查问题的好工具。

安装

# RHEL / Alma / Rocky
yum install -y dstat

# Debian / Ubuntu
apt install -y dstat

基本用法

dstat [options] [delay [count]]

delay 是刷新间隔秒数,count 是刷新次数。

常用参数

参数 含义
-c CPU 状态
-m 内存
-n 网络
-d 磁盘 I/O
-r I/O 请求
-s 交换分区
-t 加时间戳
--top-cpu CPU 占用最高的进程
--top-mem 内存占用最高的进程
--top-io I/O 最多的进程
--output file 输出到 CSV

实战例子

# CPU + 内存,每 2 秒一次,5 次
dstat -cm 2 5

# 网络 + 磁盘
dstat -dn 1

# 全量 + 时间戳
dstat -cdnmt 1

# 抓 10 秒数据落到 CSV
dstat --output /tmp/report.csv -cdn 1 10

# 看谁在吃 CPU
dstat -c --top-cpu 1

写一个监控脚本

#!/bin/bash
# /usr/local/bin/dstat_collect.sh
REPORT_DIR=/var/log/dstat
mkdir -p "$REPORT_DIR"
ts=$(date +%Y%m%d-%H%M%S)
dstat -tcmnd --output "$REPORT_DIR/$ts.csv" 1 60
# 30 天后清理
find "$REPORT_DIR" -name "*.csv" -mtime +30 -delete
# /etc/systemd/system/dstat-collect.service
[Unit]
Description=每分钟收集一次 dstat

[Service]
Type=oneshot
ExecStart=/usr/local/bin/dstat_collect.sh

# /etc/systemd/system/dstat-collect.timer
[Unit]
Description=每分钟执行

[Timer]
OnCalendar=*:0/1
Persistent=true

[Install]
WantedBy=timers.target
systemctl enable --now dstat-collect.timer

一些提示

  • 时间参数(delay count)要放在选项后
  • 选定的列越少越清晰,输出太多反而看不过来
  • CSV 输出留好日期,方便后续画图
  • 排查问题先 dstat -tcmnd 1 一眼看全局,再针对具体维度深入

参考