Linux系統監控神器--Collectl

譯者: icybreaker發表於2014-04-10

系統資源監控

為使系統良好運轉,Linux系統管理員經常需要監測cpu,記憶體,磁碟,網路等系統資訊。Linux上已有iotop,top,free,htop,sar等豐富的常規工具來實現監測功能。今天讓我們走進Collectl來了解這個集測試/監控/分析系統效能為一體的Linux工具。

Collectl作為一個輕量級的監控工具,在同類工具中是功能最全的。使用者可監測不同的複雜系統矩陣值,並可保留資料以做之後的分析。不同於其他只用來監測特定系統引數的工具,Collectl可以同時監測不同的變數,並以合適的方式記錄它們。

摘自Collectl官網 ...

不同於或聚焦於一小組統計資料、採用唯一輸出方式,或採用迭代、作為守護程式執行的大部分監測工具,collectl可以同時全部實現。使用者可選擇各種子系統中的任一系統來監測包括記憶體,CPU,磁碟,索引節點,無線頻寬,lustre,記憶體,網路,網路檔案系統,程式,二次型,slabs,套介面及TCP等資訊。

深入學習前讓我們先看以下命令。

$ collectl
waiting for 1 second sample...
#<--------CPU--------><----------Disks-----------><----------Network---------->
#cpu sys inter  ctxsw KBRead  Reads KBWrit Writes   KBIn  PktIn  KBOut  PktOut 
   0   0   864   1772      0      0      0      0      0      1      0       0 
   5   2  1338   2734      0      0      8      2      0      0      0       1 
   1   0  1222   2647      0      0     92      3      0      2      0       1 
   1   0   763   1722      0      0     80      3      0      1      0       2

CPU使用率,磁碟輸入輸出和網路活動以每秒為單位來載入。對可以理解這些資料的人來說這些資訊很容易讀懂。這項列表在給定的時間間隔裡會持續增加,並可直接形成一項檔案跟蹤日誌。collectl工具提供了各種命令來對這些資料進行記錄,查詢及做其他處理。

安裝collectl工具

對Ubuntu/Debian使用者來說Collectl是預設資源,所以使用apt命令便可獲取。

$ sudo apt-get install collectl

Fedora/CentOS使用者使用yum命令便可獲取。

$ yum install collectl

使用

必要的瞭解 - Collectl子系統

子系統是可檢測到的不同系統資源型別。像CPU,記憶體,頻寬等等都可構成一個子系統。只執行collectl命令將以批處理模式輸出CPU,磁碟和網路子系統資訊,我們在上文已看到相關內容。

從操作說明可以看到,Collectl可以識別以下子系統。

總計子系統

b - buddy info (記憶體片段)
c - CPU
d - Disk
f - NFS V3 Data
i - Inode and File System
j - Interrupts
l - Lustre
m - Memory
n - Networks
s - Sockets
t - TCP
x - Interconnect
y - Slabs (system object caches)

細節子系統

這是一系列的詳細資訊,大部分情況下都會由相應的總計資料衍生出來。目前“環境變數”和“程式”2類沒有相應的總計資料。如果有3個磁碟,選擇-sd,將只會看到3個磁碟組合出的單一資訊。如果選擇-sD,將會分別顯示各磁碟資訊。

C - CPU
D - Disk
E - Environmental data (fan, power, temp),  via ipmitool
F - NFS Data
J - Interrupts
L - Lustre OST detail OR client Filesystem detail
M - Memory node data, which is also known as numa data
N - Networks
T - 65 TCP counters only available in plot format
X - Interconnect
Y - Slabs (system object caches)
Z - Processes

使用“-s”來監測特定的子系統並向其新增子系統的識別項。現在讓我們舉幾個例子。

1. 監測cpu使用率

用“-sc”實現CPU總使用率的監測

$ collectl -sc
waiting for 1 second sample...
#<--------CPU-------->
#cpu sys inter  ctxsw 
   3   0  1800   3729 
   3   0  1767   3599

使用“-C”觀察各個單獨的CPU使用情況。結果將會輸出多行,分別對應一個CPU。

   $ collectl -sC
waiting for 1 second sample...

# SINGLE CPU STATISTICS

#   Cpu  User Nice  Sys Wait IRQ  Soft Steal Idle
      0     3    0    0    0    0    0     0   96
      1     3    0    0    0    0    0     0   96
      2     2    0    0    0    0    0     0   97
      3     1    0    0    0    0    0     0   98
      0     2    0    0    0    0    0     0   97
      1     2    0    2    0    0    0     0   95
      2     1    0    0    0    0    0     0   98
      3     4    0    1    0    0    0     0   95

如果需要,可以同時使用C和c來獲取單項CPU監測資料和彙總資料。

2. 記憶體監測

使用m子系統檢視記憶體。

$ collectl -sm
waiting for 1 second sample...
#<-----------Memory----------->
#Free Buff Cach Inac Slab  Map 
   2G 220M   1G   1G 210M   3G 
   2G 220M   1G   1G 210M   3G 
   2G 220M   1G   1G 210M   3G

不難解釋。 M用來檢視更多記憶體的詳細資訊。

$ collectl -sM
waiting for 1 second sample...

# MEMORY STATISTICS 

# Node    Total     Used     Free     Slab   Mapped     Anon   Locked    Inact Hit%
     0    7975M    5939M    2036M  215720K  372184K        0    6652K    1434M    0
     0    7975M    5939M    2036M  215720K  372072K        0    6652K    1433M    0

這類似於免費報告嗎?

3. 檢視磁碟使用情況

d和D可以檢視磁碟使用的概況和詳細情況。

$ collectl -sd
waiting for 1 second sample...
#<----------Disks----------->
#KBRead  Reads KBWrit Writes 
      4      1    136     24 
      0      0     80     13

$ collectl -sD
waiting for 1 second sample...

# DISK STATISTICS (/sec)
#          <---------reads---------><---------writes---------><--------averages--------> Pct
#Name       KBytes Merged  IOs Size  KBytes Merged  IOs Size  RWSize  QLen  Wait SvcTim Util
sda              0      0    0    0       0      0    0    0       0     0     0      0    0
sda              0      0    0    0       0      0    0    0       0     0     0      0    0
sda              1      0    2    1      17      1    5    3       2     2     6      2    1
sda              0      0    0    0      92     11    5   18      18     1    12     12    5

“--verbose”命令可以用來檢視附加資訊。和D命令不同,它將概況擴充套件,包含了更多資訊。

$ collectl -sd --verbose

4. 同時報告多系統情況

如果想要同時得到CPU,記憶體和磁碟報告,那麼同時使用子命令組合來實現。

$ collectl -scmd
waiting for 1 second sample...
#<--------CPU--------><-----------Memory-----------><----------Disks----------->
#cpu sys inter  ctxsw Free Buff Cach Inac Slab  Map KBRead  Reads KBWrit Writes 
   4   0  2187   4334   1G 221M   1G   1G 210M   3G      0      0      0      0 
   3   0  1896   4065   1G 221M   1G   1G 210M   3G      0      0     20      5

5. 顯示統計時間

若要將每行的監測資訊和時間一同顯示,使用T選項。透過在“-o”引數來指定。

$ collectl -scmd -oT
waiting for 1 second sample...
#         <--------CPU--------><-----------Memory-----------><----------Disks----------->
#Time     cpu sys inter  ctxsw Free Buff Cach Inac Slab  Map KBRead  Reads KBWrit Writes 
12:03:05    3   0  1961   4013   1G 225M   1G   1G 212M   3G      0      0      0      0 
12:03:06    3   0  1884   3810   1G 225M   1G   1G 212M   3G      0      0      0      0 
12:03:07    3   0  2011   4060   1G 225M   1G   1G 212M   3G      0      0      0      0

使用“-oTm”可將時間換成毫秒顯示。

6. 改變樣本計數

collectl報告的每行都是一份快照或樣本,它通常設定定期間隔如1秒來取樣。i可用來設定時間間隔,c用來設定樣本計數。

$ collectl -c1 -sm
waiting for 1 second sample...
#<-----------Memory----------->
#Free Buff Cach Inac Slab  Map 
   1G 261M   1G   1G 228M   3G

使用i命令可改變更新時間間隔。

$ collectl -sm -i2
waiting for 2 second sample...
#<-----------Memory----------->
#Free Buff Cach Inac Slab  Map 
   1G 261M   1G   1G 229M   3G

