shell分析服务日志命令

最近看到一篇博客,里面是一些服务器日志分析的命令,命令很多,自己也挑选了一些觉得比较重要的,整理一下,以备之后查询

1、查看有多少个IP访问:

awk '{print $1}' log_file|sort|uniq|wc -l

2、查看某一个页面被访问的次数:

grep "/index.php" log_file | wc -l

3、查看每一个IP访问了多少个页面:

awk '{++S[$1]} END {for (a in S) print a,S[a]}' log_file > log.txt
sort -n -t ' ' -k 2 log.txt 配合sort进一步排序

4、将每个IP访问的页面数进行从小到大排序:

awk '{++S[$1]} END {for (a in S) print S[a],a}' log_file | sort -n

5、查看某一个IP访问了哪些页面:

grep ^111.111.111.111 log_file| awk '{print $1,$7}'

6、去掉搜索引擎统计的页面:

awk '{print $12,$1}' log_file | grep ^"Mozilla | awk '{print $2}' |sort | uniq | wc -l

7、查看2015年8月16日14时这一个小时内有多少IP访问:

awk '{print $4,$1}' log_file | grep 16/Aug/2015:14 | awk '{print $2}'| sort | uniq | wc -l

8、查看访问前十个ip地址

awk '{print $1}' access_log |sort|uniq -c|sort -nr |head -10 

9、列出输出大于200000byte(约200kb)的页面以及对应页面发生次数

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

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

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

11、列出传输时间超过 30 秒的文件

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

12、列出当前服务器每一进程运行的数量,倒序排列

ps -ef | awk -F ' ' '{print $8 " " $9}' |sort | uniq -c |sort -nr |head -20

13、查看apache当前并发访问数

netstat -an | grep ESTABLISHED | wc -l

14、查看http进程数

ps -ef|grep httpd|wc -l

15、输出每个ip的连接数,以及总的各个状态的连接数

netstat -n | awk '/^tcp/ {n=split($(NF-1),array,":");if(n<=2)++S[array[(1)]];else++S[array[(4)]];++s[$NF];++N} END {for(a in S){printf("%-20s %s", a, S[a]);++I}printf("%-20s %s","TOTAL_IP",I);for(a in s) printf("%-20s %s",a, s[a]);printf("%-20s %s","TOTAL_LINK",N);}'

16、查找请求数前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

17、用tcpdump嗅探80端口的访问看看谁最高

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

18、查找较多time_wait连接

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

19、Linux命令分析当前的链接状况

netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
watch "netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'" # 通过watch可以一直监控

20、查看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
netstat -ant|awk '/ip:80/{split($5,ip,":");++S[ip[1]]}END{for (a in S) print S[a],a}' |sort -n 

netstat -ant|awk '/:80/{split($5,ip,":");++S[ip[1]]}END{for (a in S) print S[a],a}' |sort -rn|head -n 10 

awk 'BEGIN{printf ("http_codecount_num")}{COUNT[$10]++}END{for (a in COUNT) printf a""COUNT[a]""}'

21、其他收集

统计网站流量(G)

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

统计404的连接

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

每秒并发

watch "awk '{if($9~/200|30|404/)COUNT[$4]++}END{for( a in COUNT) print a,COUNT[a]}' log_file|sort -k 2 -nr|head -n10"

小时单位里ip连接数最多的10个时段

awk -vFS="[:]" '{gsub("-.*","",$1);num[$2" "$1]++}END{for(i in num)print i,num[i]}' log_file | sort -n -k 3 -r | head -10

找出访问次数最多的几个分钟

awk '{print $1}' access.log | grep "20/Mar/2011" |cut -c 14-18|sort|uniq -c|sort -nr|head

 

小结

主要用到了一下几个命令

netstat 查看网络连接状态

ps 查看系统进程

cat 查看文件内容

awk 文本文件处理分析

grep 行文本筛选

sort 文本内容排序

uniq 检查重复行并去重

wc 统计字节字符行数等

head  查看文件开头部分

cut 行文本截取

日志文件里一般有访问IP,访问url,访问页面,请求时间,客户端IP,传输数据大小等,根据需求把相应列打印出来,并根据时间或页面或数据大小进行筛选,然后进行排序、汇总、去重等即可。

posted on 2021-01-16 20:42  流年似水zlw  阅读(160)  评论(0编辑  收藏  举报

导航