Search in sources :

Example 1 with ClusterMetricsResponse

use of org.apache.cloudstack.response.ClusterMetricsResponse in project cloudstack by apache.

the class MetricsServiceImpl method listClusterMetrics.

@Override
public List<ClusterMetricsResponse> listClusterMetrics(List<ClusterResponse> clusterResponses) {
    final List<ClusterMetricsResponse> metricsResponses = new ArrayList<>();
    for (final ClusterResponse clusterResponse : clusterResponses) {
        ClusterMetricsResponse metricsResponse = new ClusterMetricsResponse();
        try {
            BeanUtils.copyProperties(metricsResponse, clusterResponse);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to generate cluster metrics response");
        }
        final Cluster cluster = clusterDao.findByUuid(clusterResponse.getId());
        if (cluster == null) {
            continue;
        }
        final Long clusterId = cluster.getId();
        // Thresholds
        final Double cpuThreshold = AlertManager.CPUCapacityThreshold.valueIn(clusterId);
        final Double memoryThreshold = AlertManager.MemoryCapacityThreshold.valueIn(clusterId);
        final Float cpuDisableThreshold = DeploymentClusterPlanner.ClusterCPUCapacityDisableThreshold.valueIn(clusterId);
        final Float memoryDisableThreshold = DeploymentClusterPlanner.ClusterMemoryCapacityDisableThreshold.valueIn(clusterId);
        final Double cpuOvercommitRatio = findRatioValue(ApiDBUtils.findClusterDetails(clusterId, "cpuOvercommitRatio"));
        final Double memoryOvercommitRatio = findRatioValue(ApiDBUtils.findClusterDetails(clusterId, "memoryOvercommitRatio"));
        // CPU and memory capacities
        final CapacityDaoImpl.SummedCapacity cpuCapacity = getCapacity((int) Capacity.CAPACITY_TYPE_CPU, null, clusterId);
        final CapacityDaoImpl.SummedCapacity memoryCapacity = getCapacity((int) Capacity.CAPACITY_TYPE_MEMORY, null, clusterId);
        final Metrics metrics = new Metrics(cpuCapacity, memoryCapacity);
        for (final HostJoinVO host : hostJoinDao.findByClusterId(clusterId, Host.Type.Routing)) {
            if (host.getStatus() == Status.Up) {
                metrics.incrUpResources();
            }
            metrics.incrTotalResources();
            updateHostMetrics(metrics, host);
        }
        metricsResponse.setState(clusterResponse.getAllocationState(), clusterResponse.getManagedState());
        metricsResponse.setResources(metrics.getUpResources(), metrics.getTotalResources());
        // CPU
        metricsResponse.setCpuTotal(metrics.getTotalCpu());
        metricsResponse.setCpuAllocated(metrics.getCpuAllocated(), metrics.getTotalCpu());
        if (metrics.getCpuUsedPercentage() > 0L) {
            metricsResponse.setCpuUsed(metrics.getCpuUsedPercentage(), metrics.getTotalHosts());
            metricsResponse.setCpuMaxDeviation(metrics.getMaximumCpuUsage(), metrics.getCpuUsedPercentage(), metrics.getTotalHosts());
        }
        // Memory
        metricsResponse.setMemTotal(metrics.getTotalMemory());
        metricsResponse.setMemAllocated(metrics.getMemoryAllocated(), metrics.getTotalMemory());
        if (metrics.getMemoryUsed() > 0L) {
            metricsResponse.setMemUsed(metrics.getMemoryUsed(), metrics.getTotalMemory());
            metricsResponse.setMemMaxDeviation(metrics.getMaximumMemoryUsage(), metrics.getMemoryUsed(), metrics.getTotalHosts());
        }
        // CPU thresholds
        metricsResponse.setCpuUsageThreshold(metrics.getCpuUsedPercentage(), metrics.getTotalHosts(), cpuThreshold);
        metricsResponse.setCpuUsageDisableThreshold(metrics.getCpuUsedPercentage(), metrics.getTotalHosts(), cpuDisableThreshold);
        metricsResponse.setCpuAllocatedThreshold(metrics.getCpuAllocated(), metrics.getTotalCpu(), cpuOvercommitRatio, cpuThreshold);
        metricsResponse.setCpuAllocatedDisableThreshold(metrics.getCpuAllocated(), metrics.getTotalCpu(), cpuOvercommitRatio, cpuDisableThreshold);
        // Memory thresholds
        metricsResponse.setMemoryUsageThreshold(metrics.getMemoryUsed(), metrics.getTotalMemory(), memoryThreshold);
        metricsResponse.setMemoryUsageDisableThreshold(metrics.getMemoryUsed(), metrics.getTotalMemory(), memoryDisableThreshold);
        metricsResponse.setMemoryAllocatedThreshold(metrics.getMemoryAllocated(), metrics.getTotalMemory(), memoryOvercommitRatio, memoryThreshold);
        metricsResponse.setMemoryAllocatedDisableThreshold(metrics.getMemoryAllocated(), metrics.getTotalMemory(), memoryOvercommitRatio, memoryDisableThreshold);
        metricsResponses.add(metricsResponse);
    }
    return metricsResponses;
}
Also used : ArrayList(java.util.ArrayList) ClusterResponse(org.apache.cloudstack.api.response.ClusterResponse) Cluster(com.cloud.org.Cluster) HostJoinVO(com.cloud.api.query.vo.HostJoinVO) InvocationTargetException(java.lang.reflect.InvocationTargetException) ClusterMetricsResponse(org.apache.cloudstack.response.ClusterMetricsResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) CapacityDaoImpl(com.cloud.capacity.dao.CapacityDaoImpl)

