Search in sources :

Example 1 with SummedCapacity

use of com.cloud.capacity.dao.CapacityDaoImpl.SummedCapacity in project cloudstack by apache.

the class ApiResponseHelper method getDataCenterCapacityResponse.

public static List<CapacityResponse> getDataCenterCapacityResponse(Long zoneId) {
    List<SummedCapacity> capacities = ApiDBUtils.getCapacityByClusterPodZone(zoneId, null, null);
    Set<CapacityResponse> capacityResponses = new HashSet<CapacityResponse>();
    for (SummedCapacity capacity : capacities) {
        CapacityResponse capacityResponse = new CapacityResponse();
        capacityResponse.setCapacityType(capacity.getCapacityType());
        capacityResponse.setCapacityUsed(capacity.getUsedCapacity() + capacity.getReservedCapacity());
        if (capacity.getCapacityType() == Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED) {
            List<SummedCapacity> c = ApiDBUtils.findNonSharedStorageForClusterPodZone(zoneId, null, null);
            capacityResponse.setCapacityTotal(capacity.getTotalCapacity() - c.get(0).getTotalCapacity());
            capacityResponse.setCapacityUsed(capacity.getUsedCapacity() - c.get(0).getUsedCapacity());
        } else {
            capacityResponse.setCapacityTotal(capacity.getTotalCapacity());
        }
        if (capacityResponse.getCapacityTotal() != 0) {
            capacityResponse.setPercentUsed(s_percentFormat.format((float) capacityResponse.getCapacityUsed() / (float) capacityResponse.getCapacityTotal() * 100f));
        } else {
            capacityResponse.setPercentUsed(s_percentFormat.format(0L));
        }
        capacityResponses.add(capacityResponse);
    }
    // Do it for stats as well.
    capacityResponses.addAll(getStatsCapacityresponse(null, null, null, zoneId));
    return new ArrayList<CapacityResponse>(capacityResponses);
}
Also used : CapacityResponse(org.apache.cloudstack.api.response.CapacityResponse) ArrayList(java.util.ArrayList) SummedCapacity(com.cloud.capacity.dao.CapacityDaoImpl.SummedCapacity) HashSet(java.util.HashSet)

Example 2 with SummedCapacity

use of com.cloud.capacity.dao.CapacityDaoImpl.SummedCapacity in project cloudstack by apache.

the class ManagementServerImpl method listTopConsumedResources.

@Override
public List<CapacityVO> listTopConsumedResources(final ListCapacityCmd cmd) {
    final Integer capacityType = cmd.getType();
    Long zoneId = cmd.getZoneId();
    final Long podId = cmd.getPodId();
    final Long clusterId = cmd.getClusterId();
    final Boolean fetchLatest = cmd.getFetchLatest();
    if (clusterId != null) {
        throw new InvalidParameterValueException("Currently clusterId param is not suppoerted");
    }
    zoneId = _accountMgr.checkAccessAndSpecifyAuthority(CallContext.current().getCallingAccount(), zoneId);
    if (fetchLatest != null && fetchLatest) {
        _alertMgr.recalculateCapacity();
    }
    List<SummedCapacity> summedCapacities = new ArrayList<SummedCapacity>();
    if (zoneId == null && podId == null) {
        // Group by Zone, capacity type
        final List<SummedCapacity> summedCapacitiesAtZone = _capacityDao.listCapacitiesGroupedByLevelAndType(capacityType, zoneId, podId, clusterId, 1, cmd.getPageSizeVal());
        if (summedCapacitiesAtZone != null) {
            summedCapacities.addAll(summedCapacitiesAtZone);
        }
    } else if (podId == null) {
        // Group by Pod, capacity type
        final List<SummedCapacity> summedCapacitiesAtPod = _capacityDao.listCapacitiesGroupedByLevelAndType(capacityType, zoneId, podId, clusterId, 2, cmd.getPageSizeVal());
        if (summedCapacitiesAtPod != null) {
            summedCapacities.addAll(summedCapacitiesAtPod);
        }
    } else {
        // Group by Cluster, capacity type
        final List<SummedCapacity> summedCapacitiesAtCluster = _capacityDao.listCapacitiesGroupedByLevelAndType(capacityType, zoneId, podId, clusterId, 3, cmd.getPageSizeVal());
        if (summedCapacitiesAtCluster != null) {
            summedCapacities.addAll(summedCapacitiesAtCluster);
        }
    }
    List<SummedCapacity> summedCapacitiesForSecStorage = getStorageUsed(clusterId, podId, zoneId, capacityType);
    if (summedCapacitiesForSecStorage != null) {
        summedCapacities.addAll(summedCapacitiesForSecStorage);
    }
    // Sort Capacities
    Collections.sort(summedCapacities, new Comparator<SummedCapacity>() {

        @Override
        public int compare(final SummedCapacity arg0, final SummedCapacity arg1) {
            if (arg0.getPercentUsed() < arg1.getPercentUsed()) {
                return 1;
            } else if (arg0.getPercentUsed().equals(arg1.getPercentUsed())) {
                return 0;
            }
            return -1;
        }
    });
    final List<CapacityVO> capacities = new ArrayList<CapacityVO>();
    Integer pageSize = null;
    try {
        pageSize = Integer.valueOf(cmd.getPageSizeVal().toString());
    } catch (final IllegalArgumentException e) {
        throw new InvalidParameterValueException("pageSize " + cmd.getPageSizeVal() + " is out of Integer range is not supported for this call");
    }
    summedCapacities = summedCapacities.subList(0, summedCapacities.size() < cmd.getPageSizeVal() ? summedCapacities.size() : pageSize);
    for (final SummedCapacity summedCapacity : summedCapacities) {
        final CapacityVO capacity = new CapacityVO(summedCapacity.getDataCenterId(), summedCapacity.getPodId(), summedCapacity.getClusterId(), summedCapacity.getCapacityType(), summedCapacity.getPercentUsed());
        capacity.setUsedCapacity(summedCapacity.getUsedCapacity() + summedCapacity.getReservedCapacity());
        capacity.setTotalCapacity(summedCapacity.getTotalCapacity());
        capacities.add(capacity);
    }
    return capacities;
}
Also used : CapacityVO(com.cloud.capacity.CapacityVO) ArrayList(java.util.ArrayList) SummedCapacity(com.cloud.capacity.dao.CapacityDaoImpl.SummedCapacity) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ArrayList(java.util.ArrayList) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) List(java.util.List)

