Fluentdのin_forwardに流れているレコードをtcpdumpで見る

本番環境で動いているFluentdのin_forwardに何が流れているのかを知りたい、けど設定を触りたくない…みたいな場合に流れているタグやレコードを見る方法です。特にFluentdに限った話ではないですが、備忘録として書いておきます。

1. tcpdumpでキャプチャ

調べたいサーバ上で、tcpdumpを使ってパケットキャプチャを取ります。in_forwardのポート番号などを指定してキャプチャします。

$ sudo tcpdump -i eth0 -w fluentd.pcap port 24224 and tcp

2. tcptraceでTCPストリームのデータだけ吐き出す

tcptraceは-eオプションを渡すと、TCPストリームごとのデータをファイルに吐いてくれるのでこれを利用します。なお、tcptraceはHomebrewやaptで入ると思います。

$ tcptrace -e fluentd.pcap
1 arg remaining, starting with 'fluentd.pcap'
Ostermann's tcptrace -- version 6.6.7 -- Thu Nov  4, 2004

494 packets seen, 494 TCP packets traced
elapsed wallclock time: 0:00:00.005602, 88182 pkts/sec analyzed
trace file elapsed time: 0:00:25.927472
TCP connection info:
  1: host1:53633 - host2:24224 (a2b)  213>  213<  (reset)
  2: host1:53668 - host2:24224 (c2d)    1>    1<  (reset)
  3: host1:53669 - host2:24224 (e2f)   17>   17<  (reset)
  4: host1:53670 - host2:24224 (g2h)    1>    1<  (reset)
  5: host1:53671 - host2:24224 (i2j)   15>   15<  (reset)

Warning : some extracted files are incomplete!
          Please see -l output for more detail.

$ ls *_contents.dat
a2b_contents.dat e2f_contents.dat i2j_contents.dat

3. Fluentdのin_forwardに流す

あとは*.datファイルをFluentdのin_forwardに流し直すだけです。

たとえば、out_stdoutに吐くだけのFluentdを起動して、

$ cat fluentd.conf
<source>
  @type forward
</source>

<match **>
  @type stdout
</match>
$ fluentd -c fluentd.conf

ncで先ほどのdatファイルを食わせると、

$ cat *_contents.dat | nc localhost 24224

こんな感じでタグやレコードを見られます。

2016-02-22 20:10:58 +0900 test: {"agent":"foo","uuid":"574da95b-23f1-42eb-8738-d646588b5639"}
2016-02-22 20:10:58 +0900 test: {"agent":"foo","uuid":"0b585265-72de-4277-91bb-99c94e2e745b"}
2016-02-22 20:10:58 +0900 test: {"agent":"foo","uuid":"7c4a1b43-5c04-4bfc-8532-1f9533bc7828"}
2016-02-22 20:10:58 +0900 test: {"agent":"foo","uuid":"10b24574-f74c-465b-b2fa-92f124fcf85e"}
2016-02-22 20:10:58 +0900 test: {"agent":"foo","uuid":"064ad535-764b-4eaa-a67c-4dda4b0982ba"}
2016-02-22 20:10:58 +0900 test: {"agent":"foo","uuid":"76d67398-0f55-473e-abe3-a13ce14b31ad"}
2016-02-22 20:10:58 +0900 test: {"agent":"foo","uuid":"b499a04b-3d0d-4a88-8fdd-e5d8eb501d2e"}
2016-02-22 20:10:58 +0900 test: {"agent":"foo","uuid":"a7dcfbe6-fbfc-45d8-9702-fdc7ac9993d5"}

追記 2016/02/24

上記の手順でpcapファイルを変換するスクリプトを書いた

fluentd-pcap-converter.rb · GitHub

$ ls
host1.pcap
$ ruby fluentd-pcap-converter.rb host1.pcap
$ ls
host1.pcap
host1.pcap.output.20160224_0.log