Example 2 with ClusterMetricsResponse

use of org.apache.cloudstack.response.ClusterMetricsResponse in project cloudstack by apache.

the class MetricsServiceImpl method listClusterMetrics.

@Override
public List<ClusterMetricsResponse> listClusterMetrics(Pair<List<ClusterResponse>, Integer> clusterResponses) {
    final List<ClusterMetricsResponse> metricsResponses = new ArrayList<>();
    for (final ClusterResponse clusterResponse : clusterResponses.first()) {
        ClusterMetricsResponse metricsResponse = new ClusterMetricsResponse();
        try {
            BeanUtils.copyProperties(metricsResponse, clusterResponse);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to generate cluster metrics response");
        }
        final Cluster cluster = clusterDao.findByUuid(clusterResponse.getId());
        if (cluster == null) {
            continue;
        }
        final Long clusterId = cluster.getId();
        // Thresholds
        final Double cpuThreshold = AlertManager.CPUCapacityThreshold.valueIn(clusterId);
        final Double memoryThreshold = AlertManager.MemoryCapacityThreshold.valueIn(clusterId);
        final Float cpuDisableThreshold = DeploymentClusterPlanner.ClusterCPUCapacityDisableThreshold.valueIn(clusterId);
        final Float memoryDisableThreshold = DeploymentClusterPlanner.ClusterMemoryCapacityDisableThreshold.valueIn(clusterId);
        // CPU and memory capacities
        final CapacityDaoImpl.SummedCapacity cpuCapacity = getCapacity((int) Capacity.CAPACITY_TYPE_CPU, null, clusterId);
        final CapacityDaoImpl.SummedCapacity memoryCapacity = getCapacity((int) Capacity.CAPACITY_TYPE_MEMORY, null, clusterId);
        final Metrics metrics = new Metrics(cpuCapacity, memoryCapacity);
        for (final Host host : hostDao.findByClusterId(clusterId)) {
            if (host == null || host.getType() != Host.Type.Routing) {
                continue;
            }
            if (host.getStatus() == Status.Up) {
                metrics.incrUpResources();
            }
            metrics.incrTotalResources();
            updateHostMetrics(metrics, hostJoinDao.findById(host.getId()));
        }
        metricsResponse.setState(clusterResponse.getAllocationState(), clusterResponse.getManagedState());
        metricsResponse.setResources(metrics.getUpResources(), metrics.getTotalResources());
        // CPU
        metricsResponse.setCpuTotal(metrics.getTotalCpu());
        metricsResponse.setCpuAllocated(metrics.getCpuAllocated(), metrics.getTotalCpu());
        if (metrics.getCpuUsedPercentage() > 0L) {
            metricsResponse.setCpuUsed(metrics.getCpuUsedPercentage(), metrics.getTotalHosts());
            metricsResponse.setCpuMaxDeviation(metrics.getMaximumCpuUsage(), metrics.getCpuUsedPercentage(), metrics.getTotalHosts());
        }
        // Memory
        metricsResponse.setMemTotal(metrics.getTotalMemory());
        metricsResponse.setMemAllocated(metrics.getMemoryAllocated(), metrics.getTotalMemory());
        if (metrics.getMemoryUsed() > 0L) {
            metricsResponse.setMemUsed(metrics.getMemoryUsed(), metrics.getTotalMemory());
            metricsResponse.setMemMaxDeviation(metrics.getMaximumMemoryUsage(), metrics.getMemoryUsed(), metrics.getTotalHosts());
        }
        // CPU thresholds
        metricsResponse.setCpuUsageThreshold(metrics.getCpuUsedPercentage(), metrics.getTotalHosts(), cpuThreshold);
        metricsResponse.setCpuUsageDisableThreshold(metrics.getCpuUsedPercentage(), metrics.getTotalHosts(), cpuDisableThreshold);
        metricsResponse.setCpuAllocatedThreshold(metrics.getCpuAllocated(), metrics.getTotalCpu(), cpuThreshold);
        metricsResponse.setCpuAllocatedDisableThreshold(metrics.getCpuAllocated(), metrics.getTotalCpu(), cpuDisableThreshold);
        // Memory thresholds
        metricsResponse.setMemoryUsageThreshold(metrics.getMemoryUsed(), metrics.getTotalMemory(), memoryThreshold);
        metricsResponse.setMemoryUsageDisableThreshold(metrics.getMemoryUsed(), metrics.getTotalMemory(), memoryDisableThreshold);
        metricsResponse.setMemoryAllocatedThreshold(metrics.getMemoryAllocated(), metrics.getTotalMemory(), memoryThreshold);
        metricsResponse.setMemoryAllocatedDisableThreshold(metrics.getMemoryAllocated(), metrics.getTotalMemory(), memoryDisableThreshold);
        metricsResponse.setHasAnnotation(clusterResponse.hasAnnotation());
        metricsResponses.add(metricsResponse);
    }
    return metricsResponses;
}
Also used : ArrayList(java.util.ArrayList) ClusterResponse(org.apache.cloudstack.api.response.ClusterResponse) Cluster(com.cloud.org.Cluster) Host(com.cloud.host.Host) InvocationTargetException(java.lang.reflect.InvocationTargetException) ClusterMetricsResponse(org.apache.cloudstack.response.ClusterMetricsResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) CapacityDaoImpl(com.cloud.capacity.dao.CapacityDaoImpl)

Aggregations

CapacityDaoImpl (com.cloud.capacity.dao.CapacityDaoImpl)2 Cluster (com.cloud.org.Cluster)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ArrayList (java.util.ArrayList)2 ServerApiException (org.apache.cloudstack.api.ServerApiException)2 ClusterResponse (org.apache.cloudstack.api.response.ClusterResponse)2 ClusterMetricsResponse (org.apache.cloudstack.response.ClusterMetricsResponse)2 HostJoinVO (com.cloud.api.query.vo.HostJoinVO)1 Host (com.cloud.host.Host)1