温馨提示×

cpustat如何监控Debian

小樊
42
2026-08-14 01:32:20
栏目: 智能运维

在 Debian 上用 cpustat 监控 CPU,一般指的是 sysstat 包里的 mpstat / sar,或者 GitHub 上的 cpustat 工具。下面把两种常见用法都说明一下,你可以按需要选择。


一、使用 sysstat 中的 cpustat(最常见)

1. 安装 sysstat

sudo apt update
sudo apt install sysstat

2. 启用统计(否则可能没数据)

sudo systemctl enable sysstat
sudo systemctl start sysstat

3. 使用 mpstat(推荐)

mpstat 是 sysstat 中用于 CPU 监控的工具,功能等同于“cpustat”。

查看所有 CPU

mpstat -P ALL

每 2 秒刷新一次

mpstat -P ALL 2

每 2 秒刷新一次,共 5 次

mpstat -P ALL 2 5

输出示例:

CPU    %usr   %nice    %sys   %iowait  %irq   %soft  %steal  %guest  %gnice   %idle
all    12.3    0.0     4.1     1.2     0.0    0.1    0.0     0.0     0.0     82.3

4. 使用 sar 查看历史 CPU

sar -u 1 5

查看某天的历史(如 2026-01-20):

sar -u -f /var/log/sysstat/sa20

二、使用 GitHub 的 cpustat(更直观)

如果你指的是 cpustat 单文件工具(如 rumpelsepp/cpustat):

1. 安装依赖

sudo apt install git make gcc

2. 编译安装

git clone https://github.com/rumpelsepp/cpustat.git
cd cpustat
make
sudo make install

3. 使用

cpustat

常见参数:

cpustat -i 1        # 每 1 秒刷新
cpustat -c 10       # 显示前 10 个进程
cpustat -n 20       # 显示 20 个采样

三、常见监控场景示例

1. 实时监控 CPU 使用率

mpstat -P ALL 1

2. 查看某个进程占用 CPU

top
# 或
htop

3. 写入日志,便于排查

mpstat -P ALL 1 60 > cpu.log

四、常见问题

1. 看不到数据?

  • 确认 sysstat 已启动
  • 等待几分钟后再查看

2. Debian 最小化安装没有 cpustat?

  • Debian 默认不带 cpustat 命令
  • mpstat / sar 即可

五、总结建议

需求 推荐
系统自带、稳定 mpstat / sar
实时、直观 cpustat(GitHub)
长期监控 sar + cron
服务器排查 mpstat -P ALL

如果你能贴一下 Debian 版本cat /etc/os-release)以及你是想 实时监控 / 历史分析 / 脚本采集,我可以给你更精确的方案。

0