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

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

日期:2017/2/28 14:01:39   编辑: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)系統

  1. $ sudo apt-get install python-pip

使用pip安裝ctop:

  1. poornima@poornima-Lenovo:~$ sudo pip install ctop
  2. [sudo] password for poornima:
  3. Downloading/unpacking ctop
  4. Downloading ctop-0.4.0.tar.gz
  5. Running setup.py (path:/tmp/pip_build_root/ctop/setup.py) egg_info forpackage ctop
  6. Installing collected packages: ctop
  7. Running setup.py install for ctop
  8. changing mode of build/scripts-2.7/ctop from644 to 755
  9. changing mode of /usr/local/bin/ctop to 755
  10. Successfully installed ctop
  11. Cleaning up...

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

  1. poornima@poornima-Lenovo:~$ wget https://raw.githubusercontent.com/yadutaf/ctop/master/cgroup_top.py -O ctop
  2. --2015-04-2919:32:53-- https://raw.githubusercontent.com/yadutaf/ctop/master/cgroup_top.py
  3. Resolving raw.githubusercontent.com (raw.githubusercontent.com)...199.27.78.133
  4. Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|199.27.78.133|:443... connected.
  5. HTTP request sent, awaiting response...200 OK Length:27314(27K)[text/plain]
  6. Saving to: ctop
  7. 100%[======================================>]27,314--.-K/s in0s
  8. 2015-04-2919:32:59(61.0 MB/s)- ctop saved [27314/27314]

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

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

  1. poornima@poornima-Lenovo:~$ ./ctop
  2. [ERROR]Failed to locate cgroup mountpoints.
  3. poornima@poornima-Lenovo:~$ sudo apt-get install cgroup-bin

下面是ctop的輸出樣例:

ctop屏幕

用法選項

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

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

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

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

  1. poornima@poornima-Lenovo:~$ ctop -h
  2. Usage: ctop [options]
  3. Options:
  4. -h,--help show this help message andexit
  5. --tree show tree view bydefault
  6. --refresh=REFRESH Refresh display every <seconds>
  7. --follow=FOLLOW Follow cgroup path
  8. --columns=COLUMNS List of optional columns to display.Always includes
  9. 'name'
  10. --sort-col=SORT_COL Select column to sort by initially.Can be changed
  11. 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支持的額外容器,跟蹤選項也是可用的:

  1. press 'a'-接駁到終端輸出
  2. press 'e'-打開容器中的一個 shell
  3. press 's'-停止容器(SIGTERM)
  4. press 'k'-殺死容器(SIGKILL)

目前 Jean-Tiare Le Bigot 還在積極開發 ctop 中,希望我們能在該工具中見到像本地 top 命令一樣的特性 :-)

Copyright © Linux教程網 All Rights Reserved