温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
  • 首页 > 
  • 教程 > 
  • 服务器 > 
  • shell脚本如何实现定时统计Nginx下access.log的PV并发送给API保存到数据库

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

发布时间:2021-08-21 11:08:39 来源:亿速云 阅读:279 作者:小新 栏目:服务器

这篇文章主要介绍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保存到数据库”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI