Linux 环境使用 vmstat 分析系统状态


Linux 环境使用 vmstat 监控系统状态,并通过 gnuplot 画图。

vmstat

执行:

vmstat 1 3

每 1 秒采集一次数据,共采集 3 次。输出结果:

procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 3939680 421240 5657676    0    0    51    67  458   71  4  2 93  0  0
 0  0      0 3939916 421240 5657676    0    0     0     0 2935 9898  2  1 97  0  0
 1  0      0 3939916 421248 5657668    0    0     0   124 3246 10485  2  1 96  0  0
  • procs

    • r 等待 CPU 的进程数

    • b 不可中断休眠

  • memory

    • swpd 被换出到磁盘的块

    • free 空闲块数

    • buff 缓冲块数

    • cache 系统缓存块数

  • swap

    • si 从磁盘换入

    • so 换出到磁盘

  • io

    • bi 从设备读取块数

    • bo 从设备写出块数

  • system

    • in 每秒中断次数

    • cs 上下文切换次数

  • cpu

    • us 用户代码

    • sy 系统代码

    • id 空闲

    • wa 等待 IO

    • st

gnuplot 画图

数据

通过 vmstat 生成数据

vmstat 1 10 | grep -v r > vmstat.txt
  • 每 1 秒采集一次数据,采集 10 次

  • 排除掉掉包含 r 的行(第 1,2 行)

画图

vmstat.gnuplot
# set terminal qt persist
set terminal svg 1
set output 'vmstat.svg' 2

set title 'vmstat 1 10'
set xlabel "Time (sec)"

3
plot \
         'vmstat.txt' using 13 with lines linewidth 2 title 'CPU user load', \
         'vmstat.txt' using 14 with lines linewidth 2 title 'CPU system load', \
         'vmstat.txt' using 15 with lines linewidth 2 title 'CPU idle time', \
         'vmstat.txt' using 16 with lines linewidth 2 title 'CPU IO wait time'
1 生成 SVG
2 输出文件名
3 使用 13 到 16 列划线图

之后,执行命令:

gnuplot vmstat.gnuplot

生成图片:

vm stat