精简协议分析器
thrift-tools的Python项目详细描述
目录
tl;dr
“节约工具”是一个库和一组工具,用于反思Apache Thrift流量。
安装
您可以通过pip安装旧工具:
$ pip install thrift-tools
或从源代码运行(如果已安装依赖项,请参阅 下面):
$ git clone ... $ cd thrift-tools $ sudo FROM_SOURCE=1 bin/thrift-tool --iface=eth0 --port 9091 dump --show-all --pretty ... $ FROM_SOURCE=1 bin/thrift-tool --port 9090 \ --pcap-file thrift_tools/tests/resources/calc-service-binary.pcap \ dump --show-all --pretty --color \ --idl-file=thrift_tools/tests/resources/tutorial.thrift
工具
节俭工具可以在互动模式下分析生活节俭 消息:
$ sudo thrift-tool --iface eth0 --port 9091 dump --show-all --pretty [00:39:42:850848] 10.1.8.7:49858 -> 10.1.2.20:3636: method=dosomething, type=call, seqid=1120 header: () fields: [ ( 'struct', 1, [ ('string', 1, 'something to do'), ('i32', 3, 0), ( 'struct', 9, [ ('i32', 3, 2), ('i32', 14, 0), ('i32', 16, 0), ('i32', 18, 25)])])] ------>[00:39:42:856204] 10.1.2.20:3636 -> 10.1.8.7:49858: method=dosomething, type=reply, seqid=1120 header: () fields: [ ( 'struct', 0, [ ('string', 1, 'did something'), ('string', 2, 'did something else'), ('string', 3, 'did some other thing'), ('string', 4, 'did the last thing'), ('i32', 6, 3), ('i32', 7, 11), ('i32', 8, 0), ('i32', 9, 0), ('list', 10, [0]), ...
或者,脱机PCAP文件可以被内省:
$ sudo thrift-tool --port 9091 --pcap-file /path/to/myservice.pcap dump ...
注意,您仍然需要设置正确的端口。
如果您正在使用Finagle,请尝试 例如:
$ sudo thrift-tool --iface eth0 --port 9091 dump --show-all --pretty --finagle-thrift ...
json输出可用于通过jq轻松筛选和查询。为了 例如,您可以通过:
$ sudo thrift-tool --port 3030 dump --unpaired --json | jq 'select(.method == "search" and .type == "call") | .src' "10.1.18.5:48534" "10.1.60.2:52008" "10.1.10.27:49856" "10.1.23.24:48116" "10.1.26.7:60462" "10.1.11.10:41895" "10.1.15.13:35285" "10.1.7.17:39759" "10.1.1.19:35481" ...
通过^{tt1}可以收集每个方法的延迟统计信息$ 命令:
$ sudo thrift-tool --port 6666 stats --count 100 method count avg min max p90 p95 p99 p999 -------- ------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- search2 61 0.00860996 0.00636292 0.0188479 0.010778 0.015192 0.0174422 0.0187074 doc 39 0.00134846 0.00099802 0.00274897 0.00177183 0.00199242 0.00256242 0.00273031 287 unmatched calls
您还可以指定.thrift文件以获得更好的输出:
$ sudo thrift-tool --port 9091 dump --show-all --pretty --color --idl-file /path/to/myidl.thrift ...
列出所有可用选项:
$ thrift-tool --help
请注意,对于具有高吞吐量的服务器(即:>;两个KS数据包 每秒钟),节俭的工具可能很难跟上,因为 消息检测有点贵(而且你只能这么快 使用python)。对于这些情况,最好保存一个pcap文件 (即:通过tcpdump)然后对其进行后处理,即:
$ tcpdump -nn -t port 3030 -w dump.pcap $ sudo thrift-tool --port 3030 --pcap-file dump.pcap stats --count 40000 method count avg min max p90 p95 p99 p999 -------- ------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- resize 40000 0.00850996 0.00336091 0.0101364 0.008071 0.009132 0.009890 0.01005665
库
要从另一个(python)应用程序中使用精简工具,可以将其导入 通过:
from thrift_tools.message_sniffer import MessageSnifferOptions, MessageSniffer options = MessageSnifferOptions( iface='eth0', port='3636', ip=None, # include msgs from all IPs pcap_file=None, # don't read from a pcap file, live sniff protocol=None, # auto detect protocol finagle_thrift=False, # apache thrift (not twitter's finagle) read_values=True, # read the values of each msg/struct max_queued=20000, # decent sized queue max_message_size=2000, # 2k messages to keep mem usage frugal debug=False # don't print parsing errors, etc ) def printer(timestamp, src, dst, msg): print '%s %s %s %s' % (timestamp, src, dst, msg) message_sniffer = MessageSniffer(options, printer) # loop forever message_sniffer.join()
如果要使用PCAP文件:
options = MessageSnifferOptions( iface='eth0', port='3636', ip=None, pcap_file="/tmp/myservice.pcap", protocol=None, finagle_thrift=False, read_values=True, max_queued=20000, max_message_size=2000, debug=False ) ...
如果要筛选特定IP的邮件:
options = MessageSnifferOptions( iface='eth0', port='3636', ip=['172.16.24.3', '172.16.24.4'], # ignores everyone else pcap_file="/tmp/myservice.pcap", protocol=None, finagle_thrift=False, read_values=True, max_queued=20000, max_message_size=2000, debug=False ) ...
请参阅示例/了解使用此库的更多方法!
测试
运行测试:
$ python setup.py nosetests