关键词搜索

源码搜索 ×
×

使用VMware VSphere WebService SDK进行开发 (三)——获取主机(HostSystem)的基本信息

发布2015-11-11浏览9488次

详情内容

欢迎支持笔者新作:《深入理解Kafka:核心设计与实践原理》和《RabbitMQ实战指南》,同时欢迎关注笔者的微信公众号:朱小厮的博客。

欢迎跳转到本文的原文链接:https://honeypps.com/backend/vmware-vsphere-webservice-sdk-host/

 

通过前面两篇文章的了解,详细应该很快掌握的code路数,这里首先罗列如何获取主机(接下去也会成为HostSystem)的对象。

 

  1. private static TraversalSpec getHostSystemTraversalSpec()
  2. {
  3. SelectionSpec ss = new SelectionSpec();
  4. ss.setName("VisitFolders");
  5. TraversalSpec computeResourceToHostSystem = new TraversalSpec();
  6. computeResourceToHostSystem.setName("computeResourceToHostSystem");
  7. computeResourceToHostSystem.setType("ComputeResource");
  8. computeResourceToHostSystem.setPath("host");
  9. computeResourceToHostSystem.setSkip(false);
  10. computeResourceToHostSystem.getSelectSet().add(ss);
  11. TraversalSpec hostFolderToComputeResource = new TraversalSpec();
  12. hostFolderToComputeResource.setName("hostFolderToComputeResource");
  13. hostFolderToComputeResource.setType("Folder");
  14. hostFolderToComputeResource.setPath("childEntity");
  15. hostFolderToComputeResource.setSkip(false);
  16. hostFolderToComputeResource.getSelectSet().add(ss);
  17. TraversalSpec dataCenterToHostFolder = new TraversalSpec();
  18. dataCenterToHostFolder.setName("DataCenterToHostFolder");
  19. dataCenterToHostFolder.setType("Datacenter");
  20. dataCenterToHostFolder.setPath("hostFolder");
  21. dataCenterToHostFolder.setSkip(false);
  22. dataCenterToHostFolder.getSelectSet().add(ss);
  23. TraversalSpec traversalSpec = new TraversalSpec();
  24. traversalSpec.setName("VisitFolders");
  25. traversalSpec.setType("Folder");
  26. traversalSpec.setPath("childEntity");
  27. traversalSpec.setSkip(false);
  28. List<SelectionSpec> sSpecArr = new ArrayList<SelectionSpec>();
  29. sSpecArr.add(ss);
  30. sSpecArr.add(dataCenterToHostFolder);
  31. sSpecArr.add(hostFolderToComputeResource);
  32. sSpecArr.add(computeResourceToHostSystem);
  33. traversalSpec.getSelectSet().addAll(sSpecArr);
  34. return traversalSpec;
  35. }
  1. private static ManagedObjectReference getHostByHostName(String hostName)
  2. {
  3. ManagedObjectReference retVal = null;
  4. ManagedObjectReference rootFolder = serviceContent.getRootFolder();
  5. try
  6. {
  7. TraversalSpec tSpec = getHostSystemTraversalSpec();
  8. PropertySpec propertySpec = new PropertySpec();
  9. propertySpec.setAll(Boolean.FALSE);
  10. propertySpec.getPathSet().add("name");
  11. propertySpec.setType("HostSystem");
  12. ObjectSpec objectSpec = new ObjectSpec();
  13. objectSpec.setObj(rootFolder);
  14. objectSpec.setSkip(Boolean.TRUE);
  15. objectSpec.getSelectSet().add(tSpec);
  16. PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
  17. propertyFilterSpec.getPropSet().add(propertySpec);
  18. propertyFilterSpec.getObjectSet().add(objectSpec);
  19. List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(1);
  20. listpfs.add(propertyFilterSpec);
  21. List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs);
  22. if (listobjcont != null)
  23. {
  24. for (ObjectContent oc : listobjcont)
  25. {
  26. ManagedObjectReference mr = oc.getObj();
  27. String hostnm = null;
  28. List<DynamicProperty> listDynamicProps = oc.getPropSet();
  29. DynamicProperty[] dps = listDynamicProps.toArray(new DynamicProperty[listDynamicProps.size()]);
  30. if (dps != null)
  31. {
  32. for (DynamicProperty dp : dps)
  33. {
  34. hostnm = (String) dp.getVal();
  35. }
  36. }
  37. if (hostnm != null && hostnm.equals(hostName))
  38. {
  39. retVal = mr;
  40. break;
  41. }
  42. }
  43. }
  44. else
  45. {
  46. System.out.println("The Object Content is Null");
  47. }
  48. }
  49. catch (SOAPFaultException sfe)
  50. {
  51. printSoapFaultException(sfe);
  52. }
  53. catch (Exception e)
  54. {
  55. e.printStackTrace();
  56. }
  57. return retVal;
  58. }

接下去展示获取HostSystem的性能信息的方法,和VirtualMachine的类似。

 

 

  1. private static List<List<Long>> getHostData(String hostName, String nameInfo, String groupInfo) throws RuntimeFaultFaultMsg
  2. {
  3. List<List<Long>> list = new ArrayList<List<Long>>();
  4. ManagedObjectReference vmmor = getHostByHostName(hostName);
  5. if (vmmor != null)
  6. {
  7. List<PerfCounterInfo> cInfo = getPerfCounters();
  8. List<PerfCounterInfo> vmCpuCounters = new ArrayList<PerfCounterInfo>();
  9. for (int i = 0; i < cInfo.size(); ++i)
  10. {
  11. vmCpuCounters.add(cInfo.get(i));
  12. }
  13. int i = 0;
  14. Map<Integer, PerfCounterInfo> counters = new HashMap<Integer, PerfCounterInfo>();
  15. for (Iterator<PerfCounterInfo> it = vmCpuCounters.iterator(); it.hasNext();)
  16. {
  17. PerfCounterInfo pcInfo = (PerfCounterInfo) it.next();
  18. counters.put(new Integer(pcInfo.getKey()), pcInfo);
  19. }
  20. List<PerfMetricId> listpermeid = vimPort.queryAvailablePerfMetric(perfManager, vmmor, null, null, new Integer(20));
  21. ArrayList<PerfMetricId> mMetrics = new ArrayList<PerfMetricId>();
  22. if (listpermeid != null)
  23. {
  24. for (int index = 0; index < listpermeid.size(); ++index)
  25. {
  26. if (counters.containsKey(new Integer(listpermeid.get(index).getCounterId())))
  27. {
  28. mMetrics.add(listpermeid.get(index));
  29. }
  30. }
  31. }
  32. PerfQuerySpec qSpec = new PerfQuerySpec();
  33. qSpec.setEntity(vmmor);
  34. qSpec.setMaxSample(new Integer(10));
  35. qSpec.getMetricId().addAll(mMetrics);
  36. qSpec.setIntervalId(new Integer(20));
  37. List<PerfQuerySpec> qSpecs = new ArrayList<PerfQuerySpec>();
  38. qSpecs.add(qSpec);
  39. List<PerfEntityMetricBase> listpemb = vimPort.queryPerf(perfManager, qSpecs);
  40. List<PerfEntityMetricBase> pValues = listpemb;
  41. // System.out.println("pValues.size() = "+pValues.size());
  42. for (i = 0; i < pValues.size(); i++)
  43. {
  44. List<PerfMetricSeries> listpems = ((PerfEntityMetric) pValues.get(i)).getValue();
  45. // System.out.println("listpems.size() = "+listpems.size());
  46. for (int vi = 0; vi < listpems.size(); ++vi)
  47. {
  48. String printInf = "";
  49. PerfCounterInfo pci = (PerfCounterInfo) counters.get(new Integer(listpems.get(vi).getId().getCounterId()));
  50. if (pci != null)
  51. {
  52. if (pci.getNameInfo().getKey().equalsIgnoreCase(nameInfo) && pci.getGroupInfo().getKey().equalsIgnoreCase(groupInfo))
  53. {
  54. printInf += vi + ":" + pci.getNameInfo().getSummary() + ":" + pci.getNameInfo().getKey() + ":" + pci.getNameInfo().getLabel() + ":"
  55. + pci.getGroupInfo().getKey() + ":" + pci.getGroupInfo().getLabel() + ":" + pci.getGroupInfo().getSummary() + " ";
  56. if (listpems.get(vi) instanceof PerfMetricIntSeries)
  57. {
  58. PerfMetricIntSeries val = (PerfMetricIntSeries) listpems.get(vi);
  59. List<Long> lislon = val.getValue();
  60. for (Long k : lislon)
  61. {
  62. printInf += k + " ";
  63. }
  64. list.add(lislon);
  65. }
  66. printInf += " " + pci.getUnitInfo().getKey() + " " + pci.getUnitInfo().getLabel() + " " + pci.getUnitInfo().getSummary();
  67. System.out.println(printInf);
  68. }
  69. }
  70. }
  71. }
  72. }
  73. return list;
  74. }

以获取主机cpu使用情况进行展示:

 

 

  1. public static double getHostCpuUsageByHostName(String hostName) throws RuntimeFaultFaultMsg
  2. {
  3. double ans = 0.0;
  4. List<List<Long>> list = getHostData(hostName, "usage", "cpu");
  5. long maxInner = 0;
  6. int times = 0;
  7. for (List<Long> listOuter : list)
  8. {
  9. long tempInner = 0;
  10. for (long inner : listOuter)
  11. {
  12. tempInner += inner;
  13. }
  14. if (tempInner > maxInner)
  15. {
  16. maxInner = tempInner;
  17. times = listOuter.size();
  18. }
  19. }
  20. if (times != 0)
  21. {
  22. ans = (double) maxInner / times;
  23. }
  24. ans = ans / 100;
  25. return ans;
  26. }

与VirtualMachine相同,Hostsystem也有些信息要通过另一种方式遍历。

 

