TranslateProject/translated/tech/20150507 Command Line Tool to Monitor Linux Containers Performance.md

193 lines
6.8 KiB
Markdown
Raw Normal View History

监控Linux容器性能的命令行神器
2015-05-07 16:15:40 +08:00
================================================================================
ctop是一个新的基于命令行的工具它可用于在容器层级监控进程。容器通过利用控制器组cgroup的资源管理功能提供了操作系统层级的虚拟化环境。该工具收集来自cgroup的与内存、CPU、块输入输出和诸如拥有者、开机时间等相关的元数据并以人性化的格式呈现给用户这样就可以快速对系统健康状况进行评估。基于所获得的数据它可以尝试推测潜在的容器技术。ctop也有助于在低内存环境中检测出谁在消耗大量的内存。
2015-05-07 16:15:40 +08:00
### 功能 ###
2015-05-07 16:15:40 +08:00
ctop的一些功能如下
2015-05-07 16:15:40 +08:00
- 收集CPU、内存和块输入输出的度量值
- 收集与拥有者、容器技术和任务统计相关的信息
- 使用任何栏目对信息排序
- 以树状视图显示信息
- 折叠/展开cgroup树
- 选择并跟踪cgroup/容器
- 选择显示数据刷新时间框架
- 暂停刷新数据
- 检测基于systemd、Docker和LXC的容器
- 基于Docker和LXC的容器的高级特性
- 打开/连接shell以进行深度诊断
- 停止/杀死容器类型
2015-05-07 16:15:40 +08:00
### 安装 ###
2015-05-07 16:15:40 +08:00
**ctop**是由Python写成的因此除了需要Python 2.6或其更高版本外支持内建光标别无其它外部依赖。推荐使用Python的pip进行安装如果还没有安装pip请先安装然后使用pip安装ctop。
2015-05-07 16:15:40 +08:00
*注意本文样例来自Ubuntu14.10)系统*
2015-05-07 16:15:40 +08:00
$ sudo apt-get install python-pip
使用pip安装ctop
2015-05-07 16:15:40 +08:00
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安装
2015-05-07 16:15:40 +08:00
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包没有安装你可能会碰到一个错误消息你可以通过安装需要的包来解决。
2015-05-07 16:15:40 +08:00
poornima@poornima-Lenovo:~$ ./ctop
[ERROR] Failed to locate cgroup mountpoints.
poornima@poornima-Lenovo:~$ sudo apt-get install cgroup-bin
下面是ctop的输出样例
2015-05-07 16:15:40 +08:00
![ctop screen](http://blog.linoxide.com/wp-content/uploads/2015/05/ctop.png)
ctop屏幕
2015-05-07 16:15:40 +08:00
### 用法选项 ###
2015-05-07 16:15:40 +08:00
ctop [--tree] [--refresh=] [--columns=] [--sort-col=] [--follow=] [--fold=, ...] ctop (-h | --help)
一旦你进入ctop屏幕使用上和下箭头键在容器间导航。点击某个容器就选定了该容器按q或Ctrl+C退出容器。
2015-05-07 16:15:40 +08:00
现在,让我们来看看上面列出的那一堆选项究竟是怎么用的吧。
2015-05-07 16:15:40 +08:00
-h / --help - Show the help screen
----------
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 - Display tree view of the containers
默认情况下,会显示列表视图
2015-05-07 16:15:40 +08:00
一旦你进入ctop窗口你可以使用F5按钮在树状/列表视图间切换。
2015-05-07 16:15:40 +08:00
--fold=<name> - Fold the <name> cgroup path in the tree view.
This option needs to be used in combination with --tree.
----------
Eg: ctop --tree --fold=/user.slice
![Output of 'ctop --fold'](http://blog.linoxide.com/wp-content/uploads/2015/05/ctop-fold.png)
ctop --fold的输出
2015-05-07 16:15:40 +08:00
在ctop窗口中使用+/-键来展开或折叠子cgroup。
2015-05-07 16:15:40 +08:00
注意在写本文时pip仓库中还没有最新版的ctop还不支持命令行的--fold选项
2015-05-07 16:15:40 +08:00
--follow= - Follow/Highlight the cgroup path.
----------
Eg: ctop --follow=/user.slice/user-1000.slice
正如你在下面屏幕中所见到的那样,带有“/user.slice/user-1000.slice”路径的cgroup被高亮显示这让用户易于跟踪就算显示位置变了也一样。
2015-05-07 16:15:40 +08:00
![Output of 'ctop --follow'](http://blog.linoxide.com/wp-content/uploads/2015/05/ctop-follow.png)
ctop --follow的输出
2015-05-07 16:15:40 +08:00
你也可以使用f按钮来让高亮的行跟踪选定的容器。默认情况下跟踪是关闭的。
2015-05-07 16:15:40 +08:00
--refresh= - Refresh the display at the given rate. Default 1 sec
这对于按每用户需求来显示改变刷新率时很有用。使用p按钮可以暂停刷新并选择文本。
2015-05-07 16:15:40 +08:00
--columns=<columns> - Can limit the display to selected <columns>. 'name' should be the first entry followed by other columns. By default, the columns include owner, processes,memory, cpu-sys, cpu-user, blkio, cpu-time.
----------
Eg: ctop --columns=name,owner,type,memory
![Output of 'ctop --column'](http://blog.linoxide.com/wp-content/uploads/2015/05/ctop-column.png)
ctop --column的输出
2015-05-07 16:15:40 +08:00
-sort-col=<sort-col> - column using which the displayed data should be sorted. By default it is sorted using cpu-user
----------
Eg: ctop --sort-col=blkio
如果有Docker和LXC支持的额外容器跟踪选项也是可用的
2015-05-07 16:15:40 +08:00
press 'a' - attach to console output
press 'e' - open a shell in the container context
press 's' stop the container (SIGTERM)
press 'k' - kill the container (SIGKILL)
[ctop][1]当前还处于Jean-Tiare Le Bigot的开发中希望我们能在该工具中见到像本地top命令一样的特性 :-)
2015-05-07 16:15:40 +08:00
--------------------------------------------------------------------------------
via: http://linoxide.com/how-tos/monitor-linux-containers-performance/
作者:[B N Poornima][a]
译者:[GOLinux](https://github.com/GOLinux)
2015-05-07 16:15:40 +08:00
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://linoxide.com/author/bnpoornima/
[1]:https://github.com/yadutaf/ctop