静态页面部署在nginx上,页面只有查看下载功能。
需求是统计每条访问次数和下载次数,根据日志分析写了一个shell脚本,触发脚本后生成一个html可以远程查看统计的数量。
#!/bin/bash
# nginx日志文件路径
LOG_FILE="/usr/local/nginx/logs/access.log"
#统计访问IP
#_index=$(grep "GET /public/index.html" "$LOG_FILE" | awk -F' ' '{print $1}' | sort | uniq -c)
#_pdf=$(grep "GET /public/11.15.pdf" "$LOG_FILE" | awk -F' ' '{print $1}' | sort | uniq -c)
#统计访问链接
_index=$(grep "GET /public/index.html" "$LOG_FILE" |awk -F'[][]' '{print $2}')
_pdf=$(grep "GET /public/11.15.pdf" "$LOG_FILE" | awk -F'[][]' '{print $2}')
#结果写入日志
echo "$_index" > public_index_access.log
echo "$_pdf" > public_pdf_access.log
#进行统计
echo "index.html access:<hr>" > public_access.log
cut -d: -f1 public_index_access.log | sort | uniq -c| awk '{print $2, $1}'| awk '{print $0, "<br>"}' >> public_access.log
echo "<br><br>pdf access:<hr>" >> public_access.log
cut -d: -f1 public_pdf_access.log | sort | uniq -c| awk '{print $2, $1}'| awk '{print $0, "<br>"}' >> public_access.log
sleep 1
# 将访问统计数据插入到HTML页面中
ACCESS_STATS="$(cat public_access.log)"
html_content='
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>访问统计</title>
</head>
<body><h1>访问统计数据</h1><div id="access_stats">'$ACCESS_STATS'</div>
</body>
</html>
'# 将内容写入 HTML 文件
echo "$html_content" > index.html# 输出成功信息
echo "HTML file generated: index.html"