最近在学习Linux相关的东西,参考网上的一些命令整理了一些Linux服务器故障分析常用的命令,对于初学者是个很好的参考,有需要的小伙伴可以参考一下:
1、系统连接状态篇:
(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,"\t",state[key]}' netstat -n | awk '/^tcp/ {++arr[$NF]};END {for(k in arr) print k,"t",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 443|grep tcp|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -n 20 netstat -ant |awk '/:443/{split($5,ip,":");++A[ip[1]]}END{for(i in A) print A[i],i}'|sort -rn|head -n 20
(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 -n 20
(4)、查找较多time_wait连接:
netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n 20
(5)、找查较多的SYN连接:
netstat -an|grep SYN|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr| more
(6)、根据端口列进程:
[root@localhost ~]# netstat -ntlp | grep 443 | awk '{print $7}' | cut -d/ -f1 1663 1663
(7)、查看并发连接数
netstat -nat|grep ESTABLISHED|wc -l
2、网站日志分析篇(nginx):
(1)、获得访问前10位的ip地址:
cat /data/logs/www.biancheng123.com.log|awk '{print $1}'|sort|uniq -c|sort -nr|head -n 10 cat /data/logs/www.biancheng123.com.log|awk '{counts[$(11)]+=1}; END {for(url in counts) print counts[url], url}'
(2)、访问次数最多的20个文件或页面
cat /data/logs/www.biancheng123.com.log|awk '{print $11}'|sort|uniq -c|sort -nr|head -n 20
(3)、列出传输最大的几个exe文件(分析下载站的时候常用)
cat /data/logs/www.biancheng123.com.log |awk '($7~/.exe/){print $10 " " $1 " " $4 " " $7}'|sort -nr|head -n 20
(4)、列出输出大于200000byte(约200kb)的exe文件以及对应文件发生次数
cat /data/logs/www.biancheng123.com.log|awk '($10 > 200000 && $7~/.exe/){print $7}'|sort -n|uniq -c|sort -nr|head -n 100
(5)、如果日志最后一列记录的是页面文件传输时间,则有列出到客户端最耗时的页面
cat /data/logs/www.biancheng123.com.log|awk '($7~/.php/){print $NF " " $1 " " $4 " " $7}'|sort -nr|head -n 20
(6)、列出最最耗时的页面(超过60秒的)的以及对应页面发生次数
cat /data/logs/www.biancheng123.com.log|awk '($NF > 60 && $7~/.php/){print $7}'|sort -n|uniq -c|sort -nr|head -n 20
(7)、列出传输时间超过30秒的文件
cat /data/logs/www.biancheng123.com.log|awk '($NF > 30){print $7}'|sort -n|uniq -c|sort -nr|head -n 20
(8)、统计网站流量(G)
cat /data/logs/www.biancheng123.com.log|awk '{sum+=$10} END {print sum/1024/1024/1024}'
(9)、统计404的连接,总数前20:
cat /data/logs/www.biancheng123.com.log|grep '" 404'|awk '{print $9,$7}'|sort -n|uniq -c|sort -nr|head -n 20
(10)、统计http status(待测)
cat /data/logs/www.biancheng123.com.log |awk '{counts[$(9)]+=1}; END {for(code in counts) print code, counts[code]}' cat /data/logs/www.biancheng123.com.log|awk '{print $9}'|sort|uniq -c|sort -rn
(11)、蜘蛛分析,查看是哪些蜘蛛在抓取内容
/usr/sbin/tcpdump -i eno1 -l -s 0 -w - dst port 80 | strings | grep -i user-agent | grep -i -E 'Googlebot|bingbot|spider'
3、网站日志分析(Squid篇)
(1)、按域统计流量
zcat /data/logs/www.biancheng123.com.log.tar.gz| awk '{print $10,$7}' |awk 'BEGIN{FS="[ /]"}{trfc[$4]+=$1}END{for(domain in trfc){printf "%st%dn",domain,trfc[domain]}}'
4、数据库篇:
(1)、查看数据库执行的sql:
/usr/sbin/tcpdump -i eth0 -s 0 -l -w - dst port 3306 | strings | egrep -i 'SELECT|UPDATE|DELETE|INSERT|SET|COMMIT|ROLLBACK|CREATE|DROP|ALTER|CALL'
5、系统Debug分析篇
(1)、调试命令
strace -p pid
(2)、跟踪指定进程的PID
gdb -p pid
以上就是吾爱编程为大家分享的服务器故障分析常用的命令的全部命令了,希望对大家有所帮助,了解更多相关文章请关注吾爱编程网!