Vijava 学习笔记之(虚拟机可扩容CPU核数、内存大小和关联DataStore)分析

不点 阅读:276 2021-03-31 22:27:50 评论:0

源代码:

ResourceData.java

package com.vmware.pojo; 
 
/** 
 * Created by vixuan-008 on 2015/6/30. 
 */ 
public class ResourceData { 
    int cpunum=0; 
    //memory 
    long ramNum=0; 
 
    public int getCpunum() { 
        return cpunum; 
    } 
 
    public void setCpunum(int cpunum) { 
        this.cpunum = cpunum; 
    } 
 
    public long getRamNum() { 
        return ramNum; 
    } 
 
    public void setRamNum(long ramNum) { 
        this.ramNum = ramNum; 
    } 
} 


核心功能代码:

package com.vmware.tree; 
 
import com.vmware.pojo.ResourceData; 
import com.vmware.util.Session; 
import com.vmware.vim25.GuestDiskInfo; 
import com.vmware.vim25.VirtualMachineConfigSummary; 
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/30. 
 */ 
public class VirtualMachineDemo { 
    public static void main(String[] args)throws Exception{ 
 
        String vmName="vm77"; 
        ServiceInstance serviceInstance = null; 
        //ServiceInstance----服务实例 
        //serviceInstance = Session.getInstance("192.168.0.22", "administrator@vsphere.local", "Vixuan12#"); 
        serviceInstance = Session.getInstance("172.16.1.20", "root", "vmware"); 
        Map<String,Map<String,String>> content=getContent(serviceInstance,vmName); 
        for(Map.Entry<String, Map<String,String>> entrys:content.entrySet()){ 
            System.out.println("------------"+entrys.getKey()+"---------"); 
            Map<String,String> data=entrys.getValue(); 
            for(Map.Entry<String,String> entry:data.entrySet()){ 
                System.out.println(entry.getKey()+"--------->"+entry.getValue()); 
            } 
        } 
 
    } 
 
    public static  Map<String,Map<String,String>> getContent(ServiceInstance serviceInstance, String vmName)throws Exception { 
        Map<String, Map<String, String>> content = new HashMap<String, Map<String, String>>(); 
 
        ResourceData data=getMessage(serviceInstance,vmName); 
        //rootFolder-------根文件夹 
        Folder rootFolder = serviceInstance.getRootFolder(); 
        //inventoryNavigator----文件夹目录 
        InventoryNavigator inventoryNavigator =new InventoryNavigator(rootFolder); 
 
        VirtualMachine vm = (VirtualMachine)inventoryNavigator.searchManagedEntity("VirtualMachine", vmName); 
 
        VirtualMachineConfigSummary summary=vm.getSummary().getConfig(); 
        int fenpeiCpu=vm.getSummary().getConfig().numCpu;//虚拟机CPU分配 
        long fenpeiMem=vm.getSummary().getConfig().memorySizeMB;//虚拟机memory 分配 
        //cpu和RAM 
        Map<String,String> cpu_map=new HashMap<String, String>(); 
        cpu_map.put("fenpeiCpu",""+fenpeiCpu); 
        cpu_map.put("fenpeiMem",""+fenpeiMem); 
 
        if(data!=null){ 
            cpu_map.put("cpuMax",""+data.getCpunum()); 
            cpu_map.put("memMax",""+data.getRamNum()); 
        } 
        content.put("res",cpu_map); 
 
 
 
        Map<String,String> diskdsMap=new HashMap<String,String>(); 
        Map<String,String> sysdsMap=new HashMap<String,String>(); 
        long vmtotal=0;//虚拟机磁盘总大小 
        StringBuilder builder=new StringBuilder();//虚拟机分盘; 
        //虚拟机磁盘相关信息 
        GuestDiskInfo[] diskInfos=vm.getGuest().disk; 
        if(diskInfos!=null && diskInfos.length>0){ 
            for(int i=0;i<diskInfos.length;i++){ 
                GuestDiskInfo diskInfo=diskInfos[i]; 
                if(i==1){ 
                    vmtotal=vmtotal+diskInfo.capacity/(1024*1024*1024); 
                    builder.append(""+diskInfo.capacity/(1024*1024*1024)+"/"); 
                }else{ 
                    vmtotal=vmtotal+diskInfo.capacity/(1024*1024*1024); 
                    builder.append(""+diskInfo.capacity/(1024*1024*1024)+"/"); 
 
                } 
            } 
        } 
        //虚拟机关联datastore 名称 
        StringBuilder datastorebuilder=new StringBuilder(); 
        Datastore[] datastores=vm.getDatastores(); 
        long datatotal=0; 
        long datafree=0; 
 
        if(datastores!=null && datastores.length>0){ 
            for(int i=0;i<datastores.length;i++){ 
                Datastore datastore=datastores[i]; 
                datatotal=datatotal+datastore.getSummary().getCapacity()/(1024*1024*1024); 
                datafree=datafree+datastore.getSummary().freeSpace/(1024*1024*1024); 
                datastorebuilder.append(""+datastore.getName()+"/"); 
            } 
 
        } 
        //系统盘和数据盘 
        sysdsMap.put("sysdsRl",""+datatotal); 
        sysdsMap.put("givesysRl",""+vmtotal); 
        sysdsMap.put("sysysRl",""+datafree); 
        content.put("sysds",sysdsMap); 
 
 
        //磁盘数据 
        diskdsMap.put("diskFenPei",builder.toString()); 
        diskdsMap.put("dsName",datastorebuilder.toString()); 
        content.put("diskds", diskdsMap); 
 
 
        return content; 
    } 
 
