快速的统计HTTP请求的分布

快速的统计HTTP请求的分布

需求:
有一堆CGI,都需要传入appid参数,怎么查看当前那些Appid的访问量最大?
怎么查看这些CGI的请求量分布?

解决:
假设, 我们的webserver的请求都是通过LVS接入的, 那么所有的外网请求都经过tunl0网卡, 假设本机绑定的LVS的IP是:113.108.20.23, 监听端口是80, 那么统计脚本是:

// 统计所有发往本机的请求中, appid的分布
sudo tcpdump -c 30000 -itunl0 -s 512 dst port 80 and dst host 113.108.20.23 -A 2>&1 | awk -F'appid=' '{print $2}' |tee /tmp/tee.log| awk -F '&' '{print $1}' |awk -F' ' '{print $1}'| sort | uniq -c | sort -rn | head
120298 // 这一行是空行, 可以忽略
1349 47 // appid为47的访问次数为1349次
447 61
421 2141
370 2736
253 2227
192 33
145 282
139 121282
125 343


// 统计所有发往本机的请求中, cgi的分布
sudo tcpdump -c 30000 -itunl0 -s 512 dst port 80 and dst host 113.108.20.23 -A 2>&1 | grep 'GET\|POST' | awk -F' ' '{print $(NF-1)}' | awk -F'?' '{print $1}' |grep -v '\.'| sort | uniq -c | sort -rn | head
1150 /v3/user/get_info
1011 /v3/user/is_login
991 /v3/spread/is_reminder_set
635 /v3/spread/set_reminder
240 /v3/page/is_fans
210 /v3/relation/get_app_friends
140 /v3/user/is_setup
126 /v3/user/is_vip
97 /v3/user/get_multi_info
76 /v3/user/total_vip_info

分析:
-c 30000 表示抓30000个包就退出.
-s 512 显示每个包的前512个字节. -s 0是显示全部, 太多了不多没必要.
-A 以ascii方式显示包.
tee 该命令是一个双向重定向命令, 可以方便的查看管道的中间输出结果.

扩展:
你可以统计http请求中任意一个参数的分布, 就简单的把appid 改为 pf, 就可以统计来自各个平台的请求的分布了.

你可能感兴趣的:(快速的统计HTTP请求的分布)