Vsphere虚拟机监控整理

来源:互联网 发布:游戏伽马值调整软件 编辑:程序博客网 时间:2024/05/17 11:07

Vsphere虚拟机监控整理

前言

这次整理是在官方给的样例的基础上进行整理,详细的代码请参考官方开源的代码vsphere-automation-sdk-java及官方文档

本次整理主要是使用webservice去获取vcenter的相关内容,尽量使用官方已有的代码。

在官方的代码中还有个是使用vapi进行获取的,这个由于我测试使用的是6.0版本不支持这种用法,所以使用的是webservice即官方代码中的vim而不是vapi。

备份知识

建议先看下官方文档了解一下相关sdk的用法,其次呢我这里主要对经常使用的一些类解释做下翻译

使用思路

在vsphere使用的webservice中,所有的操作例如部署,简历快照和配置信息的查询都是依靠ManagedObjectReference这类来进行的。

ManagedObjectReference不仅可以表示具体的操作方法,还可以表示返回的信息类型.

对于ManagedObjectReference表示的操作方法均可以在sdk中的vim25.jar下的ServiceContent中找到,而对于方法的使用可以在官方文档中进行查询找到即可.

对于使用webservice(vim)的所有要传递的对象和要返回的对象均在vim25.jar下

查询使用参数的简单解释

  • 对于配置信息的查询一般使用PropertyCollector
  • 使用PropertyCollector需要从serviceContent中获取PropertyCollector的ManagedObjectReference这类来进行的,它有两个域,一个是type,表示是方法还是对象类型,一个是vale,如果是方法就是方法名,类型就是类型的id。
  • PropertyCollector使用propertyfilterspec过滤结果,下面是相关参数
    • propertyfilterspec 提供对对象和属性选择数据的访问。一个propertyfilterspec必须至少有一个objectspec一propertyspec
    • objectspec 标识属性集合的开始对象。一个objectspec还确定了额外的对象集合。
    • traversalspec 标识属性集合的对象类型。它还提供了一个或多个遍历库存路径。
    • SelectionSpec 作为一个占位符参照traversalspec
    • PropertySpec 标识集合的属性。
    • View Object 确定vSphere库存对象的一个子集
  • 在vsphere中所有的查询方式,都以ManagedObjectReference表示
  • 查询结果是ObjectContent其中类型用ManagedObjectReference表示,结果放在一个DynamicProperty的集合中,DynamicProperty是几何元素,DynamicProperty包含结果值和结果名称

配置信息的查询

对于vcenter下的所有配置包括虚拟机,集群,数据中心,主机这几个主要的内容查询基本上都是一致的,一次可以获取所有的配置信息,或者只获取单个类型的详细的信息

对于配置信息的相关查询可以参考官方文档的63页的示例代码

import com.vmware.vim25.*;import java.util.*;import javax.net.ssl.HostnameVerifier;import javax.net.ssl.HttpsURLConnection;import javax.net.ssl.SSLSession;import javax.xml.ws.BindingProvider;import javax.xml.ws.soap.SOAPFaultException;// PropertyCollector example// command line input: server name, user name, passwordpublic class PCollector {    private static void collectProperties(VimPortType methods,        ServiceContent sContent) throws Exception {    // Get references to the ViewManager and PropertyCollector    ManagedObjectReference viewMgrRef = sContent.getViewManager();    ManagedObjectReference propColl = sContent.getPropertyCollector();//获取PropertyCollector    // use a container view for virtual machines to define the traversal    // - invoke the VimPortType method createContainerView (corresponds    //   to the ViewManager method) - pass the ViewManager MOR and    //   the other parameters required for the method invocation    // - createContainerView takes a string[] for the type parameter;    //   declare an arraylist and add the type string to it    List<String> vmList = new ArrayList<String>();    vmList.add("VirtualMachine");ManagedObjectReference cViewRef = methods.createContainerView(viewMgrRef,        sContent.getRootFolder(),        vmList,        true);//获取所有虚拟机的名称    // create an object spec to define the beginning of the traversal;    // container view is the root object for this traversal    //配置信息的获取的筛选条件 开始    ObjectSpec oSpec = new ObjectSpec();    oSpec.setObj(cViewRef);    oSpec.setSkip(true);    // create a traversal spec to select all objects in the view    TraversalSpec tSpec = new TraversalSpec();    tSpec.setName("traverseEntities");    tSpec.setPath("view");    tSpec.setSkip(false);    tSpec.setType("ContainerView");    // add the traversal spec to the object spec;    // the accessor method (getSelectSet) returns a reference    // to the mapped XML representation of the list; using this    // reference to add the spec will update the list    oSpec.getSelectSet().add(tSpec);    // specify the property for retrieval (virtual machine name)    PropertySpec pSpec = new PropertySpec();    pSpec.setType("VirtualMachine");    pSpec.getPathSet().add("name");    // create a PropertyFilterSpec and add the object and    // property specs to it; use the getter method to reference    // the mapped XML representation of the lists and add the specs    // directly to the list    PropertyFilterSpec fSpec = new PropertyFilterSpec();    fSpec.getObjectSet().add(oSpec);    fSpec.getPropSet().add(pSpec);// Create a list for the filters and add the spec to it List<PropertyFilterSpec> fSpecList = new ArrayList<PropertyFilterSpec>(); fSpecList.add(fSpec);// get the data from the serverRetrieveOptions ro = new RetrieveOptions();      //配置信息的获取的筛选条件 结束RetrieveResult props = methods.retrievePropertiesEx(propColl,fSpecList,ro);    // go through the returned list and print out the data    if (props != null) {        for (ObjectContent oc : props.getObjects()) {        String vmName = null;        String path = null;        List<DynamicProperty> dps = oc.getPropSet();    if (dps != null) {        for (DynamicProperty dp : dps) {        vmName = (String) dp.getVal();        path = dp.getName();    System.out.println(path + " = " + vmName);} }} }}//end collectProperties()

