Vijava 学习笔记之 VirtualMachine(虚拟磁盘已用容量和空闲容量)解析

访客 阅读:231 2021-03-31 22:37:48 评论:0
package com.vmware.util; 
 
import com.vmware.vim25.mo.ServiceInstance; 
 
import java.net.URL; 
 
/** 
 * Created by vixuan-008 on 2015/4/17. 
 */ 
public class Session { 
    private static String __serverName = ""; 
    private static String __userName = ""; 
    private static String __passWord = ""; 
    private static String __url = ""; 
 
 
    private static ServiceInstance serviceInstance = null; 
 
 
    public synchronized static ServiceInstance getInstance(String serverName, String userName, String passWord) throws Exception{ 
 
        initServiceInstance(serverName, userName, passWord); 
        return serviceInstance; 
 
    } 
 
 
    public static void initServiceInstance(String ip, String userName, String passWord) throws Exception { 
        __serverName = ip; 
        __userName = userName; 
        __passWord = passWord; 
 
        __url = "https://" + __serverName + "/sdk"; 
         if(serviceInstance==null){ 
             serviceInstance = new ServiceInstance( 
                     new URL(__url), userName, passWord, true); 
         } 
 
 
 
    } 
 
}

package com.vmware.client; 
 
import com.vmware.util.Session; 
import com.vmware.vim25.VirtualMachineStorageSummary; 
import com.vmware.vim25.VirtualMachineSummary; 
import com.vmware.vim25.mo.*; 
 
/** 
 * Created by vixuan-008 on 2015/4/17. 
 */ 
public class VirtualMachineDiskPercent { 
    public static void main(String[] args){ 
        try{ 
            //获取 HostSystem 
            ServiceInstance serviceInstance= Session.getInstance("地址", "用户名", "密码"); 
 
            Folder rootFolder = serviceInstance.getRootFolder(); 
            ManagedEntity mes=new InventoryNavigator(rootFolder).searchManagedEntity("VirtualMachine", "vCenter"); 
            if(mes==null){ 
                serviceInstance.getServerConnection().logout(); 
                throw  new Exception(); 
            }else{ 
                //虚拟机 虚拟磁盘大小 
                VirtualMachine virtualMachine=(VirtualMachine)mes; 
                VirtualMachineSummary virtualMachineSummary=virtualMachine.getSummary(); 
                VirtualMachineStorageSummary virtualMachineStorageSummary=virtualMachineSummary.storage; 
                long total=virtualMachineStorageSummary.committed;//已用容量 
                System.out.println("virtual disk size:"+total); 
                long unshared=virtualMachineStorageSummary.unshared; 
                System.out.println("virtual disk unshared:"+unshared); 
               long uncommit= virtualMachineStorageSummary.uncommitted;//空闲容量 
                System.out.println("virtual disk uncommit:"+uncommit); 
 
 
 
            } 
            serviceInstance.getServerConnection().logout(); 
 
        }catch (Exception e){ 
            e.printStackTrace(); 
        } 
    } 
} 

相关截图:


声明

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

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

KIKK导航

关注我们