The Python script handles the log file in real time

  • 2020-05-17 05:50:34
  • OfStack

The Python script is used to monitor the content of real-time files. For example, the Error or time out fields can be customized. Is my first real Python script, feel is bloated, but intend to place on blog record 1 (or a beginner, don't spray) the great god, sincerely hope to give directions again bloggers from under 1 (now record the value of the file size of each output to a file, and is nested inside the shell commands, which I think can be optimized away, just now I also don't know how to do it); The alarm is based on zabbix, and the custom template is 120s executed once


#!/usr/local/bin/python3.5
###Destription:  Real-time read log information 
###Author: Danny Deng
###Datetime: 2016-11-17
import re,time,subprocess,os,linecache
##### define log file 
file_name = "/usr/local/nginx/logs/error.log"
file_number = "/usr/local/zabbix_agent/number.txt"
j = int(0)
seek = int(0)
## Determination process: whether the file exists --- Determines if the file that stores the log size exists --- judge number size  with  filesize The size of the 
### The definition function reads the contents of the file by line 
def readline():
####if judge  seek If more than 0 , is greater than, otherwise, is initially 0
while True:
###### Define the file according to seek The value is read per line, each time tell Assigned to seek
with open(file_name,'r') as f:
global seek
#seek = seek
f.seek(seek)
data = f.readline()
if data:
seek = f.tell()
yield data
else:
######Python Variable conversion to shell variable 
global file_number
os.environ['seek'] = str(seek)
os.environ['file_number'] = str(file_number)
os.system('echo $seek > $file_number')
os.system('chown zabbix.zabbix $file_number')
return
def 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
##### There is no output 0 , there is a value output error The number of subvalues to match 
try:
print(j)
except NameError:
print(int("0"))
### Determine if the log file exists 
if os.path.isfile(file_name):
### Determines whether a file that stores the size of the file's contents exists 
if os.path.isfile(file_number):
#### The file is read if it exists size Size, assign to seek_number
seek_number = int(linecache.getline(file_number, 1))
#### Then proceed to determine the size of the stored file and the current file size ( Determines if the file is regenerated )
if os.path.getsize(file_name) >= seek_number and seek_number > 0:
seek = seek_number
func_for()
### If it is a new file, seek  The assignment for 0
else:
#open(arg1, "a+").write("0")
#seek = int(linecache.getline(file_number, 1))
seek = int(0)
func_for()
####file_number  If it doesn't exist, create it and assign it seek Variable is 0 
else:
#open(file_number, "a+").write("0")
#seek = int(linecache.getline(file_number, 1))
os.environ['file_number'] = str(file_number)
os.system('echo 0 > $file_number')
os.system('chown zabbix.zabbix $file_number')
func_for()
else:
print("Error")
quit()

Related articles: