經常使用系統分析監控工具

linux系統分析,平常分析主要有cpu、內存、磁盤I/O、網絡I/O、進程數分析等。html

經常使用的分析命令工具主要有如下幾種:node

1.sar

sar:system activity reportlinux

做爲一款linux中經常使用的分析的工具,其幾乎包含了全部的狀態分析ios

[root@iZ25b31bdp0Z ~]# sar -h
用法: sar [ 選項 ] [ <時間間隔> [ <次數> ] ]
主選項和報告:
    -b  I/O 和傳輸速率信息情況
    -B  分頁情況
    -d  塊設備情況
    -H  交換空間利用率
    -I { <中斷> | SUM | ALL | XALL }
        中斷信息情況
    -m { <關鍵詞> [,...] | ALL }
        電源管理統計信息
        關鍵字:
        CPU CPU 頻率
        FAN 風扇速度
\t\tFREQ\tCPU 平均時鐘頻率
        IN  輸入電壓
        TEMP    設備溫度
\t\tUSB\t鏈接的USB 設備
    -n { <關鍵詞> [,...] | ALL }
        網絡統計信息
        關鍵詞能夠是:
        DEV 網卡
        EDEV    網卡 (錯誤)
        NFS NFS 客戶端
        NFSD    NFS 服務器
        SOCK    Sockets (套接字)   (v4)
        IP  IP 流    (v4)
        EIP IP 流    (v4) (錯誤)
        ICMP    ICMP 流  (v4)
        EICMP   ICMP 流  (v4) (錯誤)
        TCP TCP 流   (v4)
        ETCP    TCP 流   (v4) (錯誤)
        UDP UDP 流   (v4)
        SOCK6   Sockets (套接字)   (v6)
        IP6 IP 流    (v6)
        EIP6    IP 流    (v6) (錯誤)
        ICMP6   ICMP 流  (v6)
        EICMP6  ICMP 流  (v6) (錯誤)
        UDP6    UDP 流   (v6)
    -q  隊列長度和平均負載
    -r  內存利用率
    -R  內存情況
    -S  交換空間利用率
    -u [ ALL ]
        CPU 利用率
    -v  Kernel table 情況
    -w  任務建立與系統轉換統計信息
    -W  交換信息
    -y  TTY 設備情況

sar對cpu的監控分析

[root@iZ25b31bdp0Z ~]# sar -u 1 3     ## 1表明間隔1秒,3表明取值3次
Linux 3.10.0-123.9.3.el7.x86_64 (iZ25b31bdp0Z)  2017年09月11日     _x86_64_    (4 CPU)

14時28分11秒     CPU     %user     %nice   %system   %iowait    %steal     %idle
14時28分12秒     all     15.96      0.00      8.23      0.75      0.00     75.06
14時28分13秒     all      3.27      0.00      8.31     16.37      0.00     72.04
14時28分14秒     all      7.30      0.00      4.28     35.77      0.00     52.64
平均時間:     all      8.87      0.00      6.95     17.57      0.00     66.61

其中參數分析以下:centos

CPU:all 表示統計信息爲全部 CPU 的平均值。
%user:顯示在用戶級別(application)運行使用 CPU 總時間的百分比。
%nice:顯示在用戶級別,用於nice操做,所佔用 CPU 總時間的百分比。
%system:在覈心級別(kernel)運行所使用 CPU 總時間的百分比。
%iowait:顯示用於等待I/O操做佔用 CPU 總時間的百分比。
%steal:管理程序(hypervisor)爲另外一個虛擬進程提供服務而等待虛擬 CPU 的百分比。
%idle:顯示 CPU 空閒時間佔用 CPU 總時間的百分比。

簡單分析:緩存

1.iowait太高,可能是磁盤I/O存在瓶頸,通常高於值高於50,就要查看相關進程了
2.idle太高,但系統反應慢,多是cpu在等待內存分配,可查看內存使用量
3.system太高,說明系統內核使用cpu很高,內核使用cpu進行進程的建立等,此時I/O頻繁,查看對應異常進程

sar對文件使用句柄等使用

[root@iZ25b31bdp0Z ~]# sar -v 1 3
Linux 3.10.0-123.9.3.el7.x86_64 (iZ25b31bdp0Z)  2017年09月11日     _x86_64_    (4 CPU)

14時38分35秒 dentunusd   file-nr  inode-nr    pty-nr
14時38分36秒    132654      3712    135650         8
14時38分37秒    132656      3680    135652         8
14時38分38秒    132658      3680    135654         8
平均時間:    132656      3691    135652         8

參數分析:服務器

dentunusd:目錄高速緩存中未被使用的條目數量
file-nr:文件句柄(file handle)的使用數量
inode-nr:索引節點句柄(inode handle)的使用數量
pty-nr:tty使用量,使用w也能夠查看

