Vijava 学习笔记之 HostSystem(内存总大小、内存已使用容量和CPU个数)

虾米哥 阅读:712 2021-03-31 22:37:59 评论: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.HostListSummary; 
import com.vmware.vim25.mo.*; 
 
/** 
 * Created by vixuan-008 on 2015/4/17. 
 */ 
public class HostSystemMemeoryConfig { 
    public static void main(String[] args){ 
        try{ 
            //获取 HostSystem 
            ServiceInstance serviceInstance=Session.getInstance("地址","用户名","密码"); 
 
            Folder rootFolder = serviceInstance.getRootFolder(); 
            ManagedEntity[] mes =new InventoryNavigator(rootFolder).searchManagedEntities("HostSystem"); 
            //逻辑判断 
            if(mes==null || mes.length==0){ 
                serviceInstance.getServerConnection().logout(); 
            }else{ 
                for(int i=0;i<mes.length;i++){ 
                    HostSystem hostSystem=(HostSystem)mes[i]; 
                    System.out.println("HostSystem name:"+hostSystem.getName()); 
                    double memorySize = (double) hostSystem.getHardware().memorySize / 1024 / 1024; //内存总容量 
                    Integer overallMemoryUsage =hostSystem.getSummary().quickStats.overallMemoryUsage; //内存使用容量(MB) 
                    HostListSummary summary = hostSystem.getSummary(); 
                    int numcpu=summary.getHardware().numCpuCores;cpu个数 
 
                    System.out.println("number cpu:"+numcpu); 
                    System.out.println("memery total:"+memorySize); 
                    System.out.println("memery used:"+overallMemoryUsage); 
 
                } 
            } 
 
        }catch (Exception e){ 
            e.printStackTrace(); 
        } 
 
    } 
 
}

效果截图:


声明

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

关注我们

一个IT知识分享的公众号