使用VMware VSphere WebService SDK进行开发 (三)——获取主机(HostSystem)的基本信息
生活随笔
收集整理的這篇文章主要介紹了
使用VMware VSphere WebService SDK进行开发 (三)——获取主机(HostSystem)的基本信息
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
歡迎支持筆者新作:《深入理解Kafka:核心設計與實踐原理》和《RabbitMQ實戰指南》,同時歡迎關注筆者的微信公眾號:朱小廝的博客。
歡迎跳轉到本文的原文鏈接:https://honeypps.com/backend/vmware-vsphere-webservice-sdk-host/
?
通過前面兩篇文章的了解,詳細應該很快掌握的code路數,這里首先羅列如何獲取主機(接下去也會成為HostSystem)的對象。
?
private static TraversalSpec getHostSystemTraversalSpec(){SelectionSpec ss = new SelectionSpec();ss.setName("VisitFolders");TraversalSpec computeResourceToHostSystem = new TraversalSpec();computeResourceToHostSystem.setName("computeResourceToHostSystem");computeResourceToHostSystem.setType("ComputeResource");computeResourceToHostSystem.setPath("host");computeResourceToHostSystem.setSkip(false);computeResourceToHostSystem.getSelectSet().add(ss);TraversalSpec hostFolderToComputeResource = new TraversalSpec();hostFolderToComputeResource.setName("hostFolderToComputeResource");hostFolderToComputeResource.setType("Folder");hostFolderToComputeResource.setPath("childEntity");hostFolderToComputeResource.setSkip(false);hostFolderToComputeResource.getSelectSet().add(ss);TraversalSpec dataCenterToHostFolder = new TraversalSpec();dataCenterToHostFolder.setName("DataCenterToHostFolder");dataCenterToHostFolder.setType("Datacenter");dataCenterToHostFolder.setPath("hostFolder");dataCenterToHostFolder.setSkip(false);dataCenterToHostFolder.getSelectSet().add(ss);TraversalSpec traversalSpec = new TraversalSpec();traversalSpec.setName("VisitFolders");traversalSpec.setType("Folder");traversalSpec.setPath("childEntity");traversalSpec.setSkip(false);List<SelectionSpec> sSpecArr = new ArrayList<SelectionSpec>();sSpecArr.add(ss);sSpecArr.add(dataCenterToHostFolder);sSpecArr.add(hostFolderToComputeResource);sSpecArr.add(computeResourceToHostSystem);traversalSpec.getSelectSet().addAll(sSpecArr);return traversalSpec;} private static ManagedObjectReference getHostByHostName(String hostName){ManagedObjectReference retVal = null;ManagedObjectReference rootFolder = serviceContent.getRootFolder();try{TraversalSpec tSpec = getHostSystemTraversalSpec();PropertySpec propertySpec = new PropertySpec();propertySpec.setAll(Boolean.FALSE);propertySpec.getPathSet().add("name");propertySpec.setType("HostSystem");ObjectSpec objectSpec = new ObjectSpec();objectSpec.setObj(rootFolder);objectSpec.setSkip(Boolean.TRUE);objectSpec.getSelectSet().add(tSpec);PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();propertyFilterSpec.getPropSet().add(propertySpec);propertyFilterSpec.getObjectSet().add(objectSpec);List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(1);listpfs.add(propertyFilterSpec);List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs);if (listobjcont != null){for (ObjectContent oc : listobjcont){ManagedObjectReference mr = oc.getObj();String hostnm = null;List<DynamicProperty> listDynamicProps = oc.getPropSet();DynamicProperty[] dps = listDynamicProps.toArray(new DynamicProperty[listDynamicProps.size()]);if (dps != null){for (DynamicProperty dp : dps){hostnm = (String) dp.getVal();}}if (hostnm != null && hostnm.equals(hostName)){retVal = mr;break;}}}else{System.out.println("The Object Content is Null");}}catch (SOAPFaultException sfe){printSoapFaultException(sfe);}catch (Exception e){e.printStackTrace();}return retVal;}接下去展示獲取HostSystem的性能信息的方法,和VirtualMachine的類似。
?
?
private static List<List<Long>> getHostData(String hostName, String nameInfo, String groupInfo) throws RuntimeFaultFaultMsg{List<List<Long>> list = new ArrayList<List<Long>>();ManagedObjectReference vmmor = getHostByHostName(hostName);if (vmmor != null){List<PerfCounterInfo> cInfo = getPerfCounters();List<PerfCounterInfo> vmCpuCounters = new ArrayList<PerfCounterInfo>();for (int i = 0; i < cInfo.size(); ++i){vmCpuCounters.add(cInfo.get(i));}int i = 0;Map<Integer, PerfCounterInfo> counters = new HashMap<Integer, PerfCounterInfo>();for (Iterator<PerfCounterInfo> it = vmCpuCounters.iterator(); it.hasNext();){PerfCounterInfo pcInfo = (PerfCounterInfo) it.next();counters.put(new Integer(pcInfo.getKey()), pcInfo);}List<PerfMetricId> listpermeid = vimPort.queryAvailablePerfMetric(perfManager, vmmor, null, null, new Integer(20));ArrayList<PerfMetricId> mMetrics = new ArrayList<PerfMetricId>();if (listpermeid != null){for (int index = 0; index < listpermeid.size(); ++index){if (counters.containsKey(new Integer(listpermeid.get(index).getCounterId()))){mMetrics.add(listpermeid.get(index));}}}PerfQuerySpec qSpec = new PerfQuerySpec();qSpec.setEntity(vmmor);qSpec.setMaxSample(new Integer(10));qSpec.getMetricId().addAll(mMetrics);qSpec.setIntervalId(new Integer(20));List<PerfQuerySpec> qSpecs = new ArrayList<PerfQuerySpec>();qSpecs.add(qSpec);List<PerfEntityMetricBase> listpemb = vimPort.queryPerf(perfManager, qSpecs);List<PerfEntityMetricBase> pValues = listpemb;// System.out.println("pValues.size() = "+pValues.size());for (i = 0; i < pValues.size(); i++){List<PerfMetricSeries> listpems = ((PerfEntityMetric) pValues.get(i)).getValue();// System.out.println("listpems.size() = "+listpems.size());for (int vi = 0; vi < listpems.size(); ++vi){String printInf = "";PerfCounterInfo pci = (PerfCounterInfo) counters.get(new Integer(listpems.get(vi).getId().getCounterId()));if (pci != null){if (pci.getNameInfo().getKey().equalsIgnoreCase(nameInfo) && pci.getGroupInfo().getKey().equalsIgnoreCase(groupInfo)){printInf += vi + ":" + pci.getNameInfo().getSummary() + ":" + pci.getNameInfo().getKey() + ":" + pci.getNameInfo().getLabel() + ":"+ pci.getGroupInfo().getKey() + ":" + pci.getGroupInfo().getLabel() + ":" + pci.getGroupInfo().getSummary() + " ";if (listpems.get(vi) instanceof PerfMetricIntSeries){PerfMetricIntSeries val = (PerfMetricIntSeries) listpems.get(vi);List<Long> lislon = val.getValue();for (Long k : lislon){printInf += k + " ";}list.add(lislon);}printInf += " " + pci.getUnitInfo().getKey() + " " + pci.getUnitInfo().getLabel() + " " + pci.getUnitInfo().getSummary();System.out.println(printInf);}}}}}return list;}以獲取主機cpu使用情況進行展示:
?
?
public static double getHostCpuUsageByHostName(String hostName) throws RuntimeFaultFaultMsg{double ans = 0.0;List<List<Long>> list = getHostData(hostName, "usage", "cpu");long maxInner = 0;int times = 0;for (List<Long> listOuter : list){long tempInner = 0;for (long inner : listOuter){tempInner += inner;}if (tempInner > maxInner){maxInner = tempInner;times = listOuter.size();}}if (times != 0){ans = (double) maxInner / times;}ans = ans / 100;return ans;}與VirtualMachine相同,Hostsystem也有些信息要通過另一種方式遍歷。
?
譬如獲取主機cpu的個數:
?
<span style="white-space:pre"> </span>private static String getHostPropertyByHostName(String property, String hostName) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg{String ans = null;RetrieveResult props = getRetrieveResultObjectWithProperty("HostSystem", property);if (props != null){Boolean flag = false;if (property.compareToIgnoreCase("name") < 0){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(property)){String val = String.valueOf(dp.getVal());ans = val;}if (path.equalsIgnoreCase("name")){String value = (String) dp.getVal();if (value.equals(hostName)){flag = true;break;}}}}}}else{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(hostName)){flag = true;}}if (path.equalsIgnoreCase(property)){String val = String.valueOf(dp.getVal());if (flag == true){ans = val;break;}}}}}}}return ans;} public static double getHostCpuUsageByHostName(String hostName) throws RuntimeFaultFaultMsg{double ans = 0.0;List<List<Long>> list = getHostData(hostName, "usage", "cpu");long maxInner = 0;int times = 0;for (List<Long> listOuter : list){long tempInner = 0;for (long inner : listOuter){tempInner += inner;}if (tempInner > maxInner){maxInner = tempInner;times = listOuter.size();}}if (times != 0){ans = (double) maxInner / times;}ans = ans / 100;return ans;}這里不做過多的說明,相信讀者如果真的想要迫切了解這些內容,肯定會自己研究,本人羅列的代碼都是親身試用到項目中的,保證能夠運行。希望對各位有幫助。
?
這里同樣采用獲取主機名稱的方法來結束:
?
public static List<String> getHostNames(){List<String> list = new ArrayList<String>();ManagedObjectReference rootFolder = serviceContent.getRootFolder();try{TraversalSpec tSpec = getHostSystemTraversalSpec();PropertySpec propertySpec = new PropertySpec();propertySpec.setAll(Boolean.FALSE);propertySpec.getPathSet().add("name");propertySpec.setType("HostSystem");ObjectSpec objectSpec = new ObjectSpec();objectSpec.setObj(rootFolder);objectSpec.setSkip(Boolean.TRUE);objectSpec.getSelectSet().add(tSpec);PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();propertyFilterSpec.getPropSet().add(propertySpec);propertyFilterSpec.getObjectSet().add(objectSpec);List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(1);listpfs.add(propertyFilterSpec);List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs);if (listobjcont != null){for (ObjectContent oc : listobjcont){String hostnm = null;List<DynamicProperty> listDynamicProps = oc.getPropSet();DynamicProperty[] dps = listDynamicProps.toArray(new DynamicProperty[listDynamicProps.size()]);if (dps != null){for (DynamicProperty dp : dps){hostnm = (String) dp.getVal();if (hostnm != null){list.add(hostnm);}}}}}else{System.out.println("getHostNames: Object Content is null ");}}catch (SOAPFaultException sfe){printSoapFaultException(sfe);}catch (Exception e){e.printStackTrace();}return list;}?
歡迎支持筆者新作:《深入理解Kafka:核心設計與實踐原理》和《RabbitMQ實戰指南》,同時歡迎關注筆者的微信公眾號:朱小廝的博客。歡迎跳轉到本文的原文鏈接:https://honeypps.com/backend/vmware-vsphere-webservice-sdk-host/
總結
以上是生活随笔為你收集整理的使用VMware VSphere WebService SDK进行开发 (三)——获取主机(HostSystem)的基本信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用VMware VSphere Web
- 下一篇: 使用VMware VSphere Web