Search in sources :

Example 11 with VmStatsEntry

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

the class UserVmManagerImpl method getVirtualMachineStatistics.

@Override
public HashMap<Long, VmStatsEntry> getVirtualMachineStatistics(long hostId, String hostName, List<Long> vmIds) throws CloudRuntimeException {
    HashMap<Long, VmStatsEntry> vmStatsById = new HashMap<Long, VmStatsEntry>();
    if (vmIds.isEmpty()) {
        return vmStatsById;
    }
    List<String> vmNames = new ArrayList<String>();
    for (Long vmId : vmIds) {
        UserVmVO vm = _vmDao.findById(vmId);
        vmNames.add(vm.getInstanceName());
    }
    Answer answer = _agentMgr.easySend(hostId, new GetVmStatsCommand(vmNames, _hostDao.findById(hostId).getGuid(), hostName));
    if (answer == null || !answer.getResult()) {
        s_logger.warn("Unable to obtain VM statistics.");
        return null;
    } else {
        HashMap<String, VmStatsEntry> vmStatsByName = ((GetVmStatsAnswer) answer).getVmStatsMap();
        if (vmStatsByName == null) {
            s_logger.warn("Unable to obtain VM statistics.");
            return null;
        }
        for (Map.Entry<String, VmStatsEntry> entry : vmStatsByName.entrySet()) {
            vmStatsById.put(vmIds.get(vmNames.indexOf(entry.getKey())), entry.getValue());
        }
    }
    return vmStatsById;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VmStatsEntry(com.cloud.agent.api.VmStatsEntry) GetVmStatsCommand(com.cloud.agent.api.GetVmStatsCommand) GetVmStatsAnswer(com.cloud.agent.api.GetVmStatsAnswer) Answer(com.cloud.agent.api.Answer) StartAnswer(com.cloud.agent.api.StartAnswer) RestoreVMSnapshotAnswer(com.cloud.agent.api.RestoreVMSnapshotAnswer) GetVmDiskStatsAnswer(com.cloud.agent.api.GetVmDiskStatsAnswer) GetVmStatsAnswer(com.cloud.agent.api.GetVmStatsAnswer) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 12 with VmStatsEntry

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

the class LibvirtGetVmStatsCommandWrapper method execute.

@Override
public Answer execute(final GetVmStatsCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final List<String> vmNames = command.getVmNames();
    try {
        final HashMap<String, VmStatsEntry> vmStatsNameMap = new HashMap<String, VmStatsEntry>();
        for (final String vmName : vmNames) {
            final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
            final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
            try {
                final VmStatsEntry statEntry = libvirtComputingResource.getVmStat(conn, vmName);
                if (statEntry == null) {
                    continue;
                }
                vmStatsNameMap.put(vmName, statEntry);
            } catch (LibvirtException e) {
                s_logger.warn("Can't get vm stats: " + e.toString() + ", continue");
            }
        }
        return new GetVmStatsAnswer(command, vmStatsNameMap);
    } catch (final LibvirtException e) {
        s_logger.debug("Can't get vm stats: " + e.toString());
        return new GetVmStatsAnswer(command, null);
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) HashMap(java.util.HashMap) Connect(org.libvirt.Connect) VmStatsEntry(com.cloud.agent.api.VmStatsEntry) GetVmStatsAnswer(com.cloud.agent.api.GetVmStatsAnswer)

Example 13 with VmStatsEntry

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

the class OvmResourceBase method getVmStat.

private VmStatsEntry getVmStat(String vmName) throws XmlRpcException {
    Map<String, String> vmStat = OvmVm.getVmStats(_conn, vmName);
    int nvcpus = Integer.parseInt(vmStat.get("cpuNum"));
    float cpuUtil = Float.parseFloat(vmStat.get("cpuUtil"));
    long rxBytes = Long.parseLong(vmStat.get("rxBytes"));
    long txBytes = Long.parseLong(vmStat.get("txBytes"));
    Pair<Long, Long> oldNetworkStat = _vmNetworkStats.get(vmName);
    long rx = rxBytes;
    long tx = txBytes;
    if (oldNetworkStat != null) {
        rx -= oldNetworkStat.first();
        tx -= oldNetworkStat.second();
        oldNetworkStat.set(rxBytes, txBytes);
    } else {
        oldNetworkStat = new Pair<Long, Long>(rx, tx);
    }
    _vmNetworkStats.put(vmName, oldNetworkStat);
    VmStatsEntry e = new VmStatsEntry();
    e.setCPUUtilization(cpuUtil);
    e.setNumCPUs(nvcpus);
    e.setNetworkReadKBs(rx);
    e.setNetworkWriteKBs(tx);
    e.setEntityType("vm");
    return e;
}
Also used : VmStatsEntry(com.cloud.agent.api.VmStatsEntry)

Example 14 with VmStatsEntry

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

the class OvmResourceBase method execute.

protected GetVmStatsAnswer execute(GetVmStatsCommand cmd) {
    List<String> vmNames = cmd.getVmNames();
    HashMap<String, VmStatsEntry> vmStatsNameMap = new HashMap<String, VmStatsEntry>();
    for (String vmName : vmNames) {
        try {
            VmStatsEntry e = getVmStat(vmName);
            vmStatsNameMap.put(vmName, e);
        } catch (XmlRpcException e) {
            s_logger.debug("Get vm stat for " + vmName + " failed", e);
            continue;
        }
    }
    return new GetVmStatsAnswer(cmd, vmStatsNameMap);
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) VmStatsEntry(com.cloud.agent.api.VmStatsEntry) GetVmStatsAnswer(com.cloud.agent.api.GetVmStatsAnswer) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 15 with VmStatsEntry

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

the class LibvirtComputingResource method getVmStat.

public VmStatsEntry getVmStat(final Connect conn, final String vmName) throws LibvirtException {
    Domain dm = null;
    try {
        dm = getDomain(conn, vmName);
        if (dm == null) {
            return null;
        }
        DomainInfo info = dm.getInfo();
        final VmStatsEntry stats = new VmStatsEntry();
        stats.setNumCPUs(info.nrVirtCpu);
        stats.setEntityType("vm");
        stats.setMemoryKBs(info.maxMem);
        stats.setTargetMemoryKBs(info.memory);
        stats.setIntFreeMemoryKBs(getMemoryFreeInKBs(dm));
        /* get cpu utilization */
        VmStats oldStats = null;
        final 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);
            final NodeInfo node = conn.nodeInfo();
            utilization = utilization / node.cpus;
            if (utilization > 0) {
                stats.setCPUUtilization(utilization * 100);
            }
        }
        /* get network stats */
        final List<InterfaceDef> vifs = getInterfaces(conn, vmName);
        long rx = 0;
        long tx = 0;
        for (final InterfaceDef vif : vifs) {
            final DomainInterfaceStats ifStats = dm.interfaceStats(vif.getDevName());
            rx += ifStats.rx_bytes;
            tx += ifStats.tx_bytes;
        }
        if (oldStats != null) {
            final double deltarx = rx - oldStats._rx;
            if (deltarx > 0) {
                stats.setNetworkReadKBs(deltarx / 1024);
            }
            final double deltatx = tx - oldStats._tx;
            if (deltatx > 0) {
                stats.setNetworkWriteKBs(deltatx / 1024);
            }
        }
        /* get disk stats */
        final List<DiskDef> disks = getDisks(conn, vmName);
        long io_rd = 0;
        long io_wr = 0;
        long bytes_rd = 0;
        long bytes_wr = 0;
        for (final DiskDef disk : disks) {
            final DomainBlockStats blockStats = dm.blockStats(disk.getDiskLabel());
            io_rd += blockStats.rd_req;
            io_wr += blockStats.wr_req;
            bytes_rd += blockStats.rd_bytes;
            bytes_wr += blockStats.wr_bytes;
        }
        if (oldStats != null) {
            final long deltaiord = io_rd - oldStats._ioRead;
            if (deltaiord > 0) {
                stats.setDiskReadIOs(deltaiord);
            }
            final long deltaiowr = io_wr - oldStats._ioWrote;
            if (deltaiowr > 0) {
                stats.setDiskWriteIOs(deltaiowr);
            }
            final double deltabytesrd = bytes_rd - oldStats._bytesRead;
            if (deltabytesrd > 0) {
                stats.setDiskReadKBs(deltabytesrd / 1024);
            }
            final double deltabyteswr = bytes_wr - oldStats._bytesWrote;
            if (deltabyteswr > 0) {
                stats.setDiskWriteKBs(deltabyteswr / 1024);
            }
        }
        /* save to Hashmap */
        final VmStats newStat = new VmStats();
        newStat._usedTime = info.cpuTime;
        newStat._rx = rx;
        newStat._tx = tx;
        newStat._ioRead = io_rd;
        newStat._ioWrote = io_wr;
        newStat._bytesRead = bytes_rd;
        newStat._bytesWrote = bytes_wr;
        newStat._timestamp = now;
        _vmStats.put(vmName, newStat);
        return stats;
    } finally {
        if (dm != null) {
            dm.free();
        }
    }
}
Also used : DomainInterfaceStats(org.libvirt.DomainInterfaceStats) Calendar(java.util.Calendar) VmStatsEntry(com.cloud.agent.api.VmStatsEntry) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) DiskDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef) NodeInfo(org.libvirt.NodeInfo) DomainBlockStats(org.libvirt.DomainBlockStats) DomainInfo(org.libvirt.DomainInfo) Domain(org.libvirt.Domain)

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