サーバのトラブルシューティングにsysdigが使えそう

sysdig とは?

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. With a little Lua cherry on top.

http://www.sysdig.org/

上に書いてある通り、一言で言うと strace + tcpdump + lsof + α。tcpdumpのように-wで書き出して-rで読み込めるのがありがたい。

高機能過ぎてまだ全然使いこなせてないけど、ぱっと触った感じ使えそうだなと思ったものを紹介。

1. プロセス名を指定してtrace

# sysdig proc.name=ls

もしかするとstraceでも同じことが出来るかもしれないけど、予めtraceしたいプロセス名を指定しておいて、そのプロセスが実行されたらtraceする、ということが出来る。この例の場合、別ターミナルからをlsを実行したタイミングでシステムコールが流れる。特定のコマンドの動きを見たいときやPIDを掴むのが難しいプロセスのデバッグに使えそう。

2. 特定のディレクトリ下のwriteをtrace

# sysdig evt.type=write and fd.name contains /tmp

/tmp下がすごい勢い肥大してるんだけど、何が書き出しているんだ!(大抵MySQLなんだけど)というケースに。

3. networkのtop

# sysdig -c topprocs_net

よく気になる。

4. ファイル単位でのI/O top

# sysdig -c topfiles_bytes

これもよく気になる。

5. 特定のCPU(core)のtop

# sysdig -c topprocs_cpu evt.cpu=0

/proc/interruptsでCPUの偏りが気になるときなどに。

6. 標準出力のミラーリング

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

他のターミナルで実行したコマンドの標準出力をミラーリング出来る。使い道は分からないけど面白い。

7. 特定のユーザの実行コマンドをリアルタイムで監視

# sysdig -A -c spy_users user.name=ユーザ名

まぁ色々と知りたいときがある。(新人さんに作業任せてるときとか。)

その他にもフィルタリングするフィールドやChiselというスクリプトが色々あって、かなり柔軟に組み合わせることが出来るのでsysdig -lsysdig -clを見ながらaliasを作っておくと捗りそう。 あと、まだ実装されていないけど、jsonで出力する-jオプションが使えるようになれば、他のプログラムにデータを渡すのも比較的簡単になりそうで夢が広がる。

一つ残念なのはCentOS5系には対応していないこと。ソースからビルドしてもmakeが通らなかった。

# CC="gcc44" CXX="g++44" /usr/local/cmake-2.8.12.2/bin/cmake -DCMAKE_INSTALL_PREFIX=/usr/local/sysdig ..
-- The C compiler identification is GNU 4.4.7
-- The CXX compiler identification is GNU 4.4.7
-- Check for working C compiler: /usr/bin/gcc44
-- Check for working C compiler: /usr/bin/gcc44 -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/g++44
-- Check for working CXX compiler: /usr/bin/g++44 -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Using bundled jsoncpp in '/usr/local/src/sysdig/userspace/libsinsp/third-party/jsoncpp'
-- Using bundled LuaJIT in '/usr/local/src/sysdig/third-party/LuaJIT-2.0.2'
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/local/src/sysdig/build

# make
Scanning dependencies of target luajit
[  2%] Creating directories for 'luajit'
[  5%] No download step for 'luajit'
[  7%] No patch step for 'luajit'
[ 10%] No update step for 'luajit'
[ 13%] No configure step for 'luajit'
[ 15%] Performing build step for 'luajit'
==== Building LuaJIT 2.0.2 ====
==== Successfully built LuaJIT 2.0.2 ====
[ 18%] No install step for 'luajit'
[ 21%] Completed 'luajit'
[ 21%] Built target luajit
Scanning dependencies of target driver
In file included from /usr/local/src/sysdig/driver/main.c:27:
include/linux/cdev.h:6: error: field ‘kobj’ has incomplete type
include/linux/cdev.h:24: warning: ‘struct inode’ declared inside parameter list
include/linux/cdev.h:24: warning: its scope is only this definition or declaration, which is probably not what you want
/usr/local/src/sysdig/driver/main.c:37:25: error: asm/syscall.h: No such file or directory
/usr/local/src/sysdig/driver/main.c:40:27: error: asm/unistd_64.h: No such file or directory
/usr/local/src/sysdig/driver/main.c:104: warning: ‘struct rq’ declared inside parameter list
/usr/local/src/sysdig/driver/main.c: In function ‘syscall_exit_probe’:
/usr/local/src/sysdig/driver/main.c:864: error: implicit declaration of function ‘syscall_get_nr’
/usr/local/src/sysdig/driver/main.c: In function ‘syscall_procexit_probe’:
/usr/local/src/sysdig/driver/main.c:882: error: ‘PF_KTHREAD’ undeclared (first use in this function)
/usr/local/src/sysdig/driver/main.c:882: error: (Each undeclared identifier is reported only once
/usr/local/src/sysdig/driver/main.c:882: error: for each function it appears in.)
/usr/local/src/sysdig/driver/main.c: At top level:
/usr/local/src/sysdig/driver/main.c:898: warning: ‘struct rq’ declared inside parameter list
/usr/local/src/sysdig/driver/main.c:902: error: conflicting types for ‘sched_switch_probe’
/usr/local/src/sysdig/driver/main.c:104: error: previous declaration of ‘sched_switch_probe’ was here
/usr/local/src/sysdig/driver/main.c: In function ‘init_module’:
/usr/local/src/sysdig/driver/main.c:1093: warning: too many arguments for format
make[5]: *** [/usr/local/src/sysdig/driver/main.o] Error 1
make[4]: *** [_module_/usr/local/src/sysdig/driver] Error 2
make[3]: *** [all] Error 2
make[2]: *** [driver/CMakeFiles/driver] Error 2
make[1]: *** [driver/CMakeFiles/driver.dir/all] Error 2
make: *** [all] Error 2

kernel-2.6.32以上じゃないと駄目らしい。

https://github.com/draios/sysdig/issues/60