centos 查看日志技巧

发表于

  • 通知web服务器不通ip出现的次数

    # cat access_log | awk ‘{print $1}’|sort|uniq -c |sort -n

    或者

    # cat access_log | awk ‘{print $1}’| sort -n | awk ‘{S[$NF]++}END {for(a in S) {print a ” ” S[a]}}’ | sort +1 -2nr

    解释:

    awk ‘{print $4}’ :  通过管道将第一个字段也就是ip地址筛选出来

    sort -n :将ip地址进行排序

    awk ‘{S[$NF]++} END{for(a in S) {print a” ” S[a]}}:

    $NF是awk里的一个变量,代表最后一个字段的内容,由于这晨只有一个字段,即:IP地址,所以$NF代表IP地址。

    S[$NF]++里的S代表一个数组,然后统计IP地址出现的次数. 后面是一个for in 循环语句,将这个数组里的值和键打印出来

    sort +1 -2nr:以第二个字段,也就是每个IP的访问次数进行排序

    3.分析出现次数最多的ip对网站的具体数据访问情况

    # grep -e IP access.log > filename

    # cat filename |awk ‘{print $8}’|sort|uniq -c|sort -rn

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

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

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

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

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

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

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

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

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

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

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

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

    10.统计网站流量(G)

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

    11.统计404的连接

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

    12. 统计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

    13.查找挂马内容进行批量清除

    # find /webbase/ -type f -exec grep ‘www.800816.com.cn’ -l {} ;

    # sed -i “s/body{.*www.800816.com.cn.*}//g” `grep www.800816.com.cn -rl ./`

    14.批量转换GBK为UTF-8文件编码

    # find default -type d -exec mkdir -p utf/{} ;

    # find default -type f -exec iconv -f GBK -t UTF-8 {} -o utf/{} ;

    15.find查找文件的时候怎么避开多个文件目录

    # find /usr/sam (-path /usr/sam/dir1 -o -path /usr/sam/file1 ) -prune -o -name “*.txt” -print

    16.查看tcp的并发请求数及其TCP连接状态:

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

    # netstat -nat |awk ‘{print $6}’|sort|uniq -c|sort -rn

    # 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

    17.查找请求数前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 -n10

    18.查看有多少个活动的php-cgi进程

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

    19.查找较多time_wait连接

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

    20.找查较多的SYN连接

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

    21.根据端口列进程

    # netstat -ntlp | grep 80 | awk ‘{print $7}’ | cut -d/ -f1

    22.抓包用来防止80端口被人攻击时可以分析数据

    # tcpdump -c 10000 -i eth0 -n dst port 80 > /root/pkts

     

    23.用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

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

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

    25.按域统计流量

    # zcat squid_access.log.tar.gz| awk ‘{print $10,$7}’ |awk ‘BEGIN{FS=”[ /]”}{trfc[$4]+=$1}END{for(domain in trfc){printf “%s %d “,domain,trfc[domain]}}’

    26.查看数据库执行的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’

    27.将匹配Root一行中no替换成yes

    # sed -i ‘/Root/s/no/yes/’ /etc/ssh/sshd_config

    28.去掉第一列

    # awk ‘{for(i=2;i<=NF;i++) if(i!=NF){printf $i” “}else{print $i} }’ list

    29.按内存从大到小排列

    # ps -e -o “%C : %p : %z : %a”|sort -k5 -nr

    30.按cpu利用率从大到小排列

    # ps -e -o “%C : %p : %z : %a”|sort -nr

    31.怎样知道某个进程在哪个CPU上运行

    # ps -eo pid,args,psr

    32.清除僵死进程。

    # ps -eal | awk ‘{ if ($2 == “Z”) {print $4}}’ | kill -9

    33.查看硬件制造商

    # dmidecode -s system-product-name

    34.查找占用磁盘IO最多的进程

    # wget -c linux.web.psi.ch/dist/scientific/5/gfa/all/dstat-0.6.7-1.rf.noarch.rpm”>http://linux.web.psi.ch/dist/scientific/5/gfa/all/dstat-0.6.7-1.rf.noarch.rpm

    # dstat -M topio -d -M topbio

    35.检查I/O使用率(%util)是否超过100%

    # iostat -x 1 2

    36.磁盘空间,检查是否有分区使用率(Use%)过高(比如超过90%) 如发现某个分区空间接近用尽,可以进入该分区的挂载点,用以下命令找出占用空间最多的文件或目录:

    # df -h

    # du -cks * | sort -rn | head -n 10

    37.CPU负载检查前三个输出值是否超过了系统逻辑CPU的4倍。

    # cat /proc/loadavg

    38.CPU的数量

    # cat /proc/cpuinfo |grep -c processor

    39.检查网络流量(rxbyt/s, txbyt/s)是否过高

    # sar -n DEV

    40.每隔1秒显示一下网络流量

    # watch -n 1 “/sbin/ifconfig eth0 | grep bytes”

    41.批量覆盖目录下的文件不用确定是否执行

    # cp -rf /svn/wwwroot /wwwroot

    42.调试命令

    # strace -p pid

    43.跟踪指定进程的PID

    # gdb -p pid

    44.查看当前进程打开了多少个文件句柄

    lsof -n |awk ‘{print $2}’|sort|uniq -c |sort -nr|more

    45.查找最近一天被修改的HTML文件

    find -mtime -1 -type f -name *.html

    46.修改网站的权限

    find -type f -name *.php -exec chmod 444 {} ;

    find ./ -type d -exec chmod 555{} ;

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注