欢迎支持笔者新作:《深入理解Kafka:核心设计与实践原理》和《RabbitMQ实战指南》,同时欢迎关注笔者的微信公众号:朱小厮的博客。
欢迎跳转到本文的原文链接:https://honeypps.com/backend/vmware-vsphere-webservice-sdk-host-name-and-ip/
在整个获取监视信息的过程中,最难获取的就是根据虚拟机的名称获得对应主机的IP地址的功能。(个人觉得比较绕,绕了好久我才找到)
首先根据虚拟机的名称获得对应主机(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;
- }
- }
- }
- }
- }
- }
然后根据这个(HostSystem)的ManagedObjectReference对象获取对应的主机的名称:
- 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;
- }
根据主机的名称分别获得对应的物理适配器的Map<"mac地址",“网卡名称”>和vSwitch的Map<"mac地址",“ip地址”>的信息,然后遍历获得具有相同mac地址的信息,那个mac地址对应的ip地址就是主机地址:
- 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;
- }
欢迎跳转到本文的原文链接:https://honeypps.com/backend/vmware-vsphere-webservice-sdk-host-name-and-ip/
欢迎支持笔者新作:《深入理解Kafka:核心设计与实践原理》和《RabbitMQ实战指南》,同时欢迎关注笔者的微信公众号:朱小厮的博客。