Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use shell script to count the PV of access.log under Nginx and send it to API to save it to database

Shulou Source: shulou.com Published: 2022-06-01 13:28:32 12月03日 Update

This article mainly introduces how the shell script realizes the timed statistics of the PV of access.log under Nginx and sends it to API to save to the database. The article is very detailed and has certain reference value. Interested friends must read it!

1. Statistics of PV and IP

Count the PV (Page View) of the day

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

Count the PV of a certain day

Cat access.log | sed-n'/ 20\ / Sep\ / 2018Unip'| wc-l

View the top 10 most visited IP in the log

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

View the top 10 IP with more than 1000 visits in the log

Cat access.log.1 | cut-d'- f 1 | sort | uniq-c | sort-nr | awk'{if ($1 > 1000) print $0}'| head-n 10

2the data is sent by dint curl.

Send a GET request using curl

Curl http://127.0.0.1:8080/login?admin&passwd=12345678

Send a POST request using curl

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

Use curl to send JSON data for POST

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

Send a dynamic parameter POST request using curl

Curl-I-X POST-H "'Content-type':'application/json'"-d' {"ATime": "'$atime'", "BTime": "'$btime'"}'$urlcurl-I-X POST-H "'Content-type':'application/json'"-d' {"ATime": "'${atime}'", "BTime": "'{$btime}'"}'${url}

3Jing shell script statistics and send

#! / bin/bashlog_path=/var/log/nginx/access.logdomain= "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. The server accepts it and saves it to the database

@ RequestMapping (value = "/ count") public void count (String count) {/ / Business Code} these are all the contents of the article "how to regularly count the PV of access.log under Nginx and send it to API to save in the database". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Statistics data databases scripts content logs times articles business code value interests dynamics parameters guys partners more servers knowledge industries Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux vpn MariaDB Shulou Information Shulou Technology