Vijava 学习笔记之VirtualMachine(CPU 利用率和Memory 利用率)分析

阿里 阅读:280 2021-03-31 22:37:42 评论:0
package com.vmware.util; 
 
import com.vmware.vim25.*; 
import com.vmware.vim25.mo.InventoryNavigator; 
import com.vmware.vim25.mo.ManagedEntity; 
import com.vmware.vim25.mo.PerformanceManager; 
import com.vmware.vim25.mo.ServiceInstance; 
import java.rmi.RemoteException; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
 
/** 
 * VirtualMachine CPU利用率和Memory利用率 
 * Created by vixuan-008 on 2015/4/20. 
 */ 
public class VM { 
 
 
    /** 
     * Created by vixuan-008 on 2015/4/20. 
     */ 
 
        private ServiceInstance instance = null; 
        private InventoryNavigator inventoryNavigator = null; 
        public List getVMCPUPerByTimeRange(String vcomName, ServiceInstance serviceInstance,int timeRange,String instanceName) throws Exception { 
            List result  = new ArrayList(); 
            instance = serviceInstance; 
            inventoryNavigator = new InventoryNavigator(instance.getRootFolder()); 
            ManagedEntity obj = inventoryNavigator.searchManagedEntity("VirtualMachine", vcomName); 
            if (null == obj) 
                throw new Exception(); 
            com.vmware.vim25.mo.VirtualMachine vm = (com.vmware.vim25.mo.VirtualMachine) obj; 
            PerformanceManager pfMgr = instance.getPerformanceManager(); 
            PerfProviderSummary summary = pfMgr.queryPerfProviderSummary(vm); 
            PerfInterval[] rfRates = pfMgr.getHistoricalInterval(); 
            PerfMetricId[] pfMtIds = pfMgr.queryAvailablePerfMetric(vm, null, null, timeRange); 
            List<Map> contids = getCpuUsageAndUsageMhzCounterIds(pfMgr,vm,timeRange); 
            if( null != contids && contids.size() > 0 ) { 
                int[] counterIds = new int[pfMtIds.length]; 
                int iLabel = 0; 
                for (PerfMetricId pfMt : pfMtIds) { 
                    int contId = pfMt.getCounterId(); 
                    counterIds[iLabel++] = contId; 
                } 
                List<Map> instances = new ArrayList<Map>(); 
                for (PerfMetricId mtId : pfMtIds) { 
                    int counterId = mtId.getCounterId(); 
 
                    for (Map m : contids) { 
                        Integer cntId = (Integer) m.get("counterId"); 
                        String groupName = (String) m.get("groupName"); 
                        String targetName = (String) m.get("targetName"); 
                        if (counterId == cntId && (null == instanceName || (null != instanceName && mtId.getInstance().equals(instanceName)))) { 
                            Map mm = new HashMap(); 
                            mm.put("counterId", cntId); 
                            mm.put("instanceId", mtId.getInstance()); 
                            mm.put("groupName", groupName); 
                            mm.put("targetName", targetName); 
                            instances.add(mm); 
                        } 
 
 
                    } 
 
                } 
 
 
                for (int i = 0; i < instances.size(); i++) { 
                    PerfMetricId[] cpuCores = new PerfMetricId[1]; 
                    Map m = instances.get(i); 
                    cpuCores[0] = new PerfMetricId(); 
                    cpuCores[0].setCounterId((Integer) m.get("counterId")); 
                    cpuCores[0].setInstance((String) m.get("instanceId")); 
                    PerfQuerySpec qSpec = createPerfQuerySpec(vm, cpuCores, 1, timeRange); 
                    PerfEntityMetricBase[] pValues = pfMgr.queryPerf(new PerfQuerySpec[]{qSpec}); 
                    Map resultM = new HashMap(); 
                    String values = ""; 
                    String lables = ""; 
                    String point = "" + m.get("targetName"); 
                    PerfMetricSeriesCSV[] csvs = null; 
                    String name = "" + m.get("targetName") + "" + m.get("groupName"); 
                    for (int j = 0; j < pValues.length; j++) { 
                        PerfEntityMetricCSV csvValue = (PerfEntityMetricCSV) pValues[j]; 
 
                        csvs = csvValue.getValue(); 
                        lables = csvValue.getSampleInfoCSV(); 
                        for (int k = 0; k < csvs.length; k++) { 
                            values += csvs[k].getValue() + ","; 
                        } 
 
                    } 
                    resultM.put("name", name); 
                    resultM.put("values", values); 
                    resultM.put("lables", lables); 
                    resultM.put("instance", instanceName); 
                    resultM.put("point", point); 
                    resultM.put("csvs", csvs); 
                    result.add(resultM); 
                } 
            } 
 
            return result; 
        } 
 
