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

通过前面两篇文章的了解,详细应该很快掌握的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;
	}
时间: 2024-08-03 04:45:01

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

使用VMware VSphere WebService SDK进行开发 (一)——基本信息阐述

网上对于VSphere WebService SDK的介绍比较少(至少我能查到的资料比较少),官方提供的也是英文版的api,而且没有注明使用方法.最近接触到关于VSphere WebService SDK的开发,刚开始也是烦躁,比如要获取一个cpu使用情况的信息,按照惯例,API应该提供类似:long getCpuUsage() 之类的接口,但是绝逼没有那么easy,不过很快掌握了规律.我觉得有必要分享一下我所了解到的知识点,希望能够给各位读者有那么一点抛砖引玉的作用. 我准备通过几篇文章来主要

使用VMware VSphere WebService SDK进行开发 (七)——获取数据中心、集群、主机、虚拟机的目录结构

在实际应用中,需要显示出数据中心(Datacenter).集群(ClusterComputeResource).主机(HostSystem).虚拟机(VirtualMachine)之间的目录关系.这里忽略VAPP以及APP. 正所谓无图无真相,先展示一张vSphere Client上的截图,以便形象的描述本文所要呈现的内容. 左边的目录树形象的展示了数据中心(Datacenter).集群(ClusterComputeResource).主机(HostSystem).虚拟机(VirtualMach

使用VMware VSphere WebService SDK进行开发 (二)——获取虚拟机cpu的使用情况

本文通过代码举例虚拟机cpu的使用情况来演示如何遍历搜寻VirtualMachine的对象,以及根据这个对象进行性能指标的见识.希望可以取到举一反三的效果. 首先下面先罗列出如何更具虚拟机的名称获得VirtualMachine的ManagedObjectReference对象. private static TraversalSpec getVmTraversalSpec() { TraversalSpec vAppToVM = new TraversalSpec(); vAppToVM.set

使用VMware VSphere WebService SDK进行开发 (六)——检测告警信息

获取告警信息相对而言比较简单点,这里先陈述告警信息的pojo类,作为存储告警信息的源头(省略getter和setter方法): public class AlarmItem { //对象 private String ObjectName; //状态 private ManagedEntityStatus overallStatus; //名称 private String alarmName; //触发时间 private Date time; //确认时间 private Date ackn

使用VMware VSphere WebService SDK进行开发 (五)——根据虚拟机的名称获取对应主机的IP地址

在整个获取监视信息的过程中,最难获取的就是根据虚拟机的名称获得对应主机的IP地址的功能.(个人觉得比较绕,绕了好久我才找到) 首先根据虚拟机的名称获得对应主机(HostSystem)的ManagedObjectReference对象. RetrieveResult props = getRetrieveResultObjectWithProperty("VirtualMachine", "summary.runtime.host"); ManagedObjectRe

VMware 虚拟化编程(3) —VMware vSphere Web Service API 解析

目录 目录 前文列表 VMware vSphere Web Services API VMware vSphere Web Services SDK vSphere WS API 中的托管对象 Managed Object 托管对象引用 Managed Object References 托管对象属性收集器 PropertyCollector 连接 vCenter 并获取 MO 最后 前文列表 VMware 虚拟化编程(1) - VMDK/VDDK/VixDiskLib/VADP 概念简析 VM

《构建高可用VMware vSphere 5.X虚拟化架构》——1.2 ESX 4.1主机升级

1.2 ESX 4.1主机升级 不少企业目前还使用VMware vSphere 4.0或者4.1虚拟化架构,虽然VMwware官方也发布了针对4.X版本的补丁程序修复BUG,但新版本的一些特性是老版本无法使用的.因此,升级是必要的. 1.2.1 升级ESX 4.1主机原因 ESXi 5.0/5.1版本解决了4.1版本存在的BUG,运行的稳定性更好,对硬件的支持更好,以及提供了新的特性,ESXi 5.0/5.1几个重要的变化. (1)ESXi 5.0/5.1 VMFS由原来的VMFS3升级为VMF

VMware vSphere Client WIN10安装问题

VMware vSphere Client WIN10安装问题 VMware vSphere Client 这个软件在百度一搜一大把,下面是我的版本 VMware-viclient-all-6.0.0-2502222.exe.当然你也可以在ESXI安装完成后,输入主机的IP地址进行下载 VMware Vsphere Client 肯定是要管理ESXI主机,如果你想安装ESXI主机,可以看我之前的博客 http://blog.csdn.net/wanglei_storage/article/det

《构建高可用VMware vSphere 5.X虚拟化架构》——导读

前言 虚拟化技术近几年时间得到迅速发展,使用各种虚拟化架构打造企业应用平台越来越多. 作者从2006年开始使用VMware虚拟化架构打造企业应用平台,在多个项目实施过程中,传统高可用企业应用平台需要昂贵的硬件和软件才能实现,而使用虚拟化架构可以轻松实现. 目前市场上关于虚拟化的书籍一般以入门为主,介绍如何使用虚拟化平台,包括作者的<VMware vSphere 5.0虚拟化架构实战指南>一书.市场上没有关于如何使用虚拟化架构打造高可用企业应用平台的书籍,更没有实施过程中问题的处理的相关图书.作