Search in sources :

Example 1 with NetworkUsageAnswer

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

the class LibvirtComputingResource method execute.

private Answer execute(NetworkUsageCommand cmd) {
    if (cmd.getOption() != null && cmd.getOption().equals("create")) {
        String result = networkUsage(cmd.getPrivateIP(), "create", null);
        NetworkUsageAnswer answer = new NetworkUsageAnswer(cmd, result, 0L, 0L);
        return answer;
    }
    long[] stats = getNetworkStats(cmd.getPrivateIP());
    NetworkUsageAnswer answer = new NetworkUsageAnswer(cmd, "", stats[0], stats[1]);
    return answer;
}
Also used : NetworkUsageAnswer(com.cloud.agent.api.NetworkUsageAnswer)

Example 2 with NetworkUsageAnswer

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

the class VirtualNetworkApplianceManagerImpl method prepareStop.

@Override
public void prepareStop(final VirtualMachineProfile profile) {
    // Collect network usage before stopping Vm
    final DomainRouterVO router = _routerDao.findById(profile.getVirtualMachine().getId());
    if (router == null) {
        return;
    }
    final String privateIP = router.getPrivateIpAddress();
    if (privateIP != null) {
        final boolean forVpc = router.getVpcId() != null;
        final List<? extends Nic> routerNics = _nicDao.listByVmId(router.getId());
        for (final Nic routerNic : routerNics) {
            final Network network = _networkModel.getNetwork(routerNic.getNetworkId());
            // VR
            if (forVpc && network.getTrafficType() == TrafficType.Public || !forVpc && network.getTrafficType() == TrafficType.Guest && network.getGuestType() == Network.GuestType.Isolated) {
                final NetworkUsageCommand usageCmd = new NetworkUsageCommand(privateIP, router.getHostName(), forVpc, routerNic.getIPv4Address());
                final String routerType = router.getType().toString();
                final UserStatisticsVO previousStats = _userStatsDao.findBy(router.getAccountId(), router.getDataCenterId(), network.getId(), forVpc ? routerNic.getIPv4Address() : null, router.getId(), routerType);
                NetworkUsageAnswer answer = null;
                try {
                    answer = (NetworkUsageAnswer) _agentMgr.easySend(router.getHostId(), usageCmd);
                } catch (final Exception e) {
                    s_logger.warn("Error while collecting network stats from router: " + router.getInstanceName() + " from host: " + router.getHostId(), e);
                    continue;
                }
                if (answer != null) {
                    if (!answer.getResult()) {
                        s_logger.warn("Error while collecting network stats from router: " + router.getInstanceName() + " from host: " + router.getHostId() + "; details: " + answer.getDetails());
                        continue;
                    }
                    try {
                        if (answer.getBytesReceived() == 0 && answer.getBytesSent() == 0) {
                            s_logger.debug("Recieved and Sent bytes are both 0. Not updating user_statistics");
                            continue;
                        }
                        final NetworkUsageAnswer answerFinal = answer;
                        Transaction.execute(new TransactionCallbackNoReturn() {

                            @Override
                            public void doInTransactionWithoutResult(final TransactionStatus status) {
                                final UserStatisticsVO stats = _userStatsDao.lock(router.getAccountId(), router.getDataCenterId(), network.getId(), forVpc ? routerNic.getIPv4Address() : null, router.getId(), routerType);
                                if (stats == null) {
                                    s_logger.warn("unable to find stats for account: " + router.getAccountId());
                                    return;
                                }
                                if (previousStats != null && (previousStats.getCurrentBytesReceived() != stats.getCurrentBytesReceived() || previousStats.getCurrentBytesSent() != stats.getCurrentBytesSent())) {
                                    s_logger.debug("Router stats changed from the time NetworkUsageCommand was sent. " + "Ignoring current answer. Router: " + answerFinal.getRouterName() + " Rcvd: " + answerFinal.getBytesReceived() + "Sent: " + answerFinal.getBytesSent());
                                    return;
                                }
                                if (stats.getCurrentBytesReceived() > answerFinal.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. Router: " + answerFinal.getRouterName() + " Reported: " + answerFinal.getBytesReceived() + " Stored: " + stats.getCurrentBytesReceived());
                                    }
                                    stats.setNetBytesReceived(stats.getNetBytesReceived() + stats.getCurrentBytesReceived());
                                }
                                stats.setCurrentBytesReceived(answerFinal.getBytesReceived());
                                if (stats.getCurrentBytesSent() > answerFinal.getBytesSent()) {
                                    if (s_logger.isDebugEnabled()) {
                                        s_logger.debug("Received # of bytes that's less than the last one.  " + "Assuming something went wrong and persisting it. Router: " + answerFinal.getRouterName() + " Reported: " + answerFinal.getBytesSent() + " Stored: " + stats.getCurrentBytesSent());
                                    }
                                    stats.setNetBytesSent(stats.getNetBytesSent() + stats.getCurrentBytesSent());
                                }
                                stats.setCurrentBytesSent(answerFinal.getBytesSent());
                                if (!_dailyOrHourly) {
                                    // update agg bytes
                                    stats.setAggBytesSent(stats.getNetBytesSent() + stats.getCurrentBytesSent());
                                    stats.setAggBytesReceived(stats.getNetBytesReceived() + stats.getCurrentBytesReceived());
                                }
                                _userStatsDao.update(stats.getId(), stats);
                            }
                        });
                    } catch (final Exception e) {
                        s_logger.warn("Unable to update user statistics for account: " + router.getAccountId() + " Rx: " + answer.getBytesReceived() + "; Tx: " + answer.getBytesSent());
                    }
                }
            }
        }
    }
}
Also used : Nic(com.cloud.vm.Nic) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) NetworkUsageCommand(com.cloud.agent.api.NetworkUsageCommand) ConnectionException(com.cloud.exception.ConnectionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ParseException(java.text.ParseException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) ConfigurationException(javax.naming.ConfigurationException) Network(com.cloud.network.Network) NetworkUsageAnswer(com.cloud.agent.api.NetworkUsageAnswer) DomainRouterVO(com.cloud.vm.DomainRouterVO) UserStatisticsVO(com.cloud.user.UserStatisticsVO)

Example 3 with NetworkUsageAnswer

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

the class XcpServerNetworkUsageCommandWrapper method execute.

@Override
public Answer execute(final NetworkUsageCommand command, final XcpServerResource xcpServerResource) {
    try {
        final Connection conn = xcpServerResource.getConnection();
        if (command.getOption() != null && command.getOption().equals("create")) {
            final String result = xcpServerResource.networkUsage(conn, command.getPrivateIP(), "create", null);
            final NetworkUsageAnswer answer = new NetworkUsageAnswer(command, result, 0L, 0L);
            return answer;
        }
        final long[] stats = xcpServerResource.getNetworkStats(conn, command.getPrivateIP());
        final NetworkUsageAnswer answer = new NetworkUsageAnswer(command, "", stats[0], stats[1]);
        return answer;
    } catch (final Exception ex) {
        s_logger.warn("Failed to get network usage stats due to ", ex);
        return new NetworkUsageAnswer(command, ex);
    }
}
Also used : Connection(com.xensource.xenapi.Connection) NetworkUsageAnswer(com.cloud.agent.api.NetworkUsageAnswer)

Example 4 with NetworkUsageAnswer

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

the class Ovm3VirtualRoutingSupport method vpcNetworkUsage.

/* copy paste, why isn't this just generic in the VirtualRoutingResource ? */
private NetworkUsageAnswer vpcNetworkUsage(NetworkUsageCommand cmd) {
    String privateIp = cmd.getPrivateIP();
    String option = cmd.getOption();
    String publicIp = cmd.getGatewayIP();
    String args = "-l " + publicIp + " ";
    if ("get".equals(option)) {
        args += "-g";
    } else if (CREATE.equals(option)) {
        args += "-c";
        String vpcCIDR = cmd.getVpcCIDR();
        args += " -v " + vpcCIDR;
    } else if ("reset".equals(option)) {
        args += "-r";
    } else if ("vpn".equals(option)) {
        args += "-n";
    } else if ("remove".equals(option)) {
        args += "-d";
    } else {
        return new NetworkUsageAnswer(cmd, SUCCESS, 0L, 0L);
    }
    ExecutionResult callResult = vrr.executeInVR(privateIp, "vpc_netusage.sh", args);
    if (!callResult.isSuccess()) {
        LOGGER.error("Unable to execute NetworkUsage command on DomR (" + privateIp + "), domR may not be ready yet. failure due to " + callResult.getDetails());
    }
    if ("get".equals(option) || "vpn".equals(option)) {
        String result = callResult.getDetails();
        if (result == null || result.isEmpty()) {
            LOGGER.error(" vpc network usage get returns empty ");
        }
        long[] stats = new long[2];
        if (result != null) {
            String[] splitResult = result.split(":");
            int i = 0;
            while (i < splitResult.length - 1) {
                stats[0] += (Long.parseLong(splitResult[i++]));
                stats[1] += (Long.parseLong(splitResult[i++]));
            }
            return new NetworkUsageAnswer(cmd, SUCCESS, stats[0], stats[1]);
        }
    }
    return new NetworkUsageAnswer(cmd, SUCCESS, 0L, 0L);
}
Also used : ExecutionResult(com.cloud.utils.ExecutionResult) NetworkUsageAnswer(com.cloud.agent.api.NetworkUsageAnswer)

Example 5 with NetworkUsageAnswer

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

the class VmwareResource method execute.

protected Answer execute(NetworkUsageCommand cmd) {
    if (cmd.isForVpc()) {
        return VPCNetworkUsage(cmd);
    }
    if (s_logger.isInfoEnabled()) {
        s_logger.info("Executing resource NetworkUsageCommand " + _gson.toJson(cmd));
    }
    if (cmd.getOption() != null && cmd.getOption().equals("create")) {
        String result = networkUsage(cmd.getPrivateIP(), "create", null);
        NetworkUsageAnswer answer = new NetworkUsageAnswer(cmd, result, 0L, 0L);
        return answer;
    }
    long[] stats = getNetworkStats(cmd.getPrivateIP());
    NetworkUsageAnswer answer = new NetworkUsageAnswer(cmd, "", stats[0], stats[1]);
    return answer;
}
Also used : NetworkUsageAnswer(com.cloud.agent.api.NetworkUsageAnswer)

Aggregations

NetworkUsageAnswer (com.cloud.agent.api.NetworkUsageAnswer)10 ExecutionResult (com.cloud.utils.ExecutionResult)3 Connection (com.xensource.xenapi.Connection)2 NetworkUsageCommand (com.cloud.agent.api.NetworkUsageCommand)1 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)1 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)1 ConnectionException (com.cloud.exception.ConnectionException)1 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)1 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)1 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)1 Network (com.cloud.network.Network)1 UserStatisticsVO (com.cloud.user.UserStatisticsVO)1 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)1 TransactionStatus (com.cloud.utils.db.TransactionStatus)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 DomainRouterVO (com.cloud.vm.DomainRouterVO)1 Nic (com.cloud.vm.Nic)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ParseException (java.text.ParseException)1