// persist entry in template_zone_ref table. zoneId can be empty for // region-wide image store, in that case, // we will associate the template to all the zones. @Override public void associateTemplateToZone(long templateId, Long zoneId) { List<Long> dcs = new ArrayList<Long>(); if (zoneId != null) { dcs.add(zoneId); } else { List<DataCenterVO> zones = _dcDao.listAll(); for (DataCenterVO zone : zones) { dcs.add(zone.getId()); } } for (Long id : dcs) { VMTemplateZoneVO tmpltZoneVO = _vmTemplateZoneDao.findByZoneTemplate(id, templateId); if (tmpltZoneVO == null) { tmpltZoneVO = new VMTemplateZoneVO(id, templateId, new Date()); _vmTemplateZoneDao.persist(tmpltZoneVO); } else { tmpltZoneVO.setLastUpdated(new Date()); _vmTemplateZoneDao.update(tmpltZoneVO.getId(), tmpltZoneVO); } } }
@Override public boolean downloadTemplateToStorage(VMTemplateVO template, Long zoneId) { List<DataCenterVO> dcs = new ArrayList<DataCenterVO>(); if (zoneId == null) { dcs.addAll(_dcDao.listAll()); } else { dcs.add(_dcDao.findById(zoneId)); } long templateId = template.getId(); boolean isPublic = template.isFeatured() || template.isPublicTemplate(); for (DataCenterVO dc : dcs) { List<HostVO> ssHosts = _ssvmMgr.listAllTypesSecondaryStorageHostsInOneZone(dc.getId()); for (HostVO ssHost : ssHosts) { if (isTemplateUpdateable(templateId, ssHost.getId())) { initiateTemplateDownload(templateId, ssHost); if (!isPublic) { break; } } } } return true; }
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>(); float overProvFactor = 1f; capacity = _capacityDao.findCapacityBy( capacityType.intValue(), cluster.getDataCenterId(), null, cluster.getId()); if (capacityType == Capacity.CAPACITY_TYPE_STORAGE) { capacity.add( getUsedStats( capacityType, cluster.getDataCenterId(), cluster.getPodId(), cluster.getId())); } if (capacity == null || capacity.size() == 0) { continue; } if (capacityType == Capacity.CAPACITY_TYPE_CPU) { overProvFactor = ApiDBUtils.getCpuOverprovisioningFactor(); } double totalCapacity = capacity.get(0).getTotalCapacity() * overProvFactor; double usedCapacity = capacity.get(0).getUsedCapacity() + capacity.get(0).getReservedCapacity(); if (totalCapacity != 0 && usedCapacity / totalCapacity > _capacityTypeThresholdMap.get(capacityType)) { generateEmailAlert( ApiDBUtils.findZoneById(cluster.getDataCenterId()), ApiDBUtils.findPodById(cluster.getPodId()), cluster, totalCapacity, usedCapacity, capacityType); } } } }
@Override @DB public void recalculateCapacity() { // FIXME: the right way to do this is to register a listener (see RouterStatsListener, // VMSyncListener) // for the vm sync state. The listener model has connects/disconnects to keep things in // sync much better // than this model right now, so when a VM is started, we update the amount allocated, // and when a VM // is stopped we updated the amount allocated, and when VM sync reports a changed state, // we update // the amount allocated. Hopefully it's limited to 3 entry points and will keep the // amount allocated // per host accurate. try { if (s_logger.isDebugEnabled()) { s_logger.debug("recalculating system capacity"); s_logger.debug("Executing cpu/ram capacity update"); } // Calculate CPU and RAM capacities // get all hosts...even if they are not in 'UP' state List<HostVO> hosts = _resourceMgr.listAllHostsInAllZonesByType(Host.Type.Routing); for (HostVO host : hosts) { _capacityMgr.updateCapacityForHost(host); } if (s_logger.isDebugEnabled()) { s_logger.debug("Done executing cpu/ram capacity update"); s_logger.debug("Executing storage capacity update"); } // Calculate storage pool capacity List<StoragePoolVO> storagePools = _storagePoolDao.listAll(); for (StoragePoolVO pool : storagePools) { long disk = 0l; Pair<Long, Long> sizes = _volumeDao.getCountAndTotalByPool(pool.getId()); disk = sizes.second(); if (pool.isShared()) { _storageMgr.createCapacityEntry(pool, Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED, disk); } else { _storageMgr.createCapacityEntry(pool, Capacity.CAPACITY_TYPE_LOCAL_STORAGE, disk); } } if (s_logger.isDebugEnabled()) { s_logger.debug("Done executing storage capacity update"); s_logger.debug("Executing capacity updates public ip and Vlans"); } List<DataCenterVO> datacenters = _dcDao.listAll(); for (DataCenterVO datacenter : datacenters) { long dcId = datacenter.getId(); // NOTE // What happens if we have multiple vlans? Dashboard currently shows stats // with no filter based on a vlan // ideal way would be to remove out the vlan param, and filter only on dcId // implementing the same // Calculate new Public IP capacity for Virtual Network if (datacenter.getNetworkType() == NetworkType.Advanced) { createOrUpdateIpCapacity(dcId, null, CapacityVO.CAPACITY_TYPE_VIRTUAL_NETWORK_PUBLIC_IP); } // Calculate new Public IP capacity for Direct Attached Network createOrUpdateIpCapacity(dcId, null, CapacityVO.CAPACITY_TYPE_DIRECT_ATTACHED_PUBLIC_IP); if (datacenter.getNetworkType() == NetworkType.Advanced) { // Calculate VLAN's capacity createOrUpdateVlanCapacity(dcId); } } if (s_logger.isDebugEnabled()) { s_logger.debug("Done capacity updates for public ip and Vlans"); s_logger.debug("Executing capacity updates for private ip"); } // Calculate new Private IP capacity List<HostPodVO> pods = _podDao.listAll(); for (HostPodVO pod : pods) { long podId = pod.getId(); long dcId = pod.getDataCenterId(); createOrUpdateIpCapacity(dcId, podId, CapacityVO.CAPACITY_TYPE_PRIVATE_IP); } if (s_logger.isDebugEnabled()) { s_logger.debug("Done executing capacity updates for private ip"); s_logger.debug("Done recalculating system capacity"); } } catch (Throwable t) { s_logger.error("Caught exception in recalculating capacity", t); } }