ctop是一個新的基于指令行的工具,它可用于在容器層級監控程序。容器通過利用控制器組(cgroup)的資源管理功能,提供了作業系統層級的虛拟化環境。該工具從cgroup收集與記憶體、cpu、塊輸入輸出的相關資料,以及擁有者、開機時間等中繼資料,并以人性化的格式呈現給使用者,這樣就可以快速對系統健康狀況進行評估。基于所獲得的資料,它可以嘗試推測下層的容器技術。ctop也有助于在低記憶體環境中檢測出誰在消耗大量的記憶體。
<a target="_blank"></a>
ctop的一些功能如下:
收集cpu、記憶體和塊輸入輸出的路徑成本
收集與擁有者、容器技術和任務統計相關的資訊
通過任意欄對資訊排序
以樹狀視圖顯示資訊
折疊/展開cgroup樹
選擇并跟蹤cgroup/容器
選擇顯示資料重新整理的時間視窗
暫停重新整理資料
檢測基于systemd、docker和lxc的容器
基于docker和lxc的容器的進階特性
打開/連接配接shell以進行深度診斷
停止/殺死容器類型
ctop是由python寫成的,是以,除了需要python 2.6或其更高版本外(帶有内建的光标支援),别無其它外部依賴。推薦使用python的pip進行安裝,如果還沒有安裝pip,請先安裝,然後使用pip安裝ctop。
注意:本文樣例來自ubuntu(14.10)系統
<code>$ sudo apt-get install python-pip</code>
使用pip安裝ctop:
<code>poornima@poornima-lenovo:~$ sudo pip install ctop</code>
<code></code>
<code>[sudo] password for poornima:</code>
<code>downloading/unpacking ctop</code>
<code>downloading ctop-0.4.0.tar.gz</code>
<code>running setup.py (path:/tmp/pip_build_root/ctop/setup.py) egg_info for package ctop</code>
<code>installing collected packages: ctop</code>
<code>running setup.py install for ctop</code>
<code>changing mode of build/scripts-2.7/ctop from 644 to 755</code>
<code>changing mode of /usr/local/bin/ctop to 755</code>
<code>successfully installed ctop</code>
<code>cleaning up...</code>
如果不選擇使用pip安裝,你也可以使用wget直接從github安裝:
<code>poornima@poornima-lenovo:~$ wget https://raw.githubusercontent.com/yadutaf/ctop/master/cgroup_top.py -o ctop</code>
<code>--2015-04-29 19:32:53-- https://raw.githubusercontent.com/yadutaf/ctop/master/cgroup_top.py</code>
<code>resolving raw.githubusercontent.com (raw.githubusercontent.com)... 199.27.78.133</code>
<code>connecting to raw.githubusercontent.com (raw.githubusercontent.com)|199.27.78.133|:443... connected.</code>
<code>http request sent, awaiting response... 200 ok length: 27314 (27k) [text/plain]</code>
<code>saving to: ctop</code>
<code>100%[======================================>] 27,314 --.-k/s in 0s</code>
<code>2015-04-29 19:32:59 (61.0 mb/s) - ctop saved [27314/27314]</code>
<code>poornima@poornima-lenovo:~$ chmod +x ctop</code>
如果cgroup-bin包沒有安裝,你可能會碰到一個錯誤消息,你可以通過安裝需要的包來解決。
<code>poornima@poornima-lenovo:~$ ./ctop</code>
<code>[error] failed to locate cgroup mountpoints.</code>
<code>poornima@poornima-lenovo:~$ sudo apt-get install cgroup-bin</code>
下面是ctop的輸出樣例:

ctop螢幕
<code>ctop [--tree] [--refresh=] [--columns=] [--sort-col=] [--follow=] [--fold=, ...] ctop (-h | --help)</code>
當你進入ctop螢幕,可使用上(↑)和下(↓)箭頭鍵在容器間導航。點選某個容器就標明了該容器,按q或ctrl+c退出該容器。
現在,讓我們來看看上面列出的那一堆選項究竟是怎麼用的吧。
-h / --help - 顯示幫助資訊
<code>poornima@poornima-lenovo:~$ ctop -h</code>
<code>usage: ctop [options]</code>
<code>options:</code>
<code>-h, --help show this help message and exit</code>
<code>--tree show tree view by default</code>
<code>--refresh=refresh refresh display every <seconds></code>
<code>--follow=follow follow cgroup path</code>
<code>--columns=columns list of optional columns to display. always includes</code>
<code>'name'</code>
<code>--sort-col=sort_col select column to sort by initially. can be changed</code>
<code>dynamically.</code>
--tree - 顯示容器的樹形視圖
預設情況下,會顯示清單視圖
當你進入ctop視窗,你可以使用f5按鈕在樹狀/清單視圖間切換。
--fold= - 在樹形視圖中折疊名為 <name> 的 cgroup 路徑
該選項需要與 --tree 選項組合使用。
例子: ctop --tree --fold=/user.slice
'ctop --fold'的輸出
在ctop視窗中,使用+/-鍵來展開或折疊子cgroup。
注意:在寫本文時,pip倉庫中還沒有最新版的ctop,還不支援指令行的‘--fold’選項
--follow= - 跟蹤/高亮 cgroup 路徑
例子: ctop --follow=/user.slice/user-1000.slice
正如你在下面螢幕中所見到的那樣,帶有“/user.slice/user-1000.slice”路徑的cgroup被高亮顯示,這讓使用者易于跟蹤,就算顯示位置變了也一樣。
'ctop --follow'的輸出
你也可以使用‘f’按鈕來讓高亮的行跟蹤標明的容器。預設情況下,跟蹤是關閉的。
--refresh= - 按指定頻率重新整理顯示,預設1秒
這對于按每使用者需求來顯示改變重新整理率時很有用。使用‘p’按鈕可以暫停重新整理并選擇文本。
--columns= - 限定隻顯示標明的列。'name' 需要是第一個字段,其後跟着其它字段。預設情況下,字段包括:owner, processes,memory, cpu-sys, cpu-user, blkio, cpu-time
例子: ctop --columns=name,owner,type,memory
'ctop --column'的輸出
-sort-col= - 按指定的列排序。預設使用 cpu-user 排序
例子: ctop --sort-col=blkio
如果有docker和lxc支援的額外容器,跟蹤選項也是可用的:
<code>press 'a' - 接駁到終端輸出</code>
<code>press 'e' - 打開容器中的一個 shell</code>
<code>press 's' - 停止容器 (sigterm)</code>
<code>press 'k' - 殺死容器 (sigkill)</code>
原文釋出時間:2015-05-11
本文來自雲栖合作夥伴“linux中國”