        public PerfQuerySpec createPerfQuerySpec(ManagedEntity me, 
                                                 PerfMetricId[] metricIds, int maxSample, int interval) 
        { 
            PerfQuerySpec qSpec = new PerfQuerySpec(); 
            qSpec.setEntity(me.getMOR()); 
            // set the maximum of metrics to be return 
            // only appropriate in real-time performance collecting 
            qSpec.setMaxSample(new Integer(maxSample)); 
            qSpec.setMetricId(metricIds); 
            // optionally you can set format as "normal" 
            qSpec.setFormat("csv"); 
//	    qSpec.setFormat("normal"); 
            // set the interval to the refresh rate for the entity 
            qSpec.setIntervalId(new Integer(interval)); 
 
            return qSpec; 
        } 
 
        private List<Map> getCpuUsageAndUsageMhzCounterIds(PerformanceManager pfMgr,ManagedEntity managedEntity,Integer point) throws RemoteException { 
            List<Map> result = new ArrayList<Map>(); 
            PerfMetricId[] pfMtIds = pfMgr.queryAvailablePerfMetric(managedEntity, null, null, point); 
            if(null != pfMtIds && pfMtIds.length > 0 ) { 
                int []counterIds = new int[pfMtIds.length]; 
                int iLabel = 0; 
                for(PerfMetricId pfMt: pfMtIds){ 
                    int contId = pfMt.getCounterId(); 
                    counterIds[iLabel++] = contId; 
                } 
 
                PerfCounterInfo[] pfContinfos = pfMgr.queryPerfCounter(counterIds); 
                List<String> gpList = new ArrayList<String>(); 
                for(int i = 0 ; i<  pfContinfos.length ; i++) { 
                    String strName = pfContinfos[i].getNameInfo().getKey(); 
                    String groupName = pfContinfos[i].getGroupInfo().getKey(); 
                    if (null != groupName && groupName.equals("cpu")) { 
                        if (null != strName && strName.equals("usage") || strName.equals("usagemhz")) { 
                            if (result.size() > 0) { 
                                boolean flag = true; 
                                for (Map mm : result) { 
                                    Integer key = (Integer) mm.get("counterId"); 
                                    if (key.equals(pfContinfos[i].getKey())) 
                                        flag = false; 
                                } 
                                if (flag) { 
                                    Map m = new HashMap(); 
                                    m.put("counterId", counterIds[i]); 
                                    m.put("groupName", groupName); 
                                    m.put("targetName", strName); 
                                    result.add(m); 
                                } 
 
                            } else { 
                                Map m = new HashMap(); 
 
                                m.put("counterId", counterIds[i]); 
                                m.put("groupName", groupName); 
                                m.put("targetName", strName); 
                                result.add(m); 
                            } 
                        } 
                    } 
                } 
            } 
            return result; 
        } 
 
    public List getVMMemPerByTimeRange(String vcomName, ServiceInstance serviceInstance,int timeRange,String instanceName) throws Exception { 
        List result  = new ArrayList(); 
        instance =  serviceInstance; 
        inventoryNavigator = new InventoryNavigator(instance.getRootFolder()); 
        ManagedEntity obj = inventoryNavigator.searchManagedEntity("VirtualMachine", vcomName); 
        if (null == obj) 
            throw new Exception(); 
        com.vmware.vim25.mo.VirtualMachine vm = (com.vmware.vim25.mo.VirtualMachine) obj; 
        PerformanceManager pfMgr = instance.getPerformanceManager(); 
        PerfProviderSummary  summary = pfMgr.queryPerfProviderSummary(vm); 
        PerfInterval[]rfRates = pfMgr.getHistoricalInterval(); 
        PerfMetricId[] pfMtIds = pfMgr.queryAvailablePerfMetric(vm, null, null, timeRange); 
        List<Map> contids = getMemUsageAndOverHeadCounterIds(pfMgr, vm, timeRange); 
        if( null != contids && contids.size() > 0 ) { 
            int[] counterIds = new int[pfMtIds.length]; 
            int iLabel = 0; 
            for (PerfMetricId pfMt : pfMtIds) { 
                int contId = pfMt.getCounterId(); 
                counterIds[iLabel++] = contId; 
            } 
            List<Map> instances = new ArrayList<Map>(); 
            for (PerfMetricId mtId : pfMtIds) { 
                int counterId = mtId.getCounterId(); 
 
                for (Map m : contids) { 
                    Integer cntId = (Integer) m.get("counterId"); 
                    String groupName = (String) m.get("groupName"); 
                    String targetName = (String) m.get("targetName"); 
                    if (counterId == cntId && (null == instanceName || (null != instanceName && mtId.getInstance().equals(instanceName)))) { 
                        Map mm = new HashMap(); 
                        mm.put("counterId", cntId); 
                        mm.put("instanceId", mtId.getInstance()); 
                        mm.put("groupName", groupName); 
                        mm.put("targetName", targetName); 
                        instances.add(mm); 
                    } 
 
 
                } 
 
            } 
 
 
            for (int i = 0; i < instances.size(); i++) { 
                PerfMetricId[] cpuCores = new PerfMetricId[1]; 
                Map m = instances.get(i); 
                cpuCores[0] = new PerfMetricId(); 
                cpuCores[0].setCounterId((Integer) m.get("counterId")); 
                cpuCores[0].setInstance((String) m.get("instanceId")); 
                PerfQuerySpec qSpec = createPerfQuerySpec(vm, cpuCores, 1, timeRange); 
                PerfEntityMetricBase[] pValues = pfMgr.queryPerf(new PerfQuerySpec[]{qSpec}); 
                Map resultM = new HashMap(); 
                String values = ""; 
                String lables = ""; 
                String point = "" + m.get("targetName"); 
                PerfMetricSeriesCSV[] csvs = null; 
                String name = "" + m.get("targetName") + "" + m.get("groupName"); 
                for (int j = 0; j < pValues.length; j++) { 
                    PerfEntityMetricCSV csvValue = (PerfEntityMetricCSV) pValues[j]; 
 
                    csvs = csvValue.getValue(); 
                    lables = csvValue.getSampleInfoCSV(); 
                    for (int k = 0; k < csvs.length; k++) { 
                        values += csvs[k].getValue() + ","; 
                    } 
 
                } 
                resultM.put("name", name); 
                resultM.put("values", values); 
                resultM.put("lables", lables); 
                resultM.put("instance", instanceName); 
                resultM.put("point", point); 
                resultM.put("csvs", csvs); 
                result.add(resultM); 
            } 
        } 
 
        return result; 
    } 
 
