Vmware Vsphere WebService之vijava 开发(二)一性能信息的采集(实时监控)

时间:2024-03-19 21:27:25

最近一直没有更新这部分的内容,会利用五一时间完成vcenter这一个系列。

这里先给大家一本关于vijava开发的书,比较实用。

地址:http://pan.baidu.com/s/1gfkl9mj。密码:t1y3

有网友评论要数据存储读写速度及延迟之后时间的实现。今天就先介绍关于java实现实时监控vcenter状态的内容,包括CPU、内存、网络、存储等。

在看这篇前如果还是新手,建议先看下我的第一篇  http://www.cnblogs.com/xiaodige/p/6721517.html(vijava基本连接和数据中心信息获取)

先说一下我之前是怎么发现,CPU、内存、网络、存储等可以监控的内容。我用vsphere client连接vcenter查看客户端所能监控的属性.

建议大家在实现功能前,先看下官方文档关于性能监控的知识。贴上连接vcenter6.0的官方文档连接。http://pubs.vmware.com/vsphere-60/index.jsp

比如要监控虚拟机的性能信息:首先得保证虚拟机开机,选择“性能选项”,点击“图标选项”,这样就能看到它所支持的性能监控信息。下面贴图:

Vmware Vsphere WebService之vijava 开发(二)一性能信息的采集(实时监控)

下图左边就是该对象具体能监控的信息,右边就是具体性能,使用率啊等等。

Vmware Vsphere WebService之vijava 开发(二)一性能信息的采集(实时监控)

做性能监控这块,如果不知道具体能监控对象的哪些属性就打开客户端看看。下面就贴上具体的java实现代码。

package com.iking.vmware.performance;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.iking.vmware.bean.PerformanceManage;
import com.iking.vmware.bean.VsphereConst;
import com.iking.vmware.connection.ConnectedVimServiceBase;
import com.iking.vmware.vim25.PerfCounterInfo;
import com.iking.vmware.vim25.PerfEntityMetric;
import com.iking.vmware.vim25.PerfEntityMetricBase;
import com.iking.vmware.vim25.PerfMetricId;
import com.iking.vmware.vim25.PerfMetricIntSeries;
import com.iking.vmware.vim25.PerfMetricSeries;
import com.iking.vmware.vim25.PerfQuerySpec;
import com.iking.vmware.vim25.PerfSampleInfo;
import com.iking.vmware.vim25.mo.Folder;
import com.iking.vmware.vim25.mo.HostSystem;
import com.iking.vmware.vim25.mo.InventoryNavigator;
import com.iking.vmware.vim25.mo.ManagedEntity;
import com.iking.vmware.vim25.mo.PerformanceManager;

/**
 * @description 监控统计vcenter所有对象性能数据
 * @date 2017年2月9日11:46:35
 * @version 1.1
 * @author DiWk
 */
public class PerformanceCounter {
    private ConnectedVimServiceBase cs = null;

    public ConnectedVimServiceBase getCs() {
        return cs;
    }

    public void setCs(ConnectedVimServiceBase cs) {
        this.cs = cs;
    }

