Search in sources :

Example 1 with ExternalNetworkResourceUsageAnswer

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

the class NetscalerResource method getPublicIpBytesSentAndReceived.

private ExternalNetworkResourceUsageAnswer getPublicIpBytesSentAndReceived(final ExternalNetworkResourceUsageCommand cmd) throws ExecutionException {
    final ExternalNetworkResourceUsageAnswer answer = new ExternalNetworkResourceUsageAnswer(cmd);
    try {
        final lbvserver_stats[] stats = lbvserver_stats.get(_netscalerService);
        if (stats == null || stats.length == 0) {
            return answer;
        }
        for (final lbvserver_stats stat_entry : stats) {
            final String lbvserverName = stat_entry.get_name();
            final lbvserver vserver = lbvserver.get(_netscalerService, lbvserverName);
            if (vserver != null) {
                final String lbVirtualServerIp = vserver.get_ipv46();
                long[] bytesSentAndReceived = answer.ipBytes.get(lbVirtualServerIp);
                if (bytesSentAndReceived == null) {
                    bytesSentAndReceived = new long[] { 0, 0 };
                }
                bytesSentAndReceived[0] += stat_entry.get_totalrequestbytes();
                bytesSentAndReceived[1] += stat_entry.get_totalresponsebytes();
                if (bytesSentAndReceived[0] >= 0 && bytesSentAndReceived[1] >= 0) {
                    answer.ipBytes.put(lbVirtualServerIp, bytesSentAndReceived);
                }
            }
        }
    } catch (final Exception e) {
        s_logger.error("Failed to get bytes sent and recived statistics due to " + e);
        throw new ExecutionException(e.getMessage());
    }
    return answer;
}
Also used : com.citrix.netscaler.nitro.resource.config.gslb.gslbvserver(com.citrix.netscaler.nitro.resource.config.gslb.gslbvserver) com.citrix.netscaler.nitro.resource.config.lb.lbvserver(com.citrix.netscaler.nitro.resource.config.lb.lbvserver) ExternalNetworkResourceUsageAnswer(com.cloud.agent.api.ExternalNetworkResourceUsageAnswer) com.citrix.netscaler.nitro.resource.stat.lb.lbvserver_stats(com.citrix.netscaler.nitro.resource.stat.lb.lbvserver_stats) ExecutionException(com.cloud.utils.exception.ExecutionException) ExecutionException(com.cloud.utils.exception.ExecutionException) IOException(java.io.IOException) ConfigurationException(javax.naming.ConfigurationException)

Example 2 with ExternalNetworkResourceUsageAnswer

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

the class NetScalerControlCenterResource method getPublicIpBytesSentAndReceived.

private ExternalNetworkResourceUsageAnswer getPublicIpBytesSentAndReceived(ExternalNetworkResourceUsageCommand cmd) throws ExecutionException {
    ExternalNetworkResourceUsageAnswer answer = new ExternalNetworkResourceUsageAnswer(cmd);
    long networkid = cmd.getNetworkid();
    try {
        // TODO send GET cmd to get the network stats
        URI agentUri = null;
        String response = null;
        try {
            agentUri = new URI("https", null, _ip, DEFAULT_PORT, "/cs/adcaas/v1/networks/" + networkid + "/ipStats", null, null);
            org.json.JSONObject jsonBody = new JSONObject();
            response = getHttpRequest(jsonBody.toString(), agentUri, _sessionid);
            JSONArray statsIPList = null;
            if (response != null) {
                statsIPList = new JSONObject(response).getJSONObject("stats").getJSONArray("ipBytes");
            }
            if (statsIPList != null) {
                for (int i = 0; i < statsIPList.length(); i++) {
                    JSONObject ipstat = statsIPList.getJSONObject(i);
                    JSONObject ipvalues = ipstat.getJSONObject("ipstats");
                    if (ipstat != null) {
                        long[] bytesSentAndReceived = new long[] { 0, 0 };
                        bytesSentAndReceived[0] = ipvalues.getLong("received");
                        bytesSentAndReceived[1] = ipvalues.getLong("sent");
                        if (bytesSentAndReceived[0] >= 0 && bytesSentAndReceived[1] >= 0) {
                            answer.ipBytes.put(ipstat.getString("ip"), bytesSentAndReceived);
                        }
                    }
                }
            }
            s_logger.debug("IPStats Response :" + response);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            s_logger.debug("Seesion Alive" + e.getMessage());
            e.printStackTrace();
        }
    } catch (Exception e) {
        s_logger.error("Failed to get bytes sent and recived statistics due to " + e);
        throw new ExecutionException(e.getMessage());
    }
    return answer;
}
Also used : JSONArray(org.json.JSONArray) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ExecutionException(com.cloud.utils.exception.ExecutionException) URISyntaxException(java.net.URISyntaxException) KeyStoreException(java.security.KeyStoreException) JSONException(org.json.JSONException) KeyManagementException(java.security.KeyManagementException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ClientProtocolException(org.apache.http.client.ClientProtocolException) ConfigurationException(javax.naming.ConfigurationException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) ExternalNetworkResourceUsageAnswer(com.cloud.agent.api.ExternalNetworkResourceUsageAnswer) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) ExecutionException(com.cloud.utils.exception.ExecutionException)

