Search in sources :

Example 1 with NetworkUsageAnswer

use of com.cloud.legacymodel.communication.answer.NetworkUsageAnswer in project cosmic by MissionCriticalCloud.

the class LibvirtNetworkUsageCommandWrapper method execute.

@Override
public Answer execute(final NetworkUsageCommand command, final LibvirtComputingResource libvirtComputingResource) {
    if (command.isForVpc()) {
        if (command.getOption() != null && command.getOption().equals("create")) {
            final String result = libvirtComputingResource.configureVpcNetworkUsage(command.getPrivateIP(), command.getGatewayIP(), "create", command.getVpcCIDR());
            final NetworkUsageAnswer answer = new NetworkUsageAnswer(command, result, 0L, 0L);
            return answer;
        } else if (command.getOption() != null && (command.getOption().equals("get") || command.getOption().equals("vpn"))) {
            final long[] stats = libvirtComputingResource.getVpcNetworkStats(command.getPrivateIP(), command.getGatewayIP(), command.getOption());
            final NetworkUsageAnswer answer = new NetworkUsageAnswer(command, "", stats[0], stats[1]);
            return answer;
        } else {
            final String result = libvirtComputingResource.configureVpcNetworkUsage(command.getPrivateIP(), command.getGatewayIP(), command.getOption(), command.getVpcCIDR());
            final NetworkUsageAnswer answer = new NetworkUsageAnswer(command, result, 0L, 0L);
            return answer;
        }
    } else {
        if (command.getOption() != null && command.getOption().equals("create")) {
            final String result = libvirtComputingResource.networkUsage(command.getPrivateIP(), "create", null);
            final NetworkUsageAnswer answer = new NetworkUsageAnswer(command, result, 0L, 0L);
            return answer;
        }
        final long[] stats = libvirtComputingResource.getNetworkStats(command.getPrivateIP());
        final NetworkUsageAnswer answer = new NetworkUsageAnswer(command, "", stats[0], stats[1]);
        return answer;
    }
}
Also used : NetworkUsageAnswer(com.cloud.legacymodel.communication.answer.NetworkUsageAnswer)

Example 2 with NetworkUsageAnswer

use of com.cloud.legacymodel.communication.answer.NetworkUsageAnswer in project cosmic by MissionCriticalCloud.

the class XenServer56NetworkUsageCommandWrapper method execute.

@Override
public Answer execute(final NetworkUsageCommand command, final XenServer56Resource xenServer56) {
    if (command.isForVpc()) {
        return executeNetworkUsage(command, xenServer56);
    }
    try {
        final Connection conn = xenServer56.getConnection();
        if (command.getOption() != null && command.getOption().equals("create")) {
            final String result = xenServer56.networkUsage(conn, command.getPrivateIP(), "create", null);
            final NetworkUsageAnswer answer = new NetworkUsageAnswer(command, result, 0L, 0L);
            return answer;
        }
        final long[] stats = xenServer56.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, false, ex.getMessage());
    }
}
Also used : Connection(com.xensource.xenapi.Connection) NetworkUsageAnswer(com.cloud.legacymodel.communication.answer.NetworkUsageAnswer)

Example 3 with NetworkUsageAnswer

use of com.cloud.legacymodel.communication.answer.NetworkUsageAnswer in project cosmic by MissionCriticalCloud.

the class XenServer56NetworkUsageCommandWrapper method executeNetworkUsage.

