Search in sources :

Example 6 with VmDiskStatsEntry

use of com.cloud.agent.api.VmDiskStatsEntry in project cosmic by MissionCriticalCloud.

the class LibvirtGetVmDiskStatsCommandWrapper method execute.

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

Example 7 with VmDiskStatsEntry

use of com.cloud.agent.api.VmDiskStatsEntry in project cosmic by MissionCriticalCloud.

the class UserVmManagerImpl method collectVmDiskStatistics.

@Override
public void collectVmDiskStatistics(final UserVmVO userVm) {
    // support KVM only util 2013.06.25
    if (!userVm.getHypervisorType().equals(HypervisorType.KVM)) {
        return;
    }
    s_logger.debug("Collect vm disk statistics from host before stopping Vm");
    final long hostId = userVm.getHostId();
    final List<String> vmNames = new ArrayList<>();
    vmNames.add(userVm.getInstanceName());
    final HostVO host = _hostDao.findById(hostId);
    final GetVmDiskStatsAnswer diskStatsAnswer;
    try {
        diskStatsAnswer = (GetVmDiskStatsAnswer) _agentMgr.easySend(hostId, new GetVmDiskStatsCommand(vmNames, host.getGuid(), host.getName()));
    } catch (final Exception e) {
        s_logger.warn("Error while collecting disk stats for vm: " + userVm.getInstanceName() + " from host: " + host.getName(), e);
        return;
    }
    if (diskStatsAnswer != null) {
        if (!diskStatsAnswer.getResult()) {
            s_logger.warn("Error while collecting disk stats vm: " + userVm.getInstanceName() + " from host: " + host.getName() + "; details: " + diskStatsAnswer.getDetails());
            return;
        }
        try {
            final GetVmDiskStatsAnswer diskStatsAnswerFinal = diskStatsAnswer;
            Transaction.execute(new TransactionCallbackNoReturn() {

                @Override
                public void doInTransactionWithoutResult(final TransactionStatus status) {
                    final HashMap<String, List<VmDiskStatsEntry>> vmDiskStatsByName = diskStatsAnswerFinal.getVmDiskStatsMap();
                    if (vmDiskStatsByName == null) {
                        return;
                    }
                    final List<VmDiskStatsEntry> vmDiskStats = vmDiskStatsByName.get(userVm.getInstanceName());
                    if (vmDiskStats == null) {
                        return;
                    }
                    for (final VmDiskStatsEntry vmDiskStat : vmDiskStats) {
                        final SearchCriteria<VolumeVO> sc_volume = _volsDao.createSearchCriteria();
                        sc_volume.addAnd("path", SearchCriteria.Op.EQ, vmDiskStat.getPath());
                        final List<VolumeVO> volumes = _volsDao.search(sc_volume, null);
                        if (volumes == null || volumes.size() == 0) {
                            break;
                        }
                        final VolumeVO volume = volumes.get(0);
                        final VmDiskStatisticsVO previousVmDiskStats = _vmDiskStatsDao.findBy(userVm.getAccountId(), userVm.getDataCenterId(), userVm.getId(), volume.getId());
                        final VmDiskStatisticsVO vmDiskStat_lock = _vmDiskStatsDao.lock(userVm.getAccountId(), userVm.getDataCenterId(), userVm.getId(), volume.getId());
                        if (vmDiskStat.getIORead() == 0 && vmDiskStat.getIOWrite() == 0 && vmDiskStat.getBytesRead() == 0 && vmDiskStat.getBytesWrite() == 0) {
                            s_logger.debug("Read/Write of IO and Bytes are both 0. Not updating vm_disk_statistics");
                            continue;
                        }
                        if (vmDiskStat_lock == null) {
                            s_logger.warn("unable to find vm disk stats from host for account: " + userVm.getAccountId() + " with vmId: " + userVm.getId() + " and volumeId:" + volume.getId());
                            continue;
                        }
                        if (previousVmDiskStats != null && (previousVmDiskStats.getCurrentIORead() != vmDiskStat_lock.getCurrentIORead() || previousVmDiskStats.getCurrentIOWrite() != vmDiskStat_lock.getCurrentIOWrite() || previousVmDiskStats.getCurrentBytesRead() != vmDiskStat_lock.getCurrentBytesRead() || previousVmDiskStats.getCurrentBytesWrite() != vmDiskStat_lock.getCurrentBytesWrite())) {
                            s_logger.debug("vm disk stats changed from the time GetVmDiskStatsCommand was sent. " + "Ignoring current answer. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + " IO Read: " + vmDiskStat.getIORead() + " IO Write: " + vmDiskStat.getIOWrite() + " Bytes Read: " + vmDiskStat.getBytesRead() + " Bytes Write: " + vmDiskStat.getBytesWrite());
                            continue;
                        }
                        if (vmDiskStat_lock.getCurrentIORead() > vmDiskStat.getIORead()) {
                            if (s_logger.isDebugEnabled()) {
                                s_logger.debug("Read # of IO that's less than the last one.  " + "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + " Reported: " + vmDiskStat.getIORead() + " Stored: " + vmDiskStat_lock.getCurrentIORead());
                            }
                            vmDiskStat_lock.setNetIORead(vmDiskStat_lock.getNetIORead() + vmDiskStat_lock.getCurrentIORead());
                        }
                        vmDiskStat_lock.setCurrentIORead(vmDiskStat.getIORead());
                        if (vmDiskStat_lock.getCurrentIOWrite() > vmDiskStat.getIOWrite()) {
                            if (s_logger.isDebugEnabled()) {
                                s_logger.debug("Write # of IO that's less than the last one.  " + "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + " Reported: " + vmDiskStat.getIOWrite() + " Stored: " + vmDiskStat_lock.getCurrentIOWrite());
                            }
                            vmDiskStat_lock.setNetIOWrite(vmDiskStat_lock.getNetIOWrite() + vmDiskStat_lock.getCurrentIOWrite());
                        }
                        vmDiskStat_lock.setCurrentIOWrite(vmDiskStat.getIOWrite());
                        if (vmDiskStat_lock.getCurrentBytesRead() > vmDiskStat.getBytesRead()) {
                            if (s_logger.isDebugEnabled()) {
                                s_logger.debug("Read # of Bytes that's less than the last one.  " + "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + " Reported: " + vmDiskStat.getBytesRead() + " Stored: " + vmDiskStat_lock.getCurrentBytesRead());
                            }
                            vmDiskStat_lock.setNetBytesRead(vmDiskStat_lock.getNetBytesRead() + vmDiskStat_lock.getCurrentBytesRead());
                        }
                        vmDiskStat_lock.setCurrentBytesRead(vmDiskStat.getBytesRead());
                        if (vmDiskStat_lock.getCurrentBytesWrite() > vmDiskStat.getBytesWrite()) {
                            if (s_logger.isDebugEnabled()) {
                                s_logger.debug("Write # of Bytes that's less than the last one.  " + "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + " Reported: " + vmDiskStat.getBytesWrite() + " Stored: " + vmDiskStat_lock.getCurrentBytesWrite());
                            }
                            vmDiskStat_lock.setNetBytesWrite(vmDiskStat_lock.getNetBytesWrite() + vmDiskStat_lock.getCurrentBytesWrite());
                        }
                        vmDiskStat_lock.setCurrentBytesWrite(vmDiskStat.getBytesWrite());
                        if (!_dailyOrHourly) {
                            // update agg bytes
                            vmDiskStat_lock.setAggIORead(vmDiskStat_lock.getNetIORead() + vmDiskStat_lock.getCurrentIORead());
                            vmDiskStat_lock.setAggIOWrite(vmDiskStat_lock.getNetIOWrite() + vmDiskStat_lock.getCurrentIOWrite());
                            vmDiskStat_lock.setAggBytesRead(vmDiskStat_lock.getNetBytesRead() + vmDiskStat_lock.getCurrentBytesRead());
                            vmDiskStat_lock.setAggBytesWrite(vmDiskStat_lock.getNetBytesWrite() + vmDiskStat_lock.getCurrentBytesWrite());
                        }
                        _vmDiskStatsDao.update(vmDiskStat_lock.getId(), vmDiskStat_lock);
                    }
                }
            });
        } catch (final Exception e) {
            s_logger.warn("Unable to update vm disk statistics for vm: " + userVm.getId() + " from host: " + hostId, e);
        }
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VmDiskStatisticsVO(com.cloud.user.VmDiskStatisticsVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) VmDiskStatsEntry(com.cloud.agent.api.VmDiskStatsEntry) HostVO(com.cloud.host.HostVO) ExecutionException(com.cloud.utils.exception.ExecutionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CloudException(com.cloud.exception.CloudException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) ManagementServerException(com.cloud.exception.ManagementServerException) SearchCriteria(com.cloud.utils.db.SearchCriteria) GetVmDiskStatsAnswer(com.cloud.agent.api.GetVmDiskStatsAnswer) GetVmDiskStatsCommand(com.cloud.agent.api.GetVmDiskStatsCommand) VolumeVO(com.cloud.storage.VolumeVO) ArrayList(java.util.ArrayList) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) List(java.util.List)

Example 8 with VmDiskStatsEntry

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

the class UserVmManagerImpl method collectVmDiskStatistics.

@Override
public void collectVmDiskStatistics(final UserVm userVm) {
    // Only supported for KVM and VMware
    if (!(userVm.getHypervisorType().equals(HypervisorType.KVM) || userVm.getHypervisorType().equals(HypervisorType.VMware))) {
        return;
    }
    s_logger.debug("Collect vm disk statistics from host before stopping VM");
    if (userVm.getHostId() == null) {
        s_logger.error("Unable to collect vm disk statistics for VM as the host is null, skipping VM disk statistics collection");
        return;
    }
    long hostId = userVm.getHostId();
    List<String> vmNames = new ArrayList<String>();
    vmNames.add(userVm.getInstanceName());
    final HostVO host = _hostDao.findById(hostId);
    GetVmDiskStatsAnswer diskStatsAnswer = null;
    try {
        diskStatsAnswer = (GetVmDiskStatsAnswer) _agentMgr.easySend(hostId, new GetVmDiskStatsCommand(vmNames, host.getGuid(), host.getName()));
    } catch (Exception e) {
        s_logger.warn("Error while collecting disk stats for vm: " + userVm.getInstanceName() + " from host: " + host.getName(), e);
        return;
    }
    if (diskStatsAnswer != null) {
        if (!diskStatsAnswer.getResult()) {
            s_logger.warn("Error while collecting disk stats vm: " + userVm.getInstanceName() + " from host: " + host.getName() + "; details: " + diskStatsAnswer.getDetails());
            return;
        }
        try {
            final GetVmDiskStatsAnswer diskStatsAnswerFinal = diskStatsAnswer;
            Transaction.execute(new TransactionCallbackNoReturn() {

                @Override
                public void doInTransactionWithoutResult(TransactionStatus status) {
                    HashMap<String, List<VmDiskStatsEntry>> vmDiskStatsByName = diskStatsAnswerFinal.getVmDiskStatsMap();
                    if (vmDiskStatsByName == null) {
                        return;
                    }
                    List<VmDiskStatsEntry> vmDiskStats = vmDiskStatsByName.get(userVm.getInstanceName());
                    if (vmDiskStats == null) {
                        return;
                    }
                    for (VmDiskStatsEntry vmDiskStat : vmDiskStats) {
                        SearchCriteria<VolumeVO> sc_volume = _volsDao.createSearchCriteria();
                        sc_volume.addAnd("path", SearchCriteria.Op.EQ, vmDiskStat.getPath());
                        List<VolumeVO> volumes = _volsDao.search(sc_volume, null);
                        if ((volumes == null) || (volumes.size() == 0)) {
                            break;
                        }
                        VolumeVO volume = volumes.get(0);
                        VmDiskStatisticsVO previousVmDiskStats = _vmDiskStatsDao.findBy(userVm.getAccountId(), userVm.getDataCenterId(), userVm.getId(), volume.getId());
                        VmDiskStatisticsVO vmDiskStat_lock = _vmDiskStatsDao.lock(userVm.getAccountId(), userVm.getDataCenterId(), userVm.getId(), volume.getId());
                        if ((vmDiskStat.getIORead() == 0) && (vmDiskStat.getIOWrite() == 0) && (vmDiskStat.getBytesRead() == 0) && (vmDiskStat.getBytesWrite() == 0)) {
                            s_logger.debug("Read/Write of IO and Bytes are both 0. Not updating vm_disk_statistics");
                            continue;
                        }
                        if (vmDiskStat_lock == null) {
                            s_logger.warn("unable to find vm disk stats from host for account: " + userVm.getAccountId() + " with vmId: " + userVm.getId() + " and volumeId:" + volume.getId());
                            continue;
                        }
                        if (previousVmDiskStats != null && ((previousVmDiskStats.getCurrentIORead() != vmDiskStat_lock.getCurrentIORead()) || ((previousVmDiskStats.getCurrentIOWrite() != vmDiskStat_lock.getCurrentIOWrite()) || (previousVmDiskStats.getCurrentBytesRead() != vmDiskStat_lock.getCurrentBytesRead()) || (previousVmDiskStats.getCurrentBytesWrite() != vmDiskStat_lock.getCurrentBytesWrite())))) {
                            s_logger.debug("vm disk stats changed from the time GetVmDiskStatsCommand was sent. " + "Ignoring current answer. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + " IO Read: " + vmDiskStat.getIORead() + " IO Write: " + vmDiskStat.getIOWrite() + " Bytes Read: " + vmDiskStat.getBytesRead() + " Bytes Write: " + vmDiskStat.getBytesWrite());
                            continue;
                        }
                        if (vmDiskStat_lock.getCurrentIORead() > vmDiskStat.getIORead()) {
                            if (s_logger.isDebugEnabled()) {
                                s_logger.debug("Read # of IO that's less than the last one.  " + "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + " Reported: " + vmDiskStat.getIORead() + " Stored: " + vmDiskStat_lock.getCurrentIORead());
                            }
                            vmDiskStat_lock.setNetIORead(vmDiskStat_lock.getNetIORead() + vmDiskStat_lock.getCurrentIORead());
                        }
                        vmDiskStat_lock.setCurrentIORead(vmDiskStat.getIORead());
                        if (vmDiskStat_lock.getCurrentIOWrite() > vmDiskStat.getIOWrite()) {
                            if (s_logger.isDebugEnabled()) {
                                s_logger.debug("Write # of IO that's less than the last one.  " + "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + " Reported: " + vmDiskStat.getIOWrite() + " Stored: " + vmDiskStat_lock.getCurrentIOWrite());
                            }
                            vmDiskStat_lock.setNetIOWrite(vmDiskStat_lock.getNetIOWrite() + vmDiskStat_lock.getCurrentIOWrite());
                        }
                        vmDiskStat_lock.setCurrentIOWrite(vmDiskStat.getIOWrite());
                        if (vmDiskStat_lock.getCurrentBytesRead() > vmDiskStat.getBytesRead()) {
                            if (s_logger.isDebugEnabled()) {
                                s_logger.debug("Read # of Bytes that's less than the last one.  " + "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + " Reported: " + toHumanReadableSize(vmDiskStat.getBytesRead()) + " Stored: " + toHumanReadableSize(vmDiskStat_lock.getCurrentBytesRead()));
                            }
                            vmDiskStat_lock.setNetBytesRead(vmDiskStat_lock.getNetBytesRead() + vmDiskStat_lock.getCurrentBytesRead());
                        }
                        vmDiskStat_lock.setCurrentBytesRead(vmDiskStat.getBytesRead());
                        if (vmDiskStat_lock.getCurrentBytesWrite() > vmDiskStat.getBytesWrite()) {
                            if (s_logger.isDebugEnabled()) {
                                s_logger.debug("Write # of Bytes that's less than the last one.  " + "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + " Reported: " + toHumanReadableSize(vmDiskStat.getBytesWrite()) + " Stored: " + toHumanReadableSize(vmDiskStat_lock.getCurrentBytesWrite()));
                            }
                            vmDiskStat_lock.setNetBytesWrite(vmDiskStat_lock.getNetBytesWrite() + vmDiskStat_lock.getCurrentBytesWrite());
                        }
                        vmDiskStat_lock.setCurrentBytesWrite(vmDiskStat.getBytesWrite());
                        if (!_dailyOrHourly) {
                            // update agg bytes
                            vmDiskStat_lock.setAggIORead(vmDiskStat_lock.getNetIORead() + vmDiskStat_lock.getCurrentIORead());
                            vmDiskStat_lock.setAggIOWrite(vmDiskStat_lock.getNetIOWrite() + vmDiskStat_lock.getCurrentIOWrite());
                            vmDiskStat_lock.setAggBytesRead(vmDiskStat_lock.getNetBytesRead() + vmDiskStat_lock.getCurrentBytesRead());
                            vmDiskStat_lock.setAggBytesWrite(vmDiskStat_lock.getNetBytesWrite() + vmDiskStat_lock.getCurrentBytesWrite());
                        }
                        _vmDiskStatsDao.update(vmDiskStat_lock.getId(), vmDiskStat_lock);
                    }
                }
            });
        } catch (Exception e) {
            s_logger.warn(String.format("Unable to update VM disk statistics for %s from %s", userVm.getInstanceName(), host), e);
        }
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VmDiskStatisticsVO(com.cloud.user.VmDiskStatisticsVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) VmDiskStatsEntry(com.cloud.agent.api.VmDiskStatsEntry) HostVO(com.cloud.host.HostVO) ExecutionException(com.cloud.utils.exception.ExecutionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) InsufficientServerCapacityException(com.cloud.exception.InsufficientServerCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) IOException(java.io.IOException) UnsupportedServiceException(com.cloud.exception.UnsupportedServiceException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CloudException(com.cloud.exception.CloudException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) AffinityConflictException(com.cloud.exception.AffinityConflictException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ManagementServerException(com.cloud.exception.ManagementServerException) SearchCriteria(com.cloud.utils.db.SearchCriteria) GetVmDiskStatsAnswer(com.cloud.agent.api.GetVmDiskStatsAnswer) GetVmDiskStatsCommand(com.cloud.agent.api.GetVmDiskStatsCommand) VolumeVO(com.cloud.storage.VolumeVO) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) List(java.util.List)

Example 9 with VmDiskStatsEntry

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

the class UserVmManagerImpl method getVmDiskStatistics.

@Override
public HashMap<Long, List<VmDiskStatsEntry>> getVmDiskStatistics(long hostId, String hostName, List<Long> vmIds) throws CloudRuntimeException {
    HashMap<Long, List<VmDiskStatsEntry>> vmDiskStatsById = new HashMap<Long, List<VmDiskStatsEntry>>();
    if (vmIds.isEmpty()) {
        return vmDiskStatsById;
    }
    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 GetVmDiskStatsCommand(vmNames, _hostDao.findById(hostId).getGuid(), hostName));
    if (answer == null || !answer.getResult()) {
        s_logger.warn("Unable to obtain VM disk statistics.");
        return null;
    } else {
        HashMap<String, List<VmDiskStatsEntry>> vmDiskStatsByName = ((GetVmDiskStatsAnswer) answer).getVmDiskStatsMap();
        if (vmDiskStatsByName == null) {
            s_logger.warn("Unable to obtain VM disk statistics.");
            return null;
        }
        for (Map.Entry<String, List<VmDiskStatsEntry>> entry : vmDiskStatsByName.entrySet()) {
            vmDiskStatsById.put(vmIds.get(vmNames.indexOf(entry.getKey())), entry.getValue());
        }
    }
    return vmDiskStatsById;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VmDiskStatsEntry(com.cloud.agent.api.VmDiskStatsEntry) GetVmDiskStatsAnswer(com.cloud.agent.api.GetVmDiskStatsAnswer) GetVolumeStatsAnswer(com.cloud.agent.api.GetVolumeStatsAnswer) GetVmStatsAnswer(com.cloud.agent.api.GetVmStatsAnswer) GetVmNetworkStatsAnswer(com.cloud.agent.api.GetVmNetworkStatsAnswer) Answer(com.cloud.agent.api.Answer) StartAnswer(com.cloud.agent.api.StartAnswer) RestoreVMSnapshotAnswer(com.cloud.agent.api.RestoreVMSnapshotAnswer) GetVmDiskStatsAnswer(com.cloud.agent.api.GetVmDiskStatsAnswer) GetVmDiskStatsCommand(com.cloud.agent.api.GetVmDiskStatsCommand) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) List(java.util.List) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 10 with VmDiskStatsEntry

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

the class VmwareResource method execute.

protected Answer execute(GetVmDiskStatsCommand cmd) {
    try {
        final VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
        final ManagedObjectReference perfMgr = getServiceContext().getServiceContent().getPerfManager();
        VimPortType service = getServiceContext().getService();
        Integer windowInterval = getVmwareWindowTimeInterval();
        final XMLGregorianCalendar startTime = VmwareHelper.getXMLGregorianCalendar(new Date(), windowInterval);
        final XMLGregorianCalendar endTime = VmwareHelper.getXMLGregorianCalendar(new Date(), 0);
        PerfCounterInfo diskReadIOPerfCounterInfo = null;
        PerfCounterInfo diskWriteIOPerfCounterInfo = null;
        PerfCounterInfo diskReadKbsPerfCounterInfo = null;
        PerfCounterInfo diskWriteKbsPerfCounterInfo = null;
        // https://pubs.vmware.com/vsphere-5-5/topic/com.vmware.wssdk.apiref.doc/virtual_disk_counters.html
        List<PerfCounterInfo> cInfo = getServiceContext().getVimClient().getDynamicProperty(perfMgr, "perfCounter");
        for (PerfCounterInfo info : cInfo) {
            if ("virtualdisk".equalsIgnoreCase(info.getGroupInfo().getKey()) && "average".equalsIgnoreCase(info.getRollupType().value())) {
                if ("numberReadAveraged".equalsIgnoreCase(info.getNameInfo().getKey())) {
                    diskReadIOPerfCounterInfo = info;
                }
                if ("numberWriteAveraged".equalsIgnoreCase(info.getNameInfo().getKey())) {
                    diskWriteIOPerfCounterInfo = info;
                }
                if ("read".equalsIgnoreCase(info.getNameInfo().getKey())) {
                    diskReadKbsPerfCounterInfo = info;
                }
                if ("write".equalsIgnoreCase(info.getNameInfo().getKey())) {
                    diskWriteKbsPerfCounterInfo = info;
                }
            }
        }
        final ManagedObjectReference dcMor = hyperHost.getHyperHostDatacenter();
        final DatacenterMO dcMo = new DatacenterMO(getServiceContext(), dcMor);
        final HashMap<String, List<VmDiskStatsEntry>> vmStatsMap = new HashMap<>();
        for (final String vmName : cmd.getVmNames()) {
            final VirtualMachineMO vmMo = dcMo.findVm(vmName);
            final List<VmDiskStatsEntry> diskStats = new ArrayList<>();
            for (final VirtualDisk disk : vmMo.getAllDiskDevice()) {
                final String diskBusName = vmMo.getDeviceBusName(vmMo.getAllDeviceList(), disk);
                long readReq = 0;
                long readBytes = 0;
                long writeReq = 0;
                long writeBytes = 0;
                final ArrayList<PerfMetricId> perfMetricsIds = new ArrayList<PerfMetricId>();
                if (diskReadIOPerfCounterInfo != null) {
                    perfMetricsIds.add(VmwareHelper.createPerfMetricId(diskReadIOPerfCounterInfo, diskBusName));
                }
                if (diskWriteIOPerfCounterInfo != null) {
                    perfMetricsIds.add(VmwareHelper.createPerfMetricId(diskWriteIOPerfCounterInfo, diskBusName));
                }
                if (diskReadKbsPerfCounterInfo != null) {
                    perfMetricsIds.add(VmwareHelper.createPerfMetricId(diskReadKbsPerfCounterInfo, diskBusName));
                }
                if (diskWriteKbsPerfCounterInfo != null) {
                    perfMetricsIds.add(VmwareHelper.createPerfMetricId(diskWriteKbsPerfCounterInfo, diskBusName));
                }
                if (perfMetricsIds.size() > 0) {
                    try {
                        final PerfQuerySpec qSpec = new PerfQuerySpec();
                        qSpec.setEntity(vmMo.getMor());
                        qSpec.setFormat("normal");
                        qSpec.setIntervalId(windowInterval);
                        qSpec.setStartTime(startTime);
                        qSpec.setEndTime(endTime);
                        qSpec.getMetricId().addAll(perfMetricsIds);
                        for (final PerfEntityMetricBase perfValue : service.queryPerf(perfMgr, Collections.singletonList(qSpec))) {
                            if (!(perfValue instanceof PerfEntityMetric)) {
                                continue;
                            }
                            final List<PerfMetricSeries> values = ((PerfEntityMetric) perfValue).getValue();
                            if (values == null || values.isEmpty()) {
                                continue;
                            }
                            for (final PerfMetricSeries value : values) {
                                if (!(value instanceof PerfMetricIntSeries) || !value.getId().getInstance().equals(diskBusName)) {
                                    continue;
                                }
                                final List<Long> perfStats = ((PerfMetricIntSeries) value).getValue();
                                if (perfStats.size() > 0) {
                                    long sum = 0;
                                    for (long val : perfStats) {
                                        sum += val;
                                    }
                                    long avg = sum / perfStats.size();
                                    if (value.getId().getCounterId() == diskReadIOPerfCounterInfo.getKey()) {
                                        readReq = avg;
                                    } else if (value.getId().getCounterId() == diskWriteIOPerfCounterInfo.getKey()) {
                                        writeReq = avg;
                                    } else if (value.getId().getCounterId() == diskReadKbsPerfCounterInfo.getKey()) {
                                        readBytes = avg * 1024;
                                    } else if (value.getId().getCounterId() == diskWriteKbsPerfCounterInfo.getKey()) {
                                        writeBytes = avg * 1024;
                                    }
                                }
                            }
                        }
                    } catch (Exception e) {
                        s_logger.error(String.format("Unable to execute PerfQuerySpec due to: [%s]. The window interval is enabled in vCenter?", VmwareHelper.getExceptionMessage(e)), e);
                    }
                }
                diskStats.add(new VmDiskStatsEntry(vmName, VmwareHelper.getDiskDeviceFileName(disk), writeReq, readReq, writeBytes, readBytes));
            }
            if (CollectionUtils.isNotEmpty(diskStats)) {
                vmStatsMap.put(vmName, diskStats);
            }
        }
        s_logger.debug(String.format("VM Disks Maps is: [%s].", _gson.toJson(vmStatsMap)));
        if (MapUtils.isNotEmpty(vmStatsMap)) {
            return new GetVmDiskStatsAnswer(cmd, "", cmd.getHostName(), vmStatsMap);
        }
    } catch (Exception e) {
        s_logger.error(String.format("Unable to execute GetVmDiskStatsCommand due to [%s].", VmwareHelper.getExceptionMessage(e)), e);
    }
    return new GetVmDiskStatsAnswer(cmd, null, null, null);
}
Also used : PerfCounterInfo(com.vmware.vim25.PerfCounterInfo) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PerfEntityMetricBase(com.vmware.vim25.PerfEntityMetricBase) PerfMetricIntSeries(com.vmware.vim25.PerfMetricIntSeries) ArrayList(java.util.ArrayList) List(java.util.List) PerfMetricSeries(com.vmware.vim25.PerfMetricSeries) PerfMetricId(com.vmware.vim25.PerfMetricId) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) VmDiskStatsEntry(com.cloud.agent.api.VmDiskStatsEntry) VimPortType(com.vmware.vim25.VimPortType) Date(java.util.Date) VirtualDisk(com.vmware.vim25.VirtualDisk) ConnectException(java.net.ConnectException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) CloudException(com.cloud.exception.CloudException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException) GetVmDiskStatsAnswer(com.cloud.agent.api.GetVmDiskStatsAnswer) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) PerfQuerySpec(com.vmware.vim25.PerfQuerySpec) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO) PerfEntityMetric(com.vmware.vim25.PerfEntityMetric)

Aggregations

VmDiskStatsEntry (com.cloud.agent.api.VmDiskStatsEntry)10 GetVmDiskStatsAnswer (com.cloud.agent.api.GetVmDiskStatsAnswer)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 List (java.util.List)7 GetVmDiskStatsCommand (com.cloud.agent.api.GetVmDiskStatsCommand)4 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)4 CloudException (com.cloud.exception.CloudException)4 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)4 LinkedHashMap (java.util.LinkedHashMap)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 ConfigurationException (javax.naming.ConfigurationException)4 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)3 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)3 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)3 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)3 ManagementServerException (com.cloud.exception.ManagementServerException)3 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)3 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)3 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)3