Example 3 with SummedCapacity

use of com.cloud.capacity.dao.CapacityDaoImpl.SummedCapacity in project cloudstack by apache.

the class ManagementServerImpl method listCapacities.

@Override
public List<CapacityVO> listCapacities(final ListCapacityCmd cmd) {
    final Integer capacityType = cmd.getType();
    Long zoneId = cmd.getZoneId();
    final Long podId = cmd.getPodId();
    final Long clusterId = cmd.getClusterId();
    final Boolean fetchLatest = cmd.getFetchLatest();
    zoneId = _accountMgr.checkAccessAndSpecifyAuthority(CallContext.current().getCallingAccount(), zoneId);
    if (fetchLatest != null && fetchLatest) {
        _alertMgr.recalculateCapacity();
    }
    final List<SummedCapacity> summedCapacities = _capacityDao.findCapacityBy(capacityType, zoneId, podId, clusterId);
    final List<CapacityVO> capacities = new ArrayList<CapacityVO>();
    for (final SummedCapacity summedCapacity : summedCapacities) {
        final CapacityVO capacity = new CapacityVO(null, summedCapacity.getDataCenterId(), summedCapacity.getPodId(), summedCapacity.getClusterId(), summedCapacity.getUsedCapacity() + summedCapacity.getReservedCapacity(), summedCapacity.getTotalCapacity(), summedCapacity.getCapacityType());
        capacities.add(capacity);
    }
    // op_host_Capacity contains only allocated stats and the real time
    // stats are stored "in memory".
    // Show Sec. Storage only when the api is invoked for the zone layer.
    List<DataCenterVO> dcList = new ArrayList<DataCenterVO>();
    if (zoneId == null && podId == null && clusterId == null) {
        dcList = ApiDBUtils.listZones();
    } else if (zoneId != null) {
        dcList.add(ApiDBUtils.findZoneById(zoneId));
    } else {
        if (clusterId != null) {
            zoneId = ApiDBUtils.findClusterById(clusterId).getDataCenterId();
        } else {
            zoneId = ApiDBUtils.findPodById(podId).getDataCenterId();
        }
        if (capacityType == null || capacityType == Capacity.CAPACITY_TYPE_STORAGE) {
            capacities.add(_storageMgr.getStoragePoolUsedStats(null, clusterId, podId, zoneId));
        }
    }
    for (final DataCenterVO zone : dcList) {
        zoneId = zone.getId();
        if ((capacityType == null || capacityType == Capacity.CAPACITY_TYPE_SECONDARY_STORAGE) && podId == null && clusterId == null) {
            capacities.add(_storageMgr.getSecondaryStorageUsedStats(null, zoneId));
        }
        if (capacityType == null || capacityType == Capacity.CAPACITY_TYPE_STORAGE) {
            capacities.add(_storageMgr.getStoragePoolUsedStats(null, clusterId, podId, zoneId));
        }
    }
    return capacities;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) CapacityVO(com.cloud.capacity.CapacityVO) ArrayList(java.util.ArrayList) SummedCapacity(com.cloud.capacity.dao.CapacityDaoImpl.SummedCapacity)

