开始是通过java代码调用vsphere提供的原始接口,从而控制vcenter的操作。当第一个版本做完之后发现代码执行的速度特别慢,后来在网上看到有人用vijava(对vsphere原始接口封装)编程,自己就试着换了几个接口发现代码执行速度很快。所以第二版都换了vijava操作。下面就和大家一起学习下如何通过vijava控制vcenter。
首先去github上下载vijava项目,然后将其导入自己的项目。
利用vijava完成vcenter连接类的创建:所有代码都可以之间运行。
package com.iking.vmware.connection; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import javax.xml.ws.soap.SOAPFaultException; import com.iking.exception.VcenterException; import com.iking.vmware.bean.VsphereConst; import com.iking.vmware.vim25.mo.InventoryNavigator; import com.iking.vmware.vim25.mo.ManagedEntity; import com.iking.vmware.vim25.mo.ServerConnection; import com.iking.vmware.vim25.mo.ServiceInstance; /** * @description 操作vcenter的连接和断开,以及定义公共应用类 * @date 2017年2月8日14:35:38 * @version 1.1 * @author DiWk */ public class ConnectedVimServiceBase { public ServiceInstance si = null; /** * @description 链接vcenter * @date 2017年2月8日14:23:37 * @version 1.1 * @author DiWk */ public void connect(String url, String userName, String passWord) { try { si = new ServiceInstance(new URL("https://" + url + "/sdk"), userName, passWord, true); } catch (SOAPFaultException sfe) { VcenterException.printSoapFaultException(sfe); } catch (Exception e) { e.printStackTrace(); } } /** * @description 断开vcenter链接 * @date 2017年2月8日14:23:37 * @version 1.1 * @author DiWk */ public void disconnect() { try { si.getServerConnection().logout(); } catch (SOAPFaultException sfe) { VcenterException.printSoapFaultException(sfe); } catch (Exception e) { e.printStackTrace(); } } /** * @description 获取链接URL * @date 2017年2月8日14:23:37 * @version 1.1 * @author DiWk */ public URL getUrl() { ServerConnection serverConnection = si.getServerConnection(); URL url = null; if (serverConnection != null) { url = serverConnection.getUrl(); } else { return null; } return url; }
利用vijava对获取集群对象及相关信息:
package com.iking.vmware.cluster; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import javax.xml.ws.soap.SOAPFaultException; import com.iking.exception.VcenterException; import com.iking.vmware.connection.ConnectedVimServiceBase; import com.iking.vmware.vim25.ClusterComputeResourceSummary; import com.iking.vmware.vim25.mo.ClusterComputeResource; import com.iking.vmware.vim25.mo.Datastore; import com.iking.vmware.vim25.mo.InventoryNavigator; import com.iking.vmware.vim25.mo.ManagedEntity; /** * @description 操作vcenter中的集群对象 * @date 2017年2月8日14:35:38 * @version 1.1 * @author DiWk */ public class ClusterComputerResourceSummary { private ConnectedVimServiceBase cs = null; //连接类声明 public ConnectedVimServiceBase getCs() { return cs; } public void setCs(ConnectedVimServiceBase cs) { this.cs = cs; } /** * @description 获取vcenter中所有的集群对象 * @date 2017年2月3日10:42:09 * @return clusterList 集群对象集合 * @version 1.1 * @author DiWk */ public List<ClusterComputeResource> getClusterList() { List<ClusterComputeResource> clusterList = new ArrayList<ClusterComputeResource>(); ClusterComputeResource clusterComputeResource = null; try { ManagedEntity[] managedEntities = new InventoryNavigator(cs.si.getRootFolder()) .searchManagedEntities("ClusterComputeResource"); if (managedEntities != null && managedEntities.length > 0) { for (ManagedEntity managedEntity : managedEntities) { clusterComputeResource = (ClusterComputeResource) managedEntity; clusterList.add(clusterComputeResource); } } else { return null; } } catch (SOAPFaultException sfe) { VcenterException.printSoapFaultException(sfe); } catch (Exception e) { e.printStackTrace(); } return clusterList; } /** * @description 根据集群名称获取对应的集群对象 * @date 2017年2月3日10:44:02 * @return clusterList 集群对象集合 * @version 1.1 * @author DiWk */ public List<ClusterComputeResource> getClusterListByName(List<String> ClustersName) { List<ClusterComputeResource> clusterList = new ArrayList<ClusterComputeResource>(); ClusterComputeResource clusterComputeResource = null; try { if (ClustersName == null || ClustersName.size() < 0) { return null; } List<ClusterComputeResource> clusterList2 = getClusterList(); if (clusterList2 == null || clusterList2.size() < 0) { return null; } for (String string : ClustersName) { for (ClusterComputeResource clusterComputeResource2 : clusterList2) { if (clusterComputeResource2.getName().equals(string)) { clusterList.add(clusterComputeResource); } } } } catch (SOAPFaultException sfe) { VcenterException.printSoapFaultException(sfe); } catch (Exception e) { e.printStackTrace(); } return clusterList; } /** * @description 根据集群名称获取对应的集群summary * @date 2017年2月3日10:54:18 * @return clusterSumList 集群对象summary集合 * @version 1.1 * @author DiWk */ public List<ClusterComputeResourceSummary> getClusterComputeResourceSummary(List<String> ClustersName) { List<ClusterComputeResourceSummary> clusterSumList = new ArrayList<ClusterComputeResourceSummary>(); List<ClusterComputeResource> clusterListByName = null; try { clusterListByName = getClusterListByName(ClustersName); if (clusterListByName != null && clusterListByName.size() > 0) { for (ClusterComputeResource cluster : clusterListByName) { ClusterComputeResourceSummary summary = (ClusterComputeResourceSummary) cluster.getSummary(); clusterSumList.add(summary); } } else { return null; } } catch (SOAPFaultException sfe) { VcenterException.printSoapFaultException(sfe); } catch (Exception e) { e.printStackTrace(); } return clusterSumList; } /** * @description 根据集群名称获取集群关联的数据存储 * @date 2017年2月3日11:02:09 * @return clusterDataStore 集群所关联的数据存储的集合 * @version 1.1 * @author DiWk */ public List<Datastore> getDataStoreByClusterNm(List<String> ClustersName) { List<Datastore> clusterDataStore = new ArrayList<Datastore>(); List<ClusterComputeResource> clusterListByName = null; try { clusterListByName = getClusterListByName(ClustersName); if (clusterListByName != null && clusterListByName.size() > 0) { for (ClusterComputeResource cluster : clusterListByName) { Datastore[] datastores = cluster.getDatastores(); clusterDataStore.addAll(Arrays.asList(datastores)); } } else { return null; } } catch (SOAPFaultException sfe) { VcenterException.printSoapFaultException(sfe); } catch (Exception e) { e.printStackTrace(); } return clusterDataStore; } //集群测试方法 public static void main(String[] args) { ConnectedVimServiceBase cs = new ConnectedVimServiceBase(); cs.connect("192.168.1.253", "administrator@vsphere.local","Iking!@#456"); ClusterComputerResourceSummary cluster = new ClusterComputerResourceSummary(); cluster.setCs(cs); List<ClusterComputeResource> clusterList = cluster.getClusterList(); if (clusterList != null && clusterList.size() > 0) { for (ClusterComputeResource clusterComputeResource : clusterList) { System.out.println(clusterComputeResource.getName()); } } } }
vijava将集群对象和其属性进行了封装,当获取到ClusterComputeResource、ClusterComputeResourceSummary或者其他集群的对象,我们就能够获取到集群对象关联的属性,从而完成对集群的操作和监控。这一篇是关于集群的几个简单方法,完成更具体的需求还需要多多尝试。