Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  shell  >  正文 自动统计网站访问日志的shell脚本

自动统计网站访问日志的shell脚本

发布时间:2014-10-08   编辑:www.jquerycn.cn
本文介绍下,一个可以每天自动统计网站访问日志的shell脚本,统计每天的访问日志,并发送到电子邮箱。有需要的朋友,参考下吧。

用shell脚本实现:
统计每天的访问日志,并发送到电子邮箱。

具体功能如下:
1、总访问量
2、总带宽
3、独立访客量
4、访问IP统计
5、访问url统计
6、来源统计
7、404统计
8、搜索引擎访问统计(谷歌,百度)
9、搜索引擎来源统计(谷歌,百度)

代码如下:
 

复制代码 代码示例:
#!/bin/bash
log_path=/home/www.jquerycn.cn/log/access.log.1
domain="jquerycn.cn"
email="log@jquerycn.cn"
maketime=`date +%Y-%m-%d" "%H":"%M`
logdate=`date -d "yesterday" +%Y-%m-%d`
total_visit=`wc -l ${log_path} | awk '{print $1}'`
total_bandwidth=`awk -v total=0 '{total+=$10}END{print total/1024/1024}' ${log_path}`
total_unique=`awk '{ip[$1]++}END{print asort(ip)}' ${log_path}`
ip_pv=`awk '{ip[$1]++}END{for (k in ip){print ip[k],k}}' ${log_path} | sort -rn | head -20`
url_num=`awk '{url[$7]++}END{for (k in url){print url[k],k}}' ${log_path} | sort -rn | head -20`
referer=`awk -v domain=$domain '$11 !~ /http:\/\/[^/]*'"$domain"'/{url[$11]++}END{for (k in url){print url[k],k}}' ${log_path} | sort -rn | head -20`
notfound=`awk '$9 == 404 {url[$7]++}END{for (k in url){print url[k],k}}' ${log_path} | sort -rn | head -20`
spider=`awk -F'"' '$6 ~ /Baiduspider/ {spider["baiduspider"]++} $6 ~ /Googlebot/ {spider["googlebot"]++}END{for (k in spider){print k,spider[k]}}'  ${log_path}`
search=`awk -F'"' '$4 ~ /http:\/\/www\.baidu\.com/ {search["baidu_search"]++} $4 ~ /http:\/\/www\.google\.com/ {search["google_search"]++}END{for (k in search){print k,search[k]}}' ${log_path}`
echo -e "概况\n报告生成时间:${maketime}\n总访问量:${total_visit}\n总带宽:${total_bandwidth}M\n独立访客:${total_unique}\n\n访问IP统计\n${ip_pv}\n\n访问url统计\n${url_num}\n\n来源页面统计\n${referer}\n\n404统计\n${notfound}\n\n蜘蛛统计\n${spider}\n\n搜索引擎来源统计\n${search}" | mail -s "$domain $logdate log statistics" ${email}

代码说明:
实际使用中,需要修改的三个变量log_path,domain和email。
把以上脚本添加到计划任务中,即可实现每天接收到统计数据。

编辑总结:这个小脚本确实不错,用来分析网站日志,找出蜘蛛爬行记录,而且可以针对访问量高的IP进行分析判断,或许能找到恶意的探测者。

您可能感兴趣的文章:
自动统计网站访问日志的shell脚本
如何用shell脚本分析网站日志统计PV、404、500等数据
解决PHP中Web程序中shell_exec()执行Shell脚本不成功问题
shell脚本实现网站日志分析统计
一个监控网站运行情况的shell脚本
分析日志统计网站pv 404 500状态码的shell脚本
分析apache日志中蜘蛛爬行记录数量的shell脚本(图文)
google排名因素列举
搜索引擎蜘蛛爬行统计分析
网站广告分析专用名词

[关闭]