def switch_port_mirror_remove(args): assert isinstance(args, dict) switch_info = NetworkDevice.get(NetworkDevice.mip == args['mip']) cmd = SCRIPT_SWITCH_CONF cmd += "del-mirror %s %s %s %s '%s'" % ( switch_info.brand, switch_info.mip, switch_info.username, switch_info.password, switch_info.enable) rc, output = commands.getstatusoutput(cmd) if rc: print >>sys.stderr, 'Error: "%s" failed' % cmd print >>sys.stderr, 'Error:\n%s' % output return 0
def switch_port_mirror_remove(args): assert isinstance(args, dict) switch_info = NetworkDevice.get(NetworkDevice.mip == args['mip']) cmd = SCRIPT_SWITCH_CONF cmd += "del-mirror %s %s %s %s '%s'" % ( switch_info.brand, switch_info.mip, switch_info.username, switch_info.password, switch_info.enable) rc, output = commands.getstatusoutput(cmd) if rc: print >> sys.stderr, 'Error: "%s" failed' % cmd print >> sys.stderr, 'Error:\n%s' % output return 0
def get_physical_topo(): matrix = {} # get switch and lldp info try: switchs = NetworkDevice.select() except Exception: print >> sys.stderr, 'Error: get network_device from db failed' return {} for switch in switchs: # get hostname cmd = '%s %s %s %s %s %s "%s" JSON HOSTNAME' \ % (SCRIPT_SWITCH_CONF, 'show', switch.brand, switch.mip, switch.username, switch.password, switch.enable) rc, output = commands.getstatusoutput(cmd) if rc: print >> sys.stderr, \ 'Error: cmd [%s] failed: rc=%d out=%s' % \ (cmd.replace(switch.password, '*').replace( '"%s"' % switch.enable, '*'), rc, output) continue hostname = json.loads(output).get('OUTPUT') if not hostname: continue # get lldp neighbour cmd = '%s %s %s %s %s %s "%s" JSON LLDP' \ % (SCRIPT_SWITCH_CONF, 'show', switch.brand, switch.mip, switch.username, switch.password, switch.enable) rc, output = commands.getstatusoutput(cmd) if rc: print >> sys.stderr, \ 'Error: cmd [%s] failed: rc=%d out=%s' % \ (cmd.replace(switch.password, '*').replace( '"%s"' % switch.enable, '*'), rc, output) continue edges = [] if output: # OUTPUT: # [{ # 'LOCAL_PORT_NAME': 'Ethernet21', # 'REMOTE_PORT_NAME': 'eth22', # 'REMOTE_NODE': 'KVM-81' # }] edges = json.loads(output).get('OUTPUT', []) switch.name = hostname matrix[switch.mip] = ('SWITCH', switch, edges) # get host info try: hosts = HostDevice.select() except Exception: print >> sys.stderr, 'Error: get host_device from db failed' return {} for host in hosts: matrix[host.name] = ('HOST', host) matrix[host.ip] = ('HOST', host) # get rack info try: racks = Rack.select() except Exception: print >> sys.stderr, 'Error: get rack from db failed' return {} for rack in racks: matrix[rack.id] = ('RACK', rack) return matrix # {switch.mip/host.name/host.ip/rack.id: info}