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

相关文章

服务器集群同步时间手记

1.时间服务器配置(必须root用户) (1)检查ntp是否安装 [root@node1 桌面]# rpm -qa|grep ntpntp-4.2.6p5-10.el6.centos.x86_64fontpackages-filesystem-1.41-1.1.el6.noarchntpdate-4.2.6p5-10.el6.centos.x86_64 (2)修改ntp配置文件 [r

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

中文分词jieba库的使用与实景应用(一)

知识星球:https://articles.zsxq.com/id_fxvgc803qmr2.html 目录 一.定义: 精确模式(默认模式): 全模式: 搜索引擎模式: paddle 模式(基于深度学习的分词模式): 二 自定义词典 三.文本解析   调整词出现的频率 四. 关键词提取 A. 基于TF-IDF算法的关键词提取 B. 基于TextRank算法的关键词提取

Hadoop企业开发案例调优场景

需求 (1)需求:从1G数据中,统计每个单词出现次数。服务器3台,每台配置4G内存,4核CPU,4线程。 (2)需求分析: 1G / 128m = 8个MapTask;1个ReduceTask;1个mrAppMaster 平均每个节点运行10个 / 3台 ≈ 3个任务(4    3    3) HDFS参数调优 (1)修改:hadoop-env.sh export HDFS_NAMENOD

使用SecondaryNameNode恢复NameNode的数据

1)需求: NameNode进程挂了并且存储的数据也丢失了,如何恢复NameNode 此种方式恢复的数据可能存在小部分数据的丢失。 2)故障模拟 (1)kill -9 NameNode进程 [lytfly@hadoop102 current]$ kill -9 19886 (2)删除NameNode存储的数据(/opt/module/hadoop-3.1.4/data/tmp/dfs/na

HDFS—集群扩容及缩容

白名单:表示在白名单的主机IP地址可以,用来存储数据。 配置白名单步骤如下: 1)在NameNode节点的/opt/module/hadoop-3.1.4/etc/hadoop目录下分别创建whitelist 和blacklist文件 (1)创建白名单 [lytfly@hadoop102 hadoop]$ vim whitelist 在whitelist中添加如下主机名称,假如集群正常工作的节

Hadoop集群数据均衡之磁盘间数据均衡

生产环境,由于硬盘空间不足,往往需要增加一块硬盘。刚加载的硬盘没有数据时,可以执行磁盘数据均衡命令。(Hadoop3.x新特性) plan后面带的节点的名字必须是已经存在的,并且是需要均衡的节点。 如果节点不存在,会报如下错误: 如果节点只有一个硬盘的话,不会创建均衡计划: (1)生成均衡计划 hdfs diskbalancer -plan hadoop102 (2)执行均衡计划 hd

Hadoop数据压缩使用介绍

一、压缩原则 (1)运算密集型的Job,少用压缩 (2)IO密集型的Job,多用压缩 二、压缩算法比较 三、压缩位置选择 四、压缩参数配置 1)为了支持多种压缩/解压缩算法,Hadoop引入了编码/解码器 2)要在Hadoop中启用压缩,可以配置如下参数

Makefile简明使用教程

文章目录 规则makefile文件的基本语法:加在命令前的特殊符号:.PHONY伪目标: Makefilev1 直观写法v2 加上中间过程v3 伪目标v4 变量 make 选项-f-n-C Make 是一种流行的构建工具,常用于将源代码转换成可执行文件或者其他形式的输出文件(如库文件、文档等)。Make 可以自动化地执行编译、链接等一系列操作。 规则 makefile文件

使用opencv优化图片(画面变清晰)

文章目录 需求影响照片清晰度的因素 实现降噪测试代码 锐化空间锐化Unsharp Masking频率域锐化对比测试 对比度增强常用算法对比测试 需求 对图像进行优化,使其看起来更清晰,同时保持尺寸不变,通常涉及到图像处理技术如锐化、降噪、对比度增强等 影响照片清晰度的因素 影响照片清晰度的因素有很多,主要可以从以下几个方面来分析 1. 拍摄设备 相机传感器:相机传