protected NetworkUsageAnswer executeNetworkUsage(final NetworkUsageCommand command, final XenServer56Resource xenServer56) {
    try {
        final String option = command.getOption();
        final String publicIp = command.getGatewayIP();
        String args = " -l " + publicIp + " ";
        if (option.equals("get")) {
            args += "-g";
        } else if (option.equals("create")) {
            args += "-c";
            final String vpcCIDR = command.getVpcCIDR();
            args += " -v " + vpcCIDR;
        } else if (option.equals("reset")) {
            args += "-r";
        } else if (option.equals("vpn")) {
            args += "-n";
        } else if (option.equals("remove")) {
            args += "-d";
        } else {
            return new NetworkUsageAnswer(command, "success", 0L, 0L);
        }
        final ExecutionResult result = xenServer56.executeInVR(command.getPrivateIP(), "vpc_netusage.sh", args);
        final String detail = result.getDetails();
        if (!result.isSuccess()) {
            throw new Exception(" vpc network usage plugin call failed ");
        }
        if (option.equals("get") || option.equals("vpn")) {
            final long[] stats = new long[2];
            if (detail != null) {
                final String[] splitResult = detail.split(":");
                int i = 0;
                while (i < splitResult.length - 1) {
                    stats[0] += Long.parseLong(splitResult[i++]);
                    stats[1] += Long.parseLong(splitResult[i++]);
                }
                return new NetworkUsageAnswer(command, "success", stats[0], stats[1]);
            }
        }
        return new NetworkUsageAnswer(command, "success", 0L, 0L);
    } catch (final Exception ex) {
        s_logger.warn("Failed to get network usage stats due to ", ex);
        return new NetworkUsageAnswer(command, false, ex.getMessage());
    }
}
Also used : ExecutionResult(com.cloud.legacymodel.ExecutionResult) NetworkUsageAnswer(com.cloud.legacymodel.communication.answer.NetworkUsageAnswer)

Example 4 with NetworkUsageAnswer

use of com.cloud.legacymodel.communication.answer.NetworkUsageAnswer in project cosmic by MissionCriticalCloud.

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, false, ex.getMessage());
    }
}
Also used : Connection(com.xensource.xenapi.Connection) NetworkUsageAnswer(com.cloud.legacymodel.communication.answer.NetworkUsageAnswer)

Example 5 with NetworkUsageAnswer

use of com.cloud.legacymodel.communication.answer.NetworkUsageAnswer in project cosmic by MissionCriticalCloud.

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 (network == null) {
                s_logger.error("Could not find a network with ID => " + routerNic.getNetworkId() + ".");
                continue;
            }
            if (forVpc && network.getTrafficType() == TrafficType.Public || !forVpc && network.getTrafficType() == TrafficType.Guest && network.getGuestType() == 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.legacymodel.network.Nic) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) NetworkUsageCommand(com.cloud.legacymodel.communication.command.NetworkUsageCommand) ConnectionException(com.cloud.legacymodel.exceptions.ConnectionException) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) InsufficientCapacityException(com.cloud.legacymodel.exceptions.InsufficientCapacityException) ConfigurationException(javax.naming.ConfigurationException) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) Network(com.cloud.legacymodel.network.Network) NetworkUsageAnswer(com.cloud.legacymodel.communication.answer.NetworkUsageAnswer) DomainRouterVO(com.cloud.vm.DomainRouterVO) UserStatisticsVO(com.cloud.user.UserStatisticsVO)

Aggregations

NetworkUsageAnswer (com.cloud.legacymodel.communication.answer.NetworkUsageAnswer)5 Connection (com.xensource.xenapi.Connection)2 ExecutionResult (com.cloud.legacymodel.ExecutionResult)1 NetworkUsageCommand (com.cloud.legacymodel.communication.command.NetworkUsageCommand)1 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)1 ConcurrentOperationException (com.cloud.legacymodel.exceptions.ConcurrentOperationException)1 ConnectionException (com.cloud.legacymodel.exceptions.ConnectionException)1 InsufficientCapacityException (com.cloud.legacymodel.exceptions.InsufficientCapacityException)1 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)1 OperationTimedoutException (com.cloud.legacymodel.exceptions.OperationTimedoutException)1 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)1 Network (com.cloud.legacymodel.network.Network)1 Nic (com.cloud.legacymodel.network.Nic)1 UserStatisticsVO (com.cloud.user.UserStatisticsVO)1 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)1 TransactionStatus (com.cloud.utils.db.TransactionStatus)1 DomainRouterVO (com.cloud.vm.DomainRouterVO)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ConfigurationException (javax.naming.ConfigurationException)1