统计nginx access.log日志信息
访问次数最多的前50个ip,访问最多的前50个连接,响应时间最近长的前50个连接,访问总数量,post次数,get次数,访问状态统计
#!/bin/bash
#IP查询url
check_ip_url=http://ip.ws.126.net/ipquery?ip=
#保存路径
temp_log=/tmp/temp.log
#统计前多小个数据
number=50
#log_file=/web/shobj/access_18.log
#日志文件路径
log_file=/web/shobj/aa.log
#getSessionExist 因为这个是每个接口调用前都要调用这个接口,因此把这个接口的日志记录排除掉。
awk '
BEGIN{post_number=0;get_number=0;number=0}
$0!~/get[sS]ession[eE]xist/{++number;if($9 ~ /"POST/){++post_number;}if($9 ~ /"GET/){++get_number;}}
END{print "number:"number" POST:"post_number" GET:"get_number}' $log_file | tr "\n" " " > $temp_log
echo "ip:" | tr "\n" " " >> $temp_log
awk '$0!~/get[sS]ession[eE]xist/{print $2}' $log_file | sort -n | uniq | wc -l >> $temp_log;
echo "-------------------状态码统计-----------------------------" >> $temp_log
awk '$0!~/get[sS]ession[eE]xist/{print $3}' $log_file | sort -n | uniq -c | sort -rn | sed -n 's/^ *//p' | head -n $number >> $temp_log
echo "-------------------响应最慢的${number}个请求的信息" >> $temp_log
awk '$0!~/get[sS]ession[eE]xist/{print $2}' $log_file | sort -rnk4 | head -n $number | awk '{split($10,arr,"&|?");print $1,$2,$3,$4,$7,$8,arr[1];}' >> $temp_log;
#grep -v '/Xcxping/info/getBigQrCode' $log_file | sort -rnk11 | head -n 50 | awk '{split($8,arr,"&|?");print $1,$2,$5,$6,$11,arr[1];}' >> $temp_log;
echo "-------------------请求前ip${number}统计-------------------------" >> $temp_log
awk '$0!~/get[sS]ession[eE]xist/{print $2}' $log_file | sort -n | uniq -c | awk '{print $0}'| sort -rn | sed -n 's/^ *//p' | head -n $number | awk -v url=$check_ip_url '
{com="curl -s "url $2 " | iconv -f gb2312 -t utf-8 | sed -n '1p' ";printf $0" "; system(com)}
' | sed -n 's/var lo=//p' | sed -n 's/ lc=//p' | sed -n 's/;//p' >> $temp_log
echo "-------------------请求接口前${number}统计-------------------------" >> $temp_log
awk '$0!~/get[sS]ession[eE]xist/{split($10,arr,"&|?");print arr[1]}' $log_file | sort -n | uniq -c | sort -rn | sed -n 's/^ *//p' | head -n $number >> $temp_log
/web/shobj/aa.log的内容
0c620838316e0bb12cba744dfb45a72b 36.61.90.134 200 0.145 - - [2021-06-21 00:10:33] "GET /Xcxping/index/delUploadImgKey?session_3rd=BNe2d1uyndaXHcsNJiktTc8scNVFCKoDOqgpDPiFCNbgDK05W2ORrqtsDr1UUqvGKWJ3BA5WOw5lNFZqUx6ZzdjbHZampjCmChTQtdVPVKETvGgEQegMVxvf7NPcjH34rMmSYduSDAhLj5TDSKepVN0XbUdz370KuVIDD7Vf HTTP/1.1" 5 "https://servicewechat.com/wxb4cfb48ad37a0f4f/363/page-frame.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 13_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.7(0x1800072b) NetType/4G Language/zh_CN" "-" - -
f0fe28665e7d9f4b2ab37e5aee4b13ac 36.61.90.134 200 0.323 - - [2021-06-21 00:10:33] "POST /Xcxping/index/getAct HTTP/1.1" 4582 "https://servicewechat.com/wxb4cfb48ad37a0f4f/363/page-frame.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 13_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.7(0x1800072b) NetType/4G Language/zh_CN" "-" - session_3rd=BNe2d1uyndaXHcsNJiktTc8scNVFCKoDOqgpDPiFCNbgDK05W2ORrqtsDr1UUqvGKWJ3BA5WOw5lNFZqUx6ZzdjbHZampjCmChTQtdVPVKETvGgEQegMVxvf7NPcjH34rMmSYduSDAhLj5TDSKepVN0XbUdz370KuVIDD7Vf&type=0&page=1
6880934863477d3071be59f9925ee445 120.235.155.211 200 0.047 - - [2021-06-21 00:10:35] "GET /Xcxping/Index/checkOne?session_3rd=resp HTTP/1.1" 64 "https://servicewechat.com/wxb4cfb48ad37a0f4f/363/page-frame.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.7(0x1800072b) NetType/WIFI Language/zh_CN" "-" - -
a7c2e58c941130fde344d4d95a62ca65 120.235.155.211 200 0.260 - - [2021-06-21 00:10:35] "POST /Xcxping/Card/getDetail HTTP/1.1" 20808 "https://servicewechat.com/wxb4cfb48ad37a0f4f/363/page-frame.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.7(0x1800072b) NetType/WIFI Language/zh_CN" "-" - session_3rd=resp&id=118398&tg_eid=142319&tg_uid=4019598&opengid=&eu_id=undefined&store_id=0&key=0
6c291ec22ad0078b1cda797aa2e3703d 120.235.155.211 200 0.087 - - [2021-06-21 00:10:36] "POST /Xcxping/Info/getShareList HTTP/1.1" 1319 "https://servicewechat.com/wxb4cfb48ad37a0f4f/363/page-frame.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.7(0x1800072b) NetType/WIFI Language/zh_CN" "-" - session_3rd=resp&id=118398
829424a0bd8c0d31c6a64e949d3099fc 120.235.155.211 200 0.050 - - [2021-06-21 00:10:36] "POST /Xcxping/Invite/getDiscussList HTTP/1.1" 64 "https://servicewechat.com/wxb4cfb48ad37a0f4f/363/page-frame.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.7(0x1800072b) NetType/WIFI Language/zh_CN" "-" - session_3rd=resp&id=118398&dealer_id=123247&page=1&size=1
4856651c9d336acb42c0feda3ebbbb4c 120.235.155.211 200 0.044 - - [2021-06-21 00:10:36] "POST /Xcxping/draw/getPingDrawInfo HTTP/1.1" 192 "https://servicewechat.com/wxb4cfb48ad37a0f4f/363/page-frame.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.7(0x1800072b) NetType/WIFI Language/zh_CN" "-" - session_3rd=resp&ping_id=118398&vi=&encrypted_data=&sk=
30621e94b3c6533ec05491a18ff65fc5 36.61.90.134 200 0.142 - - [2021-06-21 00:10:38] "GET /Xcxping/index/getSessionExist&session_3rd=BNe2d1uyndaXHcsNJiktTc8scNVFCKoDOqgpDPiFCNbgDK05W2ORrqtsDr1UUqvGKWJ3BA5WOw5lNFZqUx6ZzdjbHZampjCmChTQtdVPVKETvGgEQegMVxvf7NPcjH34rMmSYduSDAhLj5TDSKepVN0XbUdz370KuVIDD7Vf HTTP/1.1" 22 "https://servicewechat.com/wxb4cfb48ad37a0f4f/363/page-frame.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 13_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.7(0x1800072b) NetType/4G Language/zh_CN" "-" - -
9b1f8b88ea252474f50036fe2333b3e6 36.61.90.134 200 0.129 - - [2021-06-21 00:10:38] "GET /Xcxping/index/getSessionExist&session_3rd=BNe2d1uyndaXHcsNJiktTc8scNVFCKoDOqgpDPiFCNbgDK05W2ORrqtsDr1UUqvGKWJ3BA5WOw5lNFZqUx6ZzdjbHZampjCmChTQtdVPVKETvGgEQegMVxvf7NPcjH34rMmSYduSDAhLj5TDSKepVN0XbUdz370KuVIDD7Vf HTTP/1.1" 22 "https://servicewechat.com/wxb4cfb48ad37a0f4f/363/page-frame.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 13_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.7(0x1800072b) NetType/4G Language/zh_CN" "-" - -
69c34eddd180a9ab28f8e816fec5335c 36.61.90.134 200 0.118 - - [2021-06-21 00:10:39] "POST /Xcxping/index/store_info HTTP/1.1" 1556 "https://servicewechat.com/wxb4cfb48ad37a0f4f/363/page-frame.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 13_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.7(0x1800072b) NetType/4G Language/zh_CN" "-" - session_3rd=BNe2d1uyndaXHcsNJiktTc8scNVFCKoDOqgpDPiFCNbgDK05W2ORrqtsDr1UUqvGKWJ3BA5WOw5lNFZqUx6ZzdjbHZampjCmChTQtdVPVKETvGgEQegMVxvf7NPcjH34rMmSYduSDAhLj5TDSKepVN0XbUdz370KuVIDD7Vf
be41450c46ea847ae800e8017218f3b2 36.61.90.134 200 0.137 - - [2021-06-21 00:10:39] "GET /Xcxping/index/getUploadImgKey?session_3rd=BNe2d1uyndaXHcsNJiktTc8scNVFCKoDOqgpDPiFCNbgDK05W2ORrqtsDr1UUqvGKWJ3BA5WOw5lNFZqUx6ZzdjbHZampjCmChTQtdVPVKETvGgEQegMVxvf7NPcjH34rMmSYduSDAhLj5TDSKepVN0XbUdz370KuVIDD7Vf HTTP/1.1" 92 "https://servicewechat.com/wxb4cfb48ad37a0f4f/363/page-frame.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 13_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.7(0x1800072b) NetType/4G Language/zh_CN" "-" - -
8e96ca9c68505899fdb2868b51ebf726 36.61.90.134 200 0.134 - - [2021-06-21 00:10:41] "GET /Xcxping/index/getSessionExist&session_3rd=BNe2d1uyndaXHcsNJiktTc8scNVFCKoDOqgpDPiFCNbgDK05W2ORrqtsDr1UUqvGKWJ3BA5WOw5lNFZqUx6ZzdjbHZampjCmChTQtdVPVKETvGgEQegMVxvf7NPcjH34rMmSYduSDAhLj5TDSKepVN0XbUdz370KuVIDD7Vf HTTP/1.1" 22 "https://servicewechat.com/wxb4cfb48ad37a0f4f/363/page-frame.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 13_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.7(0x1800072b) NetType/4G Language/zh_CN" "-" - -
a5bc75349b4c976cd0464887e199ef46 36.61.90.134 200 0.132 - - [2021-06-21 00:10:41] "GET /Xcxping/index/getSessionExist&session_3rd=BNe2d1uyndaXHcsNJiktTc8scNVFCKoDOqgpDPiFCNbgDK05W2ORrqtsDr1UUqvGKWJ3BA5WOw5lNFZqUx6ZzdjbHZampjCmChTQtdVPVKETvGgEQegMVxvf7NPcjH34rMmSYduSDAhLj5TDSKepVN0XbUdz370KuVIDD7Vf HTTP/1.1" 22 "https://servicewechat.com/wxb4cfb48ad37a0f4f/363/page-frame.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 13_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.7(0x1800072b) NetType/4G Language/zh_CN" "-" - -
1c9f58b9cec9ea8d8db5281fff7ce660 36.61.90.134 200 0.132 - - [2021-06-21 00:10:41] "GET /Xcxping/index/delUploadImgKey?session_3rd=BNe2d1uyndaXHcsNJiktTc8scNVFCKoDOqgpDPiFCNbgDK05W2ORrqtsDr1UUqvGKWJ3BA5WOw5lNFZqUx6ZzdjbHZampjCmChTQtdVPVKETvGgEQegMVxvf7NPcjH34rMmSYduSDAhLj5TDSKepVN0XbUdz370KuVIDD7Vf HTTP/1.1" 5 "https://servicewechat.com/wxb4cfb48ad37a0f4f/363/page-frame.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 13_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.7(0x1800072b) NetType/4G Language/zh_CN" "-" - -
c95ba6f434790aaa84155f460cbf2dc3 36.61.90.134 200 0.301 - - [2021-06-21 00:10:41] "POST /Xcxping/index/getAct HTTP/1.1" 4582 "https://servicewechat.com/wxb4cfb48ad37a0f4f/363/page-frame.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 13_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.7(0x1800072b) NetType/4G Language/zh_CN" "-" - session_3rd=BNe2d1uyndaXHcsNJiktTc8scNVFCKoDOqgpDPiFCNbgDK05W2ORrqtsDr1UUqvGKWJ3BA5WOw5lNFZqUx6ZzdjbHZampjCmChTQtdVPVKETvGgEQegMVxvf7NPcjH34rMmSYduSDAhLj5TDSKepVN0XbUdz370KuVIDD7Vf&type=0&page=1
fda87b424a18d99554de5142e1fbb6b9 120.235.155.211 200 0.075 - - [2021-06-21 00:10:47] "POST /Xcxping/index/storeData HTTP/1.1" 208 "https://servicewechat.com/wxb4cfb48ad37a0f4f/363/page-frame.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.7(0x1800072b) NetType/WIFI Language/zh_CN" "-" - session_3rd=resp&id=118398&long=113.28182983398438&lat=23.210519790649414
2b4edc22104f460566899fa09712553d 36.61.90.134 200 0.123 - - [2021-06-21 00:10:49] "GET /Xcxping/index/getSessionExist&session_3rd=BNe2d1uyndaXHcsNJiktTc8scNVFCKoDOqgpDPiFCNbgDK05W2ORrqtsDr1UUqvGKWJ3BA5WOw5lNFZqUx6ZzdjbHZampjCmChTQtdVPVKETvGgEQegMVxvf7NPcjH34rMmSYduSDAhLj5TDSKepVN0XbUdz370KuVIDD7Vf HTTP/1.1" 22 "https://servicewechat.com/wxb4cfb48ad37a0f4f/363/page-frame.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 13_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.7(0x1800072b) NetType/4G Language/zh_CN" "-" - -
4cda0435745e8a7f8351f7681bf780f4 36.61.90.134 200 0.131 - - [2021-06-21 00:10:49] "GET /Xcxping/index/getSessionExist&session_3rd=BNe2d1uyndaXHcsNJiktTc8scNVFCKoDOqgpDPiFCNbgDK05W2ORrqtsDr1UUqvGKWJ3BA5WOw5lNFZqUx6ZzdjbHZampjCmChTQtdVPVKETvGgEQegMVxvf7NPcjH34rMmSYduSDAhLj5TDSKepVN0XbUdz370KuVIDD7Vf HTTP/1.1" 22 "https://servicewechat.com/wxb4cfb48ad37a0f4f/363/page-frame.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 13_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.7(0x1800072b) NetType/4G Language/zh_CN" "-" - -
e7c9cf86d5ed827354fbf904c32b6536 36.61.90.134 200 0.137 - - [2021-06-21 00:10:49] "GET /Xcxping/index/getUploadImgKey?session_3rd=BNe2d1uyndaXHcsNJiktTc8scNVFCKoDOqgpDPiFCNbgDK05W2ORrqtsDr1UUqvGKWJ3BA5WOw5lNFZqUx6ZzdjbHZampjCmChTQtdVPVKETvGgEQegMVxvf7NPcjH34rMmSYduSDAhLj5TDSKepVN0XbUdz370KuVIDD7Vf HTTP/1.1" 92 "https://servicewechat.com/wxb4cfb48ad37a0f4f/363/page-frame.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 13_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.7(0x1800072b) NetType/4G Language/zh_CN" "-" - -
c4f25ba0e8275722bf289239bb182000 36.61.90.134 200 0.135 - - [2021-06-21 00:10:49] "POST /Xcxping/index/store_info HTTP/1.1" 1559 "https://servicewechat.com/wxb4cfb48ad37a0f4f/363/page-frame.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 13_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.7(0x1800072b) NetType/4G Language/zh_CN" "-" - session_3rd=BNe2d1uyndaXHcsNJiktTc8scNVFCKoDOqgpDPiFCNbgDK05W2ORrqtsDr1UUqvGKWJ3BA5WOw5lNFZqUx6ZzdjbHZampjCmChTQtdVPVKETvGgEQegMVxvf7NPcjH34rMmSYduSDAhLj5TDSKepVN0XbUdz370KuVIDD7Vf
结果
number:14 POST:9 GET:5 ip: 2
-------------------状态码统计-----------------------------
14 200
-------------------响应最慢的50个请求的信息
f0fe28665e7d9f4b2ab37e5aee4b13ac 36.61.90.134 200 0.323 [2021-06-21 00:10:33] /Xcxping/index/getAct
c95ba6f434790aaa84155f460cbf2dc3 36.61.90.134 200 0.301 [2021-06-21 00:10:41] /Xcxping/index/getAct
a7c2e58c941130fde344d4d95a62ca65 120.235.155.211 200 0.260 [2021-06-21 00:10:35] /Xcxping/Card/getDetail
0c620838316e0bb12cba744dfb45a72b 36.61.90.134 200 0.145 [2021-06-21 00:10:33] /Xcxping/index/delUploadImgKey
30621e94b3c6533ec05491a18ff65fc5 36.61.90.134 200 0.142 [2021-06-21 00:10:38] /Xcxping/index/getSessionExist
e7c9cf86d5ed827354fbf904c32b6536 36.61.90.134 200 0.137 [2021-06-21 00:10:49] /Xcxping/index/getUploadImgKey
be41450c46ea847ae800e8017218f3b2 36.61.90.134 200 0.137 [2021-06-21 00:10:39] /Xcxping/index/getUploadImgKey
c4f25ba0e8275722bf289239bb182000 36.61.90.134 200 0.135 [2021-06-21 00:10:49] /Xcxping/index/store_info
8e96ca9c68505899fdb2868b51ebf726 36.61.90.134 200 0.134 [2021-06-21 00:10:41] /Xcxping/index/getSessionExist
a5bc75349b4c976cd0464887e199ef46 36.61.90.134 200 0.132 [2021-06-21 00:10:41] /Xcxping/index/getSessionExist
1c9f58b9cec9ea8d8db5281fff7ce660 36.61.90.134 200 0.132 [2021-06-21 00:10:41] /Xcxping/index/delUploadImgKey
4cda0435745e8a7f8351f7681bf780f4 36.61.90.134 200 0.131 [2021-06-21 00:10:49] /Xcxping/index/getSessionExist
9b1f8b88ea252474f50036fe2333b3e6 36.61.90.134 200 0.129 [2021-06-21 00:10:38] /Xcxping/index/getSessionExist
2b4edc22104f460566899fa09712553d 36.61.90.134 200 0.123 [2021-06-21 00:10:49] /Xcxping/index/getSessionExist
69c34eddd180a9ab28f8e816fec5335c 36.61.90.134 200 0.118 [2021-06-21 00:10:39] /Xcxping/index/store_info
6c291ec22ad0078b1cda797aa2e3703d 120.235.155.211 200 0.087 [2021-06-21 00:10:36] /Xcxping/Info/getShareList
fda87b424a18d99554de5142e1fbb6b9 120.235.155.211 200 0.075 [2021-06-21 00:10:47] /Xcxping/index/storeData
829424a0bd8c0d31c6a64e949d3099fc 120.235.155.211 200 0.050 [2021-06-21 00:10:36] /Xcxping/Invite/getDiscussList
6880934863477d3071be59f9925ee445 120.235.155.211 200 0.047 [2021-06-21 00:10:35] /Xcxping/Index/checkOne
4856651c9d336acb42c0feda3ebbbb4c 120.235.155.211 200 0.044 [2021-06-21 00:10:36] /Xcxping/draw/getPingDrawInfo
-------------------请求前ip50统计-------------------------
8 36.61.90.134 "安徽省","合肥市"
6 120.235.155.211 "广东省","云浮市"
-------------------请求接口前50统计-------------------------
2 /Xcxping/index/store_info
2 /Xcxping/index/getUploadImgKey
2 /Xcxping/index/getAct
2 /Xcxping/index/delUploadImgKey
1 /Xcxping/Invite/getDiscussList
1 /Xcxping/Info/getShareList
1 /Xcxping/index/storeData
1 /Xcxping/Index/checkOne
1 /Xcxping/draw/getPingDrawInfo
1 /Xcxping/Card/getDetail