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

本文主要是介绍使用VMware VSphere WebService SDK进行开发 (四)——获取集群(Cluster, ComputeResource)的相关信息,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

欢迎支持笔者新作:《深入理解Kafka:核心设计与实践原理》和《RabbitMQ实战指南》,同时欢迎关注笔者的微信公众号:朱小厮的博客。

欢迎跳转到本文的原文链接:https://honeypps.com/backend/vmware-vsphere-webservice-sdk-cluster/

 

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

疯狂罗列代码:

 

	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;}

 

欢迎跳转到本文的原文链接:https://honeypps.com/backend/vmware-vsphere-webservice-sdk-cluster/

 

欢迎支持笔者新作:《深入理解Kafka:核心设计与实践原理》和《RabbitMQ实战指南》,同时欢迎关注笔者的微信公众号:朱小厮的博客。




 

这篇关于使用VMware VSphere WebService SDK进行开发 (四)——获取集群(Cluster, ComputeResource)的相关信息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1129829

相关文章

使用Python实现矢量路径的压缩、解压与可视化

《使用Python实现矢量路径的压缩、解压与可视化》在图形设计和Web开发中,矢量路径数据的高效存储与传输至关重要,本文将通过一个Python示例,展示如何将复杂的矢量路径命令序列压缩为JSON格式,... 目录引言核心功能概述1. 路径命令解析2. 路径数据压缩3. 路径数据解压4. 可视化代码实现详解1

python获取网页表格的多种方法汇总

《python获取网页表格的多种方法汇总》我们在网页上看到很多的表格,如果要获取里面的数据或者转化成其他格式,就需要将表格获取下来并进行整理,在Python中,获取网页表格的方法有多种,下面就跟随小编... 目录1. 使用Pandas的read_html2. 使用BeautifulSoup和pandas3.

Pandas透视表(Pivot Table)的具体使用

《Pandas透视表(PivotTable)的具体使用》透视表用于在数据分析和处理过程中进行数据重塑和汇总,本文就来介绍一下Pandas透视表(PivotTable)的具体使用,感兴趣的可以了解一下... 目录前言什么是透视表?使用步骤1. 引入必要的库2. 读取数据3. 创建透视表4. 查看透视表总结前言

Python 交互式可视化的利器Bokeh的使用

《Python交互式可视化的利器Bokeh的使用》Bokeh是一个专注于Web端交互式数据可视化的Python库,本文主要介绍了Python交互式可视化的利器Bokeh的使用,具有一定的参考价值,感... 目录1. Bokeh 简介1.1 为什么选择 Bokeh1.2 安装与环境配置2. Bokeh 基础2

SpringBoot UserAgentUtils获取用户浏览器的用法

《SpringBootUserAgentUtils获取用户浏览器的用法》UserAgentUtils是于处理用户代理(User-Agent)字符串的工具类,一般用于解析和处理浏览器、操作系统以及设备... 目录介绍效果图依赖封装客户端工具封装IP工具实体类获取设备信息入库介绍UserAgentUtils

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Pandas使用AdaBoost进行分类的实现

《Pandas使用AdaBoost进行分类的实现》Pandas和AdaBoost分类算法,可以高效地进行数据预处理和分类任务,本文主要介绍了Pandas使用AdaBoost进行分类的实现,具有一定的参... 目录什么是 AdaBoost?使用 AdaBoost 的步骤安装必要的库步骤一:数据准备步骤二:模型

使用Pandas进行均值填充的实现

《使用Pandas进行均值填充的实现》缺失数据(NaN值)是一个常见的问题,我们可以通过多种方法来处理缺失数据,其中一种常用的方法是均值填充,本文主要介绍了使用Pandas进行均值填充的实现,感兴趣的... 目录什么是均值填充?为什么选择均值填充?均值填充的步骤实际代码示例总结在数据分析和处理过程中,缺失数

如何使用 Python 读取 Excel 数据

《如何使用Python读取Excel数据》:本文主要介绍使用Python读取Excel数据的详细教程,通过pandas和openpyxl,你可以轻松读取Excel文件,并进行各种数据处理操... 目录使用 python 读取 Excel 数据的详细教程1. 安装必要的依赖2. 读取 Excel 文件3. 读