Search in sources :

Example 1 with GetVmNetworkStatsAnswer

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

the class LibvirtGetVmNetworkStatsCommandWrapper method execute.

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

Example 2 with GetVmNetworkStatsAnswer

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

the class UserVmManagerImpl method collectVmNetworkStatistics.

@Override
public void collectVmNetworkStatistics(final UserVm userVm) {
    if (!userVm.getHypervisorType().equals(HypervisorType.KVM)) {
        return;
    }
    s_logger.debug("Collect vm network statistics from host before stopping Vm");
    long hostId = userVm.getHostId();
    List<String> vmNames = new ArrayList<String>();
    vmNames.add(userVm.getInstanceName());
    final HostVO host = _hostDao.findById(hostId);
    GetVmNetworkStatsAnswer networkStatsAnswer = null;
    try {
        networkStatsAnswer = (GetVmNetworkStatsAnswer) _agentMgr.easySend(hostId, new GetVmNetworkStatsCommand(vmNames, host.getGuid(), host.getName()));
    } catch (Exception e) {
        s_logger.warn("Error while collecting network stats for vm: " + userVm.getHostName() + " from host: " + host.getName(), e);
        return;
    }
    if (networkStatsAnswer != null) {
        if (!networkStatsAnswer.getResult()) {
            s_logger.warn("Error while collecting network stats vm: " + userVm.getHostName() + " from host: " + host.getName() + "; details: " + networkStatsAnswer.getDetails());
            return;
        }
        try {
            final GetVmNetworkStatsAnswer networkStatsAnswerFinal = networkStatsAnswer;
            Transaction.execute(new TransactionCallbackNoReturn() {

                @Override
                public void doInTransactionWithoutResult(TransactionStatus status) {
                    HashMap<String, List<VmNetworkStatsEntry>> vmNetworkStatsByName = networkStatsAnswerFinal.getVmNetworkStatsMap();
                    if (vmNetworkStatsByName == null) {
                        return;
                    }
                    List<VmNetworkStatsEntry> vmNetworkStats = vmNetworkStatsByName.get(userVm.getInstanceName());
                    if (vmNetworkStats == null) {
                        return;
                    }
                    for (VmNetworkStatsEntry vmNetworkStat : vmNetworkStats) {
                        SearchCriteria<NicVO> sc_nic = _nicDao.createSearchCriteria();
                        sc_nic.addAnd("macAddress", SearchCriteria.Op.EQ, vmNetworkStat.getMacAddress());
                        NicVO nic = _nicDao.search(sc_nic, null).get(0);
                        List<VlanVO> vlan = _vlanDao.listVlansByNetworkId(nic.getNetworkId());
                        if (vlan == null || vlan.size() == 0 || vlan.get(0).getVlanType() != VlanType.DirectAttached) {
                            // only get network statistics for DirectAttached network (shared networks in Basic zone and Advanced zone with/without SG)
                            break;
                        }
                        UserStatisticsVO previousvmNetworkStats = _userStatsDao.findBy(userVm.getAccountId(), userVm.getDataCenterId(), nic.getNetworkId(), nic.getIPv4Address(), userVm.getId(), "UserVm");
                        if (previousvmNetworkStats == null) {
                            previousvmNetworkStats = new UserStatisticsVO(userVm.getAccountId(), userVm.getDataCenterId(), nic.getIPv4Address(), userVm.getId(), "UserVm", nic.getNetworkId());
                            _userStatsDao.persist(previousvmNetworkStats);
                        }
                        UserStatisticsVO vmNetworkStat_lock = _userStatsDao.lock(userVm.getAccountId(), userVm.getDataCenterId(), nic.getNetworkId(), nic.getIPv4Address(), userVm.getId(), "UserVm");
                        if ((vmNetworkStat.getBytesSent() == 0) && (vmNetworkStat.getBytesReceived() == 0)) {
                            s_logger.debug("bytes sent and received are all 0. Not updating user_statistics");
                            continue;
                        }
                        if (vmNetworkStat_lock == null) {
                            s_logger.warn("unable to find vm network stats from host for account: " + userVm.getAccountId() + " with vmId: " + userVm.getId() + " and nicId:" + nic.getId());
                            continue;
                        }
                        if (previousvmNetworkStats != null && ((previousvmNetworkStats.getCurrentBytesSent() != vmNetworkStat_lock.getCurrentBytesSent()) || (previousvmNetworkStats.getCurrentBytesReceived() != vmNetworkStat_lock.getCurrentBytesReceived()))) {
                            s_logger.debug("vm network stats changed from the time GetNmNetworkStatsCommand was sent. " + "Ignoring current answer. Host: " + host.getName() + " . VM: " + vmNetworkStat.getVmName() + " Sent(Bytes): " + toHumanReadableSize(vmNetworkStat.getBytesSent()) + " Received(Bytes): " + toHumanReadableSize(vmNetworkStat.getBytesReceived()));
                            continue;
                        }
                        if (vmNetworkStat_lock.getCurrentBytesSent() > vmNetworkStat.getBytesSent()) {
                            if (s_logger.isDebugEnabled()) {
                                s_logger.debug("Sent # of bytes that's less than the last one.  " + "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmNetworkStat.getVmName() + " Reported: " + toHumanReadableSize(vmNetworkStat.getBytesSent()) + " Stored: " + toHumanReadableSize(vmNetworkStat_lock.getCurrentBytesSent()));
                            }
                            vmNetworkStat_lock.setNetBytesSent(vmNetworkStat_lock.getNetBytesSent() + vmNetworkStat_lock.getCurrentBytesSent());
                        }
                        vmNetworkStat_lock.setCurrentBytesSent(vmNetworkStat.getBytesSent());
                        if (vmNetworkStat_lock.getCurrentBytesReceived() > vmNetworkStat.getBytesReceived()) {
                            if (s_logger.isDebugEnabled()) {
                                s_logger.debug("Received # of bytes that's less than the last one.  " + "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmNetworkStat.getVmName() + " Reported: " + toHumanReadableSize(vmNetworkStat.getBytesReceived()) + " Stored: " + toHumanReadableSize(vmNetworkStat_lock.getCurrentBytesReceived()));
                            }
                            vmNetworkStat_lock.setNetBytesReceived(vmNetworkStat_lock.getNetBytesReceived() + vmNetworkStat_lock.getCurrentBytesReceived());
                        }
                        vmNetworkStat_lock.setCurrentBytesReceived(vmNetworkStat.getBytesReceived());
                        if (!_dailyOrHourly) {
                            // update agg bytes
                            vmNetworkStat_lock.setAggBytesReceived(vmNetworkStat_lock.getNetBytesReceived() + vmNetworkStat_lock.getCurrentBytesReceived());
                            vmNetworkStat_lock.setAggBytesSent(vmNetworkStat_lock.getNetBytesSent() + vmNetworkStat_lock.getCurrentBytesSent());
                        }
                        _userStatsDao.update(vmNetworkStat_lock.getId(), vmNetworkStat_lock);
                    }
                }
            });
        } catch (Exception e) {
            s_logger.warn("Unable to update vm network 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) TransactionStatus(com.cloud.utils.db.TransactionStatus) VmNetworkStatsEntry(com.cloud.agent.api.VmNetworkStatsEntry) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) 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) GetVmNetworkStatsCommand(com.cloud.agent.api.GetVmNetworkStatsCommand) GetVmNetworkStatsAnswer(com.cloud.agent.api.GetVmNetworkStatsAnswer) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) List(java.util.List) UserStatisticsVO(com.cloud.user.UserStatisticsVO)

Example 3 with GetVmNetworkStatsAnswer

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

the class UserVmManagerImpl method getVmNetworkStatistics.

@Override
public HashMap<Long, List<VmNetworkStatsEntry>> getVmNetworkStatistics(long hostId, String hostName, List<Long> vmIds) {
    HashMap<Long, List<VmNetworkStatsEntry>> vmNetworkStatsById = new HashMap<Long, List<VmNetworkStatsEntry>>();
    if (vmIds.isEmpty()) {
        return vmNetworkStatsById;
    }
    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 GetVmNetworkStatsCommand(vmNames, _hostDao.findById(hostId).getGuid(), hostName));
    if (answer == null || !answer.getResult()) {
        s_logger.warn("Unable to obtain VM network statistics.");
        return null;
    } else {
        HashMap<String, List<VmNetworkStatsEntry>> vmNetworkStatsByName = ((GetVmNetworkStatsAnswer) answer).getVmNetworkStatsMap();
        if (vmNetworkStatsByName == null) {
            s_logger.warn("Unable to obtain VM network statistics.");
            return null;
        }
        for (String vmName : vmNetworkStatsByName.keySet()) {
            vmNetworkStatsById.put(vmIds.get(vmNames.indexOf(vmName)), vmNetworkStatsByName.get(vmName));
        }
    }
    return vmNetworkStatsById;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VmNetworkStatsEntry(com.cloud.agent.api.VmNetworkStatsEntry) GetVmNetworkStatsCommand(com.cloud.agent.api.GetVmNetworkStatsCommand) 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) GetVmNetworkStatsAnswer(com.cloud.agent.api.GetVmNetworkStatsAnswer) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) List(java.util.List)

Aggregations

GetVmNetworkStatsAnswer (com.cloud.agent.api.GetVmNetworkStatsAnswer)3 VmNetworkStatsEntry (com.cloud.agent.api.VmNetworkStatsEntry)3 HashMap (java.util.HashMap)3 List (java.util.List)3 GetVmNetworkStatsCommand (com.cloud.agent.api.GetVmNetworkStatsCommand)2 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Answer (com.cloud.agent.api.Answer)1 GetVmDiskStatsAnswer (com.cloud.agent.api.GetVmDiskStatsAnswer)1 GetVmStatsAnswer (com.cloud.agent.api.GetVmStatsAnswer)1 GetVolumeStatsAnswer (com.cloud.agent.api.GetVolumeStatsAnswer)1 RestoreVMSnapshotAnswer (com.cloud.agent.api.RestoreVMSnapshotAnswer)1 StartAnswer (com.cloud.agent.api.StartAnswer)1 AffinityConflictException (com.cloud.exception.AffinityConflictException)1 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)1 CloudException (com.cloud.exception.CloudException)1 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)1 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)1