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;
}
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;
}
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;
}
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);
}
}
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());
}
}
Aggregations