Python real-time processing of log file scripts

Source: Internet
Author: User

This Python script is used to monitor the content of the real-time files, such as Error or the out-of-the-way fields can be customized; my first real Python script, I feel still relatively bloated, but I intend to put on the blog record (or beginners, the gods do not spray ha) , sincerely hope that Bo friends can be pointed again (now record each time the file size value is output to a file, and also nested inside the shell command, which I think can be optimized out of it, but now I do not know how to do); alarms are based on Zabbix, Custom templates are 120s executed once
#!/usr/local/bin/python3.5## #Destription: Read log information in real-time # # #Author: Danny deng## #Datetime: 2016-11-17import re,time,su bprocess,os,linecache#### #定义log文件file_name = "/usr/local/nginx/logs/error.log" File_number = "/usr/local/zabbix_ Agent/number.txt "j = Int (0) seek = Int (0) # #判断过程: Whether the file exists---determine if the file that stores the log size exists---Determine the size of the number size and FileSize # # Define function read file contents by line def readline (): # # # #if判断 If seek is greater than 0, is greater than the assignment, otherwise 0 while true:##### #定义文件, each line is read according to the seek value, each tell is assigned to the seek wit H Open (file_name, ' R ') as F:global seek #seek = Seek f.seek (seek) data = F.rea Dline () If Data:seek = F.tell () yield data else:##### #Python变量转换为she ll variable global file_number os.environ[' seek '] = str (seek) os.environ[' File_numbe R '] = str (file_number) Os.system (' echo $seek > $file _number ') os.system (' Chown Zabbix.zab Bix $file _number ') returndef func_for (): j = Int (0) for I in ReadLine (): F_find = Re.findall (r "Check Time Out", I,flags=re.    IGNORECASE) If "Check Time Out" in F_find:j + = 1#### #没有输出0, the value output has an error match to the number of times the value Try:print (j) except Nameerror:print (int ("0")) # # #判断日志文件是否存在if Os.path.isfile (file_name): # # #判断存储文件内容大小的文件是否存在 If OS.PATH.ISF Ile (File_number): # # # #存在则读取文件size大小, assigned to seek_number seek_number = Int (Linecache.getline (file_number, 1)) # # # #然后继续判断存储 The file size with the current file size (determine if the file is regenerated) if Os.path.getsize (file_name) >= seek_number and Seek_number > 0:seek = Seek_number func_for () # # #若为新文件则, Seek is assigned a value of 0 else: #open (arg1, "A +"). Write ("0") #s eek = Int (Linecache.getline (file_number, 1)) Seek = Int (0) func_for () # # # #file_number does not exist then create new, and assign the value seek change Volume is 0 else: #open (File_number, "A +"). Write ("0") #seek = Int (Linecache.getline (file_number, 1)) OS.E nviron[' file_number ' = str (file_number) oS.system (' echo 0 > $file _number ') os.system (' Chown Zabbix.zabbix $file _number ') func_for () Else:print ( "Error") Quit ()

  

Python real-time processing of log file scripts

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.