网站排障分析常用的命令

系统连接状态篇 :
1. 查看 TCP 连接状态

netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn
netstat -n | awk '/^tcp/ {++S[$NF]};END{for(a in S) print a, S[a]}'

netstat -n | awk '/^tcp/ {++state[$NF]}; END{for(key in state) print key,"	",state[key]}'
netstat -n | awk '/^tcp/ {++arr[$NF]};END{for(k in arr) print k,"	",arr[k]}'
netstat -n |awk '/^tcp/ {print $NF}'|sort|uniq -c|sort -rn
netstat -ant | awk '{print $NF}' | grep -v '[a-z]' | sort | uniq -c

2. 查找请求数前 20 个 IP(常用于查找攻来源):

netstat -anlp|grep 80|grep tcp|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -n20
netstat - ant | awk '/:80/{split($5,ip,":");++A[ip[1]]}END{for(i in A) print A[i],i}' |sort -rn|head -n20

3. 用 tcpdump 嗅探 80 端口的访问看看谁最高

tcpdump -i eth0 -tnn dst port 80 -c 1000 |awk -F"." '{print $1"."$2"."$3"."$4}' | sort |uniq -c | sort -nr |head -20

4. 查找较多 time_wait 连接

netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n20

5. 找查较多的 SYN 连接

netstat -an | grep SYN | awk '{print $5}' |awk -F: '{print $1}' | sort | uniq -c | sort -nr| more

6. 根据端口列进程

netstat -ntlp | grep 80 | awk '{print $7}' 

网站日志分析篇 1(Apache):
1. 获得访问前 10 位的 ip 地址

cat access.log|awk '{print $1}'|sort|uniq -c|sort -nr|head -10
cat access.log|awk '{counts[$(11)]+=1}; END {for(url in counts) print counts[url], url}'

2. 访问次数最多的文件或页面 , 取前 20

cat access.log|awk '{print $11}'|sort|uniq -c|sort -nr|head -20

3. 列出传输最大的几个 exe 文件(分析下载站的时候常用)

cat access.log |awk '($7~/.exe/){print $10 " " $1 " " $4 " " $7}'|sort -nr|head -20

4. 列出输出大于 200000byte( 约 200kb) 的 exe文件以及对应文件发生次数

cat access.log |awk '($10 > 200000 &&$7~/.exe/){print $7}'|sort -n|uniq -c|sort -nr|head -100

5. 如果日志最后一列记录的是页面文件传输时间,则有列出到客户端最耗时的页面

cat access.log |awk '($7~/.php/){print $NF " " $1 " " $4 " " $7}'|sort -nr|head -100

6. 列出最最耗时的页面 ( 超过 60 秒的 ) 的以及对应页面发生次数

cat access.log |awk '($NF > 60 && $7~/.php/){print $7}'|sort -n|uniq -c|sort -nr|head -100

7. 列出传输时间超过 30 秒的文件

cat access.log |awk '($NF > 30){print $7}'|sort -n|uniq -c|sort -nr|head -20

8. 统计网站流量(G)

cat access.log |awk '{sum+=$10} END {print sum/1024/1024/1024}'

9. 统计 404 的连接

awk '($9 ~/404/)' access.log | awk '{print $9,$7}' | sort

10. 统计 http status(略)

原文地址:https://www.cnblogs.com/steven9898/p/11309538.html