The shell script regularly counts the PV of access.log under Nginx and sends it to the API and saves it in the database

The shell script regularly counts the PV of access.log under Nginx and sends it to the API and saves it in the database

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\/2018/p' | wc -l

View the top 10 IP addresses with the most visits 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 addresses with more than 1,000 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

2. curl sends data

Sending a GET request using curl

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

Sending a POST request using curl

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

Send JSON data using curl

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

Sending dynamic parameter POST request using curl

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 script statistics and sending

#!/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. The server accepts and saves it to the database

@RequestMapping(value = "/count")
  public void count(String count){
  //Business code}

Summarize

The above is the shell script that I introduced to you. It regularly counts the PV of access.log under Nginx and sends it to the API to save it in the database. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • Shell script nginx automation script
  • Example code for splitting Nginx logs using shell script
  • Shell script to batch clear Nginx cache
  • nginx log cutting shell script
  • Detailed explanation of putting common nginx commands into shell scripts

<<:  Detailed tutorial on installation and configuration of MySql 5.7.17 winx64

>>:  Native js implements a minesweeper game with custom difficulty

Recommend

A brief analysis of the use of watchEffect in Vue3

Preface Everyone should be familiar with the watc...

Learn v-model and its modifiers in one article

Table of contents Preface Modifiers of v-model: l...

React implements a highly adaptive virtual list

Table of contents Before transformation: After tr...

HTML Several Special Dividing Line Effects

1. Basic lines 2. Special effects (the effects ar...

jQuery implements form validation

Use jQuery to implement form validation, for your...

Sharing tips on using scroll bars in HTML

Today, when we were learning about the Niu Nan new...

Vue uses mixins to optimize components

Table of contents Mixins implementation Hook func...

Linux kernel device driver kernel time management notes

/****************** * Linux kernel time managemen...

Specific implementation methods of MySQL table sharding and partitioning

Vertical table Vertical table splitting means spl...

Detailed explanation of primary keys and transactions in MySQL

Table of contents 1. Comments on MySQL primary ke...

Modify the jvm encoding problem when Tomcat is running

question: Recently, garbled data appeared when de...

Ubuntu 20.04 sets a static IP address (including different versions)

Because Ubuntu 20.04 manages the network through ...

Analysis of the differences between Iframe and FRAME

1. Use of Iframe tag <br />When it comes to ...

202 Free High Quality XHTML Templates (2)

Following the previous article 202 Free High-Qual...

Application of mapState idea in vuex

Table of contents 1. Map method 2. Application ba...