jaywcjlove / jaywcjlove/reference

【备忘清单】 请求: 添加sysdig命令

Open
#113 1 comment 0 reactions 1 assignee Claimed by @jaywcjlove View on GitHub
request 已添加 欢迎补充修正 需要帮助共建
Dominant language
Dockerfile
Stars
15.3k
Forks
2.2k
Avg merge
2h 17m
Merged PRs (30d)
4

Description

SYSDIG 备忘清单
===

这个 SYSDIG 快速参考备忘清单显示了常用命令参数和使用案例清单,
注意事项:此命令需要依赖kernel-devel,且最好为系统自带的大版本,如:contso7 3.10.x,否则需要自己编译处理依赖。

入门
----

### 命令安装

```shell
sudo rpm --import https://download.sysdig.com/DRAIOS-GPG-KEY.public
sudo curl -s -o /etc/yum.repos.d/draios.repo https://download.sysdig.com/stable/rpm/draios.repo
sudo yum -y install sysdig
```

### 常用参数

| 参数 | 说明|
|----|----|
-C 5 | 每个文件不超过5M
-W 10 | 保留不超过10个文件
-G 60 | 每个文件只保留一分钟内的系统活动
-w dump.pcap | 保存到文件
-e 1000 | 每个文件只有1000个事件
-z | 参数对保存的内容进行压缩
-A --print-ascii | 把buffer中数据按照ASCII格式打印,方便阅读
-x --print-hex | 把buffer中数据按照十六进制打印
-X --printhex-ascii | 把buffer中数据同时按照ASCII格式和十六进制打印
-s 1024 | 捕获buffer的数据大小,默认为80,设置过大,文件会很大
-N | 不用把端口号转成可读名字
-r | 从文件读取

### 输出含义

| 事件 | 说明|
|---- | ----|
evt.num | 递增的事件号
evt.time | 事件发生的时间
evt.cpu | 事件被捕获时所在cpu
proc.name | 生成事件的进程名字
thread.tid | 线程id,单线程则为进程id
evt.dir | 事件方向(direction), > 代表进入事件, < 代表退出事件
evt.type | 事件的名称,比如open、stat等,一般为系统调用
evt.args | 事件的参数。如果为系统调用,则对应系统调用的参数

### chisels常用工具

| 事件 | 说明|
|----|----|
httplog | 输出所有的http请求
topprocs_cpu | 输出按照cpu使用率排序
topprocs_net | 按照网络使用情况对进程排序
fdcount_by | 按照建立连接书对进程排序
echo_fds | 输出进程读写数据
netsata | 列出网络连接情况
spy_file | 输出文件的读写数据,可以提供某个文件名作为参数
spy_ip | 抓取给定ip的数据交换
spy_port | 抓取给定端口的数据交换

### 命令帮助

```shell
sysdig -l #事件类型
sysdig -cl #chisels工具类型
```

### 常用命令案例:抓取kubernetes pod 客户端ip 为172.119.100.16 的udp请求

```shell
# 列出容器监听端口
$ sudo sysdig -pc -A -c netstat container.name=aaa

# 抓取kubernetes pod 的客户端ip为172.119.100.16,3000端口的的请求内容
$ sudo sysdig -A -c echo_fds k8s.pod.name contains datacenter-web-dev and fd.port=3000 and evt.type=read and fd.cip=172.119.100.16 fd.proto=UDP

# 按照建立连接数量对进程排序 并保存到sysdig.pcap文件中
$ sysdig -c fdcount_by fd.sport "evt.type=accept" -w sysdig.pcap
```

### 自定义输出

```shell
$ sysdig -p"user:%user.name dir:%evt.arg.path" evt.type=chdir
user:ubuntu dir:/root
user:ubuntu dir:/root/tmp
user:ubuntu dir:/root/Download
字段必须用%作为前缀,所有sysdig -l列出来的字段都可以使用
如果某个字段在时间中不存在,默认这个事件会过滤掉,在这个字符串最前面加上*符号,会打印所有事件,不存在的字段会变成
$ sysdig -p"*%evt.type %evt.dir %evt.arg.name" evt.type=open
open >
open < /proc/1285/task/1399/stat
open >
open < /proc/1285/task/1400/io
open >
open < /proc/1285/task/1400/statm
open >
```

