centos8平台使用vmstat监控系统

一,vmstat的用途和特点:

vmstat 是一个常用的系统性能分析工具,主要用来分析系统的内存使用情况,也常用来分析 CPU 上下文切换和中断的次数。

相对于 iostat 来说,vmstat 可以同时提供 CPU、内存和 I/O 的使用情况。

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

         对应的源码可以访问这里获取: https://github.com/liuhongdi/

 说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,vmstat所属的包

1,查看所属的包

[root@blog ~]# whereis vmstat
vmstat: /usr/bin/vmstat /usr/share/man/man8/vmstat.8.gz

[root@blog ~]# rpm -qf /usr/bin/vmstat
procps-ng-3.3.15-1.el8.x86_64

2,默认vmstat应该已安装到了centos系统中,

   如果找不到命令,可以用yum安装:

[root@blog ~]# yum install procps-ng

三,查看vmstat的版本和帮助

1,查看版本

[root@blog ~]# vmstat --version
vmstat from procps-ng 3.3.15

2,查看帮助

[root@blog ~]# vmstat --help
Usage:
 vmstat [options] [delay [count]]

Options:
 -a, --active           active/inactive memory
 -f, --forks            number of forks since boot
 -m, --slabs            slabinfo
 -n, --one-header       do not redisplay header
 -s, --stats            event counter statistics
 -d, --disk             disk statistics
 -D, --disk-sum         summarize disk statistics
 -p, --partition <dev>  partition specific statistics
 -S, --unit <char>      define display unit
 -w, --wide             wide output
 -t, --timestamp        show timestamp
 -h, --help     display this help and exit
 -V, --version  output version information and exit

For more details see vmstat(8).

四,例子一:查看默认的输出信息

1,# 每隔5秒输出1组数据

[root@blog ~]# vmstat 5
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 1435780   2668 13109308    0    0     0     1    1    1  0  0 100  0  0
 0  0      0 1435712   2668 13109324    0    0     0     0 1533 1786  0  0 100  0  0
 0  0      0 1435712   2668 13109324    0    0     0     0 1562 1826  0  0 100  0  0

2,输出数据的说明:

procs:

r(Running or Runnable):就绪队列的长度,也就是正在运行和等待 CPU 的进程数。

说明:如果等待运行的进程数越多,意味着CPU非常繁忙

b(Blocked):处于不可中断睡眠状态的进程数。意味着被阻塞的进程的数量

system:

cs(context switch)是每秒上下文切换的次数。

in(interrupt)则是每秒中断的次数。

memory:

swpd: 使用的虚拟内存的大小

free:    空闲的物理内存的大小

buff:    用做buffer的内存大小

cache:   用做cache的内存大小

swap

si: 从磁盘交换到内存的交换页数量,单位:KB/秒

    Amount of memory swapped in from disk 

 so: 从内存交换到磁盘的交换页数量,单位:KB/秒

     Amount of memory swapped to disk (/s).

io

 bi: 每秒从块设备接收到的块数,单位:块/秒 也就是读块设备

      Blocks received from a block device (blocks/s).

bo: 每秒发送到块设备的块数,单位:块/秒  也就是写块设备。

       Blocks sent to a block device (blocks/s).

cpu:

       us: 用户CPU时间(非内核进程占用时间)(单位为百分比)

             us的值比较高时,说明用户进程消耗的CPU时间多

       sy: 系统使用的CPU时间(单位为百分比)

           sy的值高时,说明系统内核消耗的CPU资源多 

       id: 空闲的CPU的时间(百分比)

       wa: 等待IO的CPU时间

             wait越大则机器io性能就越差。说明IO等待比较严重 

       st: 代表当系统运行在虚拟机中的时候,被其他虚拟机占用的 CPU 时间。

             Time stolen from a virtual machine

五,例子二:查看活跃和非活跃内存

说明:-a 显示活跃和非活跃内存,所显示的内容除增加inact和active

[root@blog ~]# vmstat -a
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free  inact active   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 1426684 10660696 3032584    0    0     0     1    1    1  0  0 100  0  0

输出字段的说明:

inact:表示非活跃内存,也就是不常访问的内存,有可能会被系统回收。

active: 表示活跃内存,也就是最近使用过的内存,一般不会被系统回收。

六,例子三:统计磁盘的读/写信息

说明:-d参数:统计磁盘信息

[root@blog ~]$ vmstat -d
disk- ------------reads------------ ------------writes----------- -----IO------
       total merged sectors      ms  total merged sectors      ms    cur    sec
vda    17722     24 1261334   16251 1699722 146934 54970224 2269699      0    412
vdb      152      0    6576     107      0      0       0       0      0      0

 说明:数据来自/proc/diskstats

七,例子四:查看slab信息:

说明:-m参数:统计slab信息

[root@blog ~]# vmstat -m
Cache                       Num  Total   Size  Pages
nf_conntrack                300    300    320     25
xfs_dqtrx                     0      0    528     31
xfs_dquot                     0      0    504     16
xfs_rui_item                  0      0    696     23
xfs_rud_item                 92     92    176     23
xfs_inode                 95230 100480   1024     16
xfs_efd_item                198    234    440     18
xfs_buf_item                240    300    272     30
xfs_trans                   561    561    232     17 

...

说明:数据来自 /proc/slabinfo

八,例子五:查看内存统计信息:

说明: -s参数:统计内存信息

[sysop@blog ~]$ vmstat -s
     15723788 K total memory
      1180776 K used memory
      3029904 K active memory
     10660692 K inactive memory 

...

说明:数据来自/proc/meminfo

九,例子六:显示从系统启动至今的fork数量

说明: -f参数:统计fork的数量

[root@blog ~]$ vmstat -f
       403762 forks

说明:linux下创建进程的系统调用是fork

十,查看centos版本 

[root@blog ~]# cat /etc/redhat-release
CentOS Linux release 8.0.1905 (Core)