关键词搜索

源码搜索 ×
×

使用VMware VSphere WebService SDK进行开发 (四)——获取集群(Cluster, ComputeResource)的相关信息

发布2015-11-11浏览7532次

详情内容

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

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

 

获取集群的信息不再过多的赘述,详细读过前面两篇文章的读者已经很快上路子了~

疯狂罗列代码:

 

  1. private static TraversalSpec getComputeResourceTraversalSpec()
  2. {
  3. SelectionSpec ss = new SelectionSpec();
  4. ss.setName("VisitFolders");
  5. TraversalSpec hostFolderToComputeResource = new TraversalSpec();
  6. hostFolderToComputeResource.setName("hostFolderToComputeResource");
  7. hostFolderToComputeResource.setType("Folder");
  8. hostFolderToComputeResource.setPath("childEntity");
  9. hostFolderToComputeResource.setSkip(false);
  10. hostFolderToComputeResource.getSelectSet().add(ss);
  11. TraversalSpec dataCenterToHostFolder = new TraversalSpec();
  12. dataCenterToHostFolder.setName("DataCenterToHostFolder");
  13. dataCenterToHostFolder.setType("Datacenter");
  14. dataCenterToHostFolder.setPath("hostFolder");
  15. dataCenterToHostFolder.setSkip(false);
  16. dataCenterToHostFolder.getSelectSet().add(ss);
  17. TraversalSpec traversalSpec = new TraversalSpec();
  18. traversalSpec.setName("VisitFolders");
  19. traversalSpec.setType("Folder");
  20. traversalSpec.setPath("childEntity");
  21. traversalSpec.setSkip(false);
  22. List<SelectionSpec> sSpecArr = new ArrayList<SelectionSpec>();
  23. sSpecArr.add(ss);
  24. sSpecArr.add(dataCenterToHostFolder);
  25. sSpecArr.add(hostFolderToComputeResource);
  26. traversalSpec.getSelectSet().addAll(sSpecArr);
  27. return traversalSpec;
  28. }
  1. private static ManagedObjectReference getDatacenterByName(String datacenterName)
  2. {
  3. ManagedObjectReference retVal = null;
  4. ManagedObjectReference rootFolder = serviceContent.getRootFolder();
  5. try
  6. {
  7. TraversalSpec tSpec = getDatacenterTraversalSpec();
  8. PropertySpec propertySpec = new PropertySpec();
  9. propertySpec.setAll(Boolean.FALSE);
  10. propertySpec.getPathSet().add("name");
  11. propertySpec.setType("Datacenter");
  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> listfps = new ArrayList<PropertyFilterSpec>(1);
  20. listfps.add(propertyFilterSpec);
  21. List<ObjectContent> listobcont = retrievePropertiesAllObjects(listfps);
  22. if (listobcont != null)
  23. {
  24. for (ObjectContent oc : listobcont)
  25. {
  26. ManagedObjectReference mr = oc.getObj();
  27. String dcnm = null;
  28. List<DynamicProperty> dps = oc.getPropSet();
  29. if (dps != null)
  30. {
  31. for (DynamicProperty dp : dps)
  32. {
  33. dcnm = (String) dp.getVal();
  34. }
  35. }
  36. if (dcnm != null && dcnm.equals(datacenterName))
  37. {
  38. retVal = mr;
  39. break;
  40. }
  41. }
  42. }
  43. }
  44. catch (SOAPFaultException sfe)
  45. {
  46. printSoapFaultException(sfe);
  47. }
  48. catch (Exception e)
  49. {
  50. e.printStackTrace();
  51. }
  52. return retVal;
  53. }
  1. private static List<List<Long>> getClusterData(String clusterName, String nameInfo, String groupInfo) throws RuntimeFaultFaultMsg, DatatypeConfigurationException
  2. {
  3. List<List<Long>> list = new ArrayList<List<Long>>();
  4. ManagedObjectReference vmmor = getComputeResouceByCrName(clusterName);
  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. XMLGregorianCalendar beginTime = DateConvert.convertToXMLGregorianCalendar(new Date(new Date().getTime() - 1000 * 60 * 5));
  21. XMLGregorianCalendar endTime = DateConvert.convertToXMLGregorianCalendar(new Date());
  22. List<PerfMetricId> listpermeid = vimPort.queryAvailablePerfMetric(perfManager, vmmor, null, null, null);
  23. ArrayList<PerfMetricId> mMetrics = new ArrayList<PerfMetricId>();
  24. if (listpermeid != null)
  25. {
  26. for (int index = 0; index < listpermeid.size(); ++index)
  27. {
  28. if (counters.containsKey(new Integer(listpermeid.get(index).getCounterId())))
  29. {
  30. mMetrics.add(listpermeid.get(index));
  31. }
  32. }
  33. }
  34. PerfQuerySpec qSpec = new PerfQuerySpec();
  35. qSpec.setEntity(vmmor);
  36. qSpec.getMetricId().addAll(mMetrics);
  37. qSpec.setEndTime(endTime);
  38. qSpec.setStartTime(beginTime);
  39. List<PerfQuerySpec> qSpecs = new ArrayList<PerfQuerySpec>();
  40. qSpecs.add(qSpec);
  41. List<PerfEntityMetricBase> listpemb = vimPort.queryPerf(perfManager, qSpecs);
  42. List<PerfEntityMetricBase> pValues = listpemb;
  43. for (i = 0; i < pValues.size(); i++)
  44. {
  45. List<PerfMetricSeries> listpems = ((PerfEntityMetric) pValues.get(i)).getValue();
  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. }
  1. private static String getClusterPropertyByClusterName(String property, String clusterName) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg
  2. {
  3. String ans = null;
  4. RetrieveResult props = getRetrieveResultObjectWithProperty("ComputeResource", 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(clusterName))
  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(clusterName))
  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 getClusterCpuUsageByClusterName(String clusterName) throws RuntimeFaultFaultMsg, DatatypeConfigurationException
  2. {
  3. double ans = 0.0;
  4. List<List<Long>> list = getClusterData(clusterName, "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 long getClusterEffectiveMemoryByClusterName(String clusterName) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg
  2. {
  3. long ans = 0;
  4. String ret = getClusterPropertyByClusterName("summary.effectiveMemory", clusterName);
  5. ans = Long.valueOf(ret);
  6. return ans;
  7. }

同样以罗列集群名称的代码结束:

 

  1. public static List<String> getClusterNames()
  2. {
  3. List<String> list = new ArrayList<String>();
  4. ManagedObjectReference rootFolder = serviceContent.getRootFolder();
  5. try
  6. {
  7. TraversalSpec tSpec = getComputeResourceTraversalSpec();
  8. PropertySpec propertySpec = new PropertySpec();
  9. propertySpec.setAll(Boolean.FALSE);
  10. propertySpec.getPathSet().add("name");
  11. propertySpec.setType("ClusterComputeResource");
  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> listfps = new ArrayList<PropertyFilterSpec>(1);
  20. listfps.add(propertyFilterSpec);
  21. List<ObjectContent> listobcont = retrievePropertiesAllObjects(listfps);
  22. if (listobcont != null)
  23. {
  24. for (ObjectContent oc : listobcont)
  25. {
  26. String dcnm = null;
  27. List<DynamicProperty> dps = oc.getPropSet();
  28. if (dps != null)
  29. {
  30. for (DynamicProperty dp : dps)
  31. {
  32. dcnm = (String) dp.getVal();
  33. if (dcnm != null)
  34. {
  35. list.add(dcnm);
  36. }
  37. }
  38. }
  39. }
  40. }
  41. }
  42. catch (SOAPFaultException sfe)
  43. {
  44. printSoapFaultException(sfe);
  45. }
  46. catch (Exception e)
  47. {
  48. e.printStackTrace();
  49. }
  50. return list;
  51. }

 

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

 

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




 

相关技术文章

最新源码

下载排行榜

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

提示信息

×

选择支付方式

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