Example 3 with ExternalNetworkResourceUsageAnswer

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

the class F5BigIpResource method getIpBytesSentAndReceived.

// Stats methods
private ExternalNetworkResourceUsageAnswer getIpBytesSentAndReceived(ExternalNetworkResourceUsageCommand cmd) throws ExecutionException {
    ExternalNetworkResourceUsageAnswer answer = new ExternalNetworkResourceUsageAnswer(cmd);
    try {
        LocalLBVirtualServerVirtualServerStatistics stats = _virtualServerApi.get_all_statistics();
        for (LocalLBVirtualServerVirtualServerStatisticEntry entry : stats.getStatistics()) {
            String virtualServerIp = entry.getVirtual_server().getAddress();
            virtualServerIp = stripRouteDomainFromAddress(virtualServerIp);
            long[] bytesSentAndReceived = answer.ipBytes.get(virtualServerIp);
            if (bytesSentAndReceived == null) {
                bytesSentAndReceived = new long[] { 0, 0 };
            }
            for (CommonStatistic stat : entry.getStatistics()) {
                int index;
                if (stat.getType().equals(CommonStatisticType.STATISTIC_CLIENT_SIDE_BYTES_OUT)) {
                    // Add to the outgoing bytes
                    index = 0;
                } else if (stat.getType().equals(CommonStatisticType.STATISTIC_CLIENT_SIDE_BYTES_IN)) {
                    // Add to the incoming bytes
                    index = 1;
                } else {
                    continue;
                }
                long high = stat.getValue().getHigh();
                long low = stat.getValue().getLow();
                long full = getFullUsage(high, low);
                bytesSentAndReceived[index] += full;
            }
            if (bytesSentAndReceived[0] >= 0 && bytesSentAndReceived[1] >= 0) {
                answer.ipBytes.put(virtualServerIp, bytesSentAndReceived);
            }
        }
    } catch (Exception e) {
        s_logger.error(e);
        throw new ExecutionException(e.getMessage());
    }
    return answer;
}
Also used : ExternalNetworkResourceUsageAnswer(com.cloud.agent.api.ExternalNetworkResourceUsageAnswer) CommonStatistic(iControl.CommonStatistic) ExecutionException(com.cloud.utils.exception.ExecutionException) LocalLBVirtualServerVirtualServerStatistics(iControl.LocalLBVirtualServerVirtualServerStatistics) ExecutionException(com.cloud.utils.exception.ExecutionException) RemoteException(java.rmi.RemoteException) ConfigurationException(javax.naming.ConfigurationException) LocalLBVirtualServerVirtualServerStatisticEntry(iControl.LocalLBVirtualServerVirtualServerStatisticEntry)

Example 4 with ExternalNetworkResourceUsageAnswer

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

the class ExternalDeviceUsageManagerImpl method updateExternalLoadBalancerNetworkUsageStats.

