天天看点

awk正则匹配和awk命令统计某程序的CPU总的利用率

有时候我们需要统计系统中某一个程序的CPU利用率来做监控,而这个程序可能有多个进程或者多个线程,我们可以先匹配到该程序的所有进程或者线程,然后计算所有线程或者进程的CPU利用率的和,从而算出该程序的CPU总的利用率。

下面我们以nginx程序为例。

1

2

<code>root@localhost:</code><code># ps aux | grep nginx |grep -v grep |awk '{sum+=$3;}END{print sum}'</code>

<code>0</code>

如果要统计mysql所有进程的CPU利用率,则将nginx换成mysql即可。

上面的命令用到了awk编程。

awk '{sum+=$3;}END{print sum}'

这里是自定义一个变量sum,并且使sum=$sum+$3。最后计算sum的结果。

上面的命令使用grep来匹配nginx关键字,其实也可以直接使用awk的PATTERN用法来匹配。

语法为:awk [option] '/PATTERN/ {ACTION}'  /path/to/file

<code>root@localhost:~</code><code>/shell_test</code><code># ps aux  |awk '/nginx/ {sum+=$3;}END{print sum}'      </code>

但是这个命令有一点问题,就是awk这条命令本身也会被模式匹配到。看下面的命令:

3

4

5

6

7

8

<code>root@localhost:~</code><code># ps aux  |awk '/nginx/ {print }'      </code>

<code>root      4969  0.0  0.0  65248  1372 ?        Ss   May26   0:00 nginx: master process </code><code>/usr/sbin/nginx</code> <code>-c </code><code>/etc/nginx/nginx</code><code>.conf</code>

<code>nginx     4970  0.0  0.1  65788  2192 ?        S    May26   0:00 nginx: worker process                   </code>

<code>nginx     4971  0.0  0.1  65788  1940 ?        S    May26   0:06 nginx: worker process                   </code>

<code>nginx     4972  0.0  0.1  65788  1940 ?        S    May26   0:06 nginx: worker process                   </code>

<code>nginx     4973  0.0  0.1  65788  1940 ?        S    May26   0:06 nginx: worker process                   </code>

<code>nginx     4974  0.0  0.0  65472  1740 ?        S    May26   0:00 nginx: cache manager process            </code>

<code>root     18698  0.0  0.0 105948  1064 pts</code><code>/0</code>    <code>R+   17:15   0:00 </code><code>awk</code> <code>/nginx/</code> <code>{print }</code>

最后一条是awk命令本身,需要去掉。可以用awk的模糊匹配或者精确匹配进行过滤

模糊匹配

<code>'$11 ~ </code><code>/nginx/</code><code>’</code>

<code>root@localhost:~</code><code># ps aux |awk '$11 ~ /nginx/ {print}'</code>

<code>root     11766  0.0  0.0  65248  1248 ?        Ss   10:42   0:00 nginx: master process </code><code>/usr/sbin/nginx</code> <code>-c </code><code>/etc/nginx/nginx</code><code>.conf</code>

<code>nginx    11767  0.0  0.0  65788  1872 ?        S    10:42   0:00 nginx: worker process                   </code>

<code>nginx    11768  0.0  0.1  65788  1936 ?        S    10:42   0:00 nginx: worker process                   </code>

<code>nginx    11770  0.0  0.1  65788  1936 ?        S    10:42   0:00 nginx: worker process                   </code>

<code>nginx    11771  0.0  0.1  65788  1936 ?        S    10:42   0:00 nginx: worker process                   </code>

<code>nginx    11772  0.0  0.0  65472  1736 ?        S    10:42   0:00 nginx: cache manager process            </code>

<code>nginx    11773  0.0  0.0  65472  1636 ?        S    10:42   0:00 nginx: cache loader process</code>

精确匹配:

<code>'$11==</code><code>"nginx:"</code><code>’</code>

<code>root@localhost:~</code><code># ps aux |awk '$11=="nginx:"{print}'</code>

<code>root     11766  0.0  0.0  65248  1376 ?        Ss   10:42   0:00 nginx: master process </code><code>/usr/sbin/nginx</code> <code>-c </code><code>/etc/nginx/nginx</code><code>.conf</code>

<code>nginx    11767  0.0  0.1  65788  1936 ?        S    10:42   0:00 nginx: worker process                   </code>

<code>nginx    11772  0.0  0.0  65472  1736 ?        S    10:42   0:00 nginx: cache manager process</code>

反向匹配:

<code>'$11 !~ /nginx/'</code>

大小写匹配

 awk '/[zZ]eng/'  filename     #匹配含有zeng 或是Zeng的字符串

在awk中使用条件操作符

&lt;     小于        &gt;=    大于等于

&lt; =    小于等于    ~    匹配正则表达式

==    等于        !~    不匹配正则表达式

!=    不等于        

本文转自 曾哥最爱 51CTO博客,原文链接:http://blog.51cto.com/zengestudy/1784258,如需转载请自行联系原作者

继续阅读