Sysdig is an open-source, cross-platform, powerful and flexible system monitoring and troubleshooting tool for Linux; it also works on Windows and Mac OSX but with limited functionality and can be used for system analysis, inspection and debugging.

Normally, you would employ a mix of various ​​Linux performance monitoring and troubleshooting tools​​ including these ones listed below to perform the Linux monitoring and debugging tasks:

  1. strace – discover system calls and signals to a process.
  2. ​tcpdump​​ – raw network traffic monitoring.
  3. ​netstat​​ – network connections monitoring.
  4. ​htop​​ – real time process monitoring.
  5. ​iftop​​ – real time network bandwidth monitoring.
  6. ​lsof​​ – view which files are opened by which process.

However, sysdig integrates what all the above tools and many more, offer in a single and simple program, more so with amazing container support. It enables you to capture, save, filter and examine the real behavior (stream of events) of Linux systems as well as containers.

It comes with a command line interface and a powerful interactive UI (csysdig) which allow you to watch system activity in real time, or perform a trace dump and save for later analysis. 

Sysdig Features:

  • It is fast, stable and easy-to-use with comprehensively well documented.
  • Comes with native support for container technologies, including Docker, LXC.
  • It is scriptable in Lua; offers chisels (lightweight Lua scripts) for processing captured system events.
  • Supports useful filtering of output.
  • Supports system and application tracing.
  • It can be integrated with Ansible, Puppet and Logstash.
  • Enable sample advanced log analysis.
  • It also offers Linux server attack (forensics) analysis features for ethical hackers and lot’s more.

 

分析容器系统调用:Sysdig



Sysdig:一个非常强大的系统监控、分析和故障排查工具。 汇聚 strace+tcpdump+htop+iftop+lsof 工具功能于一身!(核心的功能就是将工具整合在一块)




sysdig 除了能获取系统资源利用率、进程、网络连接、系统调用等信息, 还具备了很强的分析能力,例如:



• 按照CPU使用率对进程排序



• 按照数据包对进程排序



• 打开最多的文件描述符进程



• 查看进程打开了哪些文件



• 查看进程的HTTP请求报文



• 查看机器上容器列表及资源使用情况



sysdig 通过在内核的驱动模块注册系统调用的 hook,这样当有系统调用发生和完成的时候,它会把系统调用信息拷贝到特定的buffer,然后用户态组件对数据信息处理(解压、解析、过滤等),


并最终通过 sysdig 命令行和用户进行交互。


 

分析容器系统调用:Sysdig_ios

 哪台机器有问题就可以去安装,使用这个工具

rpm --import https://s3.amazonaws.com/download.draios.com/DRAIOS-GPG-KEY.public 
curl -s -o /etc/yum.repos.d/draios.repo https://s3.amazonaws.com/download.draios.com/stable/rpm/draios.repo
yum install epel-release -y
yum install sysdig -y /usr/bin/sysdig-probe-loader # 加载驱动模块


[root@master ~]# lsmod | grep ip_vs
ip_vs_sh 12688 0
ip_vs_wrr 12697 0
ip_vs_rr 12600 37
ip_vs 145458 43 ip_vs_rr,ip_vs_sh,ip_vs_wrr
nf_conntrack 139264 10 ip_vs,xt_CT,nf_nat,nf_nat_ipv4,nf_nat_ipv6,xt_conntrack,nf_nat_masquerade_ipv4,nf_conntrack_netlink,nf_conntrack_ipv4,nf_conntrack_ipv6
libcrc32c 12644 3 ip_vs,nf_nat,nf_conntrack
[root@master ~]# lsmod | grep netfilter
br_netfilter 22256 0
bridge 151336 1 br_netfilter
[root@master ~]# lsmod | grep sysdig
sysdig_probe 639433 0


sysdig常用参数:


• -l, --list:列出可用于过滤和输出的字段


• -M <num_seconds> :多少秒后停止收集


• -p <output_format>, --print=<output_format> :指定打印事件时使用的格式


• 使用-pc或-pcontainer 容器友好的格式


• 使用-pk或-pkubernetes k8s友好的格式


• -c <chiselname> <chiselargs>:指定内置工具,可直接完成具体的数据聚合、分析工作


• -w <filename>:保存到文件中


• -r <filename>:从文件中读取



执行sysdig命令,实时输出大量系统调用。


示例:59509 23:59:19.023099531 0 kubelet (1738) < epoll_ctl


格式:%evt.num %evt.outputtime %evt.cpu %proc.name (%thread.tid) %evt.dir %evt.type %evt.info


• evt.num: 递增的事件号


• evt.time: 事件发生的时间


• evt.cpu: 事件被捕获时所在的 CPU,也就是系统调用是在哪个 CPU 执行的


• proc.name: 生成事件的进程名字


• thread.tid: 线程的 id,如果是单线程的程序,这也是进程的 pid


• evt.dir: 事件的方向(direction),> 代表进入事件,< 代表退出事件


• evt.type: 事件的名称,比如 open、stat等,一般是系统调用


• evt.args: 事件的参数。如果是系统调用,这些对应着系统调用的参数


自定义格式输出:sysdig -p "user:%user.name time:%evt.time proc_name:%proc.name"


[root@master ~]# sysdig -p "user:%user.name time:%evt.time proc_name:%proc.name" -M 3 -w xxx
[root@master ~]# sysdig -r xxx

sysdig过滤:


• fd:根据文件描述符过滤,比如 fd 标号(fd.num)、fd 名字(fd.name)


• process:根据进程信息过滤,比如进程 id(proc.id)、进程名(proc.name)


• evt:根据事件信息过滤,比如事件编号、事件名


• user:根据用户信息过滤,比如用户 id、用户名、用户 home 目录


• syslog:根据系统日志过滤,比如日志的严重程度、日志的内容


• container:根据容器信息过滤,比如容器ID、容器名称、容器镜像


查看完整过滤器列表:sysdig -l



示例:


1、查看一个进程的系统调用


sysdig proc.name=kubelet


2、查看建立TCP连接的事件


sysdig evt.type=accept


3、查看/etc目录下打开的文件描述符


sysdig fd.name contains /etc


4、查看容器的系统调用


sysdig -M 10 container.name=web


注:还支持运算操作符,=、!=、>=、>、<、


<=、contains、in 、exists、and、or、not



Chisels: 实用的工具箱,一组预定义的功能集合,用来分析特定的场景。


sysdig –cl 列出所有Chisels,以下是一些常用的:


• topprocs_cpu:输出按照 CPU 使用率排序的进程列表,例如sysdig -c


• topprocs_net:输出进程使用网络TOP


• topprocs_file:进程读写磁盘文件TOP


• topfiles_bytes:读写磁盘文件TOP


• netstat:列出网络的连接情况


 

分析容器系统调用:Sysdig_ios_02