Search in sources :

Example 1 with VmStatsEntry

use of com.cloud.agent.api.VmStatsEntry in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method getVmStat.

private VmStatsEntry getVmStat(Connect conn, String vmName) throws LibvirtException {
    Domain dm = null;
    try {
        dm = getDomain(conn, vmName);
        DomainInfo info = dm.getInfo();
        VmStatsEntry stats = new VmStatsEntry();
        stats.setNumCPUs(info.nrVirtCpu);
        stats.setEntityType("vm");
        /* get cpu utilization */
        vmStats oldStats = null;
        Calendar now = Calendar.getInstance();
        oldStats = _vmStats.get(vmName);
        long elapsedTime = 0;
        if (oldStats != null) {
            elapsedTime = now.getTimeInMillis() - oldStats._timestamp.getTimeInMillis();
            double utilization = (info.cpuTime - oldStats._usedTime) / ((double) elapsedTime * 1000000);
            NodeInfo node = conn.nodeInfo();
            utilization = utilization / node.cpus;
            stats.setCPUUtilization(utilization * 100);
        }
        /* get network stats */
        List<InterfaceDef> vifs = getInterfaces(conn, vmName);
        long rx = 0;
        long tx = 0;
        for (InterfaceDef vif : vifs) {
            DomainInterfaceStats ifStats = dm.interfaceStats(vif.getDevName());
            rx += ifStats.rx_bytes;
            tx += ifStats.tx_bytes;
        }
        if (oldStats != null) {
            long deltarx = rx - oldStats._rx;
            if (deltarx > 0)
                stats.setNetworkReadKBs(deltarx / 1000);
            long deltatx = tx - oldStats._tx;
            if (deltatx > 0)
                stats.setNetworkWriteKBs(deltatx / 1000);
        }
        vmStats newStat = new vmStats();
        newStat._usedTime = info.cpuTime;
        newStat._rx = rx;
        newStat._tx = tx;
        newStat._timestamp = now;
        _vmStats.put(vmName, newStat);
        return stats;
    } finally {
        if (dm != null) {
            dm.free();
        }
    }
}
Also used : InterfaceDef(com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef) NodeInfo(org.libvirt.NodeInfo) DomainInterfaceStats(org.libvirt.DomainInterfaceStats) Calendar(java.util.Calendar) DomainInfo(org.libvirt.DomainInfo) VmStatsEntry(com.cloud.agent.api.VmStatsEntry) Domain(org.libvirt.Domain)

Example 2 with VmStatsEntry

use of com.cloud.agent.api.VmStatsEntry in project cloudstack by apache.

the class CitrixResourceBase method getVmStats.

public HashMap<String, VmStatsEntry> getVmStats(final Connection conn, final GetVmStatsCommand cmd, final List<String> vmUUIDs, final String hostGuid) {
    final HashMap<String, VmStatsEntry> vmResponseMap = new HashMap<String, VmStatsEntry>();
    for (final String vmUUID : vmUUIDs) {
        vmResponseMap.put(vmUUID, new VmStatsEntry(0, 0, 0, 0, 0, 0, 0, "vm"));
    }
    // call rrddata with 2 for
    final Object[] rrdData = getRRDData(conn, 2);
    if (rrdData == null) {
        return null;
    }
    final Integer numRows = (Integer) rrdData[0];
    final Integer numColumns = (Integer) rrdData[1];
    final Node legend = (Node) rrdData[2];
    final Node dataNode = (Node) rrdData[3];
    final NodeList legendChildren = legend.getChildNodes();
    for (int col = 0; col < numColumns; col++) {
        if (legendChildren == null || legendChildren.item(col) == null) {
            continue;
        }
        final String columnMetadata = getXMLNodeValue(legendChildren.item(col));
        if (columnMetadata == null) {
            continue;
        }
        final String[] columnMetadataList = columnMetadata.split(":");
        if (columnMetadataList.length != 4) {
            continue;
        }
        final String type = columnMetadataList[1];
        final String uuid = columnMetadataList[2];
        final String param = columnMetadataList[3];
        if (type.equals("vm") && vmResponseMap.keySet().contains(uuid)) {
            final VmStatsEntry vmStatsAnswer = vmResponseMap.get(uuid);
            vmStatsAnswer.setEntityType("vm");
            if (param.contains("cpu")) {
                vmStatsAnswer.setNumCPUs(vmStatsAnswer.getNumCPUs() + 1);
                vmStatsAnswer.setCPUUtilization(vmStatsAnswer.getCPUUtilization() + getDataAverage(dataNode, col, numRows));
            } else if (param.matches("vif_\\d*_rx")) {
                vmStatsAnswer.setNetworkReadKBs(vmStatsAnswer.getNetworkReadKBs() + getDataAverage(dataNode, col, numRows) / BASE_TO_CONVERT_BYTES_INTO_KILOBYTES);
            } else if (param.matches("vif_\\d*_tx")) {
                vmStatsAnswer.setNetworkWriteKBs(vmStatsAnswer.getNetworkWriteKBs() + getDataAverage(dataNode, col, numRows) / BASE_TO_CONVERT_BYTES_INTO_KILOBYTES);
            } else if (param.matches("vbd_.*_read")) {
                vmStatsAnswer.setDiskReadKBs(vmStatsAnswer.getDiskReadKBs() + getDataAverage(dataNode, col, numRows) / BASE_TO_CONVERT_BYTES_INTO_KILOBYTES);
            } else if (param.matches("vbd_.*_write")) {
                vmStatsAnswer.setDiskWriteKBs(vmStatsAnswer.getDiskWriteKBs() + getDataAverage(dataNode, col, numRows) / BASE_TO_CONVERT_BYTES_INTO_KILOBYTES);
            } else if (param.contains("memory_internal_free")) {
                vmStatsAnswer.setIntFreeMemoryKBs(vmStatsAnswer.getIntFreeMemoryKBs() + getDataAverage(dataNode, col, numRows) / BASE_TO_CONVERT_BYTES_INTO_KILOBYTES);
            } else if (param.contains("memory_target")) {
                vmStatsAnswer.setTargetMemoryKBs(vmStatsAnswer.getTargetMemoryKBs() + getDataAverage(dataNode, col, numRows) / BASE_TO_CONVERT_BYTES_INTO_KILOBYTES);
            } else if (param.contains("memory")) {
                vmStatsAnswer.setMemoryKBs(vmStatsAnswer.getMemoryKBs() + getDataAverage(dataNode, col, numRows) / BASE_TO_CONVERT_BYTES_INTO_KILOBYTES);
            }
        }
    }
    for (final Map.Entry<String, VmStatsEntry> entry : vmResponseMap.entrySet()) {
        final VmStatsEntry vmStatsAnswer = entry.getValue();
        if (vmStatsAnswer.getNumCPUs() != 0) {
            vmStatsAnswer.setCPUUtilization(vmStatsAnswer.getCPUUtilization() / vmStatsAnswer.getNumCPUs());
        }
        vmStatsAnswer.setCPUUtilization(vmStatsAnswer.getCPUUtilization() * 100);
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Vm cpu utilization " + vmStatsAnswer.getCPUUtilization());
        }
    }
    return vmResponseMap;
}
Also used : HashMap(java.util.HashMap) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) VmStatsEntry(com.cloud.agent.api.VmStatsEntry) XenAPIObject(com.xensource.xenapi.XenAPIObject) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with VmStatsEntry

use of com.cloud.agent.api.VmStatsEntry in project cloudstack by apache.

the class LibvirtComputingResourceTest method testGetVmStat.

