Python automation tool log query and analysis script code implementation

Source: Internet
Author: User
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)

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.