Example 4 with SummedCapacity

use of com.cloud.capacity.dao.CapacityDaoImpl.SummedCapacity in project cloudstack by apache.

the class ManagementServerImpl method getStorageUsed.

List<SummedCapacity> getStorageUsed(Long clusterId, Long podId, Long zoneId, Integer capacityType) {
    if (capacityType == null || capacityType == Capacity.CAPACITY_TYPE_SECONDARY_STORAGE) {
        final List<SummedCapacity> list = new ArrayList<SummedCapacity>();
        if (zoneId != null) {
            final DataCenterVO zone = ApiDBUtils.findZoneById(zoneId);
            if (zone == null || zone.getAllocationState() == AllocationState.Disabled) {
                return null;
            }
            List<CapacityVO> capacities = new ArrayList<CapacityVO>();
            capacities.add(_storageMgr.getSecondaryStorageUsedStats(null, zoneId));
            capacities.add(_storageMgr.getStoragePoolUsedStats(null, clusterId, podId, zoneId));
            for (CapacityVO capacity : capacities) {
                if (capacity.getTotalCapacity() != 0) {
                    capacity.setUsedPercentage((float) capacity.getUsedCapacity() / capacity.getTotalCapacity());
                } else {
                    capacity.setUsedPercentage(0);
                }
                final SummedCapacity summedCapacity = new SummedCapacity(capacity.getUsedCapacity(), capacity.getTotalCapacity(), capacity.getUsedPercentage(), capacity.getCapacityType(), capacity.getDataCenterId(), capacity.getPodId(), capacity.getClusterId());
                list.add(summedCapacity);
            }
        } else {
            List<DataCenterVO> dcList = _dcDao.listEnabledZones();
            for (DataCenterVO dc : dcList) {
                List<CapacityVO> capacities = new ArrayList<CapacityVO>();
                capacities.add(_storageMgr.getSecondaryStorageUsedStats(null, dc.getId()));
                capacities.add(_storageMgr.getStoragePoolUsedStats(null, null, null, dc.getId()));
                for (CapacityVO capacity : capacities) {
                    if (capacity.getTotalCapacity() != 0) {
                        capacity.setUsedPercentage((float) capacity.getUsedCapacity() / capacity.getTotalCapacity());
                    } else {
                        capacity.setUsedPercentage(0);
                    }
                    SummedCapacity summedCapacity = new SummedCapacity(capacity.getUsedCapacity(), capacity.getTotalCapacity(), capacity.getUsedPercentage(), capacity.getCapacityType(), capacity.getDataCenterId(), capacity.getPodId(), capacity.getClusterId());
                    list.add(summedCapacity);
                }
            }
        // End of for
        }
        return list;
    }
    return null;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) CapacityVO(com.cloud.capacity.CapacityVO) ArrayList(java.util.ArrayList) SummedCapacity(com.cloud.capacity.dao.CapacityDaoImpl.SummedCapacity)

Example 5 with SummedCapacity

use of com.cloud.capacity.dao.CapacityDaoImpl.SummedCapacity in project cloudstack by apache.

the class AlertManagerImpl method checkForAlerts.

