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

简介: 通过前面两篇文章的了解,详细应该很快掌握的code路数,这里首先罗列如何获取主机(接下去也会成为HostSystem)的对象。 private static TraversalSpec getHostSystemTraversalSpec() { SelectionSpec ss = new SelectionSpec(); ss.

通过前面两篇文章的了解,详细应该很快掌握的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;
	}

目录
相关文章
|
2月前
|
消息中间件 物联网 网络安全
MQTT常见问题之调用.net sdk 报“不知道这样的主机”如何解决
MQTT(Message Queuing Telemetry Transport)是一个轻量级的、基于发布/订阅模式的消息协议,广泛用于物联网(IoT)中设备间的通信。以下是MQTT使用过程中可能遇到的一些常见问题及其答案的汇总:
|
8月前
|
算法 IDE 开发工具
火爆全网开源额温枪同平台之华大HC32L136 SDK开发入门
火爆全网开源额温枪同平台之华大HC32L136 SDK开发入门
151 1
|
5月前
|
开发工具 CDN 容器
基于Html+腾讯云播SDK开发的m3u8播放器
周末业余时间在家无事,学习了一下腾讯的云播放sdk,并制作了一个小demo(m3u8播放器),该在线工具是基于腾讯的云播sdk开发的,云播sdk非常牛,可以支持多种播放格式。
121 1
|
6月前
|
API 开发工具 C#
一套基于 .NET Core 开发的支付SDK集 - paylink
一套基于 .NET Core 开发的支付SDK集 - paylink
|
4月前
|
JSON 监控 数据库
使用Telegraf+Influxdb+Grafana配置VMware vSphere监控大屏
使用Telegraf+Influxdb+Grafana配置VMware vSphere监控大屏
80 0
|
5月前
|
API 开发工具 C#
[相机开发] VC++联合相机SDK开发
[相机开发] VC++联合相机SDK开发
46 0
|
5月前
|
人工智能 缓存 前端开发
下一代 AI 开发工具Vercel AI SDK 快速入门
下一代 AI 开发工具Vercel AI SDK 快速入门
102 0
|
6月前
|
开发框架 移动开发 前端开发
基于.Net Core开发的支付SDK,简化支付功能开发
基于.Net Core开发的支付SDK,简化支付功能开发
81 0
|
7月前
|
虚拟化
VMware vSphere Client中虚拟机“Disc Found”解决方法
VMware vSphere Client中虚拟机“Disc Found”解决方法
|
7月前
|
虚拟化 开发者 Windows
VM(VMware Workstation)对开发的帮助
VM(VMware Workstation)对开发的帮助