使用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实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图

MySQL 获取字符串长度及注意事项

《MySQL获取字符串长度及注意事项》本文通过实例代码给大家介绍MySQL获取字符串长度及注意事项,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 获取字符串长度详解 核心长度函数对比⚠️ 六大关键注意事项1. 字符编码决定字节长度2

Springboot如何正确使用AOP问题

《Springboot如何正确使用AOP问题》:本文主要介绍Springboot如何正确使用AOP问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录​一、AOP概念二、切点表达式​execution表达式案例三、AOP通知四、springboot中使用AOP导出