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

相关文章

C++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Redis Cluster模式配置

《RedisCluster模式配置》:本文主要介绍RedisCluster模式配置,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录分片 一、分片的本质与核心价值二、分片实现方案对比 ‌三、分片算法详解1. ‌范围分片(顺序分片)‌2. ‌哈希分片3. ‌虚

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF