shell脚本定时统计Nginx下access.log的PV并发送给API保存到数据库

1,统计PV和IP

统计当天的PV(Page View)

  cat access.log | sed -n /`date "+%d/%b/%Y"`/p |wc -l

统计某一天的PV

  cat access.log | sed -n '/20/Sep/2018/p' | wc -l

查看日志中访问次数最多的前10个IP

  cat access.log.1 |cut -d ' ' -f 1 | sort |uniq -c | sort -nr | awk '{print $0 }' | head -n 10

查看日志中访问次数超过1000次的前10个IP

  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请求

  curl -d "user=admin&passwd=12345678" http://127.0.0.1:8080/login

使用curl发送POST的JSON数据

  curl -H "Content-Type:application/json" -X POST -d '{"user": "admin", "passwd":"12345678"}' http://127.0.0.1:8000/login

使用curl发送动态参数POST请求

  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脚本统计并发送

  #!/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,服务器端接受并保存到数据库

  @RequestMapping(value = "/count")    public void count(String count){    //业务代码   }

总结

以上所述是小编给大家介绍的shell脚本定时统计Nginx下access.log的PV并发送给API保存到数据库,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

参与评论