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

本文通过代码举例虚拟机cpu的使用情况来演示如何遍历搜寻VirtualMachine的对象,以及根据这个对象进行性能指标的见识。希望可以取到举一反三的效果。

首先下面先罗列出如何更具虚拟机的名称获得VirtualMachine的ManagedObjectReference对象。

	private static TraversalSpec getVmTraversalSpec()
	{
		TraversalSpec vAppToVM = new TraversalSpec();
		vAppToVM.setName("vAppToVM");
		vAppToVM.setType("VirtualApp");
		vAppToVM.setPath("vm");

		TraversalSpec vAppToVApp = new TraversalSpec();
		vAppToVApp.setName("vAppToVApp");
		vAppToVApp.setType("VirtualApp");
		vAppToVApp.setPath("resourcePool");

		SelectionSpec vAppRecursion = new SelectionSpec();
		vAppRecursion.setName("vAppToVApp");
		SelectionSpec vmInVApp = new SelectionSpec();
		vmInVApp.setName("vAppToVM");
		List<SelectionSpec> vAppToVMSS = new ArrayList<SelectionSpec>();
		vAppToVMSS.add(vAppRecursion);
		vAppToVMSS.add(vmInVApp);
		vAppToVApp.getSelectSet().addAll(vAppToVMSS);

		SelectionSpec sSpec = new SelectionSpec();
		sSpec.setName("VisitFolders");

		TraversalSpec dataCenterToVMFolder = new TraversalSpec();
		dataCenterToVMFolder.setName("DataCenterToVMFolder");
		dataCenterToVMFolder.setType("Datacenter");
		dataCenterToVMFolder.setPath("vmFolder");
		dataCenterToVMFolder.setSkip(false);
		dataCenterToVMFolder.getSelectSet().add(sSpec);

		TraversalSpec traversalSpec = new TraversalSpec();
		traversalSpec.setName("VisitFolders");
		traversalSpec.setType("Folder");
		traversalSpec.setPath("childEntity");
		traversalSpec.setSkip(false);
		List<SelectionSpec> sSpecArr = new ArrayList<SelectionSpec>();
		sSpecArr.add(sSpec);
		sSpecArr.add(dataCenterToVMFolder);
		sSpecArr.add(vAppToVM);
		sSpecArr.add(vAppToVApp);
		traversalSpec.getSelectSet().addAll(sSpecArr);
		return traversalSpec;
	}
	private static ManagedObjectReference getVmByVmName(String vmName)
	{
		ManagedObjectReference retVal = null;
		ManagedObjectReference rootFolder = serviceContent.getRootFolder();
		try
		{
			TraversalSpec tSpec = getVmTraversalSpec();
			PropertySpec propertySpec = new PropertySpec();
			propertySpec.setAll(Boolean.FALSE);
			propertySpec.getPathSet().add("name");
			propertySpec.setType("VirtualMachine");

			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 vmnm = null;
					List<DynamicProperty> dps = oc.getPropSet();
					if (dps != null)
					{
						for (DynamicProperty dp : dps)
						{
							vmnm = (String) dp.getVal();
						}
					}
					if (vmnm != null && vmnm.equals(vmName))
					{
						retVal = mr;
						break;
					}
				}
			}
		}
		catch (SOAPFaultException sfe)
		{
			printSoapFaultException(sfe);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return retVal;
	}