sar對磁盤I/O的分析

[root@iZ25b31bdp0Z ~]# sar -b 1 3
Linux 3.10.0-123.9.3.el7.x86_64 (iZ25b31bdp0Z)  2017年09月11日     _x86_64_    (4 CPU)

14時42分07秒       tps      rtps      wtps   bread/s   bwrtn/s
14時42分08秒      2.00      0.00      2.00      0.00     16.00
14時42分09秒      0.00      0.00      0.00      0.00      0.00
14時42分10秒      3.00      0.00      3.00      0.00    128.00
平均時間:      1.67      0.00      1.67      0.00     48.00

簡單參數分析以下:網絡

tps:每秒鐘物理設備的 I/O 傳輸總量
rtps:每秒鐘從物理設備讀入的數據總量
wtps:每秒鐘向物理設備寫入的數據總量
bread/s:每秒鐘從物理設備讀入的數據量,單位爲 塊/s
bwrtn/s:每秒鐘向物理設備寫入的數據量,單位爲 塊/s

sar對內存的分析

[root@iZ25b31bdp0Z ~]# sar -r 1 3
Linux 3.10.0-123.9.3.el7.x86_64 (iZ25b31bdp0Z)  2017年09月11日     _x86_64_    (4 CPU)

14時51分41秒 kbmemfree kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty
14時51分42秒    704800   2915388     80.53    302240    454448   4976160    137.46   2267792    368308       356
14時51分43秒    704224   2915964     80.55    302248    454444   4976128    137.45   2267984    368308       356
14時51分44秒    704444   2915744     80.54    302248    454448   4976128    137.45   2267856    368308       464
平均時間:    704489   2915699     80.54    302245    454447   4976139    137.46   2267877    368308       392

參數分析:app

kbmemfree:這個值和free命令中的free值基本一致,因此它不包括buffer和cache的空間.
kbmemused:這個值和free命令中的used值基本一致,因此它包括buffer和cache的空間.
%memused:這個值是kbmemused和內存總量(不包括swap)的一個百分比.
kbbuffers和kbcached:這兩個值就是free命令中的buffer和cache.
kbcommit:保證當前系統所須要的內存,即爲了確保不溢出而須要的內存(RAM+swap).
%commit:這個值是kbcommit與內存總量(包括swap)的一個百分比.

sar對網絡I/O的分析

[root@iZ25b31bdp0Z ~]# sar -n DEV 1 3
Linux 3.10.0-123.9.3.el7.x86_64 (iZ25b31bdp0Z)  2017年09月11日     _x86_64_    (4 CPU)

14時52分48秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
14時52分49秒      eth0     10.00     10.00      0.56      0.70      0.00      0.00      0.00
14時52分49秒      eth1      1.00      0.00      0.04      0.00      0.00      0.00      0.00
14時52分49秒        lo     10.00     10.00      0.88      0.88      0.00      0.00      0.00

14時52分49秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
14時52分50秒      eth0      5.00      5.00      0.28      0.35      0.00      0.00      0.00
14時52分50秒      eth1     10.00      7.00      0.39      9.58      0.00      0.00      0.00
14時52分50秒        lo     29.00     29.00      4.66      4.66      0.00      0.00      0.00

14時52分50秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
14時52分51秒      eth0      4.00      4.00      0.96      0.31      0.00      0.00      0.00
14時52分51秒      eth1      1.00      1.00      0.04      0.48      0.00      0.00      0.00
14時52分51秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00

平均時間:     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
平均時間:      eth0      6.33      6.33      0.60      0.45      0.00      0.00      0.00
平均時間:      eth1      4.00      2.67      0.16      3.35      0.00      0.00      0.00
平均時間:        lo     13.00     13.00      1.85      1.85      0.00      0.00      0.00

簡單參數分析:socket

IFACE:網絡接口的名字,如上圖,我服務器中主要有三個網卡,eth0內網,eth1外網,還有lo迴環
rxpck/s:每秒鐘接收的數據包
txpck/s:每秒鐘發送的數據包
rxbyt/s:每秒鐘接收的字節數,網絡I/O洲入主要查看此參數
txbyt/s:每秒鐘發送的字節數,網絡I/O流出主要查看此參數
rxcmp/s:每秒鐘接收的壓縮數據包
txcmp/s:每秒鐘發送的壓縮數據包
rxmcst/s:每秒鐘接收的多播數據包

sar查看歷史系統狀態

[root@iZ25b31bdp0Z ~]# sar -f /var/log/sa/sa01 
Linux 3.10.0-123.9.3.el7.x86_64 (iZ25b31bdp0Z)  2017年09月01日     _x86_64_    (4 CPU)