上面的命令設定每2秒收集一次記憶體資訊。

7. 像iotop一樣使用collectl

top命令將collectl像iostat/top工具一樣進行智慧統計。列表會持續更新,且可以使用不同欄位實現排序。

$ collectl --top iokb

輸出如下

# TOP PROCESSES sorted by iokb (counters are /sec) 09:44:57
# PID  User     PR  PPID THRD S   VSZ   RSS CP  SysT  UsrT Pct  AccuTime  RKB  WKB MajF MinF Command
 3104  enlighte 20  2683    3 S  938M   33M  0  0.00  0.00   0  00:09.16    0    4    0    0 /usr/bin/ktorrent 
    1  root     20     0    0 S   26M    3M  2  0.00  0.00   0  00:01.30    0    0    0    0 /sbin/init 
    2  root     20     0    0 S     0     0  3  0.00  0.00   0  00:00.00    0    0    0    0 kthreadd 
    3  root     20     2    0 S     0     0  0  0.00  0.00   0  00:00.02    0    0    0    0 ksoftirqd/0 
    4  root     20     2    0 S     0     0  0  0.00  0.00   0  00:00.00    0    0    0    0 kworker/0:0 
    5  root      0     2    0 S     0     0  0  0.00  0.00   0  00:00.00    0    0    0    0 kworker/0:0H 
    7  root     RT     2    0 S     0     0  0  0.00  0.00   0  00:00.08    0    0    0    0 migration/0 
    8  root     20     2    0 S     0     0  2  0.00  0.00   0  00:00.00    0    0    0    0 rcu_bh 
    9  root     20     2    0 S     0     0  0  0.00  0.00   0  00:00.00    0    0    0    0 rcuob/0

上面的輸出很像top命令,並且它以磁碟數量降序排列程式。

若只想顯示上面的5項程式,可用以下命令實現

$ collectl --top iokb,5

若想學習上面的列表裡哪些欄位可以排序,使用如下命令

$ collectl --showtopopts

下面是應用於程式或資料的最高排序型別列表。某些情況下你可能會使用某一欄位進行排序,但它並非顯示的一部分。

TOP PROCESS SORT FIELDS
程式排序欄位

Memory
  vsz    virtual memory
  rss    resident (physical) memory

Time
  syst   system time
  usrt   user time
  time   total time
  accum  accumulated time

I/O
  rkb    KB read
  wkb    KB written
  iokb   total I/O KB

  rkbc   KB read from pagecache
  wkbc   KB written to pagecache
  iokbc  total pagecacge I/O
  ioall  total I/O KB (iokb+iokbc)

  rsys   read system calls
  wsys   write system calls
  iosys  total system calls

  iocncl Cancelled write bytes

Page Faults
  majf   major page faults
  minf   minor page faults
  flt    total page faults

Context Switches
  vctx   volunary context switches
  nctx   non-voluntary context switches

Miscellaneous (best when used with --procfilt)
  cpu    cpu number
  pid    process pid
  thread total process threads (not counting main)

TOP SLAB SORT FIELDS

  numobj    total number of slab objects
  actobj    active slab objects
  objsize   sizes of slab objects
  numslab   number of slabs
  objslab   number of objects in a slab
  totsize   total memory sizes taken by slabs
  totchg    change in memory sizes
  totpct    percent change in memory sizes
  name      slab names

8. 像top一樣使用collectl

為使collectl像top,我們只需以CPU使用率排序輸出程式。

$ collectl --top

輸出如下

# TOP PROCESSES sorted by time (counters are /sec) 14:08:46
# PID  User     PR  PPID THRD S   VSZ   RSS CP  SysT  UsrT Pct  AccuTime  RKB  WKB MajF MinF Command
 9471  enlighte 20  9102    0 R   63M   22M  3  0.03  0.10  13  00:00.81    0    0    0    3 /usr/bin/perl 
 3076  enlighte 20  2683    2 S  521M   40M  2  0.00  0.03   3  00:55.14    0    0    0    2 /usr/bin/yakuake 
 3877  enlighte 20  3356   41 S    1G  218M  1  0.00  0.03   3  10:10.50    0    0    0    0 /opt/google/chrome/chrome 
 4625  enlighte 20  2895   36 S    1G  241M  2  0.00  0.02   2  08:24.39    0    0    0   12 /usr/lib/firefox/firefox 
 5638  enlighte 20  3356    3 S    1G  265M  1  0.00  0.02   2  09:55.04    0    0    0    2 /opt/google/chrome/chrome 
 1186  root     20  1152    4 S  502M   76M  0  0.00  0.01   1  03:02.96    0    0    0    0 /usr/bin/X 
 1334  www-data 20  1329    0 S   87M    1M  2  0.00  0.01   1  00:00.85    0    0    0    0 nginx:

