sysdig 安裝與使用(轉載)

一、安裝

本篇主要介紹在centos下的用法安裝和測試,後面也會提到在ubuntu類平臺下的安裝。其支持在centos6和centos7上安裝,安裝方法十分簡潔:

1、centos下的安裝

1.1一鍵安裝

curl -s https://s3.amazonaws.com/download.draios.com/stable/install-sysdig | sudo bash
 

上面是一個shell 腳本,會識別常用的linux發行版本,並根據對應的版本配置源,最後是安裝sysdig包。在redhat/centos上首先會配置的是epel 源 ,配置該源的目的是安裝dkms包;然後會配置draios源,通過該源可以安裝sysdig包。最後在裝sysdig包之前還會先安裝kernel-devel包。

1.2、分步安裝

由於使用的源都是國外源,會出現安裝比較慢的情況,所以使用一鍵安裝失敗時,可以使用分解步驟安裝。在內網環境下的也可以將依賴包都下下來再安裝。

 

  1.  
    #導入draios源
  2.  
    rpm --import https://s3.amazonaws.com/download.draios.com/DRAIOS-GPG-KEY.public
  3.  
    curl -s -o /etc/yum.repos.d/draios.repo http://download.draios.com/stable/rpm/draios.repo
  4.  
    #導入epel 源
  5.  
    rpm -i http://mirror.us.leaseweb.net/epel/6/x86_64/epel-release-6-8.noarch.rpm
  6.  
     
  7.  
    #裝包
  8.  
    yum -y install kernel-devel*  dkms  sysdig

sysdig依賴的兩個包,一個是kernel-devel ,一個是dkms包,DKMS全稱是Dynamic Kernel Module Support (動態內核模塊支持),即在內核版本變動之後可以自動重新生成新驅動模塊。想要了解的可以自行GOOGLE 。

2、ubuntu下的安裝

 

  1.  
    curl -s https://s3.amazonaws.com/download.draios.com/DRAIOS-GPG-KEY.public | apt-key add -
  2.  
    curl -s -o /etc/apt/sources.list.d/draios.list 
  3.  
    apt-get update
  4.  
    apt-get -y install linux-headers-$(uname -r)
  5.  
    apt-get -y install sysdig

windows、Mac OS及其他linux發行版的安裝,可以參看官方安裝文檔

二、常用用法

默認按上面的方法安裝好以後,執行sysdig是會出錯的。提示如下:

 

# sysdigUnable to load the drivererror opening device /dev/sysdig0. Make sure you have root credentials and that the sysdig-probe module is loaded.
 

所以執行之前還需要使用/usr/bin/sysdig-probe-loader命令裝載內核模塊,該命令也是一個shell腳本,執行時會從aws s3上下載一個ko模塊文件。不過我在linux測試主機上下載多次都未成功。查看該腳本文件後,發現其調用下載的地址是:https://s3.amazonaws.com/download.draios.com/stable/sysdig-probe-binaries/sysdig-probe-0.6.0-x86_64-2.6.32-504.el6.x86_64-e065a96a1a7343d57e26548de23096e3.ko

注:該ko文件的URL不用記,執行該腳本時會有相應的提示“Trying to download precompiled module from” ,而且不同的內核版本下的對應ko文件也是不同的。我這裏的內核版本是2.6.32-504.el6 。下載完成後會存放在~/.sysdig 目錄。

完成後再執行sysdig-probe-loader命令就可以執行sysdig命令了,而且開機後不會自動加載,所以在不使用的情況下,該包是對主機無影響的。

1、網絡

查看佔用網絡帶寬最多的進程:

sysdig -c topprocs_net
 

顯示主機192.168.0.1的網絡傳輸數據:

  1.  
    as binary:sysdig -s2000 -X -c echo_fds fd.cip=192.168.0.1
  2.  
    as ASCII:sysdig -s2000 -A -c echo_fds fd.cip=192.168.0.1

這裏顯示網絡傳輸功能,實際效果和tcpdump抓包是一樣的。而且其本身也支持sysdig -w dump.scap抓包保存(可以配置-X或-A使用),抓好包也支持sysdir -r dump.scap讀取。

查看連接最多的服務器端口:

  1.  
    in terms of established connections:sysdig -c fdcount_by fd.sport "evt.type=accept"
  2.  
    in terms of total bytes:sysdig -c fdbytes_by fd.sport

查看客戶端連接最多的ip:

  1.  
    in terms of established connectionssysdig -c fdcount_by fd.cip "evt.type=accept"
  2.  
    in terms of total bytessysdig -c fdbytes_by fd.cip

列出所有不是訪問apache服務的訪問連接:

sysdig -p"%proc.name %fd.name" "evt.type=accept and proc.name!=httpd"
 

2、硬盤 I/O

查看使用硬盤帶寬最多的進程:

sysdig -c topprocs_file
 

列出使用大量文件描述符的進程

sysdig -c fdcount_by proc.name "fd.type=file"
 

