Sylon

作者原文https://mp.weixin.qq.com/s/cermvTInKspPzqRWZLU5Vg

自己的小网站跑在阿里云的 ECS 上面, 偶尔也去分析分析自己网站服务器日志,看看网站的访问量。看看有没有黑阔搞破坏!于是收集,整理一些服务器日志分析命令,大家可以试试!

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}\' |sort|uniq -c|sort -nr |head -10 access_log
uniq -c 相当于分组统计并把统计数放在最前面

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}
9、访问次数最多的10个文件或页面

cat log_file|awk \'{print $11}\'|sort|uniq -c|sort -nr | head -10

cat log_file|awk \'{print $11}\'|sort|uniq -c|sort -nr|head -20

awk \'{print $1}\' log_file |sort -n -r |uniq -c | sort -n -r | head -20
访问量最大的前20个ip

10、通过子域名访问次数,依据referer来计算,稍有不准

cat access.log | awk \'{print $11}\' | sed -e \' s/http:////\' -e \' s//.*//\' | sort | uniq -c | sort -rn | head -20
11、列出传输大小最大的几个文件

cat www.access.log |awk \'($7~/.php/){print $10 " " $1 " " $4 " " $7}\'|sort -nr|head -100
12、列出输出大于200000byte(约200kb)的页面以及对应页面发生次数

cat www.access.log |awk \'($10 > 200000 && $7~/.php/){print $7}\'|sort -n|uniq -c|sort -nr|head -100
13、如果日志最后一列记录的是页面文件传输时间,则有列出到客户端最耗时的页面

cat www.access.log |awk \'($7~/.php/){print $NF " " $1 " " $4 " " $7}\'|sort -nr|head -100
14、列出最最耗时的页面(超过60秒的)的以及对应页面发生次数

cat www.access.log |awk \'($NF > 60 && $7~/.php/){print $7}\'|sort -n|uniq -c|sort -nr|head -100
15、列出传输时间超过 30 秒的文件

cat www.access.log |awk \'($NF > 30){print $7}\'|sort -n|uniq -c|sort -nr|head -20
16、列出当前服务器每一进程运行的数量,倒序排列

ps -ef | awk -F \' \' \'{print $8 " " $9}\' |sort | uniq -c |sort -nr |head -20
17、查看apache当前并发访问数

对比httpd.conf中MaxClients的数字差距多少

netstat -an | grep ESTABLISHED | wc -l
18、可以使用如下参数查看数据

ps -ef|grep httpd|wc -l
1388
统计httpd进程数,连个请求会启动一个进程,使用于Apache服务器。

表示Apache能够处理1388个并发请求,这个值Apache可根据负载情况自动调整

netstat -nat|grep -i "80"|wc -l

4341
netstat -an会打印系统当前网络链接状态,而grep -i “80”是用来提取与80端口有关的连接的,wc -l进行连接数统计。
最终返回的数字就是当前所有80端口的请求总数

netstat -na|grep ESTABLISHED|wc -l

376
netstat -an会打印系统当前网络链接状态,而grep ESTABLISHED 提取出已建立连接的信息。然后wc -l统计
最终返回的数字就是当前所有80端口的已建立连接的总数。

netstat -nat||grep ESTABLISHED|wc
可查看所有建立连接的详细记录

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

netstat -n | awk \'/^tcp/ {n=split($(NF-1),array,"

分类:

技术点:

相关文章: