vijava 实时监控HostSystem主机各种指标,以及区分目标对象(直接可用)

自己百度时总能看到这个文章:  Vmware Vsphere WebService之vijava 开发(二)一性能信息的采集(实时监控)

https://www.cnblogs.com/xiaodige/p/6774413.html 

我根据他的代码 完善并有新需求  我用的是vc 6.7

 

vijava 实时监控HostSystem主机各种指标,以及区分目标对象(直接可用)_第1张图片

 

点击图标选项 自定义监控类型      

listNm.add("net");  这个意思就是  选择 -网络   例如 cpu   就是"cpu"   这个可以打断点查出来  
Map perfData = performanceCounter2.getPerfData("packetsTx", listNm, dataCenter, 20,serviceInstance,calBegin,calEnd);  这个意思就是内部名称 找到-传输的数据包数

 

vijava 实时监控HostSystem主机各种指标,以及区分目标对象(直接可用)_第2张图片xian

先把  PerformanceManage 类给你们  就是一个实体

package com.tongdaotech.modules.vcenter.entity;


import java.util.Date;
import java.util.List;

public class PerformanceManage {
    public Date startTime;
    public Date endTime;
    public List performanceValues;
    public List times;

    public List getTimes() {
        return times;
    }

    public void setTimes(List times) {
        this.times = times;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public List getPerformanceValues() {
        return performanceValues;
    }

    public void setPerformanceValues(List performanceValues) {
        this.performanceValues = performanceValues;
    }
}

以下代码直接可用   改一下 ip和密码即可

package com.tongdaotech.modules.vcenter.controller;

import com.tongdaotech.common.utils.Constant;
import com.tongdaotech.common.utils.Result;
import com.tongdaotech.modules.vcenter.entity.PerformanceManage;
import com.vmware.vim25.*;
import com.vmware.vim25.mo.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @description 监控统计vcenter所有对象性能数据
 * @date 2020年12月22日11:46:35
 * @version 1.1
 * @author Wu
 */

public class PerformanceCounter1 {
    /** main测试方法 */
    public static void main(String[] args) throws Exception {
        PerformanceCounter1 performanceCounter2 = new PerformanceCounter1();
        ServiceInstance serviceInstance = new ServiceInstance(new URL("https://12x.13x.12x.14x:29888/sdk"), "[email protected]", "123...,", true);
        HostSystem dataCenter = (HostSystem) new InventoryNavigator(serviceInstance.getRootFolder())
                .searchManagedEntity("HostSystem", "192.168.2.234");
        List listNm = new ArrayList();
//        listNm.add("datastore");
        listNm.add("net");

        Date date = new Date();
//        Date sTime = new Date(date.getTime() - 24 * 60 * 60 * 1000);   24小时
//        Date sTime = new Date(date.getTime() - 1*60* 60 * 1000);     1小时
        Date sTime = new Date(date.getTime() - 1*11* 60 * 1000);     //  11分钟  //20秒间隔  一分钟=3个数据

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

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


        Map perfData = performanceCounter2.getPerfData11("packetsTx", listNm, dataCenter, 20,serviceInstance,calBegin,calEnd);
//        PerformanceManage performanceManage = perfData.get("datastore");
        PerformanceManage performanceManage = perfData.get("net");
        List performanceValues = performanceManage.getPerformanceValues();
        System.out.println(performanceValues);

    }

    public Map getPerfData11(String nameInfo, List groupInfo, ManagedEntity mo,
                                                      Integer interval, ServiceInstance instance,Calendar calBegin ,Calendar calEnd) {
        Map PerformanceMap = null;


        try {
            PerformanceMap = new HashMap();
            if (mo != null) {
                PerformanceManager performanceManager = instance.getPerformanceManager();
//                PerformanceManager performanceManager = cs.si.getPerformanceManager();
                PerfCounterInfo[] cInfo = performanceManager.getPerfCounter();

                Map counters = new HashMap();

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

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

                ArrayList mMetrics = new ArrayList();
                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];
                                        if(val.getId().instance.equals("")){
                                        long[] lislon = val.getValue();

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

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

        return PerformanceMap;
    }


}

当我想要请求时发现      目标对象为多个  他的代码就默认取第一个   也就是说  有可能  为vmnic0   vmnic1   或者其他   所以加了

if(val.getId().instance.equals("")) 这个判断    确保拿到的是 正确的目标对象 也就是"198.98.31.xxx"的数据    

vijava 实时监控HostSystem主机各种指标,以及区分目标对象(直接可用)_第3张图片

你可能感兴趣的:(云计算,java)