linux下网站排障分析常用的命令

linux下网站排障分析常用的命令

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 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}' | cut -d/ -f1

 

网站日志分析篇1(Apache):

假设路径为/var/log/httpd/access_log

1.获得访问前10位的ip地址

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

查看到所有的tcp和udp 连接状况:

netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'     

返回结果:
 SYN_RECV 2 (SYN连接请求收到2个 等待确认)
 ESTABLISHED 1 (有1个正常数据传输状态)
 TIME_WAIT 62 (等待结束的请求62个)
 
可返回的所有状态解释:
 CLOSED:无连接是活动的或正在进行
 LISTEN:服务器在等待进入呼叫
 SYN_RECV:一个连接请求已经到达,等待确认
 SYN_SENT:应用已经开始,打开一个连接
 ESTABLISHED:正常数据传输状态
 FIN_WAIT1:应用说它已经完成
 FIN_WAIT2:另一边已同意释放
 ITMED_WAIT:等待所有分组死掉
 CLOSING:两边同时尝试关闭
 TIME_WAIT:另一边已初始化一个释放
 LAST_ACK:等待所有分组死掉

netstat -ant        #网络连接状态

netstat -nat|grep ESTABLISHED|wc -l  #只看正常的并发连接
netstat -nat|grep -i "80"|wc -l   #查看所有80端口连接数
netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n          #对连接的IP按连接数量进行排序
netstat -anlp|grep 80|grep tcp|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -n20 #查看80端口连接数最多的20个IP
netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n20    #查找较多time_wait连接
du -h -s /data/wwwroot/git/* | sort      #查看指定目录下所有文件夹的大小

tail -f /var/log/messages #实时监控messages文件中的内容

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

cat /var/log/httpd/access_log|awk '{print $11}'|sort|uniq -c|sort -nr|head -20

 

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

cat /var/log/httpd/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 /var/log/httpd/access_log |awk '($NF > 60 && $7~/\.php/){print $7}'|sort -n|uniq -c|sort -nr|head -100

 

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

cat /var/log/httpd/access_log |awk '($NF > 30){print $7}'|sort -n|uniq -c|sort -nr|head -20

 

8.统计网站流量(G)

cat /var/log/httpd/access_log |awk '{sum+=$10} END {print sum/1024/1024/1024}'

 

9.统计404的连接

awk '($9 ~/404/)' /var/log/httpd/access_log | awk '{print $9,$7}' | sort

awk '($9 ~/404/)' /var/log/httpd/access_log | awk '{print $9,$7}' | sort > /root/404page.txt

 

10. 统计http status.

cat /var/log/httpd/access_log |awk '{counts[$(9)]+=1}; END {for(code in counts) print code, counts[code]}'

cat /var/log/httpd/access_log |awk '{print $9}'|sort|uniq -c|sort -rn

 

11.蜘蛛分析

查看是哪些蜘蛛在抓取内容。

/usr/sbin/tcpdump -i eth0 -l -s 0 -w - dst port 80 | strings | grep -i user-agent | grep -i -E 'bot|crawler|slurp|spider'

 

网站日分析2(Squid篇)

 

2.按域统计流量

zcat squid_access.log.tar.gz| awk '{print $10,$7}' |awk 'BEGIN{FS="[ /]"}{trfc[$4]+=$1}END{for(domain in trfc){printf "%s\t%d\n",domain,trfc[domain]}}'

效率更高的perl版本请到此下载:http://docs.linuxtone.org/soft/tools/tr.pl

 

数据库篇

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'

 

系统Debug分析篇

1.调试命令

strace -p pid

 

2.跟踪指定进程的PID

gdb -p pid

评论

  1. 8年前
    2016-1-05 23:13:14

    了解一下、请问贵站可否友个链[嘻嘻]

    • 博主
      好书网
      8年前
      2016-1-06 9:19:37

      你好,可以交换友链啊,给个联系方式

      • 好玩吧
        8年前
        2016-1-06 9:29:27

        我的qq 1757420812 关键词:好文好书、www.54read.com [嘻嘻]

  2. 8年前
    2016-1-06 0:32:07

    楼主:政治老师结婚求对联?
    回复:上联:一上一下共创和谐境界;下联:一进一出造就一代新人。 横批:生命在于运动! 一个程序员社区 http://www.1024xyz.com

  3. 8年前
    2016-1-08 18:34:57

    此贴有意思~

  4. 8年前
    2016-2-18 1:26:55

    [吐舌头]

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