    private List<Map> getMemUsageAndOverHeadCounterIds(PerformanceManager pfMgr,ManagedEntity managedEntity,Integer point) throws RemoteException { 
        List<Map> result = new ArrayList<Map>(); 
        PerfMetricId[] pfMtIds = pfMgr.queryAvailablePerfMetric(managedEntity, null, null, point); 
        if(null != pfMtIds && pfMtIds.length > 0 ) { 
            int []counterIds = new int[pfMtIds.length]; 
            int iLabel = 0; 
            for(PerfMetricId pfMt: pfMtIds){ 
                int contId = pfMt.getCounterId(); 
                counterIds[iLabel++] = contId; 
            } 
 
            PerfCounterInfo[] pfContinfos = pfMgr.queryPerfCounter(counterIds); 
            List<String> gpList = new ArrayList<String>(); 
            for(int i = 0 ; i<  pfContinfos.length ; i++) { 
                String strName = pfContinfos[i].getNameInfo().getKey(); 
                String groupName = pfContinfos[i].getGroupInfo().getKey(); 
                if (null != groupName && groupName.equals("mem")) { 
                    if (null != strName && strName.equals("usage") || strName.equals("consumed")) { 
                        if (result.size() > 0) { 
                            boolean flag = true; 
                            for (Map mm : result) { 
                                Integer key = (Integer) mm.get("counterId"); 
                                if (key.equals(pfContinfos[i].getKey())) 
                                    flag = false; 
                            } 
                            if (flag) { 
                                Map m = new HashMap(); 
                                m.put("counterId", counterIds[i]); 
                                m.put("groupName", groupName); 
                                m.put("targetName", strName); 
                                result.add(m); 
                            } 
 
                        } else { 
                            Map m = new HashMap(); 
 
                            m.put("counterId", counterIds[i]); 
                            m.put("groupName", groupName); 
                            m.put("targetName", strName); 
                            result.add(m); 
                        } 
                    } 
                } 
            } 
        } 
        return result; 
    } 
}

package com.vmware.client; 
 
 
import com.vmware.util.Session; 
import com.vmware.util.VM; 
import com.vmware.vim25.*; 
import com.vmware.vim25.mo.*; 
import java.util.List; 
import java.util.Map; 
 
/** 
 * 测试代码 
 * Created by vixuan-008 on 2015/4/20. 
 */ 
public class VirtualMachineCPUpercent { 
    public static void main(String[] args){ 
        try{ 
            VM vm = new VM(); 
            ServiceInstance serviceInstance= Session.getInstance("172.16.1.20", "root", "vmware"); 
            //获取 PerformanceManger 
            PerformanceManager performanceManager=serviceInstance.getPerformanceManager(); 
            List cpuPerList = null; 
            cpuPerList = vm.getVMCPUPerByTimeRange("Mssql", serviceInstance, 20, ""); 
 
 
            List memPerList = vm.getVMMemPerByTimeRange("Mssql", serviceInstance, 20,""); 
 
 
            for (int s = 0; s < cpuPerList.size(); s++) { 
 
 
                Map m = (Map) cpuPerList.get(s); 
                String point = (String) m.get("point"); 
 
                PerfMetricSeriesCSV[] csvs = (PerfMetricSeriesCSV[]) m.get("csvs"); 
                if (point.equals("usage")) { 
                    PerfMetricSeriesCSV _value = csvs[0]; 
                    Float f =  Float.valueOf(_value.getValue())/100f; 
                    System.out.println("usage:"+f+"\n"); 
                } 
                else if (point.equals("usagemhz")) { 
                    PerfMetricSeriesCSV _value = csvs[0]; 
                    System.out.println("usagemhz:"+_value+"\n"); 
                } 
 
 
 
            } 
 
        if( null != memPerList&&memPerList.size() > 0) 
        { 
            for (int s = 0; s < memPerList.size(); s++) { 
                Map m = (Map) memPerList.get(s); 
                String point = (String) m.get("point"); 
 
                PerfMetricSeriesCSV[] csvs = (PerfMetricSeriesCSV[]) m.get("csvs"); 
                if (point.equals("usage")) { 
                    PerfMetricSeriesCSV _value = csvs[0]; 
                    Float f =  Float.valueOf(_value.getValue())/100f; 
                    System.out.println("usage"+f+"\n"); 
 
                } 
                else if (point.equals("consumed")) { 
                    PerfMetricSeriesCSV _value = csvs[0]; 
                    System.out.println("consumed:"+_value+"\n"); 
 
 
                } 
            } 
        } 
        }catch (Exception e){ 
            e.printStackTrace(); 
        } 
    } 
}
相关展示:
<img src="https://img-blog.csdn.net/20150420143926087?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhvdXpoaXdlbmdhbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /> 
声明

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

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号