博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用VMware VSphere WebService SDK进行开发 (四)——获取集群(Cluster, ComputeResource)的相关信息...
阅读量:6960 次
发布时间:2019-06-27

本文共 9182 字,大约阅读时间需要 30 分钟。

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

疯狂罗列代码:

private static TraversalSpec getComputeResourceTraversalSpec()	{		SelectionSpec ss = new SelectionSpec();		ss.setName("VisitFolders");		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
sSpecArr = new ArrayList
(); sSpecArr.add(ss); sSpecArr.add(dataCenterToHostFolder); sSpecArr.add(hostFolderToComputeResource); traversalSpec.getSelectSet().addAll(sSpecArr); return traversalSpec; }
private static ManagedObjectReference getDatacenterByName(String datacenterName)	{		ManagedObjectReference retVal = null;		ManagedObjectReference rootFolder = serviceContent.getRootFolder();		try		{			TraversalSpec tSpec = getDatacenterTraversalSpec();			PropertySpec propertySpec = new PropertySpec();			propertySpec.setAll(Boolean.FALSE);			propertySpec.getPathSet().add("name");			propertySpec.setType("Datacenter");			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
listfps = new ArrayList
(1); listfps.add(propertyFilterSpec); List
listobcont = retrievePropertiesAllObjects(listfps); if (listobcont != null) { for (ObjectContent oc : listobcont) { ManagedObjectReference mr = oc.getObj(); String dcnm = null; List
dps = oc.getPropSet(); if (dps != null) { for (DynamicProperty dp : dps) { dcnm = (String) dp.getVal(); } } if (dcnm != null && dcnm.equals(datacenterName)) { retVal = mr; break; } } } } catch (SOAPFaultException sfe) { printSoapFaultException(sfe); } catch (Exception e) { e.printStackTrace(); } return retVal; }
private static List
> getClusterData(String clusterName, String nameInfo, String groupInfo) throws RuntimeFaultFaultMsg, DatatypeConfigurationException { List
> list = new ArrayList
>(); ManagedObjectReference vmmor = getComputeResouceByCrName(clusterName); if (vmmor != null) { List
cInfo = getPerfCounters(); List
vmCpuCounters = new ArrayList
(); for (int i = 0; i < cInfo.size(); ++i) { vmCpuCounters.add(cInfo.get(i)); } int i = 0; Map
counters = new HashMap
(); for (Iterator
it = vmCpuCounters.iterator(); it.hasNext();) { PerfCounterInfo pcInfo = (PerfCounterInfo) it.next(); counters.put(new Integer(pcInfo.getKey()), pcInfo); } XMLGregorianCalendar beginTime = DateConvert.convertToXMLGregorianCalendar(new Date(new Date().getTime() - 1000 * 60 * 5)); XMLGregorianCalendar endTime = DateConvert.convertToXMLGregorianCalendar(new Date()); List
listpermeid = vimPort.queryAvailablePerfMetric(perfManager, vmmor, null, null, null); ArrayList
mMetrics = new ArrayList
(); 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.getMetricId().addAll(mMetrics); qSpec.setEndTime(endTime); qSpec.setStartTime(beginTime); List
qSpecs = new ArrayList
(); qSpecs.add(qSpec); List
listpemb = vimPort.queryPerf(perfManager, qSpecs); List
pValues = listpemb; for (i = 0; i < pValues.size(); i++) { List
listpems = ((PerfEntityMetric) pValues.get(i)).getValue(); 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
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; }
private static String getClusterPropertyByClusterName(String property, String clusterName) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg	{		String ans = null;		RetrieveResult props = getRetrieveResultObjectWithProperty("ComputeResource", 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
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(clusterName)) { flag = true; break; } } } } } } else { for (ObjectContent oc : props.getObjects()) { if (flag == true) { break; } String path = null; List
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(clusterName)) { flag = true; } } if (path.equalsIgnoreCase(property)) { String val = String.valueOf(dp.getVal()); if (flag == true) { ans = val; break; } } } } } } } return ans; }
public static double getClusterCpuUsageByClusterName(String clusterName) throws RuntimeFaultFaultMsg, DatatypeConfigurationException	{		double ans = 0.0;		List
> list = getClusterData(clusterName, "usage", "cpu"); long maxInner = 0; int times = 0; for (List
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 long getClusterEffectiveMemoryByClusterName(String clusterName) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg	{		long ans = 0;		String ret = getClusterPropertyByClusterName("summary.effectiveMemory", clusterName);		ans = Long.valueOf(ret);		return ans;	}
同样以罗列集群名称的代码结束:

public static List
getClusterNames() { List
list = new ArrayList
(); ManagedObjectReference rootFolder = serviceContent.getRootFolder(); try { TraversalSpec tSpec = getComputeResourceTraversalSpec(); PropertySpec propertySpec = new PropertySpec(); propertySpec.setAll(Boolean.FALSE); propertySpec.getPathSet().add("name"); propertySpec.setType("ClusterComputeResource"); 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
listfps = new ArrayList
(1); listfps.add(propertyFilterSpec); List
listobcont = retrievePropertiesAllObjects(listfps); if (listobcont != null) { for (ObjectContent oc : listobcont) { String dcnm = null; List
dps = oc.getPropSet(); if (dps != null) { for (DynamicProperty dp : dps) { dcnm = (String) dp.getVal(); if (dcnm != null) { list.add(dcnm); } } } } } } catch (SOAPFaultException sfe) { printSoapFaultException(sfe); } catch (Exception e) { e.printStackTrace(); } return list; }

转载地址:http://jnzsl.baihongyu.com/

你可能感兴趣的文章
对接新通道的分析处理
查看>>
Linux01-bash脚本编程之七case语句及脚本选项进阶27
查看>>
Java记录 -11- 面向对象之封装续II
查看>>
Sybase Anywhere 8.0 DB数据库文件损坏的恢复
查看>>
hashMap理解
查看>>
ruby升级到1.9
查看>>
job.properties
查看>>
watchdog的加载方法
查看>>
我的友情链接
查看>>
C语言基础之类型系统
查看>>
jenkins+docker+nodejs项目的自动部署环境
查看>>
网游高层离职潮例行上演:多数选择创业
查看>>
赛门铁克 BE12.5备份exchange 2010 dag问题
查看>>
如何在Root的手机上开启ViewServer,使得HierachyViewer能够连接
查看>>
mysql 导出数据
查看>>
2014-10-10 LAMP第一部分-环境搭建
查看>>
iPhone 4S
查看>>
Attribute listkey invalid for tag checkboxlist according to TLD
查看>>
IOS 的UINavigatonBar控件的titleTextAttributes的字典类型的属性
查看>>
项目实现
查看>>