歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> ctop - 監控Linux容器性能的命令行神器

ctop - 監控Linux容器性能的命令行神器

日期:2017/2/27 15:52:47   编辑:Linux教程

ctop是一個新的基於命令行的工具,它可用於在容器層級監控進程。容器通過利用控制器組(cgroup)的資源管理功能,提供了操作系統層級的虛 擬化環境。該工具從cgroup收集與內存、CPU、塊輸入輸出的相關數據,以及擁有者、開機時間等元數據,並以人性化的格式呈現給用戶,這樣就可以快速 對系統健康狀況進行評估。基於所獲得的數據,它可以嘗試推測下層的容器技術。ctop也有助於在低內存環境中檢測出誰在消耗大量的內存。

功能

ctop的一些功能如下:

  • 收集CPU、內存和塊輸入輸出的度量值
  • 收集與擁有者、容器技術和任務統計相關的信息
  • 通過任意欄對信息排序
  • 以樹狀視圖顯示信息
  • 折疊/展開cgroup樹
  • 選擇並跟蹤cgroup/容器
  • 選擇顯示數據刷新的時間窗口
  • 暫停刷新數據
  • 檢測基於systemd、Docker和LXC的容器
  • 基於Docker和LXC的容器的高級特性
    • 打開/連接shell以進行深度診斷
    • 停止/殺死容器類型

安裝

ctop是由Python寫成的,因此,除了需要Python 2.6或其更高版本外(帶有內建的光標支持),別無其它外部依賴。推薦使用Python的pip進行安裝,如果還沒有安裝pip,請先安裝,然後使用pip安裝ctop。

注意:本文樣例來自Ubuntu(14.10)系統

$ sudo apt-get install python-pip

使用pip安裝ctop:

poornima@poornima-Lenovo:~$ sudo pip install ctop

[sudo] password for poornima:

Downloading/unpacking ctop

Downloading ctop-0.4.0.tar.gz

Running setup.py (path:/tmp/pip_build_root/ctop/setup.py) egg_info for package ctop

Installing collected packages: ctop

Running setup.py install for ctop

changing mode of build/scripts-2.7/ctop from 644 to 755

changing mode of /usr/local/bin/ctop to 755

Successfully installed ctop

Cleaning up...

如果不選擇使用pip安裝,你也可以使用wget直接從github安裝:

poornima@poornima-Lenovo:~$ wget https://raw.githubusercontent.com/yadutaf/ctop/master/cgroup_top.py -O ctop

--2015-04-29 19:32:53-- https://raw.githubusercontent.com/yadutaf/ctop/master/cgroup_top.py

Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 199.27.78.133

Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|199.27.78.133|:443... connected.

HTTP request sent, awaiting response... 200 OK Length: 27314 (27K) [text/plain]

Saving to: ctop

100%[======================================>] 27,314 --.-K/s in 0s

2015-04-29 19:32:59 (61.0 MB/s) - ctop saved [27314/27314]

poornima@poornima-Lenovo:~$ chmod +x ctop

如果cgroup-bin包沒有安裝,你可能會碰到一個錯誤消息,你可以通過安裝需要的包來解決。

poornima@poornima-Lenovo:~$ ./ctop

[ERROR] Failed to locate cgroup mountpoints.

poornima@poornima-Lenovo:~$ sudo apt-get install cgroup-bin

下面是ctop的輸出樣例:

ctop屏幕

用法選項

ctop [--tree] [--refresh=] [--columns=] [--sort-col=] [--follow=] [--fold=, ...] ctop (-h | --help)

當你進入ctop屏幕,可使用上(↑)和下(↓)箭頭鍵在容器間導航。點擊某個容器就選定了該容器,按q或Ctrl+C退出該容器。

現在,讓我們來看看上面列出的那一堆選項究竟是怎麼用的吧。

-h / --help - 顯示幫助信息

poornima@poornima-Lenovo:~$ ctop -h
Usage: ctop [options]

Options:
-h, --help show this help message and exit
--tree show tree view by default
--refresh=REFRESH Refresh display every <seconds>
--follow=FOLLOW Follow cgroup path
--columns=COLUMNS List of optional columns to display. Always includes
'name'
--sort-col=SORT_COL Select column to sort by initially. Can be changed
dynamically.

--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支持的額外容器,跟蹤選項也是可用的:

press 'a' - 接駁到終端輸出

press 'e' - 打開容器中的一個 shell

press 's' - 停止容器 (SIGTERM)

press 'k' - 殺死容器 (SIGKILL)
目前 Jean-Tiare Le Bigot 還在積極開發 ctop 中,希望我們能在該工具中見到像本地 top 命令一樣的特性
譯文:http://linux.cn/article-5429-1.html
Copyright © Linux教程網 All Rights Reserved