网站运维常用小技巧,排错必备

Linux一句话命令技巧

说明:通过一句话快速理解bash/sed/awk等小工具的组合,能快速处理日常工作.

一、网站连接状态篇:

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 �Cn20


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 �C20

4.查找较多time_wait连接

netstat -n|grep TIME_WAIT|awk ‘{print $5}’|sort|uniq -c|sort -rn|head �Cn20

 

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

 

7.查看有多少个PHP-CGI进程活动

netstat -anp | grep php-cgi | grep ^tcp | wc -l

 

8.查看PHP-CGI占用内存的总数:

total=0; for i in `ps -C php-cgi -o rss=`; do total=$(($total+$i)); done; echo “PHP-CGI Memory usage: $total kb”

二、网站日志分析篇

以apache access.log实例篇:

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

cat access.log |awk ‘{counts[$(9)]+=1}; END {for(code in counts) print code, counts[code]}'

cat 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'

以Squid access.log实例篇。

1.按域统计流量

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

 

2.CDN常用测试命令:

1)测试网站是否压缩:

curl -I http://xxxxxx -H Accept-Encoding:gzip,defalte

curl -I --compressed http://xxxxx

wget -S --header="accept-encoding: gzip"  http://xxxxx

2)测试某个源服务是否正常:

如:要测试http://xxxxx/kf.html 是否正常其对应的真实源服务ip(61.160.248.176).

curl --connect-timeout 5 -k -D- -o/dev/null -H "host: www.game5.com" http://xxxxxx/kf.html

curl --connect-timeout 5  -w -k -D- -o/dev/null -H "host: xianyu.game5.com"http://xxxxxx/upload/2010/08/12/shoujieqisucanglong.jpg

三、数据库篇

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'

tcpdump_mysql.sh

#!/bin/bash
tcpdump -i eth0 -s 0 -l -w - dst port 3306 | strings | perl -e '
while(<>) { chomp; next if /^[^ ]+[ ]*$/;
if(/^(SELECT|UPDATE|DELETE|INSERT|SET|COMMIT|ROLLBACK|CREATE|DROP|ALTER|CALL)/i) {
if (defined $q) { print "$q\n"; }
$q=$_;
} else {
$_ =~ s/^[ \t]+//; $q.=" $_";
}
}'

四.系统状态分析

1.查看网络流量

sar -n 'DEV' 1  10000000|awk 'BEGIN{printf "DEVICE\tIN\t\tOUT\n"}''/eth0/ {printf $3"  ";printf $6*8/1024/1024"Mbps    ";print $7*8/1024/1024"Mbps"}'

2.查看占用磁盘IO最多的进程。

wget -c http://linux.web.psi.ch/dist/scientific/5/gfa/all/dstat-0.6.7-1.rf.noarch.rpm
dstat -M topio -d -M topbio

3.iostat -x 1 10

如果 %util 接近 100%,说明产生的I/O请求太多,I/O系统已经满负荷,该磁盘可能存在瓶颈。

idle小于70% IO压力就较大了,一般读取速度有较多的wait.

svctm < await (因为同时等待的请求的等待时间被重复计算了),

svctm的大小一般和磁盘性能有关:CPU/内存的负荷也会对其有影响,请求过多也会间接导致 svctm 的增加。

await: await的大小一般取决于服务时间(svctm) 以及 I/O 队列的长度和 I/O 请求的发出模式。

如果 svctm 比较接近 await,说明I/O 几乎没有等待时间;

如果 await 远大于 svctm,说明 I/O队列太长,应用得到的响应时间变慢,

如果响应时间超过了用户可以容许的范围,这时可以考虑更换更快的磁盘,调整内核 elevator算法,优化应用,或者升级 CPU。

队列长度(avgqu-sz)也可作为衡量系统 I/O 负荷的指标,但由于 avgqu-sz 是按照单位时间的平均值,所以不能反映瞬间的 I/O 洪水。

vmstat 1 5

r经常大于 3-4 ,且id经常少于50,表cpu的负荷很重。

pi,po 长期不等于0,表示内存不足。

disk 经常不等于0, 且在 b中的队列大于2-3, 表示 io性能不好。

Procs r: 运行的进程比较多,系统很繁忙

Io bo: 磁盘写的数据量稍大

Cpu us: 持续大于50-60,服务高峰期可以接受

Cpu wa: 稍微有些高

Cpu id:持续小于50,服务高峰期可以接受

 

4.分析CPU的几个命令:

mpstat -P ALL

sar -u -P ALL 2 5

sar -u 2 10

iostat -xtc 2

 

5.系统日志

# cat /var/log/rflogview/*errors

检查是否有异常错误记录  也可以搜寻一些异常关键字,例如:

# grep -i error /var/log/messages

# grep -i fail /var/log/messages

# egrep -i 'error|warn' /var/log/messages 查看系统异常

核心日志  # dmesg

检查是否有异常错误记录

 

6.关于ARP

清除所有arp缓存

arp -n|awk '/^[1-9]/ {print "arp -d "$1}'|sh

绑定已知机器的arp地址

cat /proc/net/arp | awk '{print $1 " " $4}' |sort -t. -n +3 -4 > /etc/ethers

五、硬件篇:

内存识别:

#内存大小

# dmidecode|grep -P -A5 "Memory\s+Device"|grep Size|grep -v Range

Size: 2048 MB

Size: No Module Installed

#最大内存容量

# dmidecode|grep -P 'Maximum\s+Capacity'

Maximum Capacity: 4 GB

#内存频率

# dmidecode|grep -A16 "Memory Device"|grep Speed

Speed: 800 MHz (1.2 ns)

