This article mainly introduces the implementation of the python log query and analysis script code, which can be controlled nodes and master nodes. for details, refer to the controlled node slave. py.
The code is as follows:
Import socket
Import re
Class Log (object ):
File_list = ['Access. log', 'C: \ access. log']
Master_ip = '1970. 168.0.103'
Def _ init _ (self ):
S = socket. socket (socket. AF_INET, socket. SOCK_STREAM)
S. bind ('', 3333 ))
S. listen (1)
While True:
Conn, addr = s. accept ()
Print addr [0]
If addr [0] = self. master_ip:
Reg = conn. recv (1024)
Result = self. all_log (reg)
Conn. sendall (result)
Conn. close ()
Def all_log (self, reg ):
Logs =''
For f in self. file_list:
Logs + = '\ n' + self. log_match (f, reg)
Return logs
Def log_match (self, f, reg ):
Log_result = '------------------------' + f + '----------------------' + '\ n'
Fo = open (f, 'r ')
Line = fo. readline ()
Rp = re. compile (reg)
While line! = '':
Log_match = rp. match (line)
If log_match:
Log_result + = '\ n' + log_match.group ()
Line = fo. readline ()
Return log_result
If _ name __= = '_ main __':
Ds = Log ()
Master node master. py
The code is as follows:
Import socket
Class SlvCluster (object ):
Ip_list = ['2017. 0.0.1 ']
Def _ init _ (self, reg ):
For ip in self. ip_list:
Self. single_slv (ip, reg)
Def single_slv (self, slv_ip, reg ):
S = socket. socket (socket. AF_INET, socket. SOCK_STREAM)
S. connect (slv_ip, 3333 ))
S. sendall (reg)
Print '-----------------------' + slv_ip + '--------------------------'
Print s. recv (102400)
S. close ()
If _ name __= = '_ main __':
Reg = raw_input ('input the regular expression :')
Print '----------- Regular Expression:' + reg + '-----------------'
SC = SlvCluster (reg)