public void checkForAlerts() {
    recalculateCapacity();
    // abort if we can't possibly send an alert...
    if (_emailAlert == null) {
        return;
    }
    //Get all datacenters, pods and clusters in the system.
    List<DataCenterVO> dataCenterList = _dcDao.listAll();
    List<ClusterVO> clusterList = _clusterDao.listAll();
    List<HostPodVO> podList = _podDao.listAll();
    //Get capacity types at different levels
    List<Short> dataCenterCapacityTypes = getCapacityTypesAtZoneLevel();
    List<Short> podCapacityTypes = getCapacityTypesAtPodLevel();
    List<Short> clusterCapacityTypes = getCapacityTypesAtClusterLevel();
    // Generate Alerts for Zone Level capacities
    for (DataCenterVO dc : dataCenterList) {
        for (Short capacityType : dataCenterCapacityTypes) {
            List<SummedCapacity> capacity = new ArrayList<SummedCapacity>();
            capacity = _capacityDao.findCapacityBy(capacityType.intValue(), dc.getId(), null, null);
            if (capacityType == Capacity.CAPACITY_TYPE_SECONDARY_STORAGE) {
                capacity.add(getUsedStats(capacityType, dc.getId(), null, null));
            }
            if (capacity == null || capacity.size() == 0) {
                continue;
            }
            double totalCapacity = capacity.get(0).getTotalCapacity();
            double usedCapacity = capacity.get(0).getUsedCapacity();
            if (totalCapacity != 0 && usedCapacity / totalCapacity > _capacityTypeThresholdMap.get(capacityType)) {
                generateEmailAlert(dc, null, null, totalCapacity, usedCapacity, capacityType);
            }
        }
    }
    // Generate Alerts for Pod Level capacities
    for (HostPodVO pod : podList) {
        for (Short capacityType : podCapacityTypes) {
            List<SummedCapacity> capacity = _capacityDao.findCapacityBy(capacityType.intValue(), pod.getDataCenterId(), pod.getId(), null);
            if (capacity == null || capacity.size() == 0) {
                continue;
            }
            double totalCapacity = capacity.get(0).getTotalCapacity();
            double usedCapacity = capacity.get(0).getUsedCapacity();
            if (totalCapacity != 0 && usedCapacity / totalCapacity > _capacityTypeThresholdMap.get(capacityType)) {
                generateEmailAlert(ApiDBUtils.findZoneById(pod.getDataCenterId()), pod, null, totalCapacity, usedCapacity, capacityType);
            }
        }
    }
    // Generate Alerts for Cluster Level capacities
    for (ClusterVO cluster : clusterList) {
        for (Short capacityType : clusterCapacityTypes) {
            List<SummedCapacity> capacity = new ArrayList<SummedCapacity>();
            capacity = _capacityDao.findCapacityBy(capacityType.intValue(), cluster.getDataCenterId(), null, cluster.getId());
            // cpu and memory allocated capacity notification threshold can be defined at cluster level, so getting the value if they are defined at cluster level
            double threshold = 0;
            switch(capacityType) {
                case Capacity.CAPACITY_TYPE_STORAGE:
                    capacity.add(getUsedStats(capacityType, cluster.getDataCenterId(), cluster.getPodId(), cluster.getId()));
                    threshold = StorageCapacityThreshold.valueIn(cluster.getId());
                    break;
                case Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED:
                    threshold = StorageAllocatedCapacityThreshold.valueIn(cluster.getId());
                    break;
                case Capacity.CAPACITY_TYPE_CPU:
                    threshold = CPUCapacityThreshold.valueIn(cluster.getId());
                    break;
                case Capacity.CAPACITY_TYPE_MEMORY:
                    threshold = MemoryCapacityThreshold.valueIn(cluster.getId());
                    break;
                default:
                    threshold = _capacityTypeThresholdMap.get(capacityType);
            }
            if (capacity == null || capacity.size() == 0) {
                continue;
            }
            double totalCapacity = capacity.get(0).getTotalCapacity();
            double usedCapacity = capacity.get(0).getUsedCapacity() + capacity.get(0).getReservedCapacity();
            if (totalCapacity != 0 && usedCapacity / totalCapacity > threshold) {
                generateEmailAlert(ApiDBUtils.findZoneById(cluster.getDataCenterId()), ApiDBUtils.findPodById(cluster.getPodId()), cluster, totalCapacity, usedCapacity, capacityType);
            }
        }
    }
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) ClusterVO(com.cloud.dc.ClusterVO) ArrayList(java.util.ArrayList) SummedCapacity(com.cloud.capacity.dao.CapacityDaoImpl.SummedCapacity) HostPodVO(com.cloud.dc.HostPodVO)

Aggregations

SummedCapacity (com.cloud.capacity.dao.CapacityDaoImpl.SummedCapacity)8 ArrayList (java.util.ArrayList)6 CapacityResponse (org.apache.cloudstack.api.response.CapacityResponse)4 CapacityVO (com.cloud.capacity.CapacityVO)3 DataCenter (com.cloud.dc.DataCenter)3 DataCenterVO (com.cloud.dc.DataCenterVO)3 HostPodVO (com.cloud.dc.HostPodVO)3 HashSet (java.util.HashSet)3 ClusterVO (com.cloud.dc.ClusterVO)2 VgpuTypesInfo (com.cloud.agent.api.VgpuTypesInfo)1 Capacity (com.cloud.capacity.Capacity)1 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)1 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 List (java.util.List)1 ClusterResponse (org.apache.cloudstack.api.response.ClusterResponse)1 PodResponse (org.apache.cloudstack.api.response.PodResponse)1