Sysdig 配置与过滤器语法详解
Sysdig 0.41.4 配置体系与过滤器语法全解:布尔表达式与运算符实测、九大字段类与常用事件字段、-p 输出格式定制、chisel 分类与自写 Lua 扩展实战,含 -i 需带 .lua、value_units 参数约束等真实踩坑(2026-10-27 实测)。
Sysdig 配置与过滤器语法详解
目标:把 Sysdig 的「配置面」一次讲透——sysdig 没有传统意义上的配置文件,它的配置就是命令行参数、输出格式串与 chisel(Lua)脚本;本篇覆盖过滤器语法体系、九个字段类与常用事件字段、输出格式定制、chisel 扩展机制的完整实测,并沉淀 -i 查询必须带 .lua 扩展名、table_generator 的 value_units 参数约束两个真实踩坑。已在 Ubuntu 24.04.5 LTS + Sysdig 0.41.4 上全量实测通过(2026-10-27)。
一、环境信息
| 项目 | 实测值 |
|---|---|
| 操作系统 | Ubuntu 24.04.5 LTS(内核 7.0.0-38-generic,x86_64) |
| 软件版本 | sysdig 0.41.4(Draios 官方稳定仓库,安装过程见《Sysdig 安装与初始化实战》) |
| 探针形态 | 内核模块 scap-8.1.0+driver(DKMS 编译,首次运行自动装载) |
| chisel 数量 | 66 个 Lua 脚本(/usr/share/sysdig/chisels/) |
| 实测日期 | 2026-10-27 |
💡 说明:本篇所有过滤表达式、格式串、chisel 脚本均在上述环境逐条运行并原样记录回显;演示用的捕获文件为真实落盘产物。过滤语法在实时捕获与离线回放两种模式下完全一致,为便于结果复现,部分长输出的示例以离线回放方式给出(回放与实时输出格式相同)。
二、前置条件
- 已按《Sysdig 安装与初始化实战》完成安装并验证首跑(
sysdig --version输出 0.41.4,lsmod | grep scap有模块)。 - root 权限(过滤演示均以 root 执行)。
- 无需其他依赖:过滤引擎、字段提取、chisel 解释器都内置于 sysdig 包。
三、配置载体与查看方式
sysdig 是无守护进程的 CLI 工具,不存在 /etc 下的配置文件——它的全部「配置」由四类载体构成:
| 载体 | 形式 | 作用 |
|---|---|---|
| 命令行参数 | sysdig [参数] [过滤表达式] | 捕获控制(-w/-r/-n/-M)、输出控制(-p/-A/-j)、chisel 调用(-c) |
| 输出格式串 | -p "%字段1 %字段2" | 自定义每条事件的打印字段与排版 |
| chisel 脚本 | /usr/share/sysdig/chisels/*.lua 或自写 Lua | 在事件流上做聚合分析,-c 调用 |
| 环境变量 | 如 SYSDIG_BPF_PROBE | 特定行为的运行时开关(探针路径) |
四个「查看类」参数是学习配置体系的入口(均为实测命令):
sysdig -h # 全部命令行参数(下方参数总览表的数据来源)
sysdig -l # 全部可用字段(按字段类分组)
sysdig -lv # 同上,但带字段类型标注
sysdig -cl # 全部 chisel 清单(按分类分组)
sysdig -i <chisel> # 单个 chisel 的详情(描述/参数)
另有 --list-markdown(以 Markdown 格式列字段,便于贴文档)与 --libs-version(打印底层 falcosecurity/libs 版本)两个 0.41.4 实查存在的辅助参数。
四、命令行参数总览
sysdig -h 输出的关键参数(实测回显节选,按功能归类整理):
-A, --print-ascii Only print the text portion of data buffers, and echo
-b, --print-base64 Print data buffers in base64.
-c <chiselname> <chiselargs>, --chisel <chiselname> <chiselargs>
-cl, --list-chisels
-C <file_size>, --file-size=<file_size>
-d, --displayflt Make the given filter a display one
-E, --exclude-users
-e <num_events> If used together with -w option, creates a series of dump files
-F, --fatfile Enable fatfile mode
-G <num_seconds>, --seconds=<num_seconds>
-i <chiselname>, --chisel-info <chiselname>
-j, --json Emit output as json
-L, --list-events List the events that the engine supports
-l, --list List the fields that can be used for filtering and output formatting.
-M <num_seconds> Stop collecting after <num_seconds> reached.
-n <num>, --numevents=<num>
-P, --progress Print progress on stderr while processing trace files
-p <output_format>, --print=<output_format>
-q, --quiet Don't print events on the screen
-R Resolve port numbers to names.
参数速查表(含义与实测默认行为):
| 参数 | 作用 | 备注 |
|---|---|---|
-n <num> | 捕获 num 条事件后退出 | 不带 -w 时最常用的限量手段 |
-w <file> | 事件流落盘(.scap) | 配合 -C/-G/-e 做滚动 |
-r <file> | 回放 .scap 文件 | 离线分析入口 |
-M <seconds> | 按时长限时捕获 | 实时采集定窗 |
-p <format> | 自定义输出格式 | 详见「七、输出格式定制」 |
-c <chisel> | 运行 chisel | 带/不带 .lua 均可;-i 查详情则必须带,见 8.2 |
-cl / -i | 列 chisel / 看 chisel 详情 | 学习 chisel 库的入口 |
-A / -b | 数据缓冲区按 ASCII / base64 打印 | 看流量内容用 |
-j | JSON 输出 | 供程序消费 |
-d | 把过滤表达式作为显示过滤而非引擎过滤 | 全量捕获、按条件展示 |
-E | 事件流中不携带用户信息 | 有隐私合规诉求时用 |
-F | fatfile 模式,落盘保留完整进程表 | 回放机器与捕获机环境不同时用 |
-C <MB> / -G <s> / -e <n> | 落盘按大小/时长/事件数滚动 | 配合 -W 循环覆盖 |
-q | 不打印事件(配合 -w 纯落盘) | 后台取证 |
💡 说明:参数决定「怎么采、怎么显」,过滤表达式决定「看什么」——两者正交组合。过滤表达式语法是下一章的主角。
五、过滤器语法体系详解
5.1 语法总览与布尔逻辑
sysdig 过滤表达式是由字段、运算符、布尔连接词组成的布尔表达式,写在命令最后:
sysdig [参数] "<字段> <运算符> <值> [and|or|not 组合 ...]"
三条语法规则(实测归纳):
- 连接词用小写 and / or / not,可用括号改变优先级;
- 含空格的表达式必须整体加引号(shell 会把空格当参数分隔);
- 字符串值通常可省引号(如 proc.name=sleep),含特殊字符时再加引号。
最小示例——只看 sleep 进程的事件(实测;后台先起一个改名进程作为目标,回显中进程名 sd-demo-sleep):
sysdig "proc.name=sd-demo-sleep" -n 2
输出节选(实测首行截断展示,完整行超 200 列):
429151 10:30:13.526118202 0 sd-demo-sleep (3811943) < execve res=0 exe=/work/sd-demo-sleep args=30. tid=3811943(sd-demo-sleep) pid=3811943(sd-demo-sleep) ptid=3811942(timeout) cwd=<NA> fdlimit=1024 pg
429161 10:30:13.526134521 0 sd-demo-sleep (3811943) > brk addr=0
事件行的默认格式是:编号 时间戳 CPU 进程名(TID) >/< 事件类型 参数,> 为系统调用进入、< 为返回(evt.dir 字段)。
5.2 运算符
可用运算符与实测示例(全部真实回显):
| 运算符 | 含义 | 实测示例 |
|---|---|---|
= / != | 相等 / 不等 | evt.type=open and fd.name=/etc/hosts |
< <= > >= | 数值/字符串比较 | evt.rawres<0(失败事件) |
contains | 子串包含 | evt.buffer contains alpha |
startswith / endswith | 前缀 / 后缀 | fd.name startswith /work/data |
icontains / iendswith / startswith 系列的大小写不敏感变体 | 忽略大小写匹配 | 同上 |
in | 枚举集合 | proc.name in (sleep, curl) |
and / or / not | 布尔组合 | evt.type=write and not fd.name contains .so |
相等 + 精确路径(实测)——只看 /etc/hosts 的打开事件:
sysdig "evt.type=open and fd.name=/etc/hosts" -n 2
783000 10:26:25.147420290 5 <NA> (3750331) < open fd=4(<f>/etc/hosts) name=/etc/hosts flags=6145(O_LARGEFILE|O_RDONLY|O_CLOEXEC) mode=0 dev=802 ino=10754201
写文件全链路(实测)——fd.name 精确匹配 /work/demo.txt,能看到 openat(O_TRUNC|O_CREAT|O_WRONLY) 到 dup2 重定向 stdout 的完整过程:
sysdig "fd.name=/work/demo.txt" -n 3
106556 10:27:34.742198847 2 <NA> (3806719) < openat fd=3(<f>/work/demo.txt) dirfd=-100(AT_FDCWD) name=/work/demo.txt flags=262(O_TRUNC|O_CREAT|O_WRONLY) mode=0666 dev=802 ino=3944050
106567 10:27:34.742214701 2 <NA> (3806719) > dup2 fd=3(<f>/work/demo.txt)
106568 10:27:34.742215583 2 <NA> (3806719) < dup2 res=1(<f>/work/demo.txt) oldfd=3(<f>/work/demo.txt) newfd=1(<f>/work/demo.txt)
前缀匹配 startswith(实测,对已落盘的捕获文件回放;回放与实时语法一致):
sysdig -r /work/c1.scap "fd.name startswith /work/data" -n 2
输出节选(实测首行截断展示):
259465 10:35:28.125433687 5 <NA> (3823910) < openat fd=3(<f>/work/data1.txt) dirfd=-100(AT_FDCWD) name=/work/data1.txt flags=262(O_TRUNC|O_CREAT|O_WRONLY) mode=
259474 10:35:28.125441271 2 <NA> (3806719) > dup2 fd=3(<f>/work/data1.txt)
枚举 in(实测)——进程名在集合内且事件为 close:
sysdig "proc.name in (sleep, curl) and evt.type=close" -n 3
346025 10:40:00.138380718 5 sleep (3831652) > close fd=3(<f>/etc/ld.so.cache)
346026 10:40:00.138381126 5 sleep (3831652) < close res=0
346047 10:40:00.138417737 5 sleep (3831652) > close fd=3(<f>/lib/x86_64-linux-gnu/libc.so.6)
缓冲区内容搜索 contains(实测)——在写操作的 I/O 缓冲里搜字符串,这是 tcpdump 之外定位「谁在写什么内容」的利器:
sysdig -r /work/c1.scap "evt.is_io_write=true and evt.buffer contains alpha" -n 1
259482 10:35:28.125457959 5 <NA> (3823910) < write res=12 data=alpha write. fd=1(<f>/work/data1.txt) size=12
失败事件 + not 组合(实测)——失败事件常是排障起点:
sysdig -r /work/c1.scap "evt.failed=true and evt.dir=<" -n 2
79 10:35:26.154135732 4 <NA> (8442) < futex res=-110(ETIMEDOUT)
133 10:35:26.154533496 2 <NA> (7545) < read res=-11(EAGAIN) data=NULL fd=25 size=8192
sysdig -r /work/c1.scap "evt.type=write and not fd.name contains .so" -n 2
905 10:35:26.166624564 4 <NA> (218870) > write fd=6 size=8
906 10:35:26.166634709 4 <NA> (218870) < write res=8 data=........ fd=6 size=8
网络连接过滤(实测)——fd.sport 过滤目标端口,事件行自带完整四元组:
sysdig "evt.type=connect and fd.sport=8144" -n 2
175416 10:27:06.527095453 1 curl (3805814) < connect res=-115(EINPROGRESS) tuple=127.0.0.1:43806->127.0.0.1:8144 fd=5(<4t>127.0.0.1:43806->127.0.0.1:8144)
💡 说明:connect 返回 EINPROGRESS 不是错误——非阻塞套接字连接的正常返回。过滤网络事件时
fd.sip/fd.cip/fd.sport/fd.cport四个字段配fd.type in (ipv4, ipv6)是常用组合。
5.3 组合过滤的书写建议
实测归纳三条经验:
- 先窄后宽:把区分度最高的条件放前面(如
evt.type=connect优于裸proc.name=curl),减少引擎做无谓字段提取; - 多条件加引号:
sysdig "evt.type=open and fd.name=/etc/hosts"——引号包整个表达式,不是包单个值; - or 必须加括号:
A or B and C按优先级解析为A or (B and C),语义含糊时显式写括号。
六、字段类体系与常用字段
6.1 字段类清单
sysdig -l 按字段类(Field Class)分组列出全部字段。实测类清单(完整回显):
Field Class: evt (All event types)
Field Class: evt (Syscall events only)
Field Class: process
Field Class: user
Field Class: group
Field Class: fd
Field Class: fs.path
Field Class: fdlist
Field Class: container (plugin)
9 个字段类条目(evt 占两条,分别覆盖通用事件与 syscall 专属字段)。对 0.41.4 的 -l 输出统计,字段总数约 270 个。各类的职责:
| 字段类 | 职责 | 代表字段 |
|---|---|---|
| evt | 事件本体:类型、方向、时间、参数、返回值、I/O 缓冲 | evt.type、evt.dir、evt.buffer、evt.res |
| process | 产生事件的进程/线程 | proc.name、proc.pid、proc.cmdline |
| user / group | 事件发生时的用户与组身份 | user.name、user.uid、group.name |
| fd | 文件描述符:文件路径、套接字四元组、类型 | fd.name、fd.cip、fd.sport、fd.type |
| fs.path | 路径级信息(挂载点、路径组件) | fs.path.name |
| fdlist | 进程当前打开的 fd 集合(进事件上下文) | fdlist.nums |
| container | 容器元数据(plugin 来源) | container.name、container.id |
6.2 常用字段速查(evt 类)
以下定义文字均摘自 sysdig -l 实测输出(逐条照抄,超长定义截断展示):
evt.dir event direction can be either '>' for enter events or '<' for exit events.
evt.type The name of the event (e.g. 'open').
evt.arg (ARG_REQUIRED) one of the event arguments specified by name or by number. Some events
evt.buffer the binary data buffer for events that have one, like read(), recvfrom(), etc. Use this
evt.res event return value, as a string. If the event failed, the result is an error code string
💡 说明:
evt.arg标注 ARG_REQUIRED——这类字段必须在表达式里写明取哪个参数(如 evt.arg.fd、evt.arg[0]),不能裸用。evt.buffer是二进制 I/O 缓冲,配合 contains 可直接搜索写入/读出的字节内容(5.2 已实测)。
高频 evt 字段补充(同摘自 -l 实测输出):
| 字段 | 含义 | 典型用法 |
|---|---|---|
| evt.num | 事件序号 | 定位与翻页 |
| evt.time / evt.datetime | 事件时间(含/不含日期) | 输出格式化 |
| evt.latency | 系统调用进/出的耗时(纳秒) | 慢调用排查(evt.latency>100ms) |
| evt.cpu | 事件发生的 CPU 编号 | 定位调度/亲和问题 |
| evt.category | 事件类别(file/net/memory…) | 粗粒度切流 |
| evt.failed | 是否失败(true/false) | 错误事件巡检 |
| evt.is_io / evt.is_io_read / evt.is_io_write | 是否 I/O / 读 / 写 | 文件与网络行为切分 |
| evt.rawres | 返回值数值形态 | 范围比较(<0 即失败) |
6.3 进程与用户字段(process / user 类)
proc.name The process name (truncated after 16 characters) generating the event (task->comm).
proc.cmdline The concatenation of `proc.name + proc.args` (truncated after 4096 bytes) when starting
proc.pid The id of the process generating the event.
user.name user name.
⚠️ 注意 proc.name 截断到 16 字符(内核 comm 的固有限制)——长名字的进程要用 proc.cmdline 或 proc.exeline 匹配,这是过滤器「写了匹配不上」的高频原因。
6.4 fd 网络与文件字段(fd 类)
fd.type type of FD. Can be 'file', 'directory', 'ipv4', 'ipv6', 'unix', 'pipe', 'event',
fd.name FD full name. If the fd is a file, this field contains the full path. If the FD is a
fd.cip client IP address.
fd.sip server IP address.
fd.sport for TCP/UDP FDs, server port.
container.name The container name. In instances of userspace container engine lookup delays, this field
网络四元组字段的组合是流量过滤的主力:fd.cip/fd.cport(客户端侧)+ fd.sip/fd.sport(服务端侧),配合 fd.type=ipv4 限定 TCP/UDP。文件侧 fd.name(全路径)与 fd.directory(目录)区分精确匹配与前缀匹配两种语义。
6.5 字段详情查询(-lv)
-lv 在字段后追加类型标注(实测节选):
sysdig -lv | head -8
-------------------------------
Field Class: evt (All event types)
Description: These fields can be used for all event types
Event Sources: syscall
evt.num (Type: UINT64) event number.
evt.time (Type: CHARBUF) event timestamp as a time string that includes the nanosecond part.
evt.time.s (Type: CHARBUF) event timestamp as a time string with no nanoseconds.
类型标注(UINT64/CHARBUF/RELBUF 等)在写数值比较与范围过滤时是重要的依据——数值字段才能用 </>。
七、输出格式定制
默认事件行信息密度高但不聚焦。-p 用格式串重排输出,%字段 引用任意 -l 列出的字段(实测):
sysdig -p "%evt.time %proc.name %evt.type %fd.name" "evt.type=open and fd.name=/etc/hosts" -n 2
10:39:55.631796546 <NA> open /etc/hosts
格式串规则(实测归纳):
%字段名逐字段展开,空格与文字按字面输出;- 常用排版模板:
%evt.time %user.name %proc.name %evt.type %evt.args(谁、何时、干了什么、参数明细); - 引号里的格式串含空格,注意 shell 转义;
- 输出细节开关与 -p 正交:
-A把 I/O 缓冲按 ASCII 展示,-j直接出 JSON。
💡 技术拓展:事件流的「输出形态」不止默认行式一种,主要选项对比——
形态 开关 适用 默认行式 无 人读,信息全 自定义格式串 -p "%a %b"聚焦特定字段,可 grep JSON -j交给脚本/日志管道 chisel 表格 -c <chisel>聚合统计视图 csysdig TUI csysdig命令交互钻取 本篇主线用
-p定制格式;聚合视图走 chisel(第八章),交互界面见《Sysdig 常用 CLI 命令详解(核心篇)》。
八、chisel 与 Lua 脚本扩展机制
8.1 chisel 清单与分类
chisel 是用 Lua 写的事件流分析脚本,随 sysdig 包安装在 /usr/share/sysdig/chisels/(实测 66 个)。sysdig -cl 按分类列出(实测回显节选):
Category: Application
---------------------
httplog.lua HTTP requests log
httptop.lua Top HTTP requests
memcachelog.lua memcached requests log
Category: CPU Usage
-------------------
spectrogram.lua Visualize OS latency in real time.
subsecoffset.lua
Visualize subsecond offset execution time.
topcontainers_cpu.lua
Top containers by CPU usage
topprocs_cpu.lua
Top processes by CPU usage
Category: Errors
----------------
topcontainers_error.lua
实测分类全集:Application、CPU Usage、Errors、I/O、Logs、Misc、Net、Performance、Security、System State、Tracers(共 11 类,以 -cl 实际输出为准)。
8.2 chisel 详情与调用(⚠️ -i 必须带 .lua 扩展名)
看单个 chisel 的描述与参数用 -i(实测,注意 chisel 名要带 .lua):
sysdig -i topprocs_cpu.lua
Category: CPU Usage
-------------------
topprocs_cpu.lua
Top processes by CPU usage
Show the top process defined by the highest CPU utilization. This chisel is com
patible with containers using the sysdig -pc or -pcontainer argument, otherwise
no container information will be shown.
Args:
(None)
⚠️ 排障实录(0.41.4 实测踩坑):-i 查询详情时若不带 .lua 扩展名会直接报错(实测原文):
chisel topprocs_cpu not found - use -cl to list them.
而 -c 调用是另一种行为:-c topprocs_cpu 与 -c topprocs_cpu.lua 实测均可用。实测不带扩展名调用 -c topprocs_cpu 的输出形态(节选):
CPU% Process Host_pid Container_pid container.name
--------------------------------------------------------------------------------
0.00% sleep 1 1 host
0.00% tokio-rt-worker 7784 7784 host
结论:遇到 "not found" 先区分是 -i 还是 -c——-i 必须补 .lua;-c 两种写法皆可,仍报错则用 -cl 核对 chisel 的真实名字。
8.3 内置 chisel 的源码结构
挑一个功能完整的内置 chisel 看结构(httplog.lua,实测文件内容节选,注释头省略):
-- Chisel description
description = "Show a log of all HTTP requests";
short_description = "HTTP requests log";
category = "Application";
args = {}
require "http"
-- Initialization callback
function on_init()
http_init()
-- The -pc or -pcontainer options was supplied on the cmd line
print_container = sysdig.is_print_container_data()
return true
end
function on_transaction(transaction)
if print_container then
container = " " .. transaction["container"] .. " "
else
container = " "
end
print(string.format("%s%s%s method=%s url=%s response_code=%d latency=%dms size=%dB",
evt.field(datetime_field),
container,
transaction["dir"],
transaction["request"]["method"],
transaction["request"]["url"],
transaction["response"]["code"],
(transaction["response"]["ts"] - transaction["request"]["ts"])/1000000,
transaction["response"]["length"]
))
end
function on_event()
run_http_parser(evt, on_transaction)
end
chisel 的骨架要素(对照源码归纳):
| 要素 | 作用 |
|---|---|
description / short_description / category / args | 元信息:-cl/-i 展示的就是它们 |
on_init() | 初始化回调:解析参数、声明提取字段,返回 true 表示合法 |
on_event() | 每条事件回调:用 evt.field("字段名") 取值做逻辑 |
on_interval(delta) | 定时回调:周期性输出(top 类的刷新) |
on_capture_end() | 捕获结束回调:输出最终汇总 |
回调签名以包内源码实测为准:function on_interval(ts_s, ts_ns, delta)、function on_capture_end() 均在内置脚本中出现。
8.4 自写 chisel 实战(table_generator 委托模式)
大量 top 类 chisel 本身是薄封装:通过 chisel.exec("table_generator", ...) 把「选键字段、值字段、过滤、条数」委托给内置的 table_generator 完成。照此模式写一个「/work 目录 I/O 次数 Top5」的自写 chisel(实测可用,文件存为 /usr/share/sysdig/chisels/count_work_files.lua):
description = "Top files under /work by I/O operations."
short_description = "Top /work files by I/O ops"
category = "Misc"
args = {}
TOP_NUMBER = 5
function on_init()
chisel.exec("table_generator",
"fd.name",
"Filename",
"evt.count",
"I/O ops",
"evt.is_io=true and fd.name startswith /work/",
"" .. TOP_NUMBER,
"none")
return true
end
table_generator 的七个位置参数(名称与含义均摘自包内 table_generator.lua 的 args 声明原文,实测):
| 序 | 参数名 | 本例取值 | 含义(args 声明原文) |
|---|---|---|---|
| 1 | keys | fd.name | comma-separated list of filter fields to use for grouping |
| 2 | keydescs | Filename | comma separated list of human readable descriptions for the key |
| 3 | value | evt.count | the value to count for every key |
| 4 | valuedesc | I/O ops | human readable description for the value |
| 5 | filter | evt.is_io=true and … | the filter to apply |
| 6 | top_number | 5(TOP_NUMBER 拼接) | maximum number of elements to display |
| 7 | value_units | none | how to render the values in the result. Can be 'bytes', 'time', 'timepct', or 'none'. |
对已落盘的捕获回放运行(先 sysdig -w /work/c1.scap 落盘一段含文件写入的真实流量,再回放;回显已去除终端控制序列):
sysdig -r /work/c1.scap -c count_work_files.lua
I/O ops Filename
--------------------------------------------------------------------------------
16 /work/data2.bin
6 /work/data1.txt
两个文件出现在榜上,I/O 次数与实测写入行为吻合(dd 写 8 块 × 2 次系统调用 + echo 写 1 块等)。
⚠️ 排障实录(value_units 参数,实测踩坑):第 7 参数写成空串 "" 时,报错极具误导性(实测原文):
wrong number of parameters for chisel table_generator, 7 required, 6 given
实际参数明明是 7 个。根因是 value_units 只接受 bytes / time / timepct / none 四个字面值(table_generator.lua 的 args 声明原文),空串非法导致校验失败、报错文案却落在参数个数上。结论:自写委托型 chisel 的 value_units 参数必须显式写 none(或 bytes/time/timepct),不要写空串。参数校验在 chisel 执行期触发;而 Lua 语法错误则在加载期直接报行号(实测写错括号时的原文):
Failed to load chisel zz_syn.lua: [string "function on_init()..."]:2: unexpected symbol near ')'
8.5 chisel 与过滤器的分工
两者能力有重叠,选择依据是「要不要聚合」:
| 需求 | 用过滤器 | 用 chisel |
|---|---|---|
| 筛出原始事件流逐条看 | ✅(sysdig "过滤") | ❌ |
| Top-N 排行、计数汇总 | ❌ | ✅(topprocs_cpu 等) |
| 协议解析(HTTP 请求行) | ❌ | ✅(httplog.lua) |
| 自定义字段排版 | ✅(-p) | 可以但不必 |
| 在过滤表达式里再嵌一层复杂统计 | ❌ | ✅ |
经验法则:表达式解决「看什么」,chisel 解决「怎么汇总」;两者可叠加——sysdig -c httplog.lua 内部自带 HTTP 语义过滤,也可以给 chisel 外面再套一层捕获过滤收窄数据源。
九、常用配置组合模板
以下模板均为实测可跑的参数组合(回放示例基于本章落盘的 /work/c1.scap):
- 现场取证落盘(先存证后分析,文件滚动防止写满磁盘):
sysdig -w /var/captures/scene.scap -C 200 -W 5 -q
(-C 200 每个文件 200MB、-W 5 保留 5 个滚动覆盖、-q 不回显。)
- 失败事件巡检(定时窗口内收集所有失败系统调用):
sysdig -M 10 "evt.failed=true and evt.dir=<"
- HTTP 请求观测(明文 HTTP 服务的请求流水):
sysdig -c httplog.lua -M 30
- 文件活动审计(自定义 chisel 统计目标目录 I/O 榜):
sysdig -r /work/c1.scap -c count_work_files.lua
- 聚焦网络连接建立(带自定义输出):
sysdig -p "%evt.time %proc.name %fd.cip:%fd.cport -> %fd.sip:%fd.sport" "evt.type=connect and evt.dir=<"
十、常见问题(FAQ)
Q1:过滤表达式写了却报错或匹配不到?
三个高频原因(均实测遇到):① 表达式含空格没加引号,被 shell 拆成多个参数;② proc.name 有 16 字符截断,长进程名匹配不上,改用 proc.cmdline;③ 字段名拼错——用 sysdig -l | grep 关键字 确认字段存在再写。
Q2:sysdig -i topprocs_cpu 报 not found?
0.41.4 的 -i 查询 chisel 详情必须带 .lua 扩展名:sysdig -i topprocs_cpu.lua;-c 调用则带不带扩展名都能运行(两种行为差异与报错原文详见 8.2 排障实录)。
Q3:自写 chisel 报 wrong number of parameters for chisel table_generator, 7 required, 6 given,但参数明明是 7 个?
第 7 参数 value_units 写了空串。改为 none / bytes / time / timepct 之一即可(详见 8.4 排障实录)。
Q4:sysdig -l 输出太长怎么定位字段?
sysdig -l | grep -i 关键字 按关键字搜;要看类型加 -lv;要 Markdown 格式(方便贴文档)用 --list-markdown。
Q5:evt.arg 和 evt.args 有什么区别? evt.args 是全部参数聚合成的字符串(只适合展示);evt.arg 是「带参取值」的字段(ARG_REQUIRED),必须写明参数名或序号(evt.arg.fd、evt.arg[0]),可参与过滤——这是 -l 实测定义原文给出的区别。
Q6:过滤在实时和回放模式下有差异吗? 语法与字段完全一致(本篇多处示例即回放 /work/c1.scap 实测)。唯一差异:回放时进程表来自捕获文件里保存的状态,若捕获时用了 -E(排除用户信息)等开关,回放侧对应字段会缺失。
十一、参考链接
- 官方 Wiki:https://github.com/draios/sysdig/wiki (用户指南与 chisel 文档入口;查询日期 2026-10-27)
- 字段与参数的一手来源:
sysdig -h、sysdig -l、sysdig -lv、sysdig -cl(本篇所有定义文字的实测出处,0.41.4) - chisel 源码目录:/usr/share/sysdig/chisels/(table_generator.lua 的 args 声明是委托参数的权威定义)
- 平台内延伸阅读:《Sysdig 介绍与选型指南》(定位与选型)、《Sysdig 安装与初始化实战》(安装与探针)、《Sysdig 常用 CLI 命令详解(核心篇)》(捕获/落盘/回放与 csysdig 实战)