def tcpdump_vgateway(args): try: vgw = VGateway.get(VGateway.id == args['id']) except Exception: print >> sys.stderr, 'Error: get vgw from db failed' return try: host = HostDevice.get(HostDevice.ip == vgw.gw_launch_server) except Exception: print >> sys.stderr, \ 'Error: get gw_launch_server %s from db failed' % \ vgw.gw_launch_server return try: vif = VInterface.get(VInterface.devicetype == VIF_DEVICE_TYPE_VGATEWAY, VInterface.deviceid == args['id'], VInterface.ifindex == args['if_index']) except Exception: print >> sys.stderr, 'Error: get vif if_index=%s from db failed' % \ args['if_index'] return if vif.iftype == VINTERFACE_TYPE_WAN: if_name = '%d-w-%d' % (vgw.id, vif.ifindex) else: if_name = '%d-l-%d' % (vgw.id, vif.ifindex) tcpdump_cmds = [ 'tcpdump', '-i', if_name, '-l', '-nn', '-e', '-v', '-c', args['count'], args['filter'] ] print ' '.join(tcpdump_cmds) for line in call_system_streaming([ '/usr/bin/timeout', args['timeout'], '/usr/bin/sshpass', '-p', host.user_passwd, 'ssh', ] + SSH_OPTION_LIST + ['%s@%s' % (host.user_name, host.ip)] + tcpdump_cmds, ignore_err=False): print line, # do not print duplicate newline
def tcpdump_vgateway(args): try: vgw = VGateway.get(VGateway.id == args['id']) except Exception: print >> sys.stderr, 'Error: get vgw from db failed' return try: host = HostDevice.get(HostDevice.ip == vgw.gw_launch_server) except Exception: print >> sys.stderr, \ 'Error: get gw_launch_server %s from db failed' % \ vgw.gw_launch_server return try: vif = VInterface.get( VInterface.devicetype == VIF_DEVICE_TYPE_VGATEWAY, VInterface.deviceid == args['id'], VInterface.ifindex == args['if_index']) except Exception: print >> sys.stderr, 'Error: get vif if_index=%s from db failed' % \ args['if_index'] return if vif.iftype == VINTERFACE_TYPE_WAN: if_name = '%d-w-%d' % (vgw.id, vif.ifindex) else: if_name = '%d-l-%d' % (vgw.id, vif.ifindex) tcpdump_cmds = [ 'tcpdump', '-i', if_name, '-l', '-nn', '-e', '-v', '-c', args['count'], args['filter']] print ' '.join(tcpdump_cmds) for line in call_system_streaming( ['/usr/bin/timeout', args['timeout'], '/usr/bin/sshpass', '-p', host.user_passwd, 'ssh', ] + SSH_OPTION_LIST + ['%s@%s' % (host.user_name, host.ip)] + tcpdump_cmds, ignore_err=False): print line, # do not print duplicate newline
def find_vif_by_id(vifid): try: vif = VInterface.get(VInterface.id == vifid) except Exception: return (None, None, None, 'Can not find vif id=%d in db' % vifid) try: if vif.devicetype == VIF_DEVICE_TYPE_VM: ins = VM.get(VM.id == vif.deviceid) host = HostDevice.get(HostDevice.ip == ins.launch_server) elif vif.deviceid == VIF_DEVICE_TYPE_VGATEWAY: ins = VGateway.get(VGateway.id == vif.deviceid) host = HostDevice.get(HostDevice.ip == ins.gw_launch_server) else: return (None, None, vif, 'Can not find device/host of %r' % vif.mac) except Exception: return (None, None, vif, 'Can not find device/host of %r' % vif.mac) return (host, ins, vif, '')
def dfs(matrix, dest_mac, depth, path, curr_hop, in_port): if not curr_hop: return indent = (' ' * depth) + str(depth + 1) depth += 1 if curr_hop[0] == 'SWITCH': switch = curr_hop[1] switch_type = 'ToR' if switch.rackid == 0: switch_type = 'Agg' rack_name = '%d' % switch.rackid if switch.rackid in matrix: rack_name = matrix[switch.rackid][1].name trace_result = trace_in_switch(switch, dest_mac) for tr in trace_result: port_name, iface_name, out_rate = tr print l_fmt % ( indent, switch.rackid, display_long_string(rack_name, 1, 3), switch_type, display_long_string(switch.name, 3, 5), switch.mip, display_long_string(in_port, 3, 5), display_long_string(iface_name, 3, 5), out_rate, display_long_string(port_name, 3, 11) if iface_name != port_name else '', # 仅当iface!=port时显示 '') if iface_name == 'Vxlan1': for hop in matrix.values(): if hop[0] != 'SWITCH' or hop[1].mip in path or \ hop[1].rackid == switch.rackid: continue dfs(matrix, dest_mac, depth, path + [hop[1].mip], hop, 'Vxlan1') else: next_hop, next_in_port = get_switch_neighbour( matrix, switch.name, iface_name) if next_hop: next_ip = '' if next_hop[0] == 'SWITCH': next_ip = next_hop[1].mip elif next_hop[0] == 'HOST': next_ip = next_hop[1].ip dfs(matrix, dest_mac, depth, path + [next_ip], next_hop, next_in_port) else: # nexthop is a spine switch and has no API support for hop in matrix.values(): if hop[0] != 'SWITCH' or hop[1].mip in path or \ hop[1].rackid == switch.rackid: continue dfs(matrix, dest_mac, depth, path + [hop[1].mip], hop, 'Spine?') elif curr_hop[0] == 'HOST': host = curr_hop[1] host_type = 'Unknown' if host.type == HOST_TYPE_VM: host_type = 'KVM' elif host.type == HOST_TYPE_NSP: host_type = 'NSP' rack_name = '%d' % host.rackid if host.rackid in matrix: rack_name = matrix[host.rackid][1].name trace_result = trace_in_host(host, dest_mac) for tr in trace_result: dom_name, br_name, br_type, in_iface, \ out_iface_type, out_port, out_iface, out_rate = tr if host_type == 'NSP': # 通过id查询vgw/bwt的name try: vgw_id = 0 if in_iface[:3] != 'eth' and in_iface[0] != '?': vgw_id = in_iface[:in_iface.index('-')] elif out_iface[:3] != 'eth' and out_iface[0] != '?': vgw_id = out_iface[:out_iface.index('-')] if vgw_id: vgw = VGateway.get(VGateway.id == vgw_id) dom_name = vgw.name else: dom_name = '?' except Exception: print >> sys.stderr, \ 'Error: get vgw %d from db failed', vgw_id print l_fmt % ( indent, host.rackid, display_long_string(rack_name, 1, 3), host_type, display_long_string(host.name, 3, 5), host.ip, # 当host上无法得知in_iface时, # 使用dfs过程中在上一跳得知的lldp neighbour作为本跳的in_port ('%s?' % display_long_string(in_port, 3, 5)) if in_iface == '?' else display_long_string(in_iface, 3, 5), display_long_string(out_iface, 3, 5), out_rate, display_long_string(out_port, 3, 11) if out_iface != out_port else '', # 仅当iface!=port时显示 display_long_string(dom_name, 12, 0)) if out_iface_type == 'PHYSICAL': next_hop, next_in_port = get_host_neighbour( matrix, host.name, out_iface) if next_hop: dfs(matrix, dest_mac, depth, path + [next_hop[1].mip], next_hop, next_in_port) else: print '%-5s UNKNOWN HOP: %r' % (indent, curr_hop)
def vif_conifg(args): assert isinstance(args, dict) assert args['devicetype'] in VINTERFACE_DEVICETYPE.keys() devicetype = VINTERFACE_DEVICETYPE[args['devicetype']] try: vif = VInterface.get((VInterface.devicetype == devicetype) & (VInterface.deviceid == args['deviceid']) & (VInterface.ifindex == args['ifindex'])) except Exception: print >>sys.stderr, 'Error: vif not found' return -1 try: if args['devicetype'] == 'VM': vdevice = VM.get(VM.id == args['deviceid']) else: vdevice = VGateway.get(VGateway.id == args['deviceid']) except Exception: print >>sys.stderr, 'Error: VM/VGATEWAY %s not found' % \ args['deviceid'] return -1 launch_server = vdevice.launch_server if args['devicetype'] == 'VM' else \ vdevice.gw_launch_server try: host = HostDevice.get(HostDevice.ip == launch_server) except Exception: print >>sys.stderr, 'Error: Host device %s not found' % launch_server return -1 cmd = 'sshpass -p %s ssh -o ConnectTimeout=9 %s@%s ' % (host.user_passwd, host.user_name, launch_server) if 'vlantag' in args: if vif.state != VINTERFACE_STATE_ATTACH: print >>sys.stderr, 'Error: vif is detached' return -1 if vif.iftype != VINTERFACE_TYPE_WAN: print >>sys.stderr, 'Error: vif is not WAN' return -1 if args['devicetype'] == 'VM': cmd += 'sh /usr/local/livecloud/pyagexec/script/vport.sh UPDATE '\ 'vlantag %s %s' % (vif.mac, args['vlantag']) else: cmd += 'sh /usr/local/livegate/script/router.sh update '\ 'vlantag %s %s' % (vif.mac, args['vlantag']) rc, output = commands.getstatusoutput(cmd) if rc: print >>sys.stderr, 'Error: "%s" failed' % cmd print >>sys.stderr, 'Error: %s' % output vif_syslog( viftype=vif.iftype, devicetype=args['devicetype'], deviceid=vif.deviceid, ifindex=vif.ifindex, vifid=vif.id, loginfo='vlantag config failed', level=VINTERFACE_CONFIG_VLANTAG_LEVEL) return -1 ips = IP.select().where(IP.vifid == vif.id) for ip in ips: ip.vlantag = args['vlantag'] ip.save() old_vlantag = vif.vlantag vif.vlantag = args['vlantag'] vif.save() vif_syslog( viftype=vif.iftype, devicetype=args['devicetype'], deviceid=vif.deviceid, ifindex=vif.ifindex, vifid=vif.id, loginfo='vlantag config successful on launch_server %s,' ' from %d to %s' % (launch_server, old_vlantag, args['vlantag']), level=VINTERFACE_CONFIG_VLANTAG_LEVEL) print "SUCCESS" return if 'broadc_bandw' in args or 'broadc_ceil_bandw' in args: if vif.state != VINTERFACE_STATE_ATTACH: print >>sys.stderr, 'Error: vif is detached' return -1 if args['devicetype'] != 'VM' and vif.iftype != VINTERFACE_TYPE_WAN: print >>sys.stderr, 'Error: vif is not VGATEWAY WAN or VM DATA' return -1 if 'broadc_bandw' not in args: print >>sys.stderr, 'Error: broadc_bandw is also required '\ 'for broadc_ceil_bandw' return -1 if 'broadc_ceil_bandw' not in args: print >>sys.stderr, 'Error: broadc_ceil_bandw is also '\ 'required for broadc_bandw' return -1 if int(args['broadc_bandw']) > int(args['broadc_ceil_bandw']): print >>sys.stderr, 'Error: broadc_bandw cannot be larger '\ 'than broadc_ceil_bandw' return -1 if int(args['broadc_ceil_bandw']) > 0: if args['devicetype'] == 'VM': cmd += 'sh /usr/local/livecloud/pyagexec/script/vport.sh ADD-BROADCAST-QOS '\ '%s %s %s' % (vif.mac, args['broadc_bandw'], args['broadc_ceil_bandw']) else: cmd += 'sh /usr/local/livegate/script/router.sh add broadcast-qos '\ '%s %s %s %s' % (vif.deviceid, vif.ifindex, args['broadc_bandw'], args['broadc_ceil_bandw']) else: if args['devicetype'] == 'VM': cmd += 'sh /usr/local/livecloud/pyagexec/script/vport.sh DEL-BROADCAST-QOS '\ '%s' % vif.mac else: cmd += 'sh /usr/local/livegate/script/router.sh delete broadcast-qos '\ '%s %s' % (vif.deviceid, vif.ifindex) rc, output = commands.getstatusoutput(cmd) if rc: print >>sys.stderr, 'Error: "%s" failed' % cmd print >>sys.stderr, 'Error: %s' % output vif_syslog( viftype=vif.iftype, devicetype=args['devicetype'], deviceid=vif.deviceid, ifindex=vif.ifindex, vifid=vif.id, loginfo='broadcast bandwidth config failed', level=VINTERFACE_CONFIG_BROADC_BANDW_LEVEL) return -1 vif.broadc_bandw = args['broadc_bandw'] vif.broadc_ceil_bandw = args['broadc_ceil_bandw'] vif.save() vif_syslog( viftype=vif.iftype, devicetype=args['devicetype'], deviceid=vif.deviceid, ifindex=vif.ifindex, vifid=vif.id, loginfo='broadcast bandwidth config successful on ' 'launch_server %s' % launch_server, level=VINTERFACE_CONFIG_BROADC_BANDW_LEVEL) print "SUCCESS" return print "ERROR: No config parameter is given"