See the top files in terms of read+write bytes

sysdig -c topfiles_bytes
 

Print the top files that apache has been reading from or writing to

sysdig -c topfiles_bytes proc.name=httpd
 

Basic opensnoop: snoop file opens as they occur

sysdig -p "%12user.name %6proc.pid %12proc.name %3fd.num %fd.typechar %fd.name" evt.type=open
 

See the top directories in terms of R+W disk activity

sysdig -c fdbytes_by fd.directory "fd.type=file"
 

See the top files in terms of R+W disk activity in the /tmp directory

sysdig -c fdbytes_by fd.filename "fd.directory=/tmp/"
 

Observe the I/O activity on all the files named 'passwd'

sysdig -A -c echo_fds "fd.filename=passwd"
 

Display I/O activity by FD type

sysdig -c fdbytes_by fd.type
 

 

3、進程和CPU使用率

See the top processes in terms of CPU usage

sysdig -c topprocs_cpu
 

See the top processes for CPU 0

sysdig -c topprocs_cpu evt.cpu=0
 

Observe the standard output of a process

sysdig -s4096 -A -c stdout proc.name=cat
 

 

4、應用

查看機器所有的HTTP請求

 sudo sysdig -s 2000 -A -c echo_fds fd.port=80 and evt.buffer contains GET
 

查看機器所有的SQL select查詢

 sudo sysdig -s 2000 -A -c echo_fds evt.buffer contains SELECT
 

See queries made via apache to an external MySQL server happening in real time

 sysdig -s 2000 -A -c echo_fds fd.sip=192.168.30.5 and proc.name=apache2 and evt.buffer contains SELECT
 

5、性能和錯誤

See the files where most time has been spent

sysdig -c topfiles_time
 

See the files where apache spent most time

sysdig -c topfiles_time proc.name=httpd
 

See the top processes in terms of I/O errors

sysdig -c topprocs_errors
 

See the top files in terms of I/O errors

sysdig -c topfiles_errors
 

See all the failed disk I/O calls

sysdig fd.type=file and evt.failed=true
 

See all the failed file opens by httpd

sysdig "proc.name=httpd and evt.type=open and evt.failed=true"
 

See the system calls where most time has been spent

sysdig -c topscalls_time
 

See the top system calls returning errors

sysdig -c topscalls "evt.failed=true"
 

snoop failed file opens as they occur

sysdig -p "%12user.name %6proc.pid %12proc.name %3fd.num %fd.typechar %fd.name" evt.type=open and evt.failed=true
 

Print the file I/O calls that have a latency greater than 1ms:

sysdig -c fileslower 1
 

6、安全

Show the directories that the user "root" visits

sysdig -p"%evt.arg.path" "evt.type=chdir and user.name=root"
 

Observe ssh activity

sysdig -A -c echo_fds fd.name=/dev/pretmx and proc.name=sshd
 

Show every file open that happens in /etc

sysdig evt.type=open and fd.name contains /etc
 

Show the ID of all the login shells that have launched the "tar" command

sysdig -r file.scap -c list_login_shells tar
 

Show all the commands executed by the login shell with the given ID

sysdig -r trace.scap.gz -c spy_users proc.loginshellid=5459
 

7、容器

查看機器上運行的容器列表及其資源使用情況

 sudo csysdig -vcontainers
 

查看容器上下文的進程列表

 sudo csysdig -pc
 

查看運行在wordpress1容器裏CPU的使用率

 sudo sysdig -pc -c topprocs_cpu container.name=wordpress1
 

查看運行在wordpress1容器裏網絡帶寬的使用率

 sudo sysdig -pc -c topprocs_net container.name=wordpress1
 

查看在wordpress1容器裏使用網絡帶寬最多的進程

 sudo sysdig -pc -c topprocs_net container.name=wordpress1
 

查看在wordpress1 容器裏佔用 I/O 字節最多的文件

 sudo sysdig -pc -c topfiles_bytes container.name=wordpress1
 

查看在wordpress1 容器裏網絡連接的排名情況

 sudo sysdig -pc -c topconns container.name=wordpress1
 

顯示wordpress1容器裏所有命令執行的情況

 sudo sysdig -pc -c spy_users container.name=wordpress1
 

功能是不是很強大,不過記起來有點麻煩,可以在使用的時候使用sysdig -l 查看所支持的事件列表,使用sysdig -L 查看事件所支持的過濾列表。具體也可以參看官方guide文檔

三、csysdig交互式處理

感覺上面的方法還是比較麻煩怎麼辦?可以不可以交互式選擇呢?當然沒問題,在sysdig包裏還提供了一個工具csysdig,該工具執行後,效果和top命令類似 ,見下圖:

csysdig

通過F1~F8 及crtl + F 等功能鍵可以方便的切換視圖和filter過濾。該功能可以參看youtube上的官方出的視頻 :https://www.youtube.com/watch?v=UJ4wVrbP-Q8 (需×××)

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章