[原创]运维主动化对象之日记查询篇

    添加时间:2013-6-16 点击量:

    固然说比拟python更喜好Ruby,然则如今的运维景象使得我们不太敢用Ruby,因为有些时辰真的不是简单地输入yum install ruby就可以装上ruby的,所以临时还是用python。


    受控节点slave.py



    import socket
    
    import re
    class Log(object):
    file_list
    =[access.logC:\\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()


    主控节点master.py



    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)


    所有随风而逝的都属于昨天的,所有历经风雨留下来的才是面向未来的。—— 玛格丽特·米切尔 《飘》
    分享到: