Search in sources :

Example 1 with PerfMetricIntSeries

use of com.vmware.vim25.PerfMetricIntSeries in project opennms by OpenNMS.

the class VmwareViJavaAccessTest method setUp.

@Before
public void setUp() throws Exception {
    // setup required objects
    managedObjectReferenceManagedEntity = new ManagedObjectReference();
    managedObjectReferenceManagedEntity.setType("ManagedEntity");
    managedObjectReferenceManagedEntity.setVal("moIdMe");
    managedObjectReferenceVirtualMachine = new ManagedObjectReference();
    managedObjectReferenceVirtualMachine.setType("VirtualMachine");
    managedObjectReferenceVirtualMachine.setVal("moIdVm");
    managedObjectReferenceHostSystem = new ManagedObjectReference();
    managedObjectReferenceHostSystem.setType("HostSystem");
    managedObjectReferenceHostSystem.setVal("moIdHs");
    // setup VmwareViJavaAccess
    vmwareViJavaAccess = new VmwareViJavaAccess("hostname", "username", "password") {

        @Override
        protected void relax() {
        }
    };
    // setup PerformanceManager
    mockPerformanceManager = createMock(PerformanceManager.class);
    // setup ServiceInstance mock
    mockServiceInstance = createMock(ServiceInstance.class);
    // setup ServerConnection
    mockServerConnection = new ServerConnection(new URL("https://hostname/sdk"), new VimPortType(new WSClient("https://hostname/sdk") {

        @Override
        protected SSLSocketFactory getTrustAllSocketFactory(boolean ignoreCert) throws RemoteException {
            return null;
        }
    }), mockServiceInstance);
    // setup AboutInfo
    mockAboutInfo = createMock(AboutInfo.class);
    expectNew(ServiceInstance.class, new Class<?>[] { URL.class, String.class, String.class }, new URL("https://hostname/sdk"), "username", "password").andReturn(mockServiceInstance).anyTimes();
    expect(mockServiceInstance.getServerConnection()).andReturn(mockServerConnection).anyTimes();
    expect(mockServiceInstance.getPerformanceManager()).andReturn(mockPerformanceManager).anyTimes();
    expect(mockServiceInstance.getAboutInfo()).andReturn(mockAboutInfo).anyTimes();
    managedEntity = new ManagedEntity(null, managedObjectReferenceManagedEntity);
    virtualMachine = new VirtualMachine(null, managedObjectReferenceVirtualMachine);
    hostSystem = new HostSystem(null, managedObjectReferenceHostSystem);
    // setup MorUtil
    mockStatic(MorUtil.class);
    expect(MorUtil.createExactManagedEntity(mockServerConnection, managedObjectReferenceManagedEntity)).andReturn(managedEntity).anyTimes();
    expect(MorUtil.createExactManagedEntity(mockServerConnection, managedObjectReferenceVirtualMachine)).andReturn(virtualMachine).anyTimes();
    expect(MorUtil.createExactManagedEntity(mockServerConnection, managedObjectReferenceHostSystem)).andReturn(hostSystem).anyTimes();
    // setup about info
    expect(mockAboutInfo.getApiVersion()).andReturn("2.x");
    expect(mockAboutInfo.getApiVersion()).andReturn("3.x");
    expect(mockAboutInfo.getApiVersion()).andReturn("4.x");
    expect(mockAboutInfo.getApiVersion()).andReturn("5.x");
    expect(mockAboutInfo.getApiVersion()).andReturn("6.x");
    expect(mockAboutInfo.getApiVersion()).andReturn("x");
    // setup performance data
    int refreshRate = 100;
    int metricCount = 15;
    int instanceModifier = 5;
    perfQuerySpec = new PerfQuerySpec();
    perfQuerySpec.setEntity(managedEntity.getMOR());
    perfQuerySpec.setMaxSample(new Integer(1));
    perfQuerySpec.setIntervalId(refreshRate);
    perfEntityMetricBases = new PerfEntityMetricBase[metricCount];
    perfCounterInfos = new PerfCounterInfo[metricCount];
    for (int i = 0; i < metricCount; i++) {
        ElementDescription groupInfo = new ElementDescription();
        groupInfo.setKey("key" + i);
        ElementDescription nameInfo = new ElementDescription();
        nameInfo.setKey("name" + i);
        perfCounterInfos[i] = new PerfCounterInfo();
        perfCounterInfos[i].setKey(i);
        perfCounterInfos[i].setGroupInfo(groupInfo);
        perfCounterInfos[i].setNameInfo(nameInfo);
        perfCounterInfos[i].setRollupType(PerfSummaryType.average);
        perfEntityMetricBases[i] = new PerfEntityMetric();
        PerfMetricIntSeries[] perfMetricIntSeries;
        int instanceCount = (i % instanceModifier) + 1;
        perfMetricIntSeries = new PerfMetricIntSeries[instanceCount];
        for (int b = 0; b < instanceCount; b++) {
            PerfMetricId perfMetricId = new PerfMetricId();
            perfMetricId.setCounterId(i);
            if (instanceCount == 1) {
                perfMetricId.setInstance(null);
            } else {
                perfMetricId.setInstance("instance" + b);
            }
            perfMetricIntSeries[b] = new PerfMetricIntSeries();
            perfMetricIntSeries[b].setValue(new long[] { (long) 42 });
            perfMetricIntSeries[b].setId(perfMetricId);
        }
        ((PerfEntityMetric) perfEntityMetricBases[i]).setValue(perfMetricIntSeries);
    }
    // setup PerfProviderSummary
    mockPerfProviderSummary = createMock(PerfProviderSummary.class);
    expect(mockPerformanceManager.queryPerfProviderSummary(managedEntity)).andReturn(mockPerfProviderSummary).anyTimes();
    expect(mockPerfProviderSummary.getRefreshRate()).andReturn(refreshRate).anyTimes();
    expect(mockPerformanceManager.getPerfCounter()).andReturn(perfCounterInfos).anyTimes();
    expect(mockPerformanceManager.queryPerf(anyObject(PerfQuerySpec[].class))).andReturn(perfEntityMetricBases).anyTimes();
    // setup network info
    HostNetworkInfo hostNetworkInfo = new HostNetworkInfo();
    int numberOfVnics = 3;
    HostVirtualNic[] hostVirtualNics = new HostVirtualNic[numberOfVnics];
    for (int i = 0; i < numberOfVnics; i++) {
        HostVirtualNicSpec hostVirtualNicSpec = new HostVirtualNicSpec();
        HostIpConfig hostIpConfig = new HostIpConfig();
        hostIpConfig.setIpAddress("192.168.1." + (i + 1));
        hostVirtualNicSpec.setIp(hostIpConfig);
        hostVirtualNics[i] = new HostVirtualNic();
        hostVirtualNics[i].setSpec(hostVirtualNicSpec);
    }
    hostNetworkInfo.setVnic(hostVirtualNics);
    HostVirtualNic[] hostVirtualConsoleNics = new HostVirtualNic[numberOfVnics];
    for (int i = 0; i < numberOfVnics; i++) {
        HostVirtualNicSpec hostVirtualNicSpec = new HostVirtualNicSpec();
        HostIpConfig hostIpConfig = new HostIpConfig();
        hostIpConfig.setIpAddress("192.168.2." + (i + 1));
        hostVirtualNicSpec.setIp(hostIpConfig);
        hostVirtualConsoleNics[i] = new HostVirtualNic();
        hostVirtualConsoleNics[i].setSpec(hostVirtualNicSpec);
    }
    hostNetworkInfo.setConsoleVnic(hostVirtualConsoleNics);
    HostServiceTicket hostServiceTicket = new HostServiceTicket();
    hostServiceTicket.setSessionId("sessionId");
    // setup HostSystem
    mockHostSystem = createMock(HostSystem.class);
    // setup HostNetworkSystem
    mockHostNetworkSystem = createMock(HostNetworkSystem.class);
    // setup CIMClient
    mockCIMClient = createPartialMock(CIMClient.class, "enumerateInstances");
    // setup the cim objects
    cimObjects = new ArrayList<CIMObject>();
    int cimObjectCount = 5;
    for (int i = 0; i < cimObjectCount; i++) {
        CIMInstance cimInstance = new CIMInstance();
        cimInstance.setName("cimInstance" + i);
        cimObjects.add(cimInstance);
    }
    expect(mockHostSystem.getName()).andReturn("mockesxi01.local").anyTimes();
    expect(mockHostSystem.getHostNetworkSystem()).andReturn(mockHostNetworkSystem).anyTimes();
    expect(mockHostSystem.acquireCimServicesTicket()).andReturn(hostServiceTicket).anyTimes();
    expect(mockHostNetworkSystem.getNetworkInfo()).andReturn(hostNetworkInfo).anyTimes();
    expectNew(CIMClient.class, new Class<?>[] { CIMNameSpace.class, Principal.class, Object.class }, anyObject(), anyObject(), anyObject()).andReturn(mockCIMClient).anyTimes();
    suppress(method(CIMClient.class, "useMPost"));
    expect(mockCIMClient.enumerateInstances(new CIMObjectPath("cimClass"))).andReturn(Collections.enumeration(cimObjects)).anyTimes();
}
Also used : HostVirtualNic(com.vmware.vim25.HostVirtualNic) PerfCounterInfo(com.vmware.vim25.PerfCounterInfo) CIMObject(org.sblim.wbem.cim.CIMObject) CIMClient(org.sblim.wbem.client.CIMClient) HostIpConfig(com.vmware.vim25.HostIpConfig) HostVirtualNicSpec(com.vmware.vim25.HostVirtualNicSpec) ServiceInstance(com.vmware.vim25.mo.ServiceInstance) URL(java.net.URL) CIMInstance(org.sblim.wbem.cim.CIMInstance) PerfMetricIntSeries(com.vmware.vim25.PerfMetricIntSeries) PerformanceManager(com.vmware.vim25.mo.PerformanceManager) HostSystem(com.vmware.vim25.mo.HostSystem) HostServiceTicket(com.vmware.vim25.HostServiceTicket) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) VmwareViJavaAccess(org.opennms.protocols.vmware.VmwareViJavaAccess) ElementDescription(com.vmware.vim25.ElementDescription) HostNetworkSystem(com.vmware.vim25.mo.HostNetworkSystem) ManagedEntity(com.vmware.vim25.mo.ManagedEntity) HostNetworkInfo(com.vmware.vim25.HostNetworkInfo) WSClient(com.vmware.vim25.ws.WSClient) PerfMetricId(com.vmware.vim25.PerfMetricId) AboutInfo(com.vmware.vim25.AboutInfo) CIMObjectPath(org.sblim.wbem.cim.CIMObjectPath) ServerConnection(com.vmware.vim25.mo.ServerConnection) VimPortType(com.vmware.vim25.VimPortType) PerfProviderSummary(com.vmware.vim25.PerfProviderSummary) PerfQuerySpec(com.vmware.vim25.PerfQuerySpec) RemoteException(java.rmi.RemoteException) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) VirtualMachine(com.vmware.vim25.mo.VirtualMachine) PerfEntityMetric(com.vmware.vim25.PerfEntityMetric) Before(org.junit.Before)