这段实例代码的重点是配置信息的获取的筛选条件 这部分,这部分其实对于集群,主机虚拟机和数据中心而言都是代码模式都是一样的,它们不一样的pSpec.setType(“VirtualMachine”);pSpec.getPathSet().add(“name”);这部分,这里只是取得一个名字,其实还可以获取其他的内容示例如下

  • 获取虚拟机所有配置信息 返回VirtualMachineSummary type=”VirtualMachine” pathSet=”summary” 包含VirtualMachineConfigSummary 等等
  • 虚拟机设置VirtualMachineConfigSummary type=”VirtualMachine” pathSet=”summary.config” 其他类似
  • 数据中心DatastoreSummary type=”Datastore” pathSet=”summary”
  • 主机HostListSummary type=”HostSystem” pathSet=”summary”
  • 集群ClusterComputeResourceSummary type=”ClusterComputeResource” pathSet=”summary”

这些返回的对象都在DynamicProperty中的value中,value是个Object,官方示例返回的是string,如果按照我上方给的五个样例返回的就是这些对象,需要注意,这些代码在这个sdk中均有封装,所以无须自己再写,其中配置信息的获取的筛选条件这部分代码没有找到,需要自己写下

监控信息的查询

要想获取监控信息需要将PropertyCollector换成perfManager,相关内容请看文档197页,示例代码在201页开始到212页.

这里主要说下PerfCounterInfo这个类中的nameInfo说明这个指标的测量的内容(有中文描述,建议debug看下),groupInfo指的测量的对象例如cpu/disk,unitInfo是单位,rollupType是所得值的意义,例如平均值.有关PerfCounterInfo的使用请看201-203页的代码.

这段代码首先获取所有可以监控的指标,也就是PerfCounterInfo的Map集合,其中的key是可以看作数据库中的主键,然后根据你所要查看的对象ManagedObjectReference(可以表示虚拟机,集群,存储器或者主机等等)获取所有的实时指标值的实例集合也就是PerfMetricId集合,其中PerfMetricId中的counterId和PerfCounterInfo集合的key是对应的.

然后根据PerfMetricId集合查询当前的所有的指标的实时值集合PerfEntityMetricBase的子类集合,这里的PerfMetricId集合可以适当筛选出自己需要查询的指标集合.每个指标的实时值都是一个集合.然后根据PerfEntityMetricBase子类的中的集合字段PerfMetricSeries的id下的counterId和PerfCounterInfo集合的key进行对应就可以获取到对应指标的指标值.

此sdk的使用

此sdk对于其他的sdk做了一定的封装对于一些信息的获取可以先继承SamplesAbstractBase,然后按照其他的样例进行重写run方法即可.其次这里不仅仅兼容6.0还可以兼容6.5及以上的版本,对于6.0建议使用vimAuthHelper.getVimPort()下面提供的一些方法,还可以自己定义,对于6.5建议使用vapiAuthHelper.getStubFactory方法

原创粉丝点击