Python program code for restructuring the log monitoring script

Source: Internet
Author: User
Tags php and

First look at the code:

Nginx log monitoring script Python

#! /Usr/bin/python2.6
# Coding = UTF-8
Import OS
Import time
# Logging
Num_file = '/data/www/www.111cn.net/log/num'
Log_file = '/data/www/www.111cn.net/log/www.111cn.net.log'
# Ip shield function
Def shellcmd (ip, con ):
OS. system ('/root/shell/nginx/editblocksip. sh add' + ip)
OS. system ('echo '+ con +' | mail-s "log info" zhangcunchao_cn@163.com ')
 
Nowfile = OS. getcwd () + "/" + _ file __
Stime = OS. stat (nowfile). st_mtime
# Exit when the modification time changes
While stime = OS. stat (nowfile). st_mtime:
Log_num = str (int (OS. popen ("cat" + num_file). read ()))
Real_num = str (int (OS. popen ("cat" + log_file + "| wc-l"). read ()))
If log_num! = Real_num:
# Insert new records
OS. system ('echo '+ real_num +'> '+ num_file)
Content = OS. popen ("tail-n +" + log_num + "" + log_file). read (). split ("n ")
For con in content:
If ""! = Con:
C = con. split ('')
If '20140901 '! = C [8] and '2014. 253.28.43 '! = C [0]:
If ". rar" in c [6]:
Shellcmd (c [0], con)
Elif '/wp-comments-post.php' in c [6] and 'msie '= c [13] and '6. 0;' = c [14]:
Shellcmd (c [0], con)
Elif '"-"' = c [11] and '"-"' = c [12] and '. Php' in c [6]:
Shellcmd (c [0], con)
Time. sleep (1)

Function I used the shell command, editblocksip for ease. the sh script was previously used to operate the nginx blacklist. This script was inspired by the write of the php daemon process. 1. Each while loop checks whether the file itself has been modified, if the modification ends, and then the process daemon shell is enabled again, 2. The intermediate implementation is also very simple. Record the recently read row number and generate a new record, and then perform the monitoring operation, 3. Filter the blocked 403 status and IP address. 4. Then, you need some blocking rules. If triggered, you can call editblocksip. sh, add this ip address to the 403 blacklist, so that user access will display my 403 page.
Check_python.sh python process daemon queue code

Shell


#! /Bin/bash
EMAIL = 'zhangcunchao _ cn@163.com'
Start ()
{
C = 'PS w-C python | grep $1 | wc-L'
If [$ c-lt 1]
Then
If [-f "$1"]; then
/Usr/bin/python $1>/dev/null &
Else
'Echo 'no such file' $1 | mail-s' process check error' $ EMAIL'
Fi
Fi
}
BASE_PATH = 'dirname $0 '"/"
Cd $ BASE_PATH
 
Start log. py
The principle is quite simple.
Here, I will also briefly talk about my feelings about python.
In general, the python syntax is indeed quite different from that of php. Its syntax is actually quite similar to that of js. The biggest feature of python is its strict indentation. It uses indentation to control code blocks, because it does not have braces such as {}, and variables use simple reference counts for optimization like php, however, the import method like import is not very efficient, because we should be able to understand it. During my learning process, I also made a special comparison between the execution efficiency of php and python, if the while loop is used, php should be about half faster than python. Of course, this cannot be explained, but python is indeed a good assistant for O & M.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.