知乎专栏 |
monitor.py
#!/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('<?xml version="1.0"?>') self.buffer.append('<?xml-stylesheet type="text/xsl" href="vs.xsl"?>') #self.make() pass def clear(self): self.buffer = [] self.ipvss = [] def make(self): self.buffer.append('<ipvs>') for key in self.ipvsdict: ipvs = self.ipvsdict[key] self.node(ipvs.nodes,ipvs.vip+':'+ipvs.vport+' '+ipvs.scheduler) self.buffer.append('</ipvs>') def header(self,vs): self.buffer.append('<!-- --------------------------------------- -->') def node(self, nodes, caption): self.buffer.append('<table>') self.buffer.append('<caption>'+caption+'</caption>') for node in nodes: self.buffer.append('<node>') self.buffer.append('<nip>'+node.nip+'</nip>') self.buffer.append('<nport>'+node.nport+'</nport>') self.buffer.append('<forward>'+node.forward+'</forward>') self.buffer.append('<weight>'+node.weight+'</weight>') self.buffer.append('<active>'+node.active+'</active>') self.buffer.append('<inact>'+node.inact+'</inact>') self.buffer.append('</node>') self.buffer.append('</table>') 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()
ipvs.xsl
<?xml version="1.0" encoding="utf-8"?> <!-- stylesheet by netkiller --> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/> <xsl:template match="/"> <html> <head> <title><xsl:value-of select="table/caption"/></title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta content="陈景峰,网路杀手,网络杀手,bg7nyt,ham,火腿" name="keywords" /> <meta content="陈景峰" name="description" /> <!-- <link rel="shortcut icon" href="favicon.ico" /> <link rel="Bookmark" href="favicon.ico" /> --> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body bgcolor="DFEFFF" text="#000000"> <a name="top" /> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="/ipvs"> <xsl:for-each select="table"> <table width="90%" border="1" cellspacing="0" cellpadding="5" bgcolor="E0F0FF" align="center" bordercolor="4FA7FF"> <caption><xsl:value-of select="caption"/></caption> <xsl:for-each select="node"> <tr> <td><xsl:value-of select="nip"/></td> <td><xsl:value-of select="nport"/></td> <td><xsl:value-of select="forward"/></td> <td><xsl:value-of select="weight"/></td> <td><xsl:value-of select="active"/></td> <td><xsl:value-of select="inact"/></td> </tr> </xsl:for-each> </table> <br /> </xsl:for-each> </xsl:template> <xsl:template match="chapter/title"> <center><h1> <xsl:apply-templates/> </h1> </center> <hr /> </xsl:template> <xsl:template match="ulink"> <a href="{@url}" border="0" > <xsl:apply-templates/> </a> <br /> </xsl:template> <!-- <xsl:apply-templates select="title"/><br /> <xsl:for-each select="setp"> </xsl:for-each> --> </xsl:stylesheet>