静态页面部署在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:
" > public_access.log - cut -d: -f1 public_index_access.log | sort | uniq -c| awk '{print $2, $1}'| awk '{print $0, "
"}' >> public_access.log - echo "
pdf access:
" >> public_access.log - cut -d: -f1 public_pdf_access.log | sort | uniq -c| awk '{print $2, $1}'| awk '{print $0, "
"}' >> public_access.log - sleep 1
- # 将访问统计数据插入到HTML页面中
- ACCESS_STATS="$(cat public_access.log)"
- html_content='
-
-
-
-
访问统计 -
访问统计数据
- '$ACCESS_STATS'
- '
-
- # 将内容写入 HTML 文件
- echo "$html_content" > index.html
-
- # 输出成功信息
- echo "HTML file generated: index.html"
