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

Hidden overhead of Unix/Linux forks

Table of contents 1. The origin of fork 2. Early ...

Solution to MySql Error 1698 (28000)

1. Problem description: MysqlERROR1698 (28000) so...

Analysis of the project process in idea packaging and uploading to cloud service

one. First of all, you have to package it in idea...

Weird and interesting Docker commands you may not know

Intro Introduces and collects some simple and pra...

HTML user registration page settings source code

Design the web page shown above: <!DOCTYPE htm...

Web page HTML code explanation: ordered list and unordered list

In this section, we will learn about list element...

Should I use UTF-8 or GB2312 encoding when building a website?

Often when we open foreign websites, garbled char...

Comprehensive explanation of CocosCreator hot update

Table of contents Preface What is Hot Change Coco...

How to configure /var/log/messages in Ubuntu system log

1. Problem Description Today I need to check the ...

Differences between proxy_pass in two modules in nginx

1. The proxy_pass directive of the 1.ngx_stream_p...

How to use MySQL group by and order by together

Suppose there is a table: reward (reward table), ...

How to use dl(dt,dd), ul(li), ol(li) in HTML

HTML <dl> Tag #Definition and Usage The <...

Detailed analysis of several situations in which MySQL indexes fail

1. Leading fuzzy query cannot use index (like ...