Python论坛  - 讨论区

标题:[python-chinese] LVS 状态监控

2007年04月07日 星期六 18:56

netkiller openunix在163.com
星期六 四月 7 18:56:01 HKT 2007

python½Å±¾£¬Éú³Éxml£¬ÒòΪÎÒµÄxmlºÜ¼òµ¥£¬Ã»ÓÐʹÓÃdom£®

#!/usr/bin/env python

class Ipvs:
    types = ''
    vip = '0.0.0.0'
    vport = '0'
    scheduler = ''
    nodes = []
    """
    def __init__(self, vs):
        self.types = vs[0]
        self.vip = vs[1]
        self.vport = vs[2]
        self.scheduler = vs[3]
        self.nodes = vs[4]
    """                                                                                    
class Node:
    nip     = '0.0.0.0'
    nport   = ''
    forward = ''
    weight = 0
    active = 0
    inact  = 0
    def __init__(self, node):
        nip = node[0]
        nport = node[1]
        forward = node[2]
        weight = node[3]
        active = node[4]
        incat = node[5]
        self.nip     = nip
        self.nport   = nport
        self.forward = forward
        self.weight = weight
        self.active = active
        self.inact  = incat

class Monitor:
    buffer = []
    ipvsdict = {}
    def __init__(self):
        self.buffer.append('')
        self.buffer.append('')
        #self.make()
 pass
    def clear(self):
        self.buffer = []
        self.ipvss = []
    def make(self):
        self.buffer.append('')
        for key in self.ipvsdict:
            ipvs = self.ipvsdict[key]
            self.node(ipvs.nodes,ipvs.vip+':'+ipvs.vport+' '+ipvs.scheduler)
        self.buffer.append('')
    def header(self,vs):
        self.buffer.append('')
    def node(self, nodes, caption):
        self.buffer.append('')
        self.buffer.append('')
 for node in nodes:
            self.buffer.append('')
            self.buffer.append(''+node.nip+'')
            self.buffer.append(''+node.nport+'')
            self.buffer.append(''+node.forward+'')
            self.buffer.append(''+node.weight+'')
            self.buffer.append(''+node.active+'')
            self.buffer.append(''+node.inact+'')
            self.buffer.append('')
        self.buffer.append('
'+caption+'
') def display(self): for buf in self.buffer: print buf def saveAs(self,filename): # if filename: f = open(filename,'w') for buf in self.buffer: f.write(buf) f.close() def save(self): self.saveAs('vs.xml') def ipvslist(self): w,r = os.popen2(IPVSADM) w.close() version = r.readline() vsfield = r.readline() nodefield = r.readline() pattern_vs = r'(\w+)\s+([0-9.]+):(\w+)\s+(\w+)' pattern_node = r'\s->\s([0-9.]+):(\w+)\s+(\w+)\s+(\d+)\s+(\d+)\s+(\d+)' cp_vs = re.compile(pattern_vs) cp_node = re.compile(pattern_node) current_vs = '' for line in r.readlines(): if line[:3] == 'TCP' or line[:3] == 'UDP': current_vs = line result = cp_vs.search(line).groups() ipvs = Ipvs() ipvs.types = result[0] ipvs.vip = result[1] ipvs.vport = result[2] ipvs.scheduler = result[3] ipvs.nodes = [] self.ipvsdict[current_vs] = ipvs elif line[2:4]== '->': result = cp_node.search(line).groups() oneNode = Node(result) #nodes.append(oneNode) self.ipvsdict[current_vs].nodes.append(oneNode) class Network: interface = [] def __init__(self): pass def hostname: pass class Ipvsadmin: cmdline = '' vscache = [] forward = {'nat':'','route':'','tunel':''} def load(self, config): pass def vip(self, vip, vport, scheduler): pass def rip(self, vip,rip,rport,forward,weight): pass def list(self): pass def saveAs(self): pass def restore(self): pass class Deploy: src = ['vs.xml','vs.xsl'] dst = '' def __init__(self): pass def target(self, dst): self.dst = dst def start(self): try: for srcfile in self.src: shutil.copy(srcfile,self.dst) except (IOError, os.error), why: print "Can't copy %s to %s: %s" % (`self.src`, `self.dst`, str(why)) import os,re import shutil IPVSADM='/sbin/ipvsadm' def main(): xml = Monitor() xml.ipvslist() xml.make() #xml.display() xml.save() #xml.saveAs('/var/www/vs.xml') deploy = Deploy() deploy.target('/var/www') deploy.start() if __name__ == "__main__": main() XSL Îļþ£º http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:value-of select="table/caption"></xsl:value-of>



´ó¼Ò¸øµã½¨Ò飮 Neo Chan 10 Fl, Libra, Apple Park, Longhua town, Baoan District, Shenzhen ÉîÛÚÊб£°²ÇøÁú»ªÕò http://www.flickr.com/photos/bg7nyt http://netkiller.8800.org AIM: bg7nyt

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号