/tcprstat

Passive TCP response time analysis tool

Primary LanguageCGNU General Public License v3.0GPL-3.0

TODO --------- 
想添加 监控响应 时常,信息到监控列表,

用Go 语言重写,并支持 发送监控信息到 db 里边,或者发送到 RabbitMq 存储,然后用grafan 展示
Added @2016-09-11 



Add Comment :

tcprstat是percona用来监测mysql响应时间的。不过对于任何运行在TCP协议上的响应时间,都可以用。
下面是一个监控示例,监控分析mysql的3306端口。


$sudo tcprstat -p 3306 -t 1 -n 5
timestamp   count   max min avg med stddev  95_max  95_avg  95_std  99_max  99_avg  99_std
1283261499  1870    559009  39  883 153 13306   1267    201 150 6792    323 685
1283261500  1865    25704   29  578 142 2755    889 175 107 23630   333 1331
1283261501  1887    26908   33  583 148 2761    714 176 94  23391   339 1340
1283261502  2015    304965  35  624 151 7204    564 171 79  8615    237 507
1283261503  1650    289087  35  462 146 7133    834 184 120 3565    244 358
根据上面的信息,我们可以知道mysql在我们检测期间,对于客户端查询的最大响应时间是559009(单位微妙),即0.559009秒。

你也可以读取tcpdump的文件进行分析。
$sudo tcpdump -i eth0 -nn port 80  -w ./tcpdump.log 
$sudo tcprstat -l 10.234.9.103 -t 2 -n 5 -r ./tcpdump.log 
timestamp       count   max     min     avg     med     stddev  95_max  95_avg  95_std  99_max  99_avg  99_std
1403180482      2       28017   26717   27367   28017   650     26717   26717   0       26717   26717   0
1403180484      0       0       0       0       0       0       0       0       0       0       0       0
注意:
如果运行的时候提示 pcap: SIOCGIFFLAGS: bonding_masters: No such device。那说明没能自动提取服务器ip。需要使用-l参数手动指定。

安装tcprstat
如果是在64位操作系统中使用,可以直接下载二进制文件使用。步骤如下:
1、下载文件 http://github.com/downloads/Lowercases/tcprstat/tcprstat-static.v0.3.1.x86_64
2、把下载的文件移动到 /usr/bin
3、把文件名修改为 tcprstat
4、修改文件权限,增加执行权限 chmod +x /usr/bin/tcprstat
如果你想在32位操作系统中使用,那你只能自己编译了。代码下载地址 https://github.com/Lowercases/tcprstat https://launchpad.net/tcprstat

参数说明

命令行参数    简短形式   类型      描述                    默认值
--format    -f        字符串     输出格式化字符串  ”%T\t%n\t%M\t%m\t%a\t%h\t%S\t%95M\t%95a\t%95S\t%99M\t%99a\t%99S\n” 
--help                          显示帮助信息
--interval  -t        数字      监控多少秒输出一次统计     10
--iterations  -n      数字      共输出几次统计信息         1
--local       -l      字符串    本级ip地址列表
--port        -p      数字      服务端口
--read        -r      字符串    pcap文件路径
--version                      显示版本信息
--no-header           字符串    输出不显示头信息
--header              字符串    指定输出的头信息
实现原理
tcprstat 是如何分析tcp包的,请查看 tcprstat源码分析之tcp数据包分析


                                 ~ tcprstat ~

tcprstat is a pcap-based tool that extracts information about TCP response
times.

It works by sniffing for TCP "requests" (inbound packets) and measures the time
that it takes for sending a "response" (next outbound packet). While this
approach might seem too simple, it works for simple protocols that are based in
such request/response system, like HTTP and the MySQL protocol. In the future,
we may write more complex protocol decoding.

tcprstat sniffs for a while, and then shows some stats about the sniffed
packets, similar to the way top(1) works.

tcprstat uses libpcap to capture TCP. Due to many distros not shipping
libpcap >= 1.0.0, tcprstat ships libpcap 1.1.1 and uses it if it can't find a
suitable version

The build process delivers a static version, tcprstat-static, with no external
linking (some castration is needed at libpcap) so it can be copied directly to
a server with no need for compilation tools.

tcprstat is released under the GPL, version 2 or 3.

################################################################################
#                                                                              #
#   tcprstat -- Extract stats about TCP response times                         #
#   Copyright (C) 2010  Ignacio Nin                                            #
#                                                                              #
#   This program is free software; you can redistribute it and/or modify       #
#   it under the terms of the GNU General Public License as published by       #
#   the Free Software Foundation; either version 2 of the License, or          #
#   (at your option) any later version.                                        #
#                                                                              #
#   This program is distributed in the hope that it will be useful,            #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of             #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              #
#   GNU General Public License for more details.                               #
#                                                                              #
#   You should have received a copy of the GNU General Public License          #
#   along with this program; if not, write to the Free Software                #
#   Foundation, Inc.,                                                          #
#   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA               #
#                                                                              #
################################################################################