def tcpdump_vm(args): try: vm = VM.get(VM.id == args['id']) except Exception: print >> sys.stderr, 'Error: get vm from db failed' return try: host = HostDevice.get(HostDevice.ip == vm.launch_server) except Exception: print >> sys.stderr, \ 'Error: get launch_server %s from db failed' % vm.launch_server return try: vif = VInterface.get(VInterface.devicetype == VIF_DEVICE_TYPE_VM, 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 cmd = 'sshpass -p %s ssh %s %s@%s "ovs-vsctl --bare -- --columns=name ' \ 'find interface \'external_ids:attached-mac=\\\"%s\\\"\'"' % \ (host.user_passwd, SSH_OPTION_STRING, host.user_name, host.ip, vif.mac) rc, output = commands.getstatusoutput(cmd) if rc: print >> sys.stderr, \ 'Error: cmd [%s] failed, rc=%d out=%s' % \ (cmd.replace(host.user_passwd, '*'), rc, output) return if_name = output 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_vm(args): try: vm = VM.get(VM.id == args['id']) except Exception: print >> sys.stderr, 'Error: get vm from db failed' return try: host = HostDevice.get(HostDevice.ip == vm.launch_server) except Exception: print >> sys.stderr, \ 'Error: get launch_server %s from db failed' % vm.launch_server return try: vif = VInterface.get( VInterface.devicetype == VIF_DEVICE_TYPE_VM, 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 cmd = 'sshpass -p %s ssh %s %s@%s "ovs-vsctl --bare -- --columns=name ' \ 'find interface \'external_ids:attached-mac=\\\"%s\\\"\'"' % \ (host.user_passwd, SSH_OPTION_STRING, host.user_name, host.ip, vif.mac) rc, output = commands.getstatusoutput(cmd) if rc: print >> sys.stderr, \ 'Error: cmd [%s] failed, rc=%d out=%s' % \ (cmd.replace(host.user_passwd, '*'), rc, output) return if_name = output 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 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 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"