如何使用Sysdig监控你的Ubuntu系统?
Sysdig是一个开源应用程序,它捕获正在运行的Linux实例的系统状态和活动,然后保存、过滤和分析这些信息。它可以使用Lua编写脚本,并包含命令行界面和有效的交互式UI。Sysdig结合了strace + tcpdump + htop + iftop + lsof + 事务跟踪 + 强大的功能。本文介绍了“如何使用Sysdig监控你的Ubuntu系统”。
安装Sysdig
使用受信任的Draios GPG密钥配置apt仓库,如下所示:
# curl -s https://s3.amazonaws.com/download.draios.com/DRAIOS-GPG-KEY.public | apt-key add - # curl -s -o /etc/apt/sources.list.d/draios.list http://download.draios.com/stable/deb/draios.list
要更新软件包列表,请使用以下命令:
# apt-get update
要安装内核头文件,请使用以下命令:
# apt-get -y install linux-headers-$(uname -r)
示例输出如下:
Reading package lists... Done Building dependency tree Reading state information... Done linux-headers-4.4.0-59-generic is already the newest version (4.4.0-59.80). linux-headers-4.4.0-59-generic set to manually installed. 0 upgraded, 0 newly installed, 0 to remove and 271 not upgraded.
要安装sysdig,请使用以下命令:
# apt-get -y install sysdig
示例输出如下:
Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: dkms The following NEW packages will be installed: dkms sysdig 0 upgraded, 2 newly installed, 0 to remove and 271 not upgraded. Need to get 5,353 kB of archives. After this operation, 13.8 MB of additional disk space will be used. Get:1 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 dkms all 2.2.0.3-2ubuntu11.3 [66.1 kB] Get:2 http://download.draios.com/stable/deb stable-amd64/ sysdig 0.14.0 [5,287 kB] Fetched 5,353 kB in 5s (947 kB/s) Selecting previously unselected package dkms. (Reading database ... 239045 files and directories currently installed.) Preparing to unpack .../dkms_2.2.0.3-2ubuntu11.3_all.deb ... ....................................................................................
实时统计
要捕获来自实时系统的全部事件,请使用以下命令:
$ sudo sysdig
示例输出如下:
....................................................... 1663107 11:14:51.504818091 2 gnome-terminal- (26186) < write res=8 data=........ 1663109 11:14:51.504819215 2 gnome-terminal- (26186) > recvmsg fd=4(<u>) 1663110 11:14:51.504819910 2 gnome-terminal- (26186) < recvmsg res=-11(EAGAIN) size=4096 data= tuple=NULL 1663111 11:14:51.504821444 2 gnome-terminal- (26186) > poll fds=4:u1 5:e1 10:u1 15:f1 timeout=21 1663113 11:14:51.504823795 2 gnome-terminal- (26186) > switch next=0 pgft_maj=9 pgft_min=40166 vm_size=807060 vm_rss=40264 vm_swap=0 1663115 11:14:51.504826246 2 (0) > switch next=26186(gnome-terminal-) pgft_maj=0 pgft_min=0 vm_size=0 vm_rss=0 vm_swap=0 1663117 11:14:51.504827348 2 gnome-terminal- (26186) < poll res=2 fds=5:e1 15:f1 1663118 11:14:51.504827869 2 gnome-terminal- (26186) > read fd=5() size=16 1663119 11:14:51.504828490 2 gnome-terminal- (26186) < read res=8 data=........ 1663120 11:14:51.504829640 2 gnome-terminal- (26186) > write fd=5() size=8
以上输出格式应如下所示:
1663120(Serial Number) 11:14:51.504829640(timestamp) 2(CPU number) gnome-terminal(process name)- (26186)(TID of an event) > write(Type of event) fd=5() size=8 (Event info)
保存实时统计数据
要捕获来自实时系统的全部事件并将其保存到磁盘,请使用以下命令:
$ sudo sysdig -w realstats.scap
在上述命令中,**realstats** 是存储统计信息的 文件名。要捕获过去24小时内的所有事件并将它们保存到磁盘中的文件中,请使用以下命令:
$ sudo sysdig -G 3600 -W 24 -w realstats.scap
要保存前10个事件,请使用以下命令:
$ sudo sysdig -n 10 -w realstats.scap
要过滤特定的进程名称,请使用以下命令:
$ sudo sysdig proc.name=cat
示例输出如下:
1595626 11:50:02.085725453 3 cat (4292) > close fd=5(/proc/meminfo) 1595627 11:50:02.085726095 3 cat (4292) close fd=1(<p>) 1595629 11:50:02.085741245 3 cat (4292) close fd=2(<p>) 1595631 11:50:02.085744024 3 cat (4292) exit_group 1595657 11:50:02.085806197 3 cat (4292) > procexit status=0 ....................................................................
读取事件
要从捕获的文件中读取事件,请使用以下命令:
$ sudo sysdig -r realstats.scap
示例输出如下:
...................................................................................... 178026 11:24:26.921163459 2 acpid (832) poll fds=18:u30 timeout=0 178028 11:24:26.921165086 2 acpid (832) rt_sigprocmask 178030 11:24:26.921166361 2 acpid (832) read fd=10(/dev/input/event4) size=24 178032 11:24:26.921168085 2 acpid (832) select 178034 11:24:26.921171578 1 Xorg (1008) < select res=-514(ERESTARTNOHAND) 178035 11:24:26.921173983 2 acpid (832) rt_sigprocmask 178037 11:24:26.921175612 2 acpid (832) poll fds=18:u30 timeout=0 178039 11:24:26.921176805 1 Xorg (1008) > signaldeliver spid=0 dpid=1008(Xorg) sig=29(SIGIO) 178040 11:24:26.921176895 2 acpid (832) rt_sigprocmask 178042 11:24:26.921178121 2 acpid (832) < rt_sigprocmask res = 1
要读取特定类型的事件,请使用以下命令:
$ sysdig -r realstats.scap evt.type=read
示例输出如下:
.................................................................................... 177983 11:24:26.900389618 0 gmain (2546) < read res=8 data=........ 178000 11:24:26.917534277 0 compiz (2555) > read fd=9() size=8192 178001 11:24:26.917540171 0 compiz (2555) < read res=-11(EAGAIN) data= 178021 11:24:26.921141528 2 acpid (832) > read fd=10(/dev/input/event4) size=24 178022 11:24:26.921149182 2 acpid (832) < read res=24 data=...X.................... 178031 11:24:26.921167148 2 acpid (832) > read fd=10(/dev/input/event4) size=24 178032 11:24:26.921168085 2 acpid (832) < read res=24 data=...X....................
打印系统调用
要打印cat调用调用的所有打开的系统调用,请使用以下命令:
$ sudo sysdig proc.name=cat and evt.type=open
示例输出如下:
.......................................................................................... 577167 11:31:37.885212361 0 cat (30960) > open 577168 11:31:37.885213414 0 cat (30960) open 577173 11:31:37.885216131 0 cat (30960) open 577179 11:31:37.885218605 0 cat (30960) open 577187 11:31:37.885221894 0 cat (30960) < open fd=3(/etc/ld.so.cache) name=/etc/ld.so.cache flags=4097(O_RDONLY|O_CLOEXEC) mode=0
打印文件名
要打印cat打开的文件的名称,请使用以下命令:
$ sudo sysdig -p"%evt.arg.name" proc.name=cat and evt.type=open
示例输出如下:
/etc/ld.so.cache /lib/x86_64-linux-gnu/libc.so.6 /usr/lib/locale/locale-archive /proc/meminfo /etc/ld.so.cache /lib/x86_64-linux-gnu/libc.so.6 /usr/lib/locale/locale-archive /proc/meminfo /etc/ld.so.cache /lib/x86_64-linux-gnu/libc.so.6 /usr/lib/locale/locale-archive /proc/meminfo /etc/ld.so.cache /lib/x86_64-linux-gnu/libc.so.6 /usr/lib/locale/locale-archive ...............................................
Sysdig Chisels
sysdig 的 chisels 包含检查 sysdig 事件流以执行操作的脚本。要获取 chisel 列表,请使用以下命令:
$ sudo sysdig -cl
示例输出如下:
Category: Application --------------------- httplog HTTP requests log httptop Top HTTP requests memcachelog memcached requests log Category: CPU Usage ------------------- spectrogram Visualize OS latency in real time. subsecoffset Visualize subsecond offset execution time. topcontainers_cpu Top containers by CPU usage topprocs_cpu Top processes by CPU usage Category: Errors ---------------- topcontainers_error Top containers by number of errors topfiles_errors Top files by number of errors topprocs_errors top processes by number of errors ............................................................................................
特定 chisel 信息
要查看特定 chisel 信息,请使用以下命令:
$ sudo sysdig -i lsof
示例输出如下:
Category: System State ---------------------- lsof List (and optionally filter) the open file descriptors. This chisel prints the open file descriptors for every process in the system, w ith an output that is similar to the one of lsof. Output is at a point in time; adjust this in the filter. It defaults to time of evt.num=0 Args: [filter] filter - A sysdig-like filter expression that allows r estricting the FD list. E.g. 'proc.name=foo and fd.name contain s /etc'.
要获取打开的文件描述符列表,请使用以下命令:
$ sudo sysdig -c lsof
示例输出如下:
............................................................................. WorkerPool/92 2991 4530 linux 52 file /usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-RI.ttf WorkerPool/92 2991 4530 linux 54 file /usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-RI.ttf WorkerPool/92 2991 4530 linux 71 file /usr/share/fonts/truetype/dejavu/DejaVuSansMono-Oblique.ttf WorkerPool/92 2991 4530 linux 65 file /usr/share/fonts/truetype/openoffice/opens___.ttf WorkerPool/92 2991 4530 linux 70 file /usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf WorkerPool/92 2991 4530 linux 75 file /home/linux/.config/google-chrome/Dictionaries/en-GB-7-1.bdic WorkerPool/92 2991 4530 linux 69 file /usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf WorkerPool/92 2991 4530 linux 73 file /usr/share/fonts/truetype/dejavu/DejaVuSansMono-Oblique.ttf
网络信息
要获取网络连接列表,请使用以下命令:
$ sudo sysdig -c netstat
示例输出如下:
................................................................................. udp 0.0.0.0:5353 0.0.0.0:* LISTEN 2799/2739/chrome tcp 127.0.0.1:3306 0.0.0.0:* LISTEN 1371/1032/mysqld tcp 127.0.0.1:3306 0.0.0.0:* LISTEN 1307/1032/mysqld tcp 74.125.68.188:5228 192.168.1.200:57158 ESTABLISHED 2867/2739/SimpleCacheWork tcp 23.20.118.252:443 192.168.1.200:42538 ESTABLISHED 2867/2739/SimpleCacheWork tcp 117.18.237.42:80 192.168.1.200:44008 ESTABLISHED 2867/2739/SimpleCacheWork udp 0.0.0.0:5353 0.0.0.0:* LISTEN 2867/2739/SimpleCacheWork tcp 54.221.3.35:443 192.168.1.200:48416 ESTABLISHED 2867/2739/SimpleCacheWork udp 0.0.0.0:5353 0.0.0.0:* LISTEN 2867/2739/SimpleCacheWork tcp 74.125.68.188:5228 192.168.1.200:57158 ESTABLISHED 2817/2739/WorkerPool/2817 tcp 23.20.118.252:443 192.168.1.200:42538 ESTABLISHED 2817/2739/WorkerPool/2817 tcp 117.18.237.42:80 192.168.1.200:44008 ESTABLISHED 2817/2739/WorkerPool/2817 udp 0.0.0.0:5353 0.0.0.0:* LISTEN 2817/2739/WorkerPool/2817 tcp 54.221.3.35:443 192.168.1.200:48416 ESTABLISHED 2817/2739/WorkerPool/2817 udp 0.0.0.0:5353 0.0.0.0:* LISTEN 2817/2739/WorkerPool/2817 tcp 74.125.68.188:5228 192.168.1.200:57158 ESTABLISHED 2819/2739/BrowserBlocking tcp 23.20.118.252:443 192.168.1.200:42538 ESTABLISHED 2819/2739/BrowserBlocking tcp 117.18.237.42:80 192.168.1.200:44008 ESTABLISHED 2819/2739/BrowserBlocking udp 0.0.0.0:5353 0.0.0.0:* LISTEN 2819/2739/BrowserBlocking tcp 54.221.3.35:443 192.168.1.200:48416 ESTABLISHED 2819/2739/BrowserBlocking udp 0.0.0.0:5353 0.0.0.0:* LISTEN 2819/2739/BrowserBlocking
监控和分析
要监控和分析系统,请使用以下命令:
$ sudo csysdig
示例输出应如下所示:
要获取有关csysdig的更多信息,请使用以下命令:
$ sudo csysdig --help
csysdig 的用法应如下所示:
$ sudo csysdig [options] [filter]
设置延迟周期
要设置更新系统统计信息的延迟周期,请使用以下命令:
$ sudo csysdig -d 100
在上述命令中,我们已将更新统计信息的延迟设置为100毫秒。
排除组/用户
要排除用户/组信息,请使用以下命令:
$ sudo csysdig -E
要配置简单的终端设置,请使用以下命令:
$ sudo csysdig --force-term-compat
特定用户信息
要仅获取指定用户信息,请使用以下命令:
$ sudo csysdig user.name=linux
在上述命令中,linux 是系统的用户名。
特定进程名称
要获取特定进程名称信息,请使用以下命令:
$sudo csysdig proc.name=cat
在普通终端中输出
要在普通终端中获取输出,请使用以下命令:
$ sudo csysdig --raw
在本文中,我们学习了如何使用Sysdig监控你的Ubuntu系统,我们将推出更多基于Linux的技巧和提示。继续关注!