@Test
public void testGetVmStat() throws LibvirtException {
    final Connect connect = Mockito.mock(Connect.class);
    final Domain domain = Mockito.mock(Domain.class);
    final DomainInfo domainInfo = new DomainInfo();
    final MemoryStatistic[] domainMem = new MemoryStatistic[2];
    domainMem[0] = Mockito.mock(MemoryStatistic.class);
    Mockito.when(domain.getInfo()).thenReturn(domainInfo);
    Mockito.when(domain.memoryStats(2)).thenReturn(domainMem);
    Mockito.when(connect.domainLookupByName(VMNAME)).thenReturn(domain);
    final NodeInfo nodeInfo = new NodeInfo();
    nodeInfo.cpus = 8;
    nodeInfo.memory = 8 * 1024 * 1024;
    nodeInfo.sockets = 2;
    nodeInfo.threads = 2;
    nodeInfo.model = "Foo processor";
    Mockito.when(connect.nodeInfo()).thenReturn(nodeInfo);
    // this is testing the interface stats, returns an increasing number of sent and received bytes
    Mockito.when(domain.interfaceStats(Matchers.anyString())).thenAnswer(new org.mockito.stubbing.Answer<DomainInterfaceStats>() {

        // increment with less than a KB, so this should be less than 1 KB
        static final int increment = 1000;

        int rxBytes = 1000;

        int txBytes = 1000;

        @Override
        public DomainInterfaceStats answer(final InvocationOnMock invocation) throws Throwable {
            final DomainInterfaceStats domainInterfaceStats = new DomainInterfaceStats();
            domainInterfaceStats.rx_bytes = rxBytes += increment;
            domainInterfaceStats.tx_bytes = txBytes += increment;
            return domainInterfaceStats;
        }
    });
    Mockito.when(domain.blockStats(Matchers.anyString())).thenAnswer(new org.mockito.stubbing.Answer<DomainBlockStats>() {

        // a little less than a KB
        static final int increment = 1000;

        int rdBytes = 0;

        int wrBytes = 1024;

        @Override
        public DomainBlockStats answer(final InvocationOnMock invocation) throws Throwable {
            final DomainBlockStats domainBlockStats = new DomainBlockStats();
            domainBlockStats.rd_bytes = rdBytes += increment;
            domainBlockStats.wr_bytes = wrBytes += increment;
            return domainBlockStats;
        }
    });
    final LibvirtComputingResource libvirtComputingResource = new LibvirtComputingResource() {

        @Override
        public List<InterfaceDef> getInterfaces(final Connect conn, final String vmName) {
            final InterfaceDef interfaceDef = new InterfaceDef();
            return Arrays.asList(interfaceDef);
        }

        @Override
        public List<DiskDef> getDisks(final Connect conn, final String vmName) {
            final DiskDef diskDef = new DiskDef();
            return Arrays.asList(diskDef);
        }
    };
    libvirtComputingResource.getVmStat(connect, VMNAME);
    final VmStatsEntry vmStat = libvirtComputingResource.getVmStat(connect, VMNAME);
    // network traffic as generated by the logic above, must be greater than zero
    Assert.assertTrue(vmStat.getNetworkReadKBs() > 0);
    Assert.assertTrue(vmStat.getNetworkWriteKBs() > 0);
    // IO traffic as generated by the logic above, must be greater than zero
    Assert.assertTrue(vmStat.getDiskReadKBs() > 0);
    Assert.assertTrue(vmStat.getDiskWriteKBs() > 0);
    // Memory limit of VM must be greater than zero
    Assert.assertTrue(vmStat.getIntFreeMemoryKBs() >= 0);
    Assert.assertTrue(vmStat.getMemoryKBs() >= 0);
    Assert.assertTrue(vmStat.getTargetMemoryKBs() >= vmStat.getMemoryKBs());
}
Also used : DomainInterfaceStats(org.libvirt.DomainInterfaceStats) Connect(org.libvirt.Connect) VmStatsEntry(com.cloud.agent.api.VmStatsEntry) MemoryStatistic(org.libvirt.MemoryStatistic) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) DiskDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef) NodeInfo(org.libvirt.NodeInfo) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DomainBlockStats(org.libvirt.DomainBlockStats) DomainInfo(org.libvirt.DomainInfo) Domain(org.libvirt.Domain) Test(org.junit.Test)

Example 4 with VmStatsEntry

use of com.cloud.agent.api.VmStatsEntry in project cloudstack by apache.

the class CitrixGetVmStatsCommandWrapper method execute.

@Override
public Answer execute(final GetVmStatsCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final List<String> vmNames = command.getVmNames();
    final HashMap<String, VmStatsEntry> vmStatsNameMap = new HashMap<String, VmStatsEntry>();
    if (vmNames.size() == 0) {
        return new GetVmStatsAnswer(command, vmStatsNameMap);
    }
    try {
        // Determine the UUIDs of the requested VMs
        final List<String> vmUUIDs = new ArrayList<String>();
        for (final String vmName : vmNames) {
            final VM vm = citrixResourceBase.getVM(conn, vmName);
            vmUUIDs.add(vm.getUuid(conn));
        }
        final HashMap<String, VmStatsEntry> vmStatsUUIDMap = citrixResourceBase.getVmStats(conn, command, vmUUIDs, command.getHostGuid());
        if (vmStatsUUIDMap == null) {
            return new GetVmStatsAnswer(command, vmStatsNameMap);
        }
        for (final Map.Entry<String, VmStatsEntry> entry : vmStatsUUIDMap.entrySet()) {
            vmStatsNameMap.put(vmNames.get(vmUUIDs.indexOf(entry.getKey())), entry.getValue());
        }
        return new GetVmStatsAnswer(command, vmStatsNameMap);
    } catch (final XenAPIException e) {
        final String msg = "Unable to get VM stats" + e.toString();
        s_logger.warn(msg, e);
        return new GetVmStatsAnswer(command, vmStatsNameMap);
    } catch (final XmlRpcException e) {
        final String msg = "Unable to get VM stats" + e.getMessage();
        s_logger.warn(msg, e);
        return new GetVmStatsAnswer(command, vmStatsNameMap);
    }
}
Also used : HashMap(java.util.HashMap) Connection(com.xensource.xenapi.Connection) ArrayList(java.util.ArrayList) XenAPIException(com.xensource.xenapi.Types.XenAPIException) VmStatsEntry(com.cloud.agent.api.VmStatsEntry) VM(com.xensource.xenapi.VM) GetVmStatsAnswer(com.cloud.agent.api.GetVmStatsAnswer) HashMap(java.util.HashMap) Map(java.util.Map) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 5 with VmStatsEntry

