Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand linux Nginx log scripts

Shulou Source: shulou.com Published: 2022-06-03 07:41:27 11月15日 Update

This article focuses on "how to understand the linux Nginx log script", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the linux Nginx log script.

Mission planning

Crontab-l

1 15 * / home/dongnan/sh/split.sh > > / home/dongnan/sh/cron.log 2 > &

Nginx log

Ls / var/log/nginx/

20130730-access.log.gz 20130801-access.log.gz 20130803-access.log.gz

20130730-error.log.gz 20130801-error.log.gz 20130803-error.log.gz

20130731-access.log.gz 20130802-access.log.gz access.log

20130731-error.log.gz 20130802-error.log.gz error.log

Shell script

Cat split.sh

The code is as follows:

#! / bin/bash

# script_name:nginx_log.sh

# description:nginx-log deleted/rotate/compress

# last_update:20130725 by zongming

# Nginx

# Signal Action

# TERM, INT Terminate the server immediately

# QUIT Stop the server

# HUP Configuration changes, start new workers, graceful stop of old workers

# USR1 Reopen log files

# USR2 Upgrade the server executable

# WINCH Graceful Stop (parent process advise the children to exit)

# variables

Log_dir=/var/log/nginx/

Log_date=$ (date + "% Y%m%d")

Nginx_pid=/var/run/nginx.pid

Keep_days=30

# old_log

Find "$log_dir"-name "*\ .log.gz"-type f-mtime + "${keep_days}"-exec rm-rf {}\

# rename_log

For log_name in `ls "$log_dir" | awk'/ .log $/'`; do

If [- e "${log_dir} ${log_date}-${log_name}"]; then

Echo "${log_dir} ${log_date}-${log_name} Already exists" & & continue

Else

/ bin/mv "${log_dir} ${log_name}"${log_dir} ${log_date}-${log_name}"

/ bin/gzip "${log_dir} ${log_date}-${log_name}"

Fi

Done

# new_log

/ bin/kill-USR1 $(cat $nginx_pid) & & / bin/sleep 1

Nginx log slicing script:

Vi / root/cutlog.sh

The code is as follows:

#! / bin/bash

I = `ps aux | grep nginx | grep root | grep-v 'grep nginx' | awk' {print $14}'`# find the nginx process

If [$I = = / usr/local/nginx/sbin/nginx]; then

ACCLOG= `cat / usr/local/nginx/conf/nginx.conf | grep 'access_log' | awk' {print $2}'`# if the nginx process is present, find the configuration file and read the accesslog path

ERRLOG= `cat / usr/local/nginx/conf/nginx.conf | grep ^ error | awk'{print $2}'| cut-d ";"-f1` # path to the error log

Ls $ACCLOG # to see if this file is available

If [$?-eq 0]; then # if any

Mv $ACCLOG $ACCLOG.`date-d "- 1 day" +% F` # rename the current log

Mv $ERRLOG $ERRLOG.`date-d "- 1 day" +% F`

Touch $ACCLOG # create an empty log

Touch $ERRLOG

Chown nginx:root $ACCLOG # modify owner

Chown nginx:root $ERRLOG

[- f / usr/local/nginx/logs/nginx.pid] & & kill-USR1 `cat / usr/local/nginx/logs/ nginx.pid` # judge the process and reload it (kill-USR1 here will cause nginx to write the newly created log to the new log you just created. )

/ mnt/logs/checklog.sh $ACCLOG.`date "- 1 day" +% F` # this is a log analysis script

Gzip $ACCLOG.`date-d "- 1 day" +% F` # compress log

Gzip $ERRLOG.`date-d "- 1 day" +% F`

Mv $ACCLOG.`date-d "- 10 day" +% F`.* / mnt/history.nginx.log/ # clean up the old logs from 10 days ago to other places (if you want to delete them, you can delete them yourself)

Mv $ERRLOG.`date-d "- 10 day" +% F`.* / mnt/history.nginx.log/

Fi

Fi

Nginx log analysis script:

Vi / mnt/logs/checklog.sh

The code is as follows:

#! / bin/bash

Echo-e "# `date +% F`" > > / mnt/logs/400.txt

Echo-e "# `date +% F`" > > / mnt/logs/URL.txt

Echo-e "# `date +% F`" > > / mnt/logs/IP.txt

Cat $1 | wc-l > > / mnt/logs/IP.txt # analyze IP

Cat $1 | awk-F'"'{print $3}'| awk'{print $1}'| sort | uniq-c | sort-rn > / mnt/logs/CODE.txt # returned value from analysis

Cat $1 | awk'{print $1}'| sort | uniq-c | sort-rn | head-N20 > > / mnt/logs/IP.txt

N = `cat / mnt/logs/CODE.txt | wc-l`

For I in $(seq 1$ N)

Do

M = `head-nailed I / mnt/logs/CODE.txt | tail-N1 | awk'{print $2}'`

If [$M-ge 400]

Then

Echo "# FIND $M#" > > / mnt/logs/400.txt # analyze error request

Cat $1 | grep "\" $M "| grep-v'-"-"-'| sort | awk'{print $1 $2 $3 $6 $7 $9 $10 $11 $12 $13 $14 $15 $16 $17 $19 $20 $21}'| sort | uniq-c | sort-rn | head-N5 > > / mnt/logs/400.txt

Fi

Done

Cat $1 | grep-v'"-'| awk-Flying T' {print $2}'| awk-Foundry'{print $1}'| sort | awk'{print $1}'| sed's /\ (\ / review\ / file\ / download\ /). * /\ 1Universe | sort | uniq-c | sort-rn | head-N20 > > / mnt/logs/URL.txt

At this point, I believe you have a deeper understanding of "how to understand the linux Nginx log script". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Logs scripts analysis code processes content files paths errors learning practical deeper tasks interests places practicality practical simple operation new production methods Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Tech Info Redmi Xiaomi Apple