上面的命令也可用於顯示子系統資訊。

$ collectl --top -scm

9. 像ps一樣列出程式

為像ps命令一樣列出所有程式且沒有後續更新,用“c”命令讓其計數至1. $ collectl -c1 -sZ -i:1

上面的命令將會列出類似“ps -e”命令的所有程式。“procfilt”用於從所有程式中過濾出特定的程式資訊。“procopts”用於指定另一組微調程式列表顯示的命令。

10. 像vmstat一樣使用collectl

Collectl有內建命令來完成像vmstat一樣的功能。

$ collectl --vmstat
waiting for 1 second sample...
#procs ---------------memory (KB)--------------- --swaps-- -----io---- --system-- ----cpu-----
# r  b   swpd   free   buff  cache  inact active   si   so    bi    bo   in    cs us sy  id wa
  1  0      0  1733M   242M  1922M  1137M   710M    0    0     0   108 1982  3918  2  0  95  1
  1  0      0  1733M   242M  1922M  1137M   710M    0    0     0     0 1906  3886  1  0  98  0
  1  0      0  1733M   242M  1922M  1137M   710M    0    0     0     0 1739  3480  3  0  96  0

11. 子系統的詳細資訊

下面的命令以一秒為間隔,統計5次CPU資訊並和時間一起顯示出詳細資訊(冗長)。

$ collectl -sc -c5 -i1 --verbose -oT
waiting for 1 second sample...

# CPU SUMMARY (INTR, CTXSW & PROC /sec)
#Time      User  Nice   Sys  Wait   IRQ  Soft Steal  Idle  CPUs  Intr  Ctxsw  Proc  RunQ   Run   Avg1  Avg5 Avg15 RunT BlkT
14:22:10     11     0     0     0     0     0     0    87     4  1312   2691     0   866     1   0.78  0.86  0.78    1    0
14:22:11     15     0     0     0     0     0     0    84     4  1283   2496     0   866     1   0.78  0.86  0.78    1    0
14:22:12     17     0     0     0     0     0     0    82     4  1342   2658     0   866     0   0.78  0.86  0.78    0    0
14:22:13     15     0     0     0     0     0     0    84     4  1241   2429     0   866     1   0.78  0.86  0.78    1    0
14:22:14     11     0     0     0     0     0     0    88     4  1270   2488     0   866     0   0.80  0.87  0.78    0    0

改變"-s"變數檢視不同的子系統。

總結

本文的介紹不過是對collectl這個強大工具的一些淺見。本文字希望展示它有多靈活,其實透過以上的討論讓我們瞭解了包括記錄及回尋捕獲的資料,以多種檔案格式匯出資料並將資料轉換為可被廣泛的工具分析的格式等等功能的實現。

collectl提供的另一大功能便是像服務一樣執行,對遠端Linux機器或完整的伺服器叢集提供遠端監控,其表現堪稱完美。

Collectl同另一批可用於處理分析收集資料的名為Collectl實用工具 (colmux, colgui, colplot)的功能相契合。如果有機會,我們在之後的文章中會介紹它們。

為更詳細的瞭解Collectl工具,請登入它的主頁來學習更多的功能。筆者建議檢視FAQs來快速瞭解collectl,讀取collectl文件獲取深層次的例子。等價命令表也可定位至更多的像sar,iostat,netstat,top等和collectl功能部分等價的常用工具。


via: http://www.binarytides.com/collectl-monitor-system-resources-linux/

譯者:icybreaker 校對:wxy

本文由 LCTT 原創翻譯,Linux中國 榮譽推出

相關文章