简体   繁体   English

解析vmstat输出的有效方法

[英]efficient way to parse vmstat output

I'm trying to efficiently parse vmstat output preferably in awk or sed, it also should work on both linux and hp-ux. 我正在尝试有效地解析vmstat输出,最好使用awk或sed,它也应该在linux和hp-ux上都可以工作。 For example I would like to cut cpu idle % ("92" in this case) from the following output: 例如,我想从以下输出中剪切cpu空闲百分比(在这种情况下为“ 92”):

$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
11  0 385372 101696  61704 650716    0    1     5     9    6   12  5  2 92  0

unfortunately the vmstat output can differ on different linux distributions and hp-ux, also columns can vary in length and can be presented in other order. 不幸的是,在不同的Linux发行版和hp-ux上,vmstat输出可能会有所不同,列的长度也可能会有所不同,并且可以以其他顺序显示。

I tried to write some nice awk oneliner, but eventually ended with python solution: 我试图写一些不错的awk oneliner,但最终以python解决方案告终:

$ vmstat | python -c 'import sys; print dict(zip(*map(str.split, sys.stdin)[-2:])).get("id")'
92

Do you know better way to parse mentioned output, to get number values of desired column name? 您是否知道解析上述输出以获取所需列名的数字值的更好方法?

using awk you can do: 使用awk,您可以执行以下操作:

vmstat | awk '(NR==2){for(i=1;i<=NF;i++)if($i=="id"){getline; print $i}}'

This should get value of "id" column on Linux as well as on HP-UX or any other standard unix system. 在Linux以及HP-UX或任何其他标准的UNIX系统上,这应该获得“ id”列的值。

Tested on Linux, HP-UX and Solaris. 在Linux,HP-UX和Solaris上进行了测试。

$ vmstat | python -c 'import sys; print sys.stdin.readlines()[-1].split()[-2]'
95

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM