Vijava学习笔记之Cluster(基础配置信息)解析

不点 阅读:205 2021-03-31 22:37:13 评论:0

Vijava 代码:

实体类:

package com.vmware.pojo; 
 
/** 
 * 集群信息 
 * @author zhb 
 * 
 */ 
public class ClusterInfo extends Entity{ 
    private String name; //名称 
    //ha配置数据 
    private Boolean haEnabled;   //是否启用ha 
    private String haVmMonitoring;   //虚拟机监控状态 
    private String haHostMonitoring; //主机监控状态 
    private Integer haFailoverLevel; //主机故障数目 
 
    private boolean admisControlEnabled; //接入控制状态 1:表示允许接入控制 	0:表示不允许接入控制 
    private Integer cpuFailoverPercent; //cpu故障切换百分比 
    private Integer memFailoverPercent; //内存故障切换百分比 
    private String restartPriority; //虚拟机重新启动优先级 
    private String isolationResponse;//主机隔离响应 
    private String dsCandidatePolicy;//数据存储检测信号 
    //drs配置数据 
    private boolean drsEnabled; //是否启用drs 
    private boolean drsEnableVmBehaviorOverrides; //启动个别虚拟机自动化级别 
    private String autoLevel;//自动化级别 manual partiallyAutomated fullyAutomated 
    private Integer dcId; //数据中心ID 
 
 
 
 
    public Boolean getHaEnabled() { 
        return haEnabled; 
    } 
    public void setHaEnabled(Boolean haEnabled) { 
        this.haEnabled = haEnabled; 
    } 
    public String getHaVmMonitoring() { 
        return haVmMonitoring; 
    } 
    public void setHaVmMonitoring(String haVmMonitoring) { 
        this.haVmMonitoring = haVmMonitoring; 
    } 
    public String getHaHostMonitoring() { 
        return haHostMonitoring; 
    } 
    public void setHaHostMonitoring(String haHostMonitoring) { 
        this.haHostMonitoring = haHostMonitoring; 
    } 
    public Integer getHaFailoverLevel() { 
        return haFailoverLevel; 
    } 
    public void setHaFailoverLevel(Integer haFailoverLevel) { 
        this.haFailoverLevel = haFailoverLevel; 
    } 
    public String getName() { 
        return name; 
    } 
    public void setName(String name) { 
        this.name = name; 
    } 
    public boolean isDrsEnabled() { 
        return drsEnabled; 
    } 
    public void setDrsEnabled(boolean drsEnabled) { 
        this.drsEnabled = drsEnabled; 
    } 
    public boolean isDrsEnableVmBehaviorOverrides() { 
        return drsEnableVmBehaviorOverrides; 
    } 
    public void setDrsEnableVmBehaviorOverrides(boolean drsEnableVmBehaviorOverrides) { 
        this.drsEnableVmBehaviorOverrides = drsEnableVmBehaviorOverrides; 
    } 
    public boolean isAdmisControlEnabled() { 
        return admisControlEnabled; 
    } 
    public void setAdmisControlEnabled(boolean admisControlEnabled) { 
        this.admisControlEnabled = admisControlEnabled; 
    } 
    public String getAutoLevel() { 
        return autoLevel; 
    } 
    public void setAutoLevel(String autoLevel) { 
        this.autoLevel = autoLevel; 
    } 
    public Integer getCpuFailoverPercent() { 
        return cpuFailoverPercent; 
    } 
    public void setCpuFailoverPercent(Integer cpuFailoverPercent) { 
        this.cpuFailoverPercent = cpuFailoverPercent; 
    } 
    public String getDsCandidatePolicy() { 
        return dsCandidatePolicy; 
    } 
    public void setDsCandidatePolicy(String dsCandidatePolicy) { 
        this.dsCandidatePolicy = dsCandidatePolicy; 
    } 
    public String getIsolationResponse() { 
        return isolationResponse; 
    } 
    public void setIsolationResponse(String isolationResponse) { 
        this.isolationResponse = isolationResponse; 
    } 
    public Integer getMemFailoverPercent() { 
        return memFailoverPercent; 
    } 
    public void setMemFailoverPercent(Integer memFailoverPercent) { 
        this.memFailoverPercent = memFailoverPercent; 
    } 
    public String getRestartPriority() { 
        return restartPriority; 
    } 
    public void setRestartPriority(String restartPriority) { 
        this.restartPriority = restartPriority; 
    } 
    public Integer getDcId() { 
        return dcId; 
    } 
    public void setDcId(Integer dcId) { 
        this.dcId = dcId; 
    } 
 
} 
逻辑层:

package com.vmware.cluster; 
 
import com.vmware.pojo.ClusterInfo; 
import com.vmware.vim25.ClusterFailoverResourcesAdmissionControlPolicy; 
import com.vmware.vim25.mo.*; 
 
import java.util.ArrayList; 
 
/** 
 * Created by vixuan-008 on 2015/4/27. 
 */ 
public class ClusterOption { 
 
    /** 
     * 数据集群发现 
     * @param serviceInstance 
     * @return 
     */ 
 
    public ArrayList<ClusterInfo> clusterDiscovery(ServiceInstance serviceInstance,String dcName){ 
        ArrayList<ClusterInfo> result = new ArrayList<ClusterInfo>(); 
       // ServiceInstance serviceInstance = null; 
        try{ 
          //  serviceInstance = resourcePoolServiceInstance.getServiceInstance(); 
            Folder rootFolder = serviceInstance.getRootFolder(); 
            Datacenter dc = (Datacenter) new InventoryNavigator(rootFolder).searchManagedEntity("Datacenter", dcName); 
            ManagedEntity[] clusters =  new InventoryNavigator(dc).searchManagedEntities("ClusterComputeResource"); 
            for(int i=0;i<clusters.length;i++) 
            { 
                ClusterComputeResource cluster = (ClusterComputeResource)clusters[i]; 
                ClusterInfo clusterInfo = getClusterInfo(cluster); 
                //被充信息 
                result.add(clusterInfo); 
            } 
 
        }catch(Exception e){ 
 
        } 
        return result; 
    } 
 
 
    /** 
     * 得取集群信息 
     * @param cluster 
     * @return 
     */ 
    public ClusterInfo getClusterInfo(ClusterComputeResource cluster){ 
        ClusterInfo clusterInfo = new ClusterInfo(); 
        clusterInfo.setName(cluster.getName()); 
        clusterInfo.setHaEnabled(cluster.getConfiguration().getDasConfig().getEnabled()); 
        clusterInfo.setHaFailoverLevel(cluster.getConfiguration().getDasConfig().getFailoverLevel()); 
        clusterInfo.setHaHostMonitoring(cluster.getConfiguration().getDasConfig().getHostMonitoring()); 
        clusterInfo.setHaVmMonitoring(cluster.getConfiguration().getDasConfig().getVmMonitoring()); 
        clusterInfo.setAdmisControlEnabled(cluster.getConfiguration().getDasConfig().getAdmissionControlEnabled()); 
        if(cluster.getConfiguration().getDasConfig().getAdmissionControlPolicy() instanceof ClusterFailoverResourcesAdmissionControlPolicy) 
        { 
            ClusterFailoverResourcesAdmissionControlPolicy policy = (ClusterFailoverResourcesAdmissionControlPolicy)cluster.getConfiguration().getDasConfig().getAdmissionControlPolicy(); 
            clusterInfo.setCpuFailoverPercent(policy.getCpuFailoverResourcesPercent()); 
            clusterInfo.setMemFailoverPercent(policy.getMemoryFailoverResourcesPercent()); 
        } 
        clusterInfo.setRestartPriority(cluster.getConfiguration().getDasConfig().getDefaultVmSettings().getRestartPriority()); 
        clusterInfo.setIsolationResponse(cluster.getConfiguration().getDasConfig().getDefaultVmSettings().getIsolationResponse()); 
        clusterInfo.setDsCandidatePolicy(cluster.getConfiguration().getDasConfig().getHBDatastoreCandidatePolicy()); 
        clusterInfo.setDrsEnabled(cluster.getConfiguration().getDrsConfig().getEnabled()); 
        clusterInfo.setDrsEnableVmBehaviorOverrides(cluster.getConfiguration().getDrsConfig().getEnableVmBehaviorOverrides()); 
        clusterInfo.setAutoLevel(cluster.getConfiguration().getDrsConfig().getDefaultVmBehavior().name()); 
        clusterInfo.setProgId(cluster.getMOR().val); 
        return clusterInfo; 
    } 
 
} 

测试代码:

package com.vmware.main; 
 
 
import com.vmware.cluster.ClusterOption; 
import com.vmware.pojo.ClusterInfo; 
import com.vmware.util.Session; 
import com.vmware.vim25.mo.*; 
 
import java.util.ArrayList; 
 
 
/** 
 * Created by vixuan-008 on 2015/4/27. 
 */ 
public class ClusterOptinTest { 
    public static void main(String[] args){ 
        try{ 
            ServiceInstance serviceInstance= Session.getInstance("***", "**", "**"); 
            ClusterOption clusterOption=new ClusterOption(); 
            ArrayList<ClusterInfo> list=clusterOption.clusterDiscovery(serviceInstance,"Datacenter"); 
            if(list.size()>0){ 
                for(int i=0;i<list.size();i++){ 
                    ClusterInfo clusterInfo=list.get(i); 
                    System.out.println("name:"+clusterInfo.getName()); 
                    System.out.println("haEnabled:"+clusterInfo.getHaEnabled()); 
                    System.out.println("haVmMonitoring:"+clusterInfo.getHaVmMonitoring()); 
                    System.out.println("haHostMonitoring:"+clusterInfo.getHaHostMonitoring()); 
                    System.out.println("haFailoverLevel:"+clusterInfo.getHaFailoverLevel()); 
                    System.out.println("cpuFailoverPercent:"+clusterInfo.getCpuFailoverPercent()); 
                    System.out.println("memFailoverPercent:"+clusterInfo.getMemFailoverPercent()); 
                } 
            } 
            serviceInstance.getServerConnection().logout(); 
 
 
 
        }catch (Exception e){ 
            e.printStackTrace(); 
        } 
    } 
} 

效果展示:



声明

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

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

KIKK导航

关注我们