### io案例

```shell
#查看io错误最多的进程
$ sysdig -c topprocs_errors

#查看io错误最多的文件
$ sysdig -c topfiles_errors

#查看磁盘io失败的调用
$ sysdig fd.type=file and evt.failed=true

#查看httpd打开失败的文件
$ sysdig "proc.name=httpd and evt.type=open and evt.failed=true"

#查看最花费时间的系统调用
sysdig -c topscalls_time

#查看系统调用失败返回最多的系统调用
sysdig -c topscalls "evt.failed=true"

#查看打开文件失败
sysdig -p "%12user.name %6proc.pid %12proc.name %3fd.num %fd.typechar %fd.name" evt.type=open and evt.failed=true

#打印延迟大于1ms的文件I/O调用
sysdig -c fileslower 1

#查看使用硬盘带宽最多的进程
sysdig -c topprocs_file

#列出大量使用文件描述符的进程
sysdig -c fdcount_by proc.name "fd.type=file"

#查看读写bytes最多的文件
sysdig -c topfiles_bytes

#打印httpd进程已经读取中和写入中的文件
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

#查看活跃中的读和写最多的目录
sysdig -c fdbytes_by fd.directory "fd.type=file"

#查看目录/tmp活跃中的读写最多的文件
sysdig -c fdbytes_by fd.filename "fd.directory=/tmp/"

#查看所有文件名为passwd的i/O活动
sysdig -A -c echo_fds "fd.filename=passwd"

#展示FD类型的活跃I/O
sysdig -c fdbytes_by fd.type
```

### 网络

```shell
#抓取kubernetes pod 的客户端ip为172.119.100.17,3000端口的的请求内容
$ sudo sysdig -A -c echo_fds k8s.pod.name contains datacenter-web-dev and fd.port=3000 and evt.type=read and fd.cip=172.119.100.17 fd.proto=UDP

#查看占用网络带宽最多的进程
sysdig -c topprocs_net
#显示主机192.168.0.1的网络传输数据
as binary:
sysdig -s2000 -X -c echo_fds fd.cip=192.168.0.1
as ASCII:
sysdig -s2000 -A -c echo_fds fd.cip=192.168.0.1

#查看连接最多的服务器端口
in terms of established connections:
sysdig -c fdcount_by fd.sport "evt.type=accept"
in terms of total bytes:
sysdig -c fdbytes_by fd.sport

#查看客户端连接最多的ip
in terms of established connections
sysdig -c fdcount_by fd.cip "evt.type=accept"
in terms of total bytes
sysdig -c fdbytes_by fd.cip

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

#显示 wordpress1 容器在端口 80 上发送和接收的数据:
sysdig -A -cecho_fds container.name=wordpress1 and fd.port=80

#实时打印 mysql 容器接收的所有新连接
sysdig -p"%fd.name" container.name=mysql and evt.type=accept
```

### 进程

```shell
#查看哪些文件花费时间做多
sysdig -c topfiles_time

#查看httpd进程哪些文件花费最多时间
sysdig -c topfiles_time proc.name=httpd

#查看io错误最多的进程
sysdig -c topprocs_errors

#查看io错误最多的文件
sysdig -c topfiles_errors

#查看磁盘io失败的调用
sysdig fd.type=file and evt.failed=true

#查看httpd打开失败的文件
sysdig "proc.name=httpd and evt.type=open and evt.failed=true"

#查看最花费时间的系统调用
sysdig -c topscalls_time

#查看系统调用失败返回最多的系统调用
sysdig -c topscalls "evt.failed=true"

#查看打开文件失败
sysdig -p "%12user.name %6proc.pid %12proc.name %3fd.num %fd.typechar %fd.name" evt.type=open and evt.failed=true

#打印延迟大于1ms的文件I/O调用
sysdig -c fileslower 1
```

另见
----------

- [sysdig wiki](https://github.com/draios/sysdig/wiki)_(github.com)_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.