00時00分01秒     CPU     %user     %nice   %system   %iowait    %steal     %idle
00時10分01秒     all      0.49      0.01      0.41      0.13      0.03     98.93
00時20分01秒     all      0.49      0.01      0.41      0.12      0.03     98.94
00時30分01秒     all      0.50      0.01      0.41      0.17      0.03     98.88
00時40分01秒     all      0.47      0.01      0.41      0.10      0.03     98.97
00時50分01秒     all      0.48      0.01      0.41      0.12      0.03     98.96
01時00分01秒     all      0.48      0.01      0.40      0.15      0.03     98.92
01時10分01秒     all      0.48      0.01      0.40      0.12      0.03     98.95
01時20分01秒     all      0.47      0.01      0.41      0.07      0.03     99.01

sar默認保存一個月的系統報告,在其中能夠簡單的查看其中的狀態。

相關時長配置文件更改,可在/etc/sysconfig/sysstat調整history值
[root@iZ25b31bdp0Z ~]# grep ^[^#] /etc/sysconfig/sysstat HISTORY=28 COMPRESSAFTER=31 SADC_OPTIONS="-S DISK"

固然sar只能提供簡單的歷史數據,相關詳細的系統狀態值,仍是得去zabbix中查看系統狀態值


2.dstat

dstat沒有sar那麼繁雜的參數選定,基本使用只有dstat連續滾動,或者使用dstat 1 10這樣次數限定的顯示,不過centos7上面纔會自帶,centos6上面並無加入

[root@iZ25b31bdp0Z ~]# dstat 
You did not select any stats, using -cdngy by default.
----total-cpu-usage---- -dsk/total- -net/total- ---paging-- ---system--
usr sys idl wai hiq siq| read  writ| recv  send|  in   out | int   csw 
  1   1  98   1   0   0| 143k  251k|   0     0 |   0     0 | 688   954 
  0   0  99   0   0   0|   0     0 |3372B   28k|   0     0 | 668   910 
  0   0  99   0   0   0|   0    80k|1257B 3302B|   0     0 | 489   732 
  1   0  99   0   0   0|   0     0 | 775B 9720B|   0     0 | 565   830 
  0   1  98   1   0   0| 120k   80k| 780B 3983B|   0     0 | 558   836 
  0   1  99   0   0   0|   0     0 |1189B   10k|   0     0 | 496   731

其中相比於top而言,在網絡上面有recv和send的區分,也不至於像sar那樣過於細分,能夠很方便的顯示

最重要的,它是彩色顯示!


3.iftop

iftop是一款專門用來顯示網絡I/O的工具,其相對於dstat而言,其能夠顯示相關進程的網絡I/O使用,在epel源中,默認沒有安裝

經常使用的命令

[root@iZ25b31bdp0Z ~]# iftop -n -P -o 2s -i eth1

參數簡單講解:

-n:不對主機名進行反解,直接顯示ip
-i:選擇對應的網卡,不選擇默認顯示第一個網卡,eth0爲個人內網網卡,eth1才爲外網網卡
-P:顯示進程端口
-o 2s: 2秒的統計結果,進行排序


4.nethogs

與iftop相似的一款工具,能夠顯示程序詳細的進程網絡I/O。不過由於命令難記,因此本人通常逐漸使用iftop進行了替代

使用時方便,不須要使用其它的附帶參數


5.free

free -h爲內存使用量查看,由於使用人不少,因此不過多介紹

[root@iZ25b31bdp0Z ~]# free -h
             total       used       free     shared    buffers     cached
Mem:          3.5G       2.6G       833M       248M       194M       118M
-/+ buffers/cache:       2.3G       1.1G
Swap:           0B         0B         0B

6.iostat

用於查看cpu和磁盤I/O的工具,相比於sar而言,其優勢在於能夠顯示每塊盤的I/O情況,其中-x選項使用頻率挺高,能夠查看util的值,若是util值太高,說明該磁盤io繁忙,不少監控系統會監控此選項值