    public static ResourceData getMessage(ServiceInstance serviceInstance,String vmName)throws Exception{ 
        ResourceData data=new ResourceData(); 
        //cpu核数 
        int cpunum=0; 
        //memory 
        long ramNum=0; 
 
        List<String> hostList=new ArrayList<String>();//服务器名称 
        List<String> AllhostList=new ArrayList<String>();//服务器名称 
 
        //rootFolder-------根文件夹 
        Folder rootFolder = serviceInstance.getRootFolder(); 
        //inventoryNavigator----文件夹目录 
        InventoryNavigator inventoryNavigator =new InventoryNavigator(rootFolder); 
        ManagedEntity[] managedEntities=inventoryNavigator.searchManagedEntities("ClusterComputeResource"); 
        ManagedEntity[] managedEntities1=inventoryNavigator.searchManagedEntities("HostSystem"); 
        //datacenter 关联exsi 
        if(managedEntities1 !=null && managedEntities1.length>0){ 
            for (int i=0;i<managedEntities1.length;i++){ 
                HostSystem hostSystem=(HostSystem)managedEntities1[i]; 
                AllhostList.add(hostSystem.getName()); 
            } 
        } 
        //集群判断 
        if(managedEntities!=null && managedEntities.length>0){ 
            for(int i=0;i<managedEntities.length;i++){ 
                ClusterComputeResource cluster = (ClusterComputeResource)managedEntities[i]; 
                HostSystem[] hostSystems=cluster.getHosts(); 
                //遍历服务器 
                if(hostSystems!=null && hostSystems.length>0){ 
                    for(int j=0;j<hostSystems.length;j++){ 
                        HostSystem hostSystem=hostSystems[j]; 
                        hostList.add(hostSystem.getName()); 
                        List<String> vmList=new ArrayList<String>();//虚拟机名称 
                        //服务器关联虚拟机 
                        VirtualMachine[] virtualMachines=hostSystem.getVms(); 
                        if(virtualMachines!=null && virtualMachines.length>0){ 
                            for(int k=0;k<virtualMachines.length;k++){ 
                                VirtualMachine virtualMachine=virtualMachines[k]; 
                                vmList.add(virtualMachine.getName()); 
                            } 
                        } 
                        //判断虚拟机是否存在于集群中 
                        if(vmList!=null&& vmList.size()>0){ 
                            for(int a=0;a<vmList.size();a++){ 
                                String content=vmList.get(a); 
                                if(content.equalsIgnoreCase(vmName)){ 
 
                                    HostSystem[] s=cluster.getHosts(); 
                                    for(int b=0;b<s.length;b++){ 
                                        HostSystem hostCpu=s[b]; 
                                        cpunum=cpunum+hostCpu.getSummary().getHardware().numCpuCores;//cpu个数 
                                        if(hostCpu.getSummary().quickStats.overallMemoryUsage!=null) { 
                                            long memorytotal = hostCpu.getHardware().memorySize / (1024 * 1024);//memory总容量--单位MB 
                                            long memoryuse = hostCpu.getSummary().quickStats.overallMemoryUsage;//memory 已使用容量----单位MB 
                                            ramNum = ramNum + (memorytotal - memoryuse); 
                                        } 
                                    } 
                                   data.setCpunum(cpunum); 
                                    data.setRamNum(ramNum); 
 
                                } 
                            } 
                        } 
 
 
 
                    } 
 
                } 
 
            } 
 
        } 
 
        //非集群判断 
        List<String> a=getdifference(AllhostList,hostList); 
        if(a!=null && a.size()>0){ 
            for (int i=0;i<a.size();i++){ 
                String name=a.get(i); 
                HostSystem hostSystem=(HostSystem)inventoryNavigator.searchManagedEntity("HostSystem", name); 
                List<String> vmList=new ArrayList<String>();//虚拟机名称 
                //服务器关联虚拟机 
                VirtualMachine[] virtualMachines=hostSystem.getVms(); 
                if(virtualMachines!=null && virtualMachines.length>0){ 
                    for(int k=0;k<virtualMachines.length;k++){ 
                        VirtualMachine virtualMachine=virtualMachines[k]; 
                        vmList.add(virtualMachine.getName()); 
                    } 
                } 
                //判断虚拟机是否存在非集群中 
                if(vmList!=null&& vmList.size()>0){ 
                    for(int j=0;j<vmList.size();j++){ 
                        String content=vmList.get(j); 
                        if(content.equalsIgnoreCase(vmName)){ 
 
                            cpunum=cpunum+hostSystem.getSummary().getHardware().numCpuCores;//cpu个数 
                            if(hostSystem.getSummary().quickStats.overallMemoryUsage!=null) { 
                                long memorytotal = hostSystem.getHardware().memorySize / (1024 * 1024);//memory总容量--单位MB 
                                long memoryuse = hostSystem.getSummary().quickStats.overallMemoryUsage;//memory 已使用容量----单位MB 
                                ramNum = ramNum + (memorytotal - memoryuse); 
                            } 
                            data.setCpunum(cpunum); 
                            data.setRamNum(ramNum); 
                        } 
                    } 
                } 
            } 
        } 
        return data; 
    } 
 
    public static List<String> getdifference(List<String> a,List<String> b){ 
        List<String> content=new ArrayList<String>(); 
        if(a !=null &&  b!=null){ 
            if(a.size()>0 && b.size()>0){ 
                for(int i=0;i<a.size();i++){ 
                    boolean target=false;//默认在b集合中不存在 
                    String diffA=a.get(i); 
                    //判断是否字符串在指定的集合当中 
                    for(int j=0;j<b.size();j++){ 
                        String diffB=b.get(j); 
                        if(diffA.equalsIgnoreCase(diffB)){ 
                            target=true; 
                        } 
                    } 
                    //返回相关数据集合 
                    if(!target){ 
                        content.add(diffA); 
                    } 
 
 
                } 
            } 
        } 
 
        return content; 
    } 
} 




声明

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

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号