use of com.cloud.agent.api.VmStatsEntry in project cloudstack by apache.

the class Ovm3VmSupport method getVmStat.

private VmStatsEntry getVmStat(String vmName) {
    CloudstackPlugin cSp = new CloudstackPlugin(c);
    Map<String, String> oldVmStats = null;
    Map<String, String> newVmStats = null;
    VmStatsEntry stats = new VmStatsEntry();
    try {
        if (vmStats.containsKey(vmName)) {
            oldVmStats = new HashMap<String, String>();
            oldVmStats.putAll(vmStats.get(vmName));
        }
        newVmStats = cSp.ovsDomUStats(vmName);
    } catch (Ovm3ResourceException e) {
        LOGGER.info("Unable to retrieve stats from " + vmName, e);
        return stats;
    }
    if (oldVmStats == null) {
        LOGGER.debug("No old stats retrieved stats from " + vmName);
        stats.setNumCPUs(1);
        stats.setNetworkReadKBs(0);
        stats.setNetworkWriteKBs(0);
        stats.setDiskReadKBs(0);
        stats.setDiskWriteKBs(0);
        stats.setDiskReadIOs(0);
        stats.setDiskWriteIOs(0);
        stats.setCPUUtilization(0);
        stats.setEntityType("vm");
    } else {
        LOGGER.debug("Retrieved new stats from " + vmName);
        int cpus = Integer.parseInt(newVmStats.get("vcpus"));
        stats.setNumCPUs(cpus);
        stats.setNetworkReadKBs(doubleMin(newVmStats.get("rx_bytes"), oldVmStats.get("rx_bytes")));
        stats.setNetworkWriteKBs(doubleMin(newVmStats.get("tx_bytes"), oldVmStats.get("tx_bytes")));
        stats.setDiskReadKBs(doubleMin(newVmStats.get("rd_bytes"), oldVmStats.get("rd_bytes")));
        stats.setDiskWriteKBs(doubleMin(newVmStats.get("rw_bytes"), oldVmStats.get("rw_bytes")));
        stats.setDiskReadIOs(doubleMin(newVmStats.get("rd_ops"), oldVmStats.get("rd_ops")));
        stats.setDiskWriteIOs(doubleMin(newVmStats.get("rw_ops"), oldVmStats.get("rw_ops")));
        Double dCpu = doubleMin(newVmStats.get("cputime"), oldVmStats.get("cputime"));
        Double dTime = doubleMin(newVmStats.get("uptime"), oldVmStats.get("uptime"));
        Double cpupct = dCpu / dTime * 100 * cpus;
        stats.setCPUUtilization(cpupct);
        stats.setEntityType("vm");
    }
    ((ConcurrentHashMap<String, Map<String, String>>) vmStats).put(vmName, newVmStats);
    return stats;
}
Also used : Ovm3ResourceException(com.cloud.hypervisor.ovm3.objects.Ovm3ResourceException) VmStatsEntry(com.cloud.agent.api.VmStatsEntry) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CloudstackPlugin(com.cloud.hypervisor.ovm3.objects.CloudstackPlugin)

Aggregations

VmStatsEntry (com.cloud.agent.api.VmStatsEntry)16 HashMap (java.util.HashMap)10 GetVmStatsAnswer (com.cloud.agent.api.GetVmStatsAnswer)9 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)7 ArrayList (java.util.ArrayList)4 Connect (org.libvirt.Connect)3 Answer (com.cloud.agent.api.Answer)2 GetVmDiskStatsAnswer (com.cloud.agent.api.GetVmDiskStatsAnswer)2 StartAnswer (com.cloud.agent.api.StartAnswer)2 Map (java.util.Map)2 Domain (org.libvirt.Domain)2 DomainInfo (org.libvirt.DomainInfo)2 DomainInterfaceStats (org.libvirt.DomainInterfaceStats)2 LibvirtException (org.libvirt.LibvirtException)2 NodeInfo (org.libvirt.NodeInfo)2 BackupSnapshotAnswer (com.cloud.agent.api.BackupSnapshotAnswer)1 CheckHealthAnswer (com.cloud.agent.api.CheckHealthAnswer)1 CheckNetworkAnswer (com.cloud.agent.api.CheckNetworkAnswer)1 CheckOnHostAnswer (com.cloud.agent.api.CheckOnHostAnswer)1 CheckVirtualMachineAnswer (com.cloud.agent.api.CheckVirtualMachineAnswer)1