温馨提示×

如何监控Debian swapper

小樊
32
2026-07-28 00:23:09
栏目: 智能运维

在 Debian 系统中,swapper 通常指的是 swap 空间(交换分区或交换文件) 的使用情况,而不是某个具体进程。下面从监控手段、常用命令、自动化监控、日志与告警几个方面系统说明。


一、快速查看当前 swap 使用情况

1️⃣ 查看 swap 总览

swapon --show

示例输出:

NAME      TYPE      SIZE USED PRIO
/swapfile file        2G 512M   -2

2️⃣ 查看内存 + swap 使用情况

free -h

关注:

  • Swap
  • used 是否持续增长

3️⃣ 实时查看 swap 使用

top
# 或
htop

top 中按 f,确认显示 SWAP 列。


二、查看哪些进程在使用 swap

1️⃣ 使用 smem(推荐)

sudo apt install smem
smem -t -k

查看:

  • Swap
  • 哪个进程占用最多 swap

2️⃣ 使用 awk 从 proc 统计

for pid in $(ls /proc | grep '^[0-9]'); do
  if [ -f /proc/$pid/status ]; then
    swap=$(grep VmSwap /proc/$pid/status | awk '{print $2}')
    if [ "$swap" != "0" ] && [ -n "$swap" ]; then
      name=$(cat /proc/$pid/comm)
      echo "$name ($pid): ${swap}kB"
    fi
  fi
done | sort -k3 -n

三、使用 vmstat / sar 进行性能监控

1️⃣ vmstat(实时)

vmstat 1

重点关注:

  • swpd:已用 swap
  • si / so:swap in / swap out
    ⚠️ 长期非零说明内存不足

2️⃣ sar(历史分析,推荐)

sudo apt install sysstat
sar -S 1 5

或查看历史:

sar -S

指标说明:

  • %swpused:swap 使用率
  • kbswpfree / kbswpused

四、使用 systemd 和 cgroup 监控(高级)

1️⃣ systemd 服务级别 swap 使用

systemctl status <service>

或:

cat /sys/fs/cgroup/system.slice/<service>.service/memory.swap.current

五、自动化监控与告警(生产推荐)

✅ Prometheus + node_exporter

node_memory_SwapTotal_bytes
node_memory_SwapFree_bytes

Grafana 示例告警条件:

(node_memory_SwapTotal_bytes - node_memory_SwapFree_bytes) / node_memory_SwapTotal_bytes > 0.8

✅ Zabbix / Nagios

监控项:

  • swap 使用率
  • swap in/out 速率
  • swap 使用趋势

六、日志与异常排查

1️⃣ 查看 OOM 相关日志

dmesg | grep -i oom
journalctl -k | grep -i oom

2️⃣ 查看 swap 异常

journalctl -k | grep -i swap

七、swap 监控最佳实践 ✅

✅ 监控重点:

  • swap 使用率(不要长期 > 30–50%)
  • swap in/out 是否频繁
  • 是否出现 OOM

✅ 调优建议:

cat /proc/sys/vm/swappiness
  • 默认 60,建议服务器设为 10–30:
sudo sysctl vm.swappiness=10

八、总结(速查表)

目的 命令
查看 swap 状态 swapon --show
内存 + swap free -h
实时性能 vmstat 1
历史分析 sar -S
进程级 swap smem -t -k
自动监控 Prometheus / Zabbix

如果你愿意,我可以:

  • ✅ 帮你写 swap 监控脚本
  • ✅ 给你 Prometheus + Grafana 的 dashboard JSON
  • ✅ 针对 数据库 / Docker / K8s 场景给出优化建议

只要告诉我你的使用场景即可。

0