然后根据上面的VirtualMachine的ManagedObjectReference对象获取虚拟机的实时性能信息:

	private static List<List<Long>> getVmData(String vmName, String nameInfo, String groupInfo) throws RuntimeFaultFaultMsg, DatatypeConfigurationException
	{
		List<List<Long>> list = new ArrayList<List<Long>>();
		ManagedObjectReference vmmor = getVmByVmName(vmName);
		if (vmmor != null)
		{
			List<PerfCounterInfo> cInfo = getPerfCounters();

			int i = 0;
			Map<Integer, PerfCounterInfo> counters = new HashMap<Integer, PerfCounterInfo>();
			for (Iterator<PerfCounterInfo> it = cInfo.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;
			for (i = 0; i < pValues.size(); i++)
			{
				List<PerfMetricSeries> listpems = ((PerfEntityMetric) pValues.get(i)).getValue();
				List<PerfSampleInfo> listinfo = ((PerfEntityMetric) pValues.get(i)).getSampleInfo();
				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() + " ";

							for (PerfMetricId pmi : mMetrics)
							{
								int counterId = pmi.getCounterId();
								if (counterId == listpems.get(vi).getId().getCounterId())
								{
									printInf += "[" + pmi.getCounterId() + ":" + pmi.getInstance() + "]    ";
								}
							}

							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 getVmCpuUsageByVmName(String VmName) throws RuntimeFaultFaultMsg, DatatypeConfigurationException
	{
		double ans = 0.0;
		List<List<Long>> list = getVmData(VmName, "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;
	}

这里的虚拟机cpu使用情况是百分制的,而且vsphere sdk返回的百分比的结果都是要除以100的,譬如真实结果是5%,那么vsphere sdk返回的就是500,需要做一下处理。

接下来获取cpu的可用量:

	public static double getVmCpuEntitlementByVmName(String VmName) throws RuntimeFaultFaultMsg, DatatypeConfigurationException
	{
		double ans = 0;
		List<List<Long>> list = getVmData(VmName, "entitlement", "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;
		}

		return ans;
	}

细心的读者可能发现的差别,这里只需要修改nameinfo以及groupinfo即可以获取不同的结果。
有一些监控数据不是实时获取的,通过上面的方法是获取不到的,比如:获取虚拟机cpu的个数,类似这种比较固定的信息,或者说配置信息更,就要另辟蹊径了。

	public static int getVmCpuNumByVmName(String VmName) throws RuntimeFaultFaultMsg, InvalidPropertyFaultMsg
	{
		int ans = 0;
		String cpuNum = getVmPropertyByVmName("summary.config.numCpu", VmName);
		ans = Integer.valueOf(cpuNum);

		return ans;
	}
	private static String getVmPropertyByVmName(String property, String VmName) throws RuntimeFaultFaultMsg, InvalidPropertyFaultMsg
	{
		String ans = null;
		RetrieveResult props = getRetrieveResultObjectWithProperty("VirtualMachine", 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(VmName))
								{
									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(VmName))
								{
									flag = true;
								}
							}
							if (path.equalsIgnoreCase(property))
							{
								String val = String.valueOf(dp.getVal());
								if (flag == true)
								{
									ans = val;
									break;
								}
							}
						}
					}
				}
			}
		}

		return ans;
	}
	private static RetrieveResult getRetrieveResultObjectWithProperty(String MorName, String property) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg
	{
		ManagedObjectReference viewMgrRef = serviceContent.getViewManager();
		ManagedObjectReference propColl = serviceContent.getPropertyCollector();

		List<String> vmList = new ArrayList<String>();
		vmList.add(MorName);

		ManagedObjectReference cViewRef = vimPort.createContainerView(viewMgrRef, serviceContent.getRootFolder(), vmList, true);

		ObjectSpec oSpec = new ObjectSpec();
		oSpec.setObj(cViewRef);
		oSpec.setSkip(true);

		TraversalSpec tSpec = new TraversalSpec();
		tSpec.setName("traversalEntities");
		tSpec.setPath("view");
		tSpec.setSkip(false);
		tSpec.setType("ContainerView");

		oSpec.getSelectSet().add(tSpec);

		PropertySpec pSpec = new PropertySpec();
		pSpec.setType(MorName);
		pSpec.getPathSet().add("name");

		PropertySpec pSpecRPr = new PropertySpec();
		pSpecRPr.setType(MorName);
		pSpecRPr.getPathSet().add(property);

		PropertyFilterSpec fSpec = new PropertyFilterSpec();
		fSpec.getObjectSet().add(oSpec);
		fSpec.getPropSet().add(pSpec);
		fSpec.getPropSet().add(pSpecRPr);

		List<PropertyFilterSpec> fSpecList = new ArrayList<PropertyFilterSpec>();
		fSpecList.add(fSpec);

		RetrieveOptions ro = new RetrieveOptions();
		RetrieveResult props = vimPort.retrievePropertiesEx(propColl, fSpecList, ro);

		return props;
	}

这样就可以获取虚拟机的cpu个数的信息。