@Override
public void updateExternalLoadBalancerNetworkUsageStats(long loadBalancerRuleId) {
    LoadBalancerVO lb = _loadBalancerDao.findById(loadBalancerRuleId);
    if (lb == null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Cannot update usage stats, LB rule is not found");
        }
        return;
    }
    long networkId = lb.getNetworkId();
    Network network = _networkDao.findById(networkId);
    if (network == null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Cannot update usage stats, Network is not found");
        }
        return;
    }
    ExternalLoadBalancerDeviceVO lbDeviceVO = getExternalLoadBalancerForNetwork(network);
    if (lbDeviceVO == null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Cannot update usage stats,  No external LB device found");
        }
        return;
    }
    // Get network stats from the external load balancer
    ExternalNetworkResourceUsageAnswer lbAnswer = null;
    HostVO externalLoadBalancer = _hostDao.findById(lbDeviceVO.getHostId());
    if (externalLoadBalancer != null) {
        ExternalNetworkResourceUsageCommand cmd = new ExternalNetworkResourceUsageCommand();
        lbAnswer = (ExternalNetworkResourceUsageAnswer) _agentMgr.easySend(externalLoadBalancer.getId(), cmd);
        if (lbAnswer == null || !lbAnswer.getResult()) {
            String details = (lbAnswer != null) ? lbAnswer.getDetails() : "details unavailable";
            String msg = "Unable to get external load balancer stats for network" + networkId + " due to: " + details + ".";
            s_logger.error(msg);
            return;
        }
    }
    long accountId = lb.getAccountId();
    AccountVO account = _accountDao.findById(accountId);
    if (account == null) {
        s_logger.debug("Skipping stats update for external LB for account with ID " + accountId);
        return;
    }
    String publicIp = _networkModel.getIp(lb.getSourceIpAddressId()).getAddress().addr();
    DataCenterVO zone = _dcDao.findById(network.getDataCenterId());
    String statsEntryIdentifier = "account " + account.getAccountName() + ", zone " + zone.getName() + ", network ID " + networkId + ", host ID " + externalLoadBalancer.getName();
    long newCurrentBytesSent = 0;
    long newCurrentBytesReceived = 0;
    if (publicIp != null) {
        long[] bytesSentAndReceived = null;
        statsEntryIdentifier += ", public IP: " + publicIp;
        boolean inline = _networkModel.isNetworkInlineMode(network);
        if (externalLoadBalancer.getType().equals(Host.Type.ExternalLoadBalancer) && inline) {
            // Look up stats for the guest IP address that's mapped to the public IP address
            InlineLoadBalancerNicMapVO mapping = _inlineLoadBalancerNicMapDao.findByPublicIpAddress(publicIp);
            if (mapping != null) {
                NicVO nic = _nicDao.findById(mapping.getNicId());
                String loadBalancingIpAddress = nic.getIPv4Address();
                bytesSentAndReceived = lbAnswer.ipBytes.get(loadBalancingIpAddress);
                if (bytesSentAndReceived != null) {
                    bytesSentAndReceived[0] = 0;
                }
            }
        } else {
            bytesSentAndReceived = lbAnswer.ipBytes.get(publicIp);
        }
        if (bytesSentAndReceived == null) {
            s_logger.debug("Didn't get an external network usage answer for public IP " + publicIp);
        } else {
            newCurrentBytesSent += bytesSentAndReceived[0];
            newCurrentBytesReceived += bytesSentAndReceived[1];
        }
        commitStats(networkId, externalLoadBalancer, accountId, publicIp, zone, statsEntryIdentifier, newCurrentBytesSent, newCurrentBytesReceived);
    }
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) NetworkExternalLoadBalancerVO(com.cloud.network.dao.NetworkExternalLoadBalancerVO) ExternalNetworkResourceUsageCommand(com.cloud.agent.api.ExternalNetworkResourceUsageCommand) InlineLoadBalancerNicMapVO(com.cloud.network.dao.InlineLoadBalancerNicMapVO) AccountVO(com.cloud.user.AccountVO) HostVO(com.cloud.host.HostVO) ExternalLoadBalancerDeviceVO(com.cloud.network.dao.ExternalLoadBalancerDeviceVO) ExternalNetworkResourceUsageAnswer(com.cloud.agent.api.ExternalNetworkResourceUsageAnswer) NicVO(com.cloud.vm.NicVO)

Example 5 with ExternalNetworkResourceUsageAnswer

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

the class JuniperSrxResource method getUsageAnswer.

/*
     * Usage
     */
