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保存到數據庫,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://www.cnblogs.com/i-tao/archive/2018/09/18/9668113.html