譬如获取主机cpu的个数:

 

  1. <span style="white-space:pre"> </span>private static String getHostPropertyByHostName(String property, String hostName) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg
  2. {
  3. String ans = null;
  4. RetrieveResult props = getRetrieveResultObjectWithProperty("HostSystem", property);
  5. if (props != null)
  6. {
  7. Boolean flag = false;
  8. if (property.compareToIgnoreCase("name") < 0)
  9. {
  10. for (ObjectContent oc : props.getObjects())
  11. {
  12. if (flag == true)
  13. {
  14. break;
  15. }
  16. String path = null;
  17. List<DynamicProperty> dps = oc.getPropSet();
  18. if (dps != null)
  19. {
  20. for (DynamicProperty dp : dps)
  21. {
  22. path = dp.getName();
  23. if (path.equalsIgnoreCase(property))
  24. {
  25. String val = String.valueOf(dp.getVal());
  26. ans = val;
  27. }
  28. if (path.equalsIgnoreCase("name"))
  29. {
  30. String value = (String) dp.getVal();
  31. if (value.equals(hostName))
  32. {
  33. flag = true;
  34. break;
  35. }
  36. }
  37. }
  38. }
  39. }
  40. }
  41. else
  42. {
  43. for (ObjectContent oc : props.getObjects())
  44. {
  45. if (flag == true)
  46. {
  47. break;
  48. }
  49. String path = null;
  50. List<DynamicProperty> dps = oc.getPropSet();
  51. if (dps != null)
  52. {
  53. for (DynamicProperty dp : dps)
  54. {
  55. path = dp.getName();
  56. if (path.equalsIgnoreCase("name"))
  57. {
  58. String value = (String) dp.getVal();
  59. if (value.equals(hostName))
  60. {
  61. flag = true;
  62. }
  63. }
  64. if (path.equalsIgnoreCase(property))
  65. {
  66. String val = String.valueOf(dp.getVal());
  67. if (flag == true)
  68. {
  69. ans = val;
  70. break;
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }
  78. return ans;
  79. }
  1. public static double getHostCpuUsageByHostName(String hostName) throws RuntimeFaultFaultMsg
  2. {
  3. double ans = 0.0;
  4. List<List<Long>> list = getHostData(hostName, "usage", "cpu");
  5. long maxInner = 0;
  6. int times = 0;
  7. for (List<Long> listOuter : list)
  8. {
  9. long tempInner = 0;
  10. for (long inner : listOuter)
  11. {
  12. tempInner += inner;
  13. }
  14. if (tempInner > maxInner)
  15. {
  16. maxInner = tempInner;
  17. times = listOuter.size();
  18. }
  19. }
  20. if (times != 0)
  21. {
  22. ans = (double) maxInner / times;
  23. }
  24. ans = ans / 100;
  25. return ans;
  26. }

这里不做过多的说明,相信读者如果真的想要迫切了解这些内容,肯定会自己研究,本人罗列的代码都是亲身试用到项目中的,保证能够运行。希望对各位有帮助。

 

这里同样采用获取主机名称的方法来结束:

 

  1. public static List<String> getHostNames()
  2. {
  3. List<String> list = new ArrayList<String>();
  4. ManagedObjectReference rootFolder = serviceContent.getRootFolder();
  5. try
  6. {
  7. TraversalSpec tSpec = getHostSystemTraversalSpec();
  8. PropertySpec propertySpec = new PropertySpec();
  9. propertySpec.setAll(Boolean.FALSE);
  10. propertySpec.getPathSet().add("name");
  11. propertySpec.setType("HostSystem");
  12. ObjectSpec objectSpec = new ObjectSpec();
  13. objectSpec.setObj(rootFolder);
  14. objectSpec.setSkip(Boolean.TRUE);
  15. objectSpec.getSelectSet().add(tSpec);
  16. PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
  17. propertyFilterSpec.getPropSet().add(propertySpec);
  18. propertyFilterSpec.getObjectSet().add(objectSpec);
  19. List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(1);
  20. listpfs.add(propertyFilterSpec);
  21. List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs);
  22. if (listobjcont != null)
  23. {
  24. for (ObjectContent oc : listobjcont)
  25. {
  26. String hostnm = null;
  27. List<DynamicProperty> listDynamicProps = oc.getPropSet();
  28. DynamicProperty[] dps = listDynamicProps.toArray(new DynamicProperty[listDynamicProps.size()]);
  29. if (dps != null)
  30. {
  31. for (DynamicProperty dp : dps)
  32. {
  33. hostnm = (String) dp.getVal();
  34. if (hostnm != null)
  35. {
  36. list.add(hostnm);
  37. }
  38. }
  39. }
  40. }
  41. }
  42. else
  43. {
  44. System.out.println("getHostNames: Object Content is null ");
  45. }
  46. }
  47. catch (SOAPFaultException sfe)
  48. {
  49. printSoapFaultException(sfe);
  50. }
  51. catch (Exception e)
  52. {
  53. e.printStackTrace();
  54. }
  55. return list;
  56. }

 

欢迎支持笔者新作:《深入理解Kafka:核心设计与实践原理》和《RabbitMQ实战指南》,同时欢迎关注笔者的微信公众号:朱小厮的博客。

欢迎跳转到本文的原文链接:https://honeypps.com/backend/vmware-vsphere-webservice-sdk-host/

相关技术文章

最新源码

下载排行榜

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载