sysdig是sysdig cloud 出品的主要基于Lua语言开发一个超强的工具,就像其在站点首页上所描述的Sysdig is open source, system-level exploration: capture system state and activity from a running Linux instance, then save, filter and analyze. Think of it as strace + tcpdump + lsof + awesome sauce.,sysdig相当于strace + tcpdump + lsof + htop + iftop 以及其他工具的合集 ,除此之外其还能对容器如docker、coreOS、LXC进行监控,是不是感觉牛皮吹的很大?到底功能如何,看下面。

注:另外该工具也支持windows平台和mac平台,由于平时工作主要涉及linux平台,有兴趣的可以自行测试。

一、安装

本篇主要介绍在centos下的用法安装和测试,后面也会提到在ubuntu类平台下的安装。其支持在centos6和centos7上安装,安装方法十分简洁:

1、centos下的安装

1.1一键安装

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

sysdig依赖的两个包,一个是kernel-devel ,一个是dkms包,DKMS全称是Dynamic Kernel Module Support (动态内核模块支持),即在内核版本变动之后可以自动重新生成新驱动模块。想要了解的可以自行GOOGLE 。

2、ubuntu下的安装

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

windows、Mac OS及其他linux发行版的安装,可以参看官方安装文档

二、常用用法

默认按上面的方法安装好以后,执行sysdig是会出错的。提示如下:

1# sysdig
2Unable to load the driver
3error 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的网络传输数据:

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

这里显示网络传输功能,实际效果和tcpdump抓包是一样的。而且其本身也支持sysdig -w dump.scap抓包保存(可以配置-X或-A使用),抓好包也支持sysdir -r dump.scap读取。

查看连接最多的服务器端口:

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

查看客户端连接最多的ip:

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

列出所有不是访问apache服务的访问连接:sysdig -p"%proc.name %fd.name" "evt.type=accept and proc.name!=httpd"

2、硬盘 I/O

 1查看使用硬盘带宽最多的进程:sysdig -c topprocs_file
 2列出使用大量文件描述符的进程sysdig -c fdcount_by proc.name "fd.type=file"
 3
 4# See the top files in terms of read+write bytes:
 5sysdig -c topfiles_bytes
 6
 7# Print the top files that apache has been reading from or writing to:
 8sysdig -c topfiles_bytes proc.name=httpd
 9
10# Basic opensnoop: snoop file opens as they occur:
11sysdig -p "%12user.name %6proc.pid %12proc.name %3fd.num %fd.typechar %fd.name" evt.type=open
12
13# See the top directories in terms of R+W disk activity
14sysdig -c fdbytes_by fd.directory "fd.type=file"
15
16# See the top files in terms of R+W disk activity in the /tmp directory
17sysdig -c fdbytes_by fd.filename "fd.directory=/tmp/"
18
19# Observe the I/O activity on all the files named 'passwd'
20sysdig -A -c echo_fds "fd.filename=passwd"
21
22# Display I/O activity by FD type
23sysdig -c fdbytes_by fd.type

3、进程和CPU使用率

1# See the top processes in terms of CPU usage
2sysdig -c topprocs_cpu
3
4# See the top processes for CPU 0
5sysdig -c topprocs_cpu evt.cpu=0
6
7# Observe the standard output of a process
8sysdig -s4096 -A -c stdout proc.name=cat

4、应用

1# 查看机器所有的HTTP请求
2sudo sysdig -s 2000 -A -c echo_fds fd.port=80 and evt.buffer contains GET
3
4# 查看机器所有的SQL select查询
5sudo sysdig -s 2000 -A -c echo_fds evt.buffer contains SELECT
6
7# See queries made via apache to an external MySQL server happening in real time
8sysdig -s 2000 -A -c echo_fds fd.sip=192.168.30.5 and proc.name=apache2 and evt.buffer contains SELECT

5、性能和错误

 1See the files where most time has been spent
 2sysdig -c topfiles_time
 3
 4See the files where apache spent most time
 5sysdig -c topfiles_time proc.name=httpd
 6
 7See the top processes in terms of I/O errors
 8sysdig -c topprocs_errors
 9
10See the top files in terms of I/O errors
11sysdig -c topfiles_errors
12
13See all the failed disk I/O calls
14sysdig fd.type=file and evt.failed=true
15
16See all the failed file opens by httpd
17sysdig "proc.name=httpd and evt.type=open and evt.failed=true"
18
19See the system calls where most time has been spent
20sysdig -c topscalls_time
21
22See the top system calls returning errors
23sysdig -c topscalls "evt.failed=true"
24
25snoop failed file opens as they occur
26sysdig -p "%12user.name %6proc.pid %12proc.name %3fd.num %fd.typechar %fd.name" evt.type=open and evt.failed=true
27
28Print the file I/O calls that have a latency greater than 1ms:
29sysdig -c fileslower 1

6、安全

 1Show the directories that the user “root” visits
 2sysdig -p"%evt.arg.path" "evt.type=chdir and user.name=root"
 3
 4Observe ssh activity
 5sysdig -A -c echo_fds fd.name=/dev/pretmx and proc.name=sshd
 6
 7Show every file open that happens in /etc
 8sysdig evt.type=open and fd.name contains /etc
 9
10Show the ID of all the login shells that have launched the "tar" command
11sysdig -r file.scap -c list_login_shells tar
12
13Show all the commands executed by the login shell with the given ID
14sysdig -r trace.scap.gz -c spy_users proc.loginshellid=5459

7、容器

 1查看机器上运行的容器列表及其资源使用情况
 2 sudo csysdig -vcontainers
 3
 4查看容器上下文的进程列表
 5 sudo csysdig -pc
 6
 7查看运行在wordpress1容器里CPU的使用率
 8 sudo sysdig -pc -c topprocs_cpu container.name=wordpress1
 9
10查看运行在wordpress1容器里网络带宽的使用率
11 sudo sysdig -pc -c topprocs_net container.name=wordpress1
12
13查看在wordpress1容器里使用网络带宽最多的进程
14 sudo sysdig -pc -c topprocs_net container.name=wordpress1
15
16查看在wordpress1 容器里占用 I/O 字节最多的文件
17 sudo sysdig -pc -c topfiles_bytes container.name=wordpress1
18
19查看在wordpress1 容器里网络连接的排名情况
20 sudo sysdig -pc -c topconns container.name=wordpress1
21
22显示wordpress1容器里所有命令执行的情况
23 sudo sysdig -pc -c spy_users container.name=wordpress1

功能是不是很强大,不过记起来有点麻烦,可以在使用的时候使用sysdig -l 查看所支持的事件列表,使用sysdig -L 查看事件所支持的过滤列表。具体也可以参看官方guide文档

三、csysdig交互式处理

感觉上面的方法还是比较麻烦怎么办?可以不可以交互式选择呢?当然没问题,在sysdig包里还提供了一个工具csysdig,该工具执行后,效果和top命令类似 ,见下图:

csysdig
csysdig

通过F1~F8 及crtl + F 等功能键可以方便的切换视图和filter过滤。该功能可以参看youtube上的官方出的视频 :https://www.youtube.com/watch?v=UJ4wVrbP-Q8 (需翻墙)