Vijava 学习笔记之(Cluster 关联资源信息:CPU、内存、Datastore容量信息)解析

符号 阅读:229 2021-03-31 22:27:45 评论:0
源代码:
package com.vmware.tree; 
 
import com.vmware.pojo.ClusterMsgObj; 
 
import com.vmware.util.Session; 
import com.vmware.vim25.mo.*; 
 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
 
/** 
 * Created by vixuan-008 on 2015/6/28. 
 */ 
public class ClusterData { 
    public static void main(String[] args)throws Exception{ 
        List<ClusterMsgObj> list=new ArrayList<ClusterMsgObj>(); 
        ServiceInstance serviceInstance = null; 
        //ServiceInstance----服务实例 
        //serviceInstance = Session.getInstance("192.168.0.22", "administrator@vsphere.local", "Vixuan12#"); 
        serviceInstance = Session.getInstance("172.16.1.20", "root", "vmware"); 
 
        list=getClusterData(serviceInstance); 
        if(list!=null && list.size()>0){ 
            for(int i=0;i<list.size();i++){ 
                ClusterMsgObj obj=list.get(i); 
                System.out.println("name is:"+obj.getName()); 
                System.out.println("cpu is:"+obj.getCpuNum()); 
                System.out.println("memory is:"+obj.getRamNum()); 
                Map<String,Long>  map=obj.getDsMap(); 
                for(Map.Entry<String, Long> entry:map.entrySet()){ 
                    System.out.println(entry.getKey()+"--->"+entry.getValue()); 
                } 
 
            } 
        } 
    } 
 
    public static List<ClusterMsgObj> getClusterData( ServiceInstance serviceInstance) throws Exception{ 
        List<ClusterMsgObj> list=new ArrayList<ClusterMsgObj>(); 
 
        //rootFolder-------根文件夹 
        Folder rootFolder = serviceInstance.getRootFolder(); 
        System.out.println("datacenter is:"+rootFolder.getName()); 
 
 
 
        //inventoryNavigator----文件夹目录 
        InventoryNavigator inventoryNavigator =new InventoryNavigator(rootFolder); 
        //managedEntities------查询实体对象 
        ManagedEntity[] managedEntities=inventoryNavigator.searchManagedEntities("ClusterComputeResource"); 
        if(managedEntities!=null && managedEntities.length>0){ 
            for(int i=0;i<managedEntities.length;i++){ 
                ClusterComputeResource cluster = (ClusterComputeResource)managedEntities[i]; 
                ClusterMsgObj clusterMsgObj=new ClusterMsgObj(); 
                //cluster.getName();//集群名称 
                clusterMsgObj.setName(cluster.getName()); 
 
 
                //集群关联服务器 
                int cpuNum=0; 
                long ramNum=0; 
                HostSystem[] hostSystems=cluster.getHosts(); 
                if(hostSystems!=null && hostSystems.length>0){ 
                    for(int j=0;j<hostSystems.length;j++){ 
                        HostSystem system=hostSystems[j]; 
                        cpuNum=cpuNum+system.getSummary().getHardware().numCpuCores;//cpu个数 
                        if(system.getSummary().quickStats.overallMemoryUsage!=null){ 
                            long memorytotal=system.getHardware().memorySize/(1024*1024);//memory总容量--单位MB 
                            long memoryuse=system.getSummary().quickStats.overallMemoryUsage;//memory 已使用容量----单位MB 
                            ramNum=ramNum+(memorytotal-memoryuse); 
                        } 
 
 
                    } 
                } 
                clusterMsgObj.setCpuNum(cpuNum); 
                clusterMsgObj.setRamNum(ramNum); 
 
 
 
                //集群关联DataStore 
                Map<String,Long> dsMap=new HashMap<String,Long>(); 
                Datastore[] datastores=cluster.getDatastores(); 
                if(datastores!=null && datastores.length>0){ 
                    for(int k=0;k<datastores.length;k++){ 
                        Datastore datastore=datastores[k]; 
 
                        String datastoreName=datastore.getName(); 
                        long freespace=datastore.getSummary().freeSpace/(1024*1024*1024);//单位:GB 
 
                        dsMap.put(datastoreName,freespace); 
 
 
                    } 
                } 
                clusterMsgObj.setDsMap(dsMap); 
 
 
                list.add(clusterMsgObj); 
 
 
 
 
            } 
 
        } 
 
 
        return list; 
    } 
} 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
KIKK导航

KIKK导航

关注我们