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

简介: 获取集群的信息不再过多的赘述,详细读过前面两篇文章的读者已经很快上路子了~ 疯狂罗列代码: private static TraversalSpec getComputeResourceTraversalSpec() { SelectionSpec ss = new SelectionSpec(); ss.

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

疯狂罗列代码:

	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<SelectionSpec> sSpecArr = new ArrayList<SelectionSpec>();
		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<PropertyFilterSpec> listfps = new ArrayList<PropertyFilterSpec>(1);
			listfps.add(propertyFilterSpec);
			List<ObjectContent> listobcont = retrievePropertiesAllObjects(listfps);

			if (listobcont != null)
			{
				for (ObjectContent oc : listobcont)
				{
					ManagedObjectReference mr = oc.getObj();
					String dcnm = null;
					List<DynamicProperty> 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<List<Long>> getClusterData(String clusterName, String nameInfo, String groupInfo) throws RuntimeFaultFaultMsg, DatatypeConfigurationException
	{
		List<List<Long>> list = new ArrayList<List<Long>>();
		ManagedObjectReference vmmor = getComputeResouceByCrName(clusterName);
		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);
			}

			XMLGregorianCalendar beginTime = DateConvert.convertToXMLGregorianCalendar(new Date(new Date().getTime() - 1000 * 60 * 5));
			XMLGregorianCalendar endTime = DateConvert.convertToXMLGregorianCalendar(new Date());
			List<PerfMetricId> listpermeid = vimPort.queryAvailablePerfMetric(perfManager, vmmor, null, null, null);

			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.getMetricId().addAll(mMetrics);
			qSpec.setEndTime(endTime);
			qSpec.setStartTime(beginTime);

			List<PerfQuerySpec> qSpecs = new ArrayList<PerfQuerySpec>();
			qSpecs.add(qSpec);

			List<PerfEntityMetricBase> listpemb = vimPort.queryPerf(perfManager, qSpecs);
			List<PerfEntityMetricBase> pValues = listpemb;

			for (i = 0; i < pValues.size(); i++)
			{
				List<PerfMetricSeries> 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<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;
	}
	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<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(clusterName))
								{
									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(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<Long>> list = getClusterData(clusterName, "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 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<String> getClusterNames()
	{
		List<String> list = new ArrayList<String>();
		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<PropertyFilterSpec> listfps = new ArrayList<PropertyFilterSpec>(1);
			listfps.add(propertyFilterSpec);
			List<ObjectContent> listobcont = retrievePropertiesAllObjects(listfps);

			if (listobcont != null)
			{
				for (ObjectContent oc : listobcont)
				{
					String dcnm = null;
					List<DynamicProperty> 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;
	}








目录
相关文章
|
29天前
|
存储 监控 固态存储
【vSAN分布式存储服务器数据恢复】VMware vSphere vSAN 分布式存储虚拟化平台VMDK文件1KB问题数据恢复案例
在一例vSAN分布式存储故障中,因替换故障闪存盘后磁盘组失效,一台采用RAID0策略且未使用置备的虚拟机VMDK文件受损,仅余1KB大小。经分析发现,该VMDK文件与内部虚拟对象关联失效导致。恢复方案包括定位虚拟对象及组件的具体物理位置,解析分配空间,并手动重组RAID0结构以恢复数据。此案例强调了深入理解vSAN分布式存储机制的重要性,以及定制化数据恢复方案的有效性。
53 5
|
29天前
|
存储 固态存储 虚拟化
【vSAN分布式存储服务器数据恢复】VMware vSphere vSAN ESXi超融合HCI分布式存储数据恢复案例
近期,我司处理了一个由10台华为OceanStor存储组成的vSAN超融合架构,其中一台存储闪存盘出现故障,用户取下后用新的闪存盘代替,然后对该闪存盘所在的磁盘组进行重建,导致集群中一台使用0置备策略的虚拟机数据丢失。
42 6
|
3月前
|
JSON 前端开发 JavaScript
技术笔记:vmware之VMwareRemoteConsole(VMRC)SDK(二)
技术笔记:vmware之VMwareRemoteConsole(VMRC)SDK(二)
90 0
|
4月前
|
缓存 应用服务中间件 数据库
Python Web Service开发及优化
随着互联网的快速发展,Web服务已成为现代技术的核心。Python作为一种功能强大且易于学习的编程语言,在Web服务开发领域占据着重要地位。Python Web服务开发的重要性在于它能够提供高效、可扩展且易于维护的解决方案。本篇博客将探讨如何使用Python的Flask框架、Gunicorn WSGI服务器和Nginx网页服务器来实现高性能的Web服务。
|
4月前
|
大数据 Linux 虚拟化
大数据软件基础(3) —— 在VMware上安装Linux集群
大数据软件基础(3) —— 在VMware上安装Linux集群
67 0
|
4月前
|
JSON 监控 数据库
使用Telegraf+Influxdb+Grafana配置VMware vSphere监控大屏
使用Telegraf+Influxdb+Grafana配置VMware vSphere监控大屏
156 0
|
4月前
|
XML Java 应用服务中间件
WebService - Axis2基于JAX-WS开发WebService并发布多个WebService
WebService - Axis2基于JAX-WS开发WebService并发布多个WebService
70 0
|
4月前
|
Java 应用服务中间件 Spring
WebService - Axis2使用services.xml进行开发server与client(未与Spring整合)
WebService - Axis2使用services.xml进行开发server与client(未与Spring整合)
116 0
|
4月前
|
Java 应用服务中间件 Spring
WebService - CXF开发Server和Client(main方法测试)
WebService - CXF开发Server和Client(main方法测试)
74 0
|
11月前
|
虚拟化
VMware vSphere Client中虚拟机“Disc Found”解决方法
VMware vSphere Client中虚拟机“Disc Found”解决方法