Speed: Unknown

CPU 识别:

①物理cpu个数:

# cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l

②每个物理cpu中core的个数(即核数)

# cat /proc/cpuinfo | grep "cpu cores" | uniq

cpu cores : 4

③每个物理cpu中逻辑cpu(可能是core、threads或both)的个数

# cat /proc/cpuinfo | grep "siblings" | uniq

siblings : 8

查看CPU主频:

#cat /proc/cpuinfo |grep MHz|uniq

逻辑CPU,CPU型号

#cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c

#当前CPU运行模式,32,64位

getconf LONG_BIT

# cat /proc/cpuinfo | grep flags | grep ' lm ' | wc -l

8

(结果大于0, 说明支持64bit计算. lm指long mode, 支持lm则是64bit)

五、技巧篇

1.查找并删除0字节文件:

find -type f -size 0 -exec rm -rf {} \;

 

2.查看进程

(按内存从大到小排序)

ps -e  -o "%C  : %p : %z : %a"|sort -k5 -nr|more

(按cpu利用率从大到小排列)

ps -e  -o "%C  : %p : %z : %a"|sort  -nr|more

 

3.打印Cache里的URL

grep -r -a  jpg /data/cache/* | strings | grep "http:" | awk -F'http:' '{print "http:"$2;}'

 

4.sed替换

sed -i '/Root/s/no/yes/' /etc/ssh/sshd_config  sed在这个文里Root的一行,匹配Root一行,将no替换成yes.

 

5.如何杀掉进程

ps aux|grep 进程名|grep -v grep|awk '{print $2}'|xargs kill -9

killall -9 nginx

 

6.NGINX操作常用命令:

检查语法:

/usr/local/nginx/sbin/nginx -t

杀掉进程重启:

killall -HUP nginx (重栽配置文件)

 

7.显示运行3级别开启的服务

ls /etc/rc3.d/S* |cut -c 15-

 

8.如何在编写SHELL显示多个信息,用EOF

cat << EOF

+--------------------------------------------------------------+

|         === Welcome to Tunoff services ===                   |

+--------------------------------------------------------------+

EOF

 

9.利用for循环快速给mysql建立链接连接:

cd /usr/local/mysql/bin

for i in *

do ln /usr/local/mysql/bin/$i /usr/bin/$i

done

 

10.取IP地址(可以了解,sed,awk,cut截取字符方面的应用)

ifconfig eth0|sed -n '2p'|awk '{print $2}'|cut -c 6-30

ifconfig eth0 |grep "inet addr:" |awk '{print $2}'|cut -c 6-

ifconfig eth0 | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'

ifconfig eth0 | sed -n '/inet /{s/.*addr://;s/ .*//;p}'

ifconfig eth0 | grep "inet addr:"|awk '{print substr($2,6,30)}'

ifconfig  | grep -Eo \([0-9]\{1,3\}[\.]\)\{3\}[0-9] |sed -n '1p'

perl正则实现获取IP地址的方法:

ifconfig -a | perl -ne 'if ( m/^\s*inet (?:addr:)?([\d.]+).*?cast/ ) { print qq($1\n); exit 0; }'

 

11.统计一下服务器下面所有的jpg的文件的大小。

find / -name "*.jpg" -exec wc -c {} \;|awk '{print $1}'|awk '{a+=$1}END{print a}'

统计目录下文件的大小:

du $1 --max-depth=1 | sort -n|awk '{printf "%7.2fM ----> %s\n",$1/1024,$2}'|sed 's:/.*/\([^/]\{1,\}\)$:\1:g'

统计一个目录中的目录个数

ls -l | awk '/^d/' | wc -l

统计一个目录中的文件个数

ls -l | awk '/^-/' | wc -l

统计一个目录中的全部文件数

find ./ -type f -print | wc -l

统计一个目录中的全部子目录数

find ./ -type d -print | wc -l

列出当前文件夹目录的大小:

du -b --max-depth 1 | sort -nr | perl -pe 's{([0-9]+)}{sprintf"%.1f%s", $1>=2**30? ($1/2**30, "G"): $1>=2**20? ($1/2**20, "M"):$1>=2**10? ($1/2**10, "K"): ($1, "")}e'

shaw答案 :du -hs $(du -sk ./`ls -F |grep /` |sort -nr |awk '{print $NF}')

也可 以实现,不过不是特别完美。但好记。

 

12.sed查找并替换内容:

sed -i "s/varnish/LTCache/g"   `grep "Via" -rl /usr/local/src/varnish-2.0.4`

sed -i "s/X-Varnish/X-LTCache/g"     `grep "X-Varnish" -rl /usr/local/src/varnish-2.0.4`

 

13. 去掉第一列的行代号。

awk '{for(i=2;i<=NF;i++) if(i!=NF){printf $i" "}else{print $i} }' list

 

14.消除vim中的^M的几种方法

1)dos2uninx filename

2)sed -e 's/^M//' filename

3)vim中 :s/^M//gc

4)col -bx < dosfile > newfile

5)tr -s "\r\n" "\n" < file > newfile

 

15.在当前目录及子目录下搜索所有后缀为 .php 的文件,并且将 字符串 baidu.com 替换成 linuxtone.org

find -name *.php |xargs perl -pi -e 's|baidu.com|linuxtone.org|g';

sed -i "s/baidu.com/opendoc.com.cn/g" `grep baidu.com -rl ./`

 

16. 统计文件中词语出现的频次:

awk '{arr[$1]+=1 } END{for(i in arr){print arr[i]}}' 文件名 | sort �Crn



你可能感兴趣的:(linux技巧)