shell脚本定时统计Nginx下access.log的PV并发送给API保存到数据库
这篇文章主要介绍了shell脚本定时统计Nginx下access.log的PV并发送给API保存到数据库的实现方法 ,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
1,统计PV和IP
统计当天的PV(Page View)
1 | cat access.log | sed -n /` date "+%d\/%b\/%Y" ` /p | wc -l |
统计某一天的PV
1 | cat access.log | sed -n '/20\/Sep\/2018/p' | wc -l |
查看日志中访问次数最多的前10个IP
1 | cat access.log.1 | cut -d ' ' -f 1 | sort | uniq -c | sort -nr | awk '{print $0 }' | head -n 10 |
查看日志中访问次数超过1000次的前10个IP
1 | cat access.log.1 | cut -d ' ' -f 1 | sort | uniq -c | sort -nr | awk '{if($1>1000) print $0 }' | head -n 10 |
2,curl发送数据
使用curl发送GET请求
curl http://127.0.0.1:8080/login?admin&passwd=12345678
使用curl发送POST请求
1 | curl -d "user=admin&passwd=12345678" http: //127 .0.0.1:8080 /login |
使用curl发送POST的JSON数据
1 | curl -H "Content-Type:application/json" -X POST -d '{"user": "admin", "passwd":"12345678"}' http: //127 .0.0.1:8000 /login |
使用curl发送动态参数POST请求
1 2 | curl -i -X POST -H "'Content-type':'application/json'" -d '{"ATime":"' $atime '","BTime":"' $btime '"}' $url curl -i -X POST -H "'Content-type':'application/json'" -d '{"ATime":"' ${atime} '","BTime":"' {$btime} '"}' ${url} |
3,shell脚本统计并发送
1 2 3 4 5 6 7 8 | #!/bin/bash log_path= /var/log/nginx/access .log domain= "http://127.0.0.1:8080/data/count" log_date=` date "+%d/%b/%Y" ` echo ${log_date} total_visit=` cat ${log_path} | grep $log_date| wc -l` curl -d "count=${total_visit}" ${domain} echo $total_visit |
4,服务器端接受并保存到数据库
1 2 3 4 | @RequestMapping(value = "/count" ) public void count(String count){ // 业务代码 } |
总结
以上所述是小编给大家介绍的shell脚本定时统计Nginx下access.log的PV并发送给API保存到数据库,希望对大家有所帮助