附加一个方法(获取vCenter下所有虚拟机的名称)

	public static List<String> getVmNames()
	{
		List<String> list = new ArrayList<String>();
		ManagedObjectReference rootFolder = serviceContent.getRootFolder();
		try
		{
			TraversalSpec tSpec = getVmTraversalSpec();
			PropertySpec propertySpec = new PropertySpec();
			propertySpec.setAll(Boolean.FALSE);
			propertySpec.getPathSet().add("name");
			propertySpec.setType("VirtualMachine");

			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 vmnm = null;
					List<DynamicProperty> dps = oc.getPropSet();
					if (dps != null)
					{
						for (DynamicProperty dp : dps)
						{
							vmnm = (String) dp.getVal();
							if (vmnm != null)
							{
								list.add(vmnm);
							}
						}
					}
				}
			}
		}
		catch (SOAPFaultException sfe)
		{
			printSoapFaultException(sfe);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return list;
	}

关于主机(HostSystem)信息的获取将在下一篇文章中呈现。敬请期待。

Modified: 居然忘了写一个重要的函数了,这里赶紧补上:

	private static List<PerfCounterInfo> getPerfCounters()
	{
		List<PerfCounterInfo> pciArr = null;

		try
		{
			PropertySpec propertySpec = new PropertySpec();
			propertySpec.setAll(Boolean.FALSE);
			propertySpec.getPathSet().add("perfCounter");
			propertySpec.setType("PerformanceManager");
			List<PropertySpec> propertySpecs = new ArrayList<PropertySpec>();
			propertySpecs.add(propertySpec);

			ObjectSpec objectSpec = new ObjectSpec();
			objectSpec.setObj(perfManager);
			List<ObjectSpec> objectSpecs = new ArrayList<ObjectSpec>();
			objectSpecs.add(objectSpec);

			PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
			propertyFilterSpec.getPropSet().add(propertySpec);
			propertyFilterSpec.getObjectSet().add(objectSpec);

			List<PropertyFilterSpec> propertyFilterSpecs = new ArrayList<PropertyFilterSpec>();
			propertyFilterSpecs.add(propertyFilterSpec);

			List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(10);
			listpfs.add(propertyFilterSpec);
			List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs);

			if (listobjcont != null)
			{
				for (ObjectContent oc : listobjcont)
				{
					List<DynamicProperty> dps = oc.getPropSet();
					if (dps != null)
					{
						for (DynamicProperty dp : dps)
						{
							List<PerfCounterInfo> pcinfolist = ((ArrayOfPerfCounterInfo) dp.getVal()).getPerfCounterInfo();
							pciArr = pcinfolist;
						}
					}
				}
			}
		}
		catch (SOAPFaultException sfe)
		{
			printSoapFaultException(sfe);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return pciArr;
	}
时间: 2024-10-24 17:19:47

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

使用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进行开发 (六)——检测告警信息

获取告警信息相对而言比较简单点,这里先陈述告警信息的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进行开发 (三)——获取主机(HostSystem)的基本信息

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

使用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

fence vmware use vmware vSphere SDK for Perl

我们可以通过fence_vmware命令连接到vCenter来fence vCenter管理的虚拟机. 本文将简单的描述一下fence_vmware的使用, 以及/etc/cluster/cluster.conf的配置. 命令行帮助如下 : [root@digoal_02 soft_bak]# fence_vmware --help Usage: fence_vmware [options] Options: -o, --action=<action> Action: status, rebo

vSphere二次开发,如何实时获取虚拟机的性能?例如cpu使用率和内存使用情况

问题描述 对于vSphereSDK的二次开发,使用的是javaweb的形式,想在我自己的代码里实时获取到虚拟机的资源使用情况,但是找了好多代码也没有找到太合适的,有高人指导下我吗,谢谢了 解决方案 解决方案二:我实现了...but好像回复的有点晚解决方案三:这是我找到一个http://blog.csdn.net/zhouzhiwengang/article/details/45150113解决方案四:引用1楼u013256816的回复: 我实现了...but好像回复的有点晚 如何实现的分享一下吧

《VMware vSphere设计(原书第2版)》——3.1 检查管理层的组件

3.1 检查管理层的组件 什么是管理层呢?当然不是指公司的执行官或董事会.我们所说的管理层是指用于日常管理整个虚拟化基础设施的组件.本节将简要介绍vSphere的管理组件.首先从最主要的vCenter 服务器开始. VMware vCenter服务器 vCenter服务器(原名VirtualCenter服务器)是虚拟化基础设施中最关键的元素之一,是用于管理虚拟数据中心的管理应用程序.你会创建数据中心.集群.资源池.网络和数据存储:分配权限:配置告警:监控性能等.所有这些功能都是在vCenter服