如何使用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的技巧和提示。繼續關注!

更新於:2020年1月23日

145 次瀏覽

啟動你的職業生涯

透過完成課程獲得認證

開始學習
廣告