ctop:监控 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)系统
<span class="pln">$ sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install python</span><span class="pun">-</span><span class="pln">pip</span>
使用pip安装ctop:
<span class="pln">poornima@poornima</span><span class="pun">-</span><span class="typ">Lenovo</span><span class="pun">:~</span><span class="pln">$ sudo pip install ctop</span>
<span class="pun">[</span><span class="pln">sudo</span><span class="pun">]</span><span class="pln"> password </span><span class="kwd">for</span><span class="pln"> poornima</span><span class="pun">:</span>
<span class="typ">Downloading</span><span class="pun">/</span><span class="pln">unpacking ctop</span>
<span class="typ">Downloading</span><span class="pln"> ctop</span><span class="pun">-</span><span class="lit">0.4</span><span class="pun">.</span><span class="lit">0.tar</span><span class="pun">.</span><span class="pln">gz</span>
<span class="typ">Running</span><span class="pln"> setup</span><span class="pun">.</span><span class="pln">py </span><span class="pun">(</span><span class="pln">path</span><span class="pun">:</span><span class="str">/tmp/</span><span class="pln">pip_build_root</span><span class="pun">/</span><span class="pln">ctop</span><span class="pun">/</span><span class="pln">setup</span><span class="pun">.</span><span class="pln">py</span><span class="pun">)</span><span class="pln"> egg_info </span><span class="kwd">for</span><span class="kwd">package</span><span class="pln"> ctop</span>
<span class="typ">Installing</span><span class="pln"> collected packages</span><span class="pun">:</span><span class="pln"> ctop</span>
<span class="typ">Running</span><span class="pln"> setup</span><span class="pun">.</span><span class="pln">py install </span><span class="kwd">for</span><span class="pln"> ctop</span>
<span class="pln">changing mode of build</span><span class="pun">/</span><span class="pln">scripts</span><span class="pun">-</span><span class="lit">2.7</span><span class="pun">/</span><span class="pln">ctop </span><span class="kwd">from</span><span class="lit">644</span><span class="pln"> to </span><span class="lit">755</span>
<span class="pln">changing mode of </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">ctop to </span><span class="lit">755</span>
<span class="typ">Successfully</span><span class="pln"> installed ctop</span>
<span class="typ">Cleaning</span><span class="pln"> up</span><span class="pun">...</span>
如果不选择使用pip安装,你也可以使用wget直接从github安装:
<span class="pln">poornima@poornima</span><span class="pun">-</span><span class="typ">Lenovo</span><span class="pun">:~</span><span class="pln">$ wget https</span><span class="pun">:</span><span class="com">//raw.githubusercontent.com/yadutaf/ctop/master/cgroup_top.py -O ctop</span>
<span class="pun">--</span><span class="lit">2015</span><span class="pun">-</span><span class="lit">04</span><span class="pun">-</span><span class="lit">29</span><span class="lit">19</span><span class="pun">:</span><span class="lit">32</span><span class="pun">:</span><span class="lit">53</span><span class="pun">--</span><span class="pln"> https</span><span class="pun">:</span><span class="com">//raw.githubusercontent.com/yadutaf/ctop/master/cgroup_top.py</span>
<span class="typ">Resolving</span><span class="pln"> raw</span><span class="pun">.</span><span class="pln">githubusercontent</span><span class="pun">.</span><span class="pln">com </span><span class="pun">(</span><span class="pln">raw</span><span class="pun">.</span><span class="pln">githubusercontent</span><span class="pun">.</span><span class="pln">com</span><span class="pun">)...</span><span class="lit">199.27</span><span class="pun">.</span><span class="lit">78.133</span>
<span class="typ">Connecting</span><span class="pln"> to raw</span><span class="pun">.</span><span class="pln">githubusercontent</span><span class="pun">.</span><span class="pln">com </span><span class="pun">(</span><span class="pln">raw</span><span class="pun">.</span><span class="pln">githubusercontent</span><span class="pun">.</span><span class="pln">com</span><span class="pun">)|</span><span class="lit">199.27</span><span class="pun">.</span><span class="lit">78.133</span><span class="pun">|:</span><span class="lit">443.</span><span class="pun">..</span><span class="pln"> connected</span><span class="pun">.</span>
<span class="pln">HTTP request sent</span><span class="pun">,</span><span class="pln"> awaiting response</span><span class="pun">...</span><span class="lit">200</span><span class="pln"> OK </span><span class="typ">Length</span><span class="pun">:</span><span class="lit">27314</span><span class="pun">(</span><span class="lit">27K</span><span class="pun">)</span><span class="pun">[</span><span class="pln">text</span><span class="pun">/</span><span class="pln">plain</span><span class="pun">]</span>
<span class="typ">Saving</span><span class="pln"> to</span><span class="pun">:</span><span class="pln"> ctop</span>
<span class="lit">100</span><span class="pun">%[======================================>]</span><span class="lit">27</span><span class="pun">,</span><span class="lit">314</span><span class="pun">--.-</span><span class="pln">K</span><span class="pun">/</span><span class="pln">s </span><span class="kwd">in</span><span class="lit">0s</span>
<span class="lit">2015</span><span class="pun">-</span><span class="lit">04</span><span class="pun">-</span><span class="lit">29</span><span class="lit">19</span><span class="pun">:</span><span class="lit">32</span><span class="pun">:</span><span class="lit">59</span><span class="pun">(</span><span class="lit">61.0</span><span class="pln"> MB</span><span class="pun">/</span><span class="pln">s</span><span class="pun">)</span><span class="pun">-</span><span class="pln"> ctop saved </span><span class="pun">[</span><span class="lit">27314</span><span class="pun">/</span><span class="lit">27314</span><span class="pun">]</span>
<span class="pln">poornima@poornima</span><span class="pun">-</span><span class="typ">Lenovo</span><span class="pun">:~</span><span class="pln">$ chmod </span><span class="pun">+</span><span class="pln">x ctop</span>
如果cgroup-bin包没有安装,你可能会碰到一个错误消息,你可以通过安装需要的包来解决。
<span class="pln">poornima@poornima</span><span class="pun">-</span><span class="typ">Lenovo</span><span class="pun">:~</span><span class="pln">$ </span><span class="pun">./</span><span class="pln">ctop</span>
<span class="pun">[</span><span class="pln">ERROR</span><span class="pun">]</span><span class="typ">Failed</span><span class="pln"> to locate cgroup mountpoints</span><span class="pun">.</span>
<span class="pln">poornima@poornima</span><span class="pun">-</span><span class="typ">Lenovo</span><span class="pun">:~</span><span class="pln">$ sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install cgroup</span><span class="pun">-</span><span class="pln">bin</span>
下面是ctop的输出样例:
ctop屏幕
用法选项
<span class="pln">ctop </span><span class="pun">[--</span><span class="pln">tree</span><span class="pun">]</span><span class="pun">[--</span><span class="pln">refresh</span><span class="pun">=]</span><span class="pun">[--</span><span class="pln">columns</span><span class="pun">=]</span><span class="pun">[--</span><span class="pln">sort</span><span class="pun">-</span><span class="pln">col</span><span class="pun">=]</span><span class="pun">[--</span><span class="pln">follow</span><span class="pun">=]</span><span class="pun">[--</span><span class="pln">fold</span><span class="pun">=,</span><span class="pun">...]</span><span class="pln"> ctop </span><span class="pun">(-</span><span class="pln">h </span><span class="pun">|</span><span class="pun">--</span><span class="pln">help</span><span class="pun">)</span>
当你进入ctop屏幕,可使用上(↑)和下(↓)箭头键在容器间导航。点击某个容器就选定了该容器,按q或Ctrl+C退出该容器。
现在,让我们来看看上面列出的那一堆选项究竟是怎么用的吧。
-h / --help - 显示帮助信息
<span class="pln">poornima@poornima</span><span class="pun">-</span><span class="typ">Lenovo</span><span class="pun">:~</span><span class="pln">$ ctop </span><span class="pun">-</span><span class="pln">h</span>
<span class="typ">Usage</span><span class="pun">:</span><span class="pln"> ctop </span><span class="pun">[</span><span class="pln">options</span><span class="pun">]</span>
<span class="typ">Options</span><span class="pun">:</span>
<span class="pun">-</span><span class="pln">h</span><span class="pun">,</span><span class="pun">--</span><span class="pln">help show </span><span class="kwd">this</span><span class="pln"> help message </span><span class="kwd">and</span><span class="kwd">exit</span>
<span class="pun">--</span><span class="pln">tree show tree view </span><span class="kwd">by</span><span class="kwd">default</span>
<span class="pun">--</span><span class="pln">refresh</span><span class="pun">=</span><span class="pln">REFRESH </span><span class="typ">Refresh</span><span class="pln"> display every </span><span class="str"><seconds></span>
<span class="pun">--</span><span class="pln">follow</span><span class="pun">=</span><span class="pln">FOLLOW </span><span class="typ">Follow</span><span class="pln"> cgroup path</span>
<span class="pun">--</span><span class="pln">columns</span><span class="pun">=</span><span class="pln">COLUMNS </span><span class="typ">List</span><span class="pln"> of optional columns to display</span><span class="pun">.</span><span class="typ">Always</span><span class="pln"> includes</span>
<span class="str">'name'</span>
<span class="pun">--</span><span class="pln">sort</span><span class="pun">-</span><span class="pln">col</span><span class="pun">=</span><span class="pln">SORT_COL </span><span class="typ">Select</span><span class="pln"> column to sort </span><span class="kwd">by</span><span class="pln"> initially</span><span class="pun">.</span><span class="typ">Can</span><span class="pln"> be changed</span>
<span class="pln">dynamically</span><span class="pun">.</span>
--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支持的额外容器,跟踪选项也是可用的:
<span class="pln">press </span><span class="str">'a'</span><span class="pun">-</span><span class="pun">接驳到终端输出</span>
<span class="pln">press </span><span class="str">'e'</span><span class="pun">-</span><span class="pun">打开容器中的一个</span><span class="pln"> shell</span>
<span class="pln">press </span><span class="str">'s'</span><span class="pun">-</span><span class="pun">停止容器</span><span class="pun">(</span><span class="pln">SIGTERM</span><span class="pun">)</span>
<span class="pln">press </span><span class="str">'k'</span><span class="pun">-</span><span class="pun">杀死容器</span><span class="pun">(</span><span class="pln">SIGKILL</span><span class="pun">)</span>
目前 Jean-Tiare Le Bigot 还在积极开发 ctop 中,希望我们能在该工具中见到像本地 top 命令一样的特性 :-)