[root@10-110-151-75 ~]# iostat -x 1 10
Linux 2.6.32-926.573.26.1.letv.el6.x86_64 (10-110-151-75)   01/31/2018  _x86_64_    (6 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
          26.42    0.00    3.85    2.41    0.00   67.32

Device:         rrqm/s   wrqm/s     r/s     w/s   rsec/s   wsec/s avgrq-sz avgqu-sz   await  svctm  %util
sda               0.03     1.55    0.61    1.36    22.44    23.32    23.31     0.01    3.11   1.07   0.21
sdb               0.53     0.27    6.93    6.85  2230.21  2502.06   343.57     0.22   16.10   0.80   1.10
sdc               0.52     0.27    6.38    5.99  1946.43  2102.32   327.34     0.21   16.64   0.78   0.97
sdd               0.52     0.26    6.91    5.97  2230.60  2091.13   335.64     0.20   15.50   0.84   1.08
sde               0.49     0.27    6.79    6.00  2180.22  2108.09   335.43     0.21   16.53   0.84   1.07
sdf               0.53     0.27    6.21    6.01  1826.62  2112.35   322.40     0.21   16.81   0.76   0.92
sdg               0.49     0.27    6.83    5.98  2164.34  2093.05   332.56     0.20   15.85   0.83   1.06
sdh               0.50     0.27    6.74    5.98  2153.15  2092.00   333.82     0.21   16.14   0.85   1.08
sdi               0.51     0.27    6.90    5.99  2200.24  2103.99   333.95     0.21   16.09   0.83   1.08
sdj               0.54     0.27    6.97    5.97  2251.68  2091.70   335.60     0.21   16.13   0.85   1.10
sdk               0.61     0.42    6.87    5.82  2214.42  2081.00   338.41     0.20   15.45  17.41  22.10
sdl               0.49     0.26    6.19    6.12  1739.91  2116.46   313.28     0.22   17.82   0.84   1.03
sdm               0.50     0.26    6.20    6.01  1845.52  2092.76   322.60     0.21   17.08   0.79   0.96

7.netstat

netstat主要用於查看本機端口使用,網絡狀態鏈接數

1.查看端口使用netstat -tnl

[root@iZ25b31bdp0Z ~]# netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:953           0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:15770         0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:188             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:8800            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:55203         0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:33060         0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:3690            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:718             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:81              0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:178             0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:8181          0.0.0.0:*               LISTEN     
tcp        0      0 101.200.215.232:53      0.0.0.0:*               LISTEN     
tcp        0      0 10.44.182.73:53         0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:53            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:8088            0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN     
tcp6       0      0 :::21                   :::*                    LISTEN

2.查看網絡鏈接數
netstat -n |grep 'WORD'

通常使用爲查看tcp鏈接最多的ip

經常使用命令:

[root@iZ25b31bdp0Z ~]# netstat -na|grep ESTABLISHED |awk '{print $5}' | awk -F: '{ip[$1]++}END{for (i in ip){print ip[i],i}}' | sort -nr
166 118.192.170.71
42 123.207.167.163
9 114.242.249.229
8 61.148.244.65
8 183.198.197.154

或者直接使用sortuniq來進行獲取

[root@iZ25b31bdp0Z ~]# netstat -na|grep ESTABLISHED |awk '{print $5}' | awk -F: '{print $1}' | sort -n | uniq -c | sort -nr
    165 118.192.170.71
     42 123.207.167.163
      9 114.242.249.229
      8 61.148.244.65
      8 183.198.197.154

8.ss

ss,man的解釋中說的是another utility to investigate sockets,可是我以爲其能夠是netstat的替代者,至少在端口占用這方面,其少了鏈接數等查看,可是在程序佔用端口方面卻直觀不少。

主要使用命令有

[root@wlinux conf.d]# ss -tnl
State      Recv-Q Send-Q             Local Address:Port                            Peer Address:Port              
LISTEN     0      128                    127.0.0.1:9000                                       *:*                  
LISTEN     0      128                            *:80                                         *:*                  
LISTEN     0      128                            *:50998                                      *:*                  
LISTEN     0      80                            :::3306                                      :::*

其能夠更直觀的顯示端口,同時加上-p參數,能夠顯示佔用對應端口的程序


9.ps

ps:snapshot of the current processes.顯示當時運行進程

ps通常有兩種風格,一種是BSD風格,一種是NUIX風格

兩種經常使用的命令有

ps auxps -ef

固然ps中還有其它的參數,可是以這二者組合居多

[root@wlinux conf.d]# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Sep01 ?        00:00:17 /usr/lib/systemd/systemd --system --deserialize 20
root         2     0  0 Sep01 ?        00:00:00 [kthreadd]
root         3     2  0 Sep01 ?        00:00:00 [ksoftirqd/0]
如下略
[root@wlinux conf.d]# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.3 125120  3352 ?        Ss   Sep01   0:17 /usr/lib/systemd/systemd --system --deserialize 20
root         2  0.0  0.0      0     0 ?        S    Sep01   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    Sep01   0:00 [ksoftirqd/0]
如下略

通常相比而言,本人使用ps aux多於ps -ef,前者相比於後者,能夠更好的看出資源的使用量,不事後面能夠直接看出ppid,不過ps中使用ps ax -o user,pid,ppid,stat,command加入-o的選項,加上ppid的參數,也是能顯示ppid值的。

10.top,glance,hop

這三者均也是經常使用工具,特別是top,其相關解釋能夠查看top命令使用

相關文章
相關標籤/搜索