private ExternalNetworkResourceUsageAnswer getUsageAnswer(ExternalNetworkResourceUsageCommand cmd) throws ExecutionException {
    try {
        String socOpenException = "Failed to open a connection for Usage data.";
        String socCloseException = "Unable to close connection for Usage data.";
        if (!openUsageSocket()) {
            throw new ExecutionException(socOpenException);
        }
        ExternalNetworkResourceUsageAnswer answer = new ExternalNetworkResourceUsageAnswer(cmd);
        String xml = SrxXml.FIREWALL_FILTER_BYTES_GETALL.getXml();
        String rawUsageData = sendUsageRequest(xml);
        Document doc = getDocument(rawUsageData);
        NodeList counters = doc.getElementsByTagName("counter");
        for (int i = 0; i < counters.getLength(); i++) {
            Node n = counters.item(i);
            if (n.getNodeName().equals("counter")) {
                NodeList counterInfoList = n.getChildNodes();
                String counterName = null;
                long byteCount = 0;
                for (int j = 0; j < counterInfoList.getLength(); j++) {
                    Node counterInfo = counterInfoList.item(j);
                    if (counterInfo.getNodeName().equals("counter-name")) {
                        counterName = counterInfo.getFirstChild().getNodeValue();
                    } else if (counterInfo.getNodeName().equals("byte-count")) {
                        try {
                            byteCount = Long.parseLong(counterInfo.getFirstChild().getNodeValue());
                        } catch (Exception e) {
                            s_logger.debug(e);
                            byteCount = 0;
                        }
                    }
                }
                if (byteCount >= 0) {
                    updateUsageAnswer(answer, counterName, byteCount);
                }
            }
        }
        if (!closeUsageSocket()) {
            throw new ExecutionException(socCloseException);
        }
        return answer;
    } catch (Exception e) {
        closeUsageSocket();
        throw new ExecutionException(e.getMessage());
    }
}
Also used : ExternalNetworkResourceUsageAnswer(com.cloud.agent.api.ExternalNetworkResourceUsageAnswer) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ExecutionException(com.cloud.utils.exception.ExecutionException) Document(org.w3c.dom.Document) ExecutionException(com.cloud.utils.exception.ExecutionException) ConfigurationException(javax.naming.ConfigurationException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException)

Aggregations

ExternalNetworkResourceUsageAnswer (com.cloud.agent.api.ExternalNetworkResourceUsageAnswer)5 ExecutionException (com.cloud.utils.exception.ExecutionException)4 ConfigurationException (javax.naming.ConfigurationException)4 IOException (java.io.IOException)3 com.citrix.netscaler.nitro.resource.config.gslb.gslbvserver (com.citrix.netscaler.nitro.resource.config.gslb.gslbvserver)1 com.citrix.netscaler.nitro.resource.config.lb.lbvserver (com.citrix.netscaler.nitro.resource.config.lb.lbvserver)1 com.citrix.netscaler.nitro.resource.stat.lb.lbvserver_stats (com.citrix.netscaler.nitro.resource.stat.lb.lbvserver_stats)1 ExternalNetworkResourceUsageCommand (com.cloud.agent.api.ExternalNetworkResourceUsageCommand)1 DataCenterVO (com.cloud.dc.DataCenterVO)1 HostVO (com.cloud.host.HostVO)1 ExternalLoadBalancerDeviceVO (com.cloud.network.dao.ExternalLoadBalancerDeviceVO)1 InlineLoadBalancerNicMapVO (com.cloud.network.dao.InlineLoadBalancerNicMapVO)1 LoadBalancerVO (com.cloud.network.dao.LoadBalancerVO)1 NetworkExternalLoadBalancerVO (com.cloud.network.dao.NetworkExternalLoadBalancerVO)1 AccountVO (com.cloud.user.AccountVO)1 NicVO (com.cloud.vm.NicVO)1 CommonStatistic (iControl.CommonStatistic)1 LocalLBVirtualServerVirtualServerStatisticEntry (iControl.LocalLBVirtualServerVirtualServerStatisticEntry)1 LocalLBVirtualServerVirtualServerStatistics (iControl.LocalLBVirtualServerVirtualServerStatistics)1 SocketTimeoutException (java.net.SocketTimeoutException)1