protected String getVRouterVMIpFabricAddress(String hostName, HostSystem host, String vmNamePrefix) throws Exception { // Find if vRouter Ip Fabric mapping exists.. String vRouterIpAddress = esxiToVRouterIpMap.get(hostName); if (host.getRuntime().isInMaintenanceMode()) { VRouterNotifier.setVrouterActive(vRouterIpAddress, false); } if (vRouterIpAddress != null) { return vRouterIpAddress; } else { s_logger.debug(" vRouter IP mapping for Host: " + hostName + "does not exist"); } VirtualMachine[] vms = host.getVms(); for (VirtualMachine vm : vms) { String vmName = vm.getName(); if (!vmName.toLowerCase().contains(vmNamePrefix.toLowerCase())) { continue; } // Assumption here is that VMware Tools are installed // and IP address is available GuestInfo guestInfo = vm.getGuest(); if (guestInfo == null) { s_logger.debug(" Host: " + hostName + " vm:" + vmName + " GuestInfo - VMware Tools " + " NOT installed"); continue; } GuestNicInfo[] nicInfos = guestInfo.getNet(); if (nicInfos == null) { s_logger.debug(" Host: " + hostName + " vm:" + vmName + " GuestNicInfo - VMware Tools " + " NOT installed"); continue; } for (GuestNicInfo nicInfo : nicInfos) { // Extract the IP address associated with simple port // group. Assumption here is that Contrail VRouter VM will // have only one standard port group String networkName = nicInfo.getNetwork(); if (networkName == null || !networkName.equals(contrailIpFabricPgName)) { continue; } Network network = (Network) inventoryNavigator.searchManagedEntity("Network", networkName); if (network == null) { s_logger.debug("Host: " + hostName + " vm: " + vmName + " network: " + networkName + " NOT found"); continue; } NetIpConfigInfo ipConfigInfo = nicInfo.getIpConfig(); if (ipConfigInfo == null) { continue; } NetIpConfigInfoIpAddress[] ipAddrConfigInfos = ipConfigInfo.getIpAddress(); if (ipAddrConfigInfos == null || ipAddrConfigInfos.length == 0) { continue; } for (NetIpConfigInfoIpAddress ipAddrConfigInfo : ipAddrConfigInfos) { String ipAddress = ipAddrConfigInfo.getIpAddress(); // Choose IPv4 only InetAddress ipAddr = InetAddress.getByName(ipAddress); if (ipAddr instanceof Inet4Address) { // found vRouter VM ip-fabric address. Store it. esxiToVRouterIpMap.put(hostName, ipAddress); return ipAddress; } } } } return null; }
public void readVirtualMachineInterfaces(VirtualMachineInfo vmInfo) throws IOException, Exception { VirtualMachine vm = vmInfo.vm; Network[] nets = vm.getNetworks(); for (Network net: nets) { VirtualNetworkInfo vnInfo = null; switch (mode) { case VCENTER_ONLY: String netName = net.getName(); vnInfo = MainDB.getVnByName(netName); if (vnInfo == null) { if (mode == Mode.VCENTER_ONLY) { s_logger.info("Skipping VMI in unmanaged network " + netName); continue; } } break; case VCENTER_AS_COMPUTE: // network is managed by Openstack or other entity // UUID is used in the name because name is not unique String uuid = net.getName(); /* From Mitaka nova driver will append cluster_id to port group therefore need to extract the appended cluster id */ uuid = uuid.substring(Math.max(0, uuid.length() - 36)); vnInfo = MainDB.getVnById(uuid); if (vnInfo == null) { s_logger.info("Skipping VMI in unmanaged network " + uuid); continue; } break; default: throw new Exception("Unhandled mode " + mode.name()); } VirtualMachineInterfaceInfo vmiInfo = new VirtualMachineInterfaceInfo(vmInfo, vnInfo); vmiInfo.setMacAddress(getVirtualMachineMacAddress(vm.getConfig(), vnInfo.getDpg())); if (mode != Mode.VCENTER_AS_COMPUTE && vnInfo.getExternalIpam() ){ if (vmInfo.getToolsRunningStatus().equals( VirtualMachineToolsRunningStatus.guestToolsRunning.toString())) { // static IP Address & vmWare tools installed // see if we can read it from Guest Nic Info vmiInfo.setIpAddress(getVirtualMachineIpAddress(vm, vnInfo.getName())); } VCenterNotify.watchVm(vmiInfo.vmInfo); } vmInfo.created(vmiInfo); } }
public Map<String, String> getVirtualMachine(String name) throws Exception { if(name == null || name.isEmpty()) { throw new Exception("invalid virtual machine name"); } Map<String,String> _machine = new HashMap<String, String>(); ServiceInstance si = new ServiceInstance(new URL(this._url), this._user, this._password, true); try { Folder rootFolder = si.getRootFolder(); VirtualMachine _vm = (VirtualMachine) new InventoryNavigator(rootFolder).searchManagedEntity("VirtualMachine", name); if(_vm == null) { throw new Exception("virtual machine not found"); } StringBuilder _sb = new StringBuilder(); _machine.put("name", _vm.getName()); for(Datastore _ds : _vm.getDatastores()) { if(_sb.length() > 0) { _sb.append(":"); } _sb.append(_ds.getName()); } _machine.put("datastore", _sb.toString()); _sb = new StringBuilder(); for(Network _nw : _vm.getNetworks()) { if(_sb.length() > 0) { _sb.append(":"); } _sb.append(_nw.getName()); } _machine.put("network", _sb.toString()); _sb = new StringBuilder(); VirtualMachineSnapshotTree[] _stree = _vm.getSnapshot().getRootSnapshotList(); if(_stree != null) { for(VirtualMachineSnapshotTree _st : _stree) { if(_sb.length() > 0) { _sb.append(":"); } _sb.append(_st.getName()); } _machine.put("snapshot", _sb.toString()); } else { _machine.put("snapshot", ""); } } catch(Exception _ex) { throw new Exception("hypervisor error - " + _ex.getMessage()); } finally { si.getServerConnection().logout(); } return _machine; }