Example 2 with PerfMetricIntSeries

use of com.vmware.vim25.PerfMetricIntSeries in project opennms by OpenNMS.

the class VmwareViJavaAccessTest method testQueryPerformanceValues.

@Test
public void testQueryPerformanceValues() {
    replay(mockPerformanceManager, mockPerfProviderSummary, mockServiceInstance, ServiceInstance.class);
    VmwarePerformanceValues vmwarePerformanceValues = null;
    try {
        vmwareViJavaAccess.connect();
        vmwarePerformanceValues = vmwareViJavaAccess.queryPerformanceValues(managedEntity);
    } catch (MalformedURLException e) {
        Assert.fail(e.getMessage());
    } catch (RemoteException e) {
        Assert.fail(e.getMessage());
    }
    Assert.assertNotNull(vmwarePerformanceValues);
    for (int i = 0; i < perfCounterInfos.length; i++) {
        PerfCounterInfo perfCounterInfo = perfCounterInfos[i];
        String expectedName = perfCounterInfo.getGroupInfo().getKey() + "." + perfCounterInfo.getNameInfo().getKey() + "." + perfCounterInfo.getRollupType().toString();
        if (vmwarePerformanceValues.hasInstances(expectedName)) {
            Set<String> instances = vmwarePerformanceValues.getInstances(expectedName);
            Assert.assertEquals(instances.size(), ((PerfEntityMetric) perfEntityMetricBases[i]).getValue().length);
            PerfMetricIntSeries[] perfMetricIntSeries = (PerfMetricIntSeries[]) ((PerfEntityMetric) perfEntityMetricBases[i]).getValue();
            for (int b = 0; b < perfMetricIntSeries.length; b++) {
                Assert.assertTrue(instances.contains(perfMetricIntSeries[b].getId().getInstance()));
            }
        } else {
            Assert.assertEquals(1, ((PerfEntityMetric) perfEntityMetricBases[i]).getValue().length);
        }
    }
    verify(mockPerformanceManager, mockPerfProviderSummary, mockServiceInstance, ServiceInstance.class);
}
Also used : PerfMetricIntSeries(com.vmware.vim25.PerfMetricIntSeries) MalformedURLException(java.net.MalformedURLException) PerfCounterInfo(com.vmware.vim25.PerfCounterInfo) VmwarePerformanceValues(org.opennms.netmgt.collectd.vmware.vijava.VmwarePerformanceValues) RemoteException(java.rmi.RemoteException) PerfEntityMetric(com.vmware.vim25.PerfEntityMetric) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with PerfMetricIntSeries

use of com.vmware.vim25.PerfMetricIntSeries in project opennms by OpenNMS.

the class VmwareConfigBuilder method lookupMetrics.

private void lookupMetrics(String collectionName, String managedObjectId) throws Exception {
    ManagedObjectReference managedObjectReference = new ManagedObjectReference();
    managedObjectReference.setType("ManagedEntity");
    managedObjectReference.setVal(managedObjectId);
    ManagedEntity managedEntity = MorUtil.createExactManagedEntity(serviceInstance.getServerConnection(), managedObjectReference);
    int refreshRate = performanceManager.queryPerfProviderSummary(managedEntity).getRefreshRate();
    PerfQuerySpec perfQuerySpec = new PerfQuerySpec();
    perfQuerySpec.setEntity(managedEntity.getMOR());
    perfQuerySpec.setMaxSample(Integer.valueOf(1));
    perfQuerySpec.setIntervalId(refreshRate);
    PerfEntityMetricBase[] perfEntityMetricBases = performanceManager.queryPerf(new PerfQuerySpec[] { perfQuerySpec });
    HashMap<String, TreeSet<VMwareConfigMetric>> groupMap = new HashMap<String, TreeSet<VMwareConfigMetric>>();
    HashMap<String, Boolean> multiInstance = new HashMap<String, Boolean>();
    if (perfEntityMetricBases != null) {
        for (int i = 0; i < perfEntityMetricBases.length; i++) {
            PerfMetricSeries[] perfMetricSeries = ((PerfEntityMetric) perfEntityMetricBases[i]).getValue();
            for (int j = 0; perfMetricSeries != null && j < perfMetricSeries.length; j++) {
                if (perfMetricSeries[j] instanceof PerfMetricIntSeries) {
                    long[] longs = ((PerfMetricIntSeries) perfMetricSeries[j]).getValue();
                    if (longs.length == 1) {
                        PerfCounterInfo perfCounterInfo = perfCounterInfoMap.get(perfMetricSeries[j].getId().getCounterId());
                        String instanceName = perfMetricSeries[j].getId().getInstance();
                        String humanReadableName = getHumanReadableName(perfCounterInfo);
                        String aliasName = getAliasName(perfCounterInfo);
                        String groupName = perfCounterInfo.getGroupInfo().getKey();
                        String normalizedGroupName = normalizeGroupName(groupName);
                        Boolean b = multiInstance.get(getHumanReadableName(perfCounterInfo));
                        if (b == null) {
                            b = Boolean.valueOf(instanceName != null && !"".equals(instanceName));
                        } else {
                            b = Boolean.valueOf(b.booleanValue() || (instanceName != null && !"".equals(instanceName)));
                        }
                        if (!b) {
                            groupName = "Node";
                            normalizedGroupName = "Node";
                        }
                        if (!groupMap.containsKey(normalizedGroupName)) {
                            groupMap.put(normalizedGroupName, new TreeSet<VMwareConfigMetric>());
                        }
                        TreeSet<VMwareConfigMetric> counterSet = groupMap.get(normalizedGroupName);
                        multiInstance.put(getHumanReadableName(perfCounterInfo), b);
                        counterSet.add(new VMwareConfigMetric(perfCounterInfo, humanReadableName, aliasName, b, normalizedGroupName));
                    }
                }
            }
        }
    }
    collections.put(collectionName, groupMap);
}
Also used : ManagedEntity(com.vmware.vim25.mo.ManagedEntity) PerfCounterInfo(com.vmware.vim25.PerfCounterInfo) PerfMetricSeries(com.vmware.vim25.PerfMetricSeries) HashMap(java.util.HashMap) PerfEntityMetricBase(com.vmware.vim25.PerfEntityMetricBase) PerfMetricIntSeries(com.vmware.vim25.PerfMetricIntSeries) TreeSet(java.util.TreeSet) PerfQuerySpec(com.vmware.vim25.PerfQuerySpec) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) PerfEntityMetric(com.vmware.vim25.PerfEntityMetric)

Example 4 with PerfMetricIntSeries

use of com.vmware.vim25.PerfMetricIntSeries in project opennms by OpenNMS.

the class VmwareViJavaAccess method queryPerformanceValues.

/**
     * This method queries performance values for a given managed entity.
     *
     * @param managedEntity the managed entity to query
     * @return the perfomance values
     * @throws RemoteException
     */
public VmwarePerformanceValues queryPerformanceValues(ManagedEntity managedEntity) throws RemoteException {
    VmwarePerformanceValues vmwarePerformanceValues = new VmwarePerformanceValues();
    int refreshRate = getPerformanceManager().queryPerfProviderSummary(managedEntity).getRefreshRate();
    PerfQuerySpec perfQuerySpec = new PerfQuerySpec();
    perfQuerySpec.setEntity(managedEntity.getMOR());
    perfQuerySpec.setMaxSample(Integer.valueOf(1));
    perfQuerySpec.setIntervalId(refreshRate);
    PerfEntityMetricBase[] perfEntityMetricBases = getPerformanceManager().queryPerf(new PerfQuerySpec[] { perfQuerySpec });
    if (perfEntityMetricBases != null) {
        for (int i = 0; i < perfEntityMetricBases.length; i++) {
            PerfMetricSeries[] perfMetricSeries = ((PerfEntityMetric) perfEntityMetricBases[i]).getValue();
            for (int j = 0; perfMetricSeries != null && j < perfMetricSeries.length; j++) {
                if (perfMetricSeries[j] instanceof PerfMetricIntSeries) {
                    long[] longs = ((PerfMetricIntSeries) perfMetricSeries[j]).getValue();
                    if (longs.length == 1) {
                        PerfCounterInfo perfCounterInfo = getPerfCounterInfoMap().get(perfMetricSeries[j].getId().getCounterId());
                        String instance = perfMetricSeries[j].getId().getInstance();
                        String name = getHumanReadableName(perfCounterInfo);
                        if (instance != null && !"".equals(instance)) {
                            vmwarePerformanceValues.addValue(name, instance, longs[0]);
                        } else {
                            vmwarePerformanceValues.addValue(name, longs[0]);
                        }
                    }
                }
            }
        }
    }
    return vmwarePerformanceValues;
}
Also used : PerfMetricIntSeries(com.vmware.vim25.PerfMetricIntSeries) PerfCounterInfo(com.vmware.vim25.PerfCounterInfo) PerfMetricSeries(com.vmware.vim25.PerfMetricSeries) VmwarePerformanceValues(org.opennms.netmgt.collectd.vmware.vijava.VmwarePerformanceValues) PerfQuerySpec(com.vmware.vim25.PerfQuerySpec) PerfEntityMetricBase(com.vmware.vim25.PerfEntityMetricBase) PerfEntityMetric(com.vmware.vim25.PerfEntityMetric)

Example 5 with PerfMetricIntSeries

use of com.vmware.vim25.PerfMetricIntSeries in project cloudstack by apache.

the class VmwareResource method getVmStats.

private HashMap<String, VmStatsEntry> getVmStats(List<String> vmNames) throws Exception {
    VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
    HashMap<String, VmStatsEntry> vmResponseMap = new HashMap<String, VmStatsEntry>();
    ManagedObjectReference perfMgr = getServiceContext().getServiceContent().getPerfManager();
    VimPortType service = getServiceContext().getService();
    PerfCounterInfo rxPerfCounterInfo = null;
    PerfCounterInfo txPerfCounterInfo = null;
    List<PerfCounterInfo> cInfo = getServiceContext().getVimClient().getDynamicProperty(perfMgr, "perfCounter");
    for (PerfCounterInfo info : cInfo) {
        if ("net".equalsIgnoreCase(info.getGroupInfo().getKey())) {
            if ("transmitted".equalsIgnoreCase(info.getNameInfo().getKey())) {
                txPerfCounterInfo = info;
            }
            if ("received".equalsIgnoreCase(info.getNameInfo().getKey())) {
                rxPerfCounterInfo = info;
            }
        }
    }
    int key = ((HostMO) hyperHost).getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
    if (key == 0) {
        s_logger.warn("Custom field " + CustomFieldConstants.CLOUD_VM_INTERNAL_NAME + " is not registered ?!");
    }
    String instanceNameCustomField = "value[" + key + "]";
    final String numCpuStr = "summary.config.numCpu";
    final String cpuUseStr = "summary.quickStats.overallCpuUsage";
    final String guestMemUseStr = "summary.quickStats.guestMemoryUsage";
    final String memLimitStr = "resourceConfig.memoryAllocation.limit";
    final String memMbStr = "config.hardware.memoryMB";
    final String allocatedCpuStr = "summary.runtime.maxCpuUsage";
    ObjectContent[] ocs = hyperHost.getVmPropertiesOnHyperHost(new String[] { "name", numCpuStr, cpuUseStr, guestMemUseStr, memLimitStr, memMbStr, allocatedCpuStr, instanceNameCustomField });
    if (ocs != null && ocs.length > 0) {
        for (ObjectContent oc : ocs) {
            List<DynamicProperty> objProps = oc.getPropSet();
            if (objProps != null) {
                String name = null;
                String numberCPUs = null;
                double maxCpuUsage = 0;
                String memlimit = null;
                String memkb = null;
                String guestMemusage = null;
                String vmNameOnVcenter = null;
                String vmInternalCSName = null;
                double allocatedCpu = 0;
                for (DynamicProperty objProp : objProps) {
                    if (objProp.getName().equals("name")) {
                        vmNameOnVcenter = objProp.getVal().toString();
                    } else if (objProp.getName().contains(instanceNameCustomField)) {
                        if (objProp.getVal() != null)
                            vmInternalCSName = ((CustomFieldStringValue) objProp.getVal()).getValue();
                    } else if (objProp.getName().equals(guestMemusage)) {
                        guestMemusage = objProp.getVal().toString();
                    } else if (objProp.getName().equals(numCpuStr)) {
                        numberCPUs = objProp.getVal().toString();
                    } else if (objProp.getName().equals(cpuUseStr)) {
                        maxCpuUsage = NumberUtils.toDouble(objProp.getVal().toString());
                    } else if (objProp.getName().equals(memLimitStr)) {
                        memlimit = objProp.getVal().toString();
                    } else if (objProp.getName().equals(memMbStr)) {
                        memkb = objProp.getVal().toString();
                    } else if (objProp.getName().equals(allocatedCpuStr)) {
                        allocatedCpu = NumberUtils.toDouble(objProp.getVal().toString());
                    }
                }
                maxCpuUsage = (maxCpuUsage / allocatedCpu) * 100;
                new VirtualMachineMO(hyperHost.getContext(), oc.getObj());
                if (vmInternalCSName != null) {
                    name = vmInternalCSName;
                } else {
                    name = vmNameOnVcenter;
                }
                if (!vmNames.contains(name)) {
                    continue;
                }
                ManagedObjectReference vmMor = hyperHost.findVmOnHyperHost(name).getMor();
                assert (vmMor != null);
                ArrayList<PerfMetricId> vmNetworkMetrics = new ArrayList<PerfMetricId>();
                // get all the metrics from the available sample period
                List<PerfMetricId> perfMetrics = service.queryAvailablePerfMetric(perfMgr, vmMor, null, null, null);
                if (perfMetrics != null) {
                    for (int index = 0; index < perfMetrics.size(); ++index) {
                        if (((rxPerfCounterInfo != null) && (perfMetrics.get(index).getCounterId() == rxPerfCounterInfo.getKey())) || ((txPerfCounterInfo != null) && (perfMetrics.get(index).getCounterId() == txPerfCounterInfo.getKey()))) {
                            vmNetworkMetrics.add(perfMetrics.get(index));
                        }
                    }
                }
                double networkReadKBs = 0;
                double networkWriteKBs = 0;
                long sampleDuration = 0;
                if (vmNetworkMetrics.size() != 0) {
                    PerfQuerySpec qSpec = new PerfQuerySpec();
                    qSpec.setEntity(vmMor);
                    PerfMetricId[] availableMetricIds = vmNetworkMetrics.toArray(new PerfMetricId[0]);
                    qSpec.getMetricId().addAll(Arrays.asList(availableMetricIds));
                    List<PerfQuerySpec> qSpecs = new ArrayList<PerfQuerySpec>();
                    qSpecs.add(qSpec);
                    List<PerfEntityMetricBase> values = service.queryPerf(perfMgr, qSpecs);
                    for (int i = 0; i < values.size(); ++i) {
                        List<PerfSampleInfo> infos = ((PerfEntityMetric) values.get(i)).getSampleInfo();
                        if (infos != null && infos.size() > 0) {
                            int endMs = infos.get(infos.size() - 1).getTimestamp().getSecond() * 1000 + infos.get(infos.size() - 1).getTimestamp().getMillisecond();
                            int beginMs = infos.get(0).getTimestamp().getSecond() * 1000 + infos.get(0).getTimestamp().getMillisecond();
                            sampleDuration = (endMs - beginMs) / 1000;
                            List<PerfMetricSeries> vals = ((PerfEntityMetric) values.get(i)).getValue();
                            for (int vi = 0; ((vals != null) && (vi < vals.size())); ++vi) {
                                if (vals.get(vi) instanceof PerfMetricIntSeries) {
                                    PerfMetricIntSeries val = (PerfMetricIntSeries) vals.get(vi);
                                    List<Long> perfValues = val.getValue();
                                    Long sumRate = 0L;
                                    for (int j = 0; j < infos.size(); j++) {
                                        // Size of the array matches the size as the PerfSampleInfo
                                        sumRate += perfValues.get(j);
                                    }
                                    Long averageRate = sumRate / infos.size();
                                    if (vals.get(vi).getId().getCounterId() == rxPerfCounterInfo.getKey()) {
                                        //get the average RX rate multiplied by sampled duration
                                        networkReadKBs = sampleDuration * averageRate;
                                    }
                                    if (vals.get(vi).getId().getCounterId() == txPerfCounterInfo.getKey()) {
                                        //get the average TX rate multiplied by sampled duration
                                        networkWriteKBs = sampleDuration * averageRate;
                                    }
                                }
                            }
                        }
                    }
                }
                vmResponseMap.put(name, new VmStatsEntry(NumberUtils.toDouble(memkb) * 1024, NumberUtils.toDouble(guestMemusage) * 1024, NumberUtils.toDouble(memlimit) * 1024, maxCpuUsage, networkReadKBs, networkWriteKBs, NumberUtils.toInt(numberCPUs), "vm"));
            }
        }
    }
    return vmResponseMap;
}
Also used : PerfCounterInfo(com.vmware.vim25.PerfCounterInfo) DynamicProperty(com.vmware.vim25.DynamicProperty) HashMap(java.util.HashMap) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) ArrayList(java.util.ArrayList) VmStatsEntry(com.cloud.agent.api.VmStatsEntry) PerfEntityMetricBase(com.vmware.vim25.PerfEntityMetricBase) ObjectContent(com.vmware.vim25.ObjectContent) PerfMetricIntSeries(com.vmware.vim25.PerfMetricIntSeries) PerfMetricSeries(com.vmware.vim25.PerfMetricSeries) PerfMetricId(com.vmware.vim25.PerfMetricId) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) VimPortType(com.vmware.vim25.VimPortType) PerfSampleInfo(com.vmware.vim25.PerfSampleInfo) PerfQuerySpec(com.vmware.vim25.PerfQuerySpec) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) PerfEntityMetric(com.vmware.vim25.PerfEntityMetric)

Aggregations

PerfCounterInfo (com.vmware.vim25.PerfCounterInfo)5 PerfEntityMetric (com.vmware.vim25.PerfEntityMetric)5 PerfMetricIntSeries (com.vmware.vim25.PerfMetricIntSeries)5 PerfQuerySpec (com.vmware.vim25.PerfQuerySpec)4 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)3 PerfEntityMetricBase (com.vmware.vim25.PerfEntityMetricBase)3 PerfMetricSeries (com.vmware.vim25.PerfMetricSeries)3 PerfMetricId (com.vmware.vim25.PerfMetricId)2 VimPortType (com.vmware.vim25.VimPortType)2 ManagedEntity (com.vmware.vim25.mo.ManagedEntity)2 RemoteException (java.rmi.RemoteException)2 HashMap (java.util.HashMap)2 VmwarePerformanceValues (org.opennms.netmgt.collectd.vmware.vijava.VmwarePerformanceValues)2 VmStatsEntry (com.cloud.agent.api.VmStatsEntry)1 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)1 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)1 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)1 AboutInfo (com.vmware.vim25.AboutInfo)1 DynamicProperty (com.vmware.vim25.DynamicProperty)1 ElementDescription (com.vmware.vim25.ElementDescription)1