Python Automation tool log Query Analysis script code implementation _python

Source: Internet
Author: User
Tags readline

Managed node slave.py

Copy Code code as follows:

Import socket
Import re
Class Log (object):
file_list=[' Access.log ', ' C:\\access.log ']
master_ip= ' 192.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 ()

Main control Node master.py

Copy Code code as follows:

Import socket
Class Slvcluster (object):
ip_list=[' 127.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)

Related Article

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.