天天看點

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,如需轉載請自行聯系原作者

繼續閱讀