    /**
     * @description 根据属性名称、类型、对象、采集间隔获取所有的性能数据
     * @date 2017年2月8日14:37:58
     * @return PerformanceMap 性能数据map对象
     * @version 1.1
     * @author DiWk
     */
    public Map<String, PerformanceManage> getPerfData(String nameInfo, List<String> groupInfo, ManagedEntity mo,
            Integer interval) {
        Map<String, PerformanceManage> PerformanceMap = null;

        Date date = new Date();
        Date sTime = new Date(date.getTime() - 24 * 60 * 60 * 1000);

        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(sTime);

        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(date);

        try {
            PerformanceMap = new HashMap<String, PerformanceManage>();
            if (mo != null) {
                PerformanceManager performanceManager = cs.si.getPerformanceManager();
                PerfCounterInfo[] cInfo = performanceManager.getPerfCounter();

                Map<Integer, PerfCounterInfo> counters = new HashMap<Integer, PerfCounterInfo>();

                for (PerfCounterInfo pcInfo : cInfo) {
                    counters.put(new Integer(pcInfo.getKey()), pcInfo);
                }

                PerfMetricId[] listpermeid = performanceManager.queryAvailablePerfMetric(mo, null, null, interval);

                ArrayList<PerfMetricId> mMetrics = new ArrayList<PerfMetricId>();
                if (listpermeid != null) {
                    for (int index = 0; index < listpermeid.length; ++index) {
                        if (counters.containsKey(new Integer(listpermeid[index].getCounterId()))) {
                            mMetrics.add(listpermeid[index]);
                        }
                    }
                }

                PerfQuerySpec qSpec = new PerfQuerySpec();
                qSpec.setEntity(mo.getMOR());
                qSpec.setMetricId(listpermeid);
                qSpec.setStartTime(calBegin);
                qSpec.setEndTime(calEnd);
                qSpec.setIntervalId(interval);
                qSpec.setFormat("normal");

                PerfQuerySpec[] arryQuery = { qSpec };

                PerfEntityMetricBase[] pValues = performanceManager.queryPerf(arryQuery);


                if (pValues == null || pValues.length <= 0) {
                    return null;
                }
                PerfSampleInfo[] listperfsinfo = ((PerfEntityMetric) pValues[0]).getSampleInfo();
                for (int i = 0; i < pValues.length; i++) {
                    PerfMetricSeries[] listpems = ((PerfEntityMetric) pValues[i]).getValue();

                    for (int vi = 0; vi < listpems.length; ++vi) {
                        PerfCounterInfo pci = (PerfCounterInfo) counters
                                .get(new Integer(listpems[vi].getId().getCounterId()));

                        if (pci != null) {
                            for (String Info : groupInfo) {
                                PerformanceManage performanceManage = new PerformanceManage();
                                performanceManage.setStartTime(listperfsinfo[0].getTimestamp().getTime());
                                performanceManage
                                        .setEndTime((listperfsinfo[listperfsinfo.length - 1]).getTimestamp().getTime());
                                if (pci.getNameInfo().getKey().equalsIgnoreCase(nameInfo)
                                        && pci.getGroupInfo().getKey().equalsIgnoreCase(Info)) {
                                    if (listpems[vi] instanceof PerfMetricIntSeries) {
                                        PerfMetricIntSeries val = (PerfMetricIntSeries) listpems[vi];
                                        long[] lislon = val.getValue();

                                        List<Long> asList = new ArrayList<Long>();
                                        for (Long k : lislon) {
                                            asList.add(k);
                                        }
                                        performanceManage.setPerformanceValues(asList);
                                        PerformanceMap.put(Info, performanceManage);
                                    }
                                }
                            }
                        }
                    }
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return PerformanceMap;
    }

    /** main测试方法 */
    public static void main(String[] args) throws Exception {
        PerformanceCounter performanceCounter2 = new PerformanceCounter();
        ConnectedVimServiceBase cs = new ConnectedVimServiceBase();
        cs.connect("192.168.1.253", "[email protected]", "[email protected]#456");
        performanceCounter2.setCs(cs);
        Folder rootFolder = cs.si.getRootFolder();
        HostSystem dataCenter = (HostSystem) new InventoryNavigator(rootFolder)
                .searchManagedEntity(VsphereConst.HOSTSYSTEM, "192.168.1.254");
        List<String> listNm = new ArrayList<String>();
        listNm.add("datastore");
        Map<String, PerformanceManage> perfData = performanceCounter2.getPerfData("write", listNm, dataCenter, 20);
        PerformanceManage performanceManage = perfData.get("datastore");
        List<Long> performanceValues = performanceManage.getPerformanceValues();
        System.out.println(performanceValues.toString());
    }
}

原文地址:https://www.cnblogs.com/xiaodige/p/6774413.html