使用VMware VSphere WebService SDK進行開發 (五)——根據虛擬機器的名稱獲取對應主機的IP地址
阿新 • • 發佈:2019-01-03
在整個獲取監視資訊的過程中,最難獲取的就是根據虛擬機器的名稱獲得對應主機的IP地址的功能。(個人覺得比較繞,繞了好久我才找到)
首先根據虛擬機器的名稱獲得對應主機(HostSystem)的ManagedObjectReference物件。
然後根據這個(HostSystem)的ManagedObjectReference物件獲取對應的主機的名稱:RetrieveResult props = getRetrieveResultObjectWithProperty("VirtualMachine", "summary.runtime.host"); ManagedObjectReference mor = null; if (props != null) { Boolean flag = false; for (ObjectContent oc : props.getObjects()) { if (flag == true) { break; } String path = null; List<DynamicProperty> dps = oc.getPropSet(); if (dps != null) { for (DynamicProperty dp : dps) { path = dp.getName(); if (path.equalsIgnoreCase("name")) { String value = (String) dp.getVal(); if (value.equals(VmName)) { flag = true; } } if (path.equalsIgnoreCase("summary.runtime.host")) { mor = (ManagedObjectReference) dp.getVal(); if (flag == true) { break; } } } } } }
根據主機的名稱分別獲得對應的物理介面卡的Map<"mac地址",“網絡卡名稱”>和vSwitch的Map<"mac地址",“ip地址”>的資訊,然後遍歷獲得具有相同mac地址的資訊,那個mac地址對應的ip地址就是主機地址:private static String getObjectName(ManagedObjectReference mor) throws Exception { String objectName = null; PropertySpec propSpec = new PropertySpec(); propSpec.setAll(new Boolean(false)); propSpec.getPathSet().add("name"); propSpec.setType(mor.getType()); ObjectSpec objSpec = new ObjectSpec(); objSpec.setObj(mor); objSpec.setSkip(new Boolean(false)); PropertyFilterSpec spec = new PropertyFilterSpec(); spec.getPropSet().add(propSpec); spec.getObjectSet().add(objSpec); ArrayList<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(); listpfs.add(spec); List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs); if(listobjcont != null) { ObjectContent oc = (ObjectContent) listobjcont.get(0); objectName = (String) oc.getPropSet().get(0).getVal(); } return objectName; }
private static Map<String,String> getHostPhyIpByHostName(String hostName) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { Map<String,String> map = new HashMap<String,String>(); RetrieveResult propsHost = getRetrieveResultObjectWithProperty("HostSystem", "config.network.pnic"); List<PhysicalNic> listpnic = null; if(propsHost != null) { Boolean flagpnic = false; for (ObjectContent oc : propsHost.getObjects()) { if (flagpnic == true) { break; } String path = null; List<DynamicProperty> dps = oc.getPropSet(); if (dps != null) { for (DynamicProperty dp : dps) { path = dp.getName(); if (path.equalsIgnoreCase("config.network.pnic")) { listpnic = ((ArrayOfPhysicalNic)dp.getVal()).getPhysicalNic(); } if (path.equalsIgnoreCase("name")) { String value = (String) dp.getVal(); if (value.equals(hostName)) { flagpnic = true; break; } } } } } } if(listpnic != null) { for(PhysicalNic pnic : listpnic) { PhysicalNicSpec pns = pnic.getSpec(); String mac = pnic.getMac(); if(pns != null) { HostIpConfig hic = pns.getIp(); if(hic != null) { String ipAddress = hic.getIpAddress(); map.put(mac, ipAddress); } } } } return map; }
private static Map<String,String> getHostVirIpByHostName(String hostName) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg
{
Map<String,String> map = new HashMap<String,String>();
RetrieveResult propsHost = getRetrieveResultObjectWithProperty("HostSystem", "config.network.vnic");
List<HostVirtualNic> listpnic = null;
if(propsHost != null)
{
Boolean flagpnic = false;
for (ObjectContent oc : propsHost.getObjects())
{
if (flagpnic == true)
{
break;
}
String path = null;
List<DynamicProperty> dps = oc.getPropSet();
if (dps != null)
{
for (DynamicProperty dp : dps)
{
path = dp.getName();
if (path.equalsIgnoreCase("config.network.vnic"))
{
listpnic = ((ArrayOfHostVirtualNic)dp.getVal()).getHostVirtualNic();
}
if (path.equalsIgnoreCase("name"))
{
String value = (String) dp.getVal();
if (value.equals(hostName))
{
flagpnic = true;
break;
}
}
}
}
}
}
if(listpnic != null)
{
for(HostVirtualNic pnic : listpnic)
{
HostVirtualNicSpec pns = pnic.getSpec();
if(pns != null)
{
HostIpConfig hic = pns.getIp();
String mac = pns.getMac();
if(hic != null)
{
String ipAddress = hic.getIpAddress();
map.put(mac, ipAddress);
}
}
}
}
return map;
}
最後展示主要的方法(完整版):
public static String getVmHostIpByVmName(String VmName) throws Exception
{
List<String> ret = new ArrayList<String>();
RetrieveResult props = getRetrieveResultObjectWithProperty("VirtualMachine", "summary.runtime.host");
ManagedObjectReference mor = null;
if (props != null)
{
Boolean flag = false;
for (ObjectContent oc : props.getObjects())
{
if (flag == true)
{
break;
}
String path = null;
List<DynamicProperty> dps = oc.getPropSet();
if (dps != null)
{
for (DynamicProperty dp : dps)
{
path = dp.getName();
if (path.equalsIgnoreCase("name"))
{
String value = (String) dp.getVal();
if (value.equals(VmName))
{
flag = true;
}
}
if (path.equalsIgnoreCase("summary.runtime.host"))
{
mor = (ManagedObjectReference) dp.getVal();
if (flag == true)
{
break;
}
}
}
}
}
}
String hostName = null;
if (mor != null)
{
hostName = getObjectName(mor);
}
if(hostName != null)
{
Map<String,String> phyMap = getHostPhyIpByHostName(hostName);
Map<String,String> virMap = getHostVirIpByHostName(hostName);
for(Map.Entry<String, String> entry : phyMap.entrySet())
{
String phyMac = entry.getKey();
for(Map.Entry<String, String> entryvir : virMap.entrySet())
{
String virMac = entryvir.getKey();
if(phyMac.equalsIgnoreCase(virMac))
{
ret.add(entryvir.getValue());
}
}
}
}
String ipAddress = ret.get(0);
return ipAddress;
}