脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - shell - shell脚本实现的网站日志分析统计(可以统计9种数据)

shell脚本实现的网站日志分析统计(可以统计9种数据)

2023-02-09 11:50shell教程网 shell

这篇文章主要介绍了shell脚本实现的网站日志分析统计,可以统计9种数据,如访问量、带宽、访客量、IP统计、搜索引擎等,需要的朋友可以参考下

写了个shell脚本,可以用来统计每天的访问日志,并发送到电子邮箱,方便每天了解网站情况。
脚本统计了:
1、总访问量
2、总带宽
3、独立访客量
4、访问IP统计
5、访问url统计
6、来源统计
7、404统计
8、搜索引擎访问统计(谷歌,百度)
9、搜索引擎来源统计(谷歌,百度)

 

复制代码 代码如下:

#!/bin/bash
log_path=/home/www.tuohang.net/log/access.log.1
domain="www.tuohang.net"
email="log@www.tuohang.net"
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 "概况 报告生成时间:${maketime} 总访问量:${total_visit} 总带宽:${total_bandwidth}M 独立访客:${total_unique} 访问IP统计 ${ip_pv} 访问url统计 ${url_num} 来源页面统计 ${referer} 404统计 ${notfound} 蜘蛛统计 ${spider} 搜索引擎来源统计 ${search}" | mail -s "$domain $logdate log statistics" ${email}

 

需要修改的三个变量log_path,domain和email,然后把此脚本添加到计划任务,就可以每天接收到统计的数据了。

延伸 · 阅读

精彩推荐