From b336a8b8130c99854be3a94cab44229af8f89508 Mon Sep 17 00:00:00 2001 From: Nitin Date: Mon, 24 Oct 2011 16:19:54 +0530 Subject: [PATCH] bug 10774: On removing storage, local storage, cluster handle deletion of corresponding op_host_capacity rows. --- .../cloud/api/response/CapacityResponse.java | 24 +++++++++- core/src/com/cloud/alert/AlertManager.java | 3 ++ .../src/com/cloud/alert/AlertManagerImpl.java | 32 +++++++++---- server/src/com/cloud/api/ApiDBUtils.java | 8 ++++ .../src/com/cloud/api/ApiResponseHelper.java | 48 ++++++++++++++++--- 5 files changed, 99 insertions(+), 16 deletions(-) mode change 100644 => 100755 api/src/com/cloud/api/response/CapacityResponse.java diff --git a/api/src/com/cloud/api/response/CapacityResponse.java b/api/src/com/cloud/api/response/CapacityResponse.java old mode 100644 new mode 100755 index 6284551bd79..6071e1fc549 --- a/api/src/com/cloud/api/response/CapacityResponse.java +++ b/api/src/com/cloud/api/response/CapacityResponse.java @@ -36,6 +36,12 @@ public class CapacityResponse extends BaseResponse { @SerializedName("podname") @Param(description="the Pod name") private String podName; + + @SerializedName(ApiConstants.CLUSTER_ID) @Param(description="the Cluster ID") + private Long clusterId; + + @SerializedName("clustername") @Param(description="the Cluster name") + private String clusterName; @SerializedName("capacityused") @Param(description="the capacity currently in use") private Long capacityUsed; @@ -86,7 +92,23 @@ public class CapacityResponse extends BaseResponse { this.podName = podName; } - public Long getCapacityUsed() { + public Long getClusterId() { + return clusterId; + } + + public void setClusterId(Long clusterId) { + this.clusterId = clusterId; + } + + public String getClusterName() { + return clusterName; + } + + public void setClusterName(String clusterName) { + this.clusterName = clusterName; + } + + public Long getCapacityUsed() { return capacityUsed; } diff --git a/core/src/com/cloud/alert/AlertManager.java b/core/src/com/cloud/alert/AlertManager.java index ecb52e92f80..3c53975f945 100755 --- a/core/src/com/cloud/alert/AlertManager.java +++ b/core/src/com/cloud/alert/AlertManager.java @@ -46,6 +46,9 @@ public interface AlertManager extends Manager { public static final short ALERT_TYPE_STORAGE_DELETE = 20; public static final short ALERT_TYPE_UPDATE_RESOURCE_COUNT = 21; // Generated when we fail to update the resource count public static final short ALERT_TYPE_USAGE_SANITY_RESULT = 22; + public static final short ALERT_TYPE_DIRECT_ATTACHED_PUBLIC_IP = 23; + public static final short ALERT_TYPE_LOCAL_STORAGE = 24; + void clearAlert(short alertType, long dataCenterId, long podId); diff --git a/server/src/com/cloud/alert/AlertManagerImpl.java b/server/src/com/cloud/alert/AlertManagerImpl.java index 98ecb126d7d..7592469c78c 100755 --- a/server/src/com/cloud/alert/AlertManagerImpl.java +++ b/server/src/com/cloud/alert/AlertManagerImpl.java @@ -53,6 +53,7 @@ import com.cloud.configuration.dao.ConfigurationDao; import com.cloud.dc.ClusterVO; import com.cloud.dc.DataCenterVO; import com.cloud.dc.HostPodVO; +import com.cloud.dc.DataCenter.NetworkType; import com.cloud.dc.Vlan.VlanType; import com.cloud.dc.dao.ClusterDao; import com.cloud.dc.dao.DataCenterDao; @@ -313,19 +314,23 @@ public class AlertManagerImpl implements AlertManager { //implementing the same // Calculate new Public IP capacity for Virtual Network - s_logger.trace("Executing public ip capacity update for Virtual Network"); - createOrUpdateIpCapacity(dcId, null, CapacityVO.CAPACITY_TYPE_VIRTUAL_NETWORK_PUBLIC_IP); - s_logger.trace("Done with public ip capacity update for Virtual Network"); + if (datacenter.getNetworkType() == NetworkType.Advanced){ + s_logger.trace("Executing public ip capacity update for Virtual Network"); + createOrUpdateIpCapacity(dcId, null, CapacityVO.CAPACITY_TYPE_VIRTUAL_NETWORK_PUBLIC_IP); + s_logger.trace("Done with public ip capacity update for Virtual Network"); + } // Calculate new Public IP capacity for Direct Attached Network s_logger.trace("Executing public ip capacity update for Direct Attached Network"); createOrUpdateIpCapacity(dcId, null, CapacityVO.CAPACITY_TYPE_DIRECT_ATTACHED_PUBLIC_IP); s_logger.trace("Done with public ip capacity update for Direct Attached Network"); - //Calculate VLAN's capacity - s_logger.trace("Executing VLAN capacity update"); - createOrUpdateVlanCapacity(dcId); - s_logger.trace("Executing VLAN capacity update"); + if (datacenter.getNetworkType() == NetworkType.Advanced){ + //Calculate VLAN's capacity + s_logger.trace("Executing VLAN capacity update"); + createOrUpdateVlanCapacity(dcId); + s_logger.trace("Executing VLAN capacity update"); + } } @@ -497,6 +502,7 @@ public class AlertManagerImpl implements AlertManager { String totalStr; String usedStr; String pctStr = formatPercent(usedCapacity/totalCapacity); + short alertType = -1; switch (capacityType) { @@ -506,30 +512,35 @@ public class AlertManagerImpl implements AlertManager { totalStr = formatBytesToMegabytes(totalCapacity); usedStr = formatBytesToMegabytes(usedCapacity); msgContent = "System memory is low, total: " + totalStr + " MB, used: " + usedStr + " MB (" + pctStr + "%)"; + alertType = ALERT_TYPE_MEMORY; break; case CapacityVO.CAPACITY_TYPE_CPU: msgSubject = "System Alert: Low Unallocated CPU in cluster " +cluster.getName()+ " pod " +pod.getName()+ " of availablity zone " + dc.getName(); totalStr = _dfWhole.format(totalCapacity); usedStr = _dfWhole.format(usedCapacity); msgContent = "Unallocated CPU is low, total: " + totalStr + " Mhz, used: " + usedStr + " Mhz (" + pctStr + "%)"; + alertType = ALERT_TYPE_CPU; break; case CapacityVO.CAPACITY_TYPE_STORAGE: msgSubject = "System Alert: Low Available Storage in cluster " +cluster.getName()+ " pod " +pod.getName()+ " of availablity zone " + dc.getName(); totalStr = formatBytesToMegabytes(totalCapacity); usedStr = formatBytesToMegabytes(usedCapacity); msgContent = "Available storage space is low, total: " + totalStr + " MB, used: " + usedStr + " MB (" + pctStr + "%)"; + alertType = ALERT_TYPE_STORAGE; break; case CapacityVO.CAPACITY_TYPE_STORAGE_ALLOCATED: msgSubject = "System Alert: Remaining unallocated Storage is low in cluster " +cluster.getName()+ " pod " +pod.getName()+ " of availablity zone " + dc.getName(); totalStr = formatBytesToMegabytes(totalCapacity); usedStr = formatBytesToMegabytes(usedCapacity); msgContent = "Unallocated storage space is low, total: " + totalStr + " MB, allocated: " + usedStr + " MB (" + pctStr + "%)"; + alertType = ALERT_TYPE_STORAGE_ALLOCATED; break; case CapacityVO.CAPACITY_TYPE_LOCAL_STORAGE: msgSubject = "System Alert: Remaining unallocated Local Storage is low in cluster " +cluster.getName()+ " pod " +pod.getName()+ " of availablity zone " + dc.getName(); totalStr = formatBytesToMegabytes(totalCapacity); usedStr = formatBytesToMegabytes(usedCapacity); msgContent = "Unallocated storage space is low, total: " + totalStr + " MB, allocated: " + usedStr + " MB (" + pctStr + "%)"; + alertType = ALERT_TYPE_LOCAL_STORAGE; break; //Pod Level @@ -538,6 +549,7 @@ public class AlertManagerImpl implements AlertManager { totalStr = Double.toString(totalCapacity); usedStr = Double.toString(usedCapacity); msgContent = "Number of unallocated private IPs is low, total: " + totalStr + ", allocated: " + usedStr + " (" + pctStr + "%)"; + alertType = ALERT_TYPE_PRIVATE_IP; break; //Zone Level @@ -546,29 +558,33 @@ public class AlertManagerImpl implements AlertManager { totalStr = formatBytesToMegabytes(totalCapacity); usedStr = formatBytesToMegabytes(usedCapacity); msgContent = "Available secondary storage space is low, total: " + totalStr + " MB, used: " + usedStr + " MB (" + pctStr + "%)"; + alertType = ALERT_TYPE_SECONDARY_STORAGE; break; case CapacityVO.CAPACITY_TYPE_VIRTUAL_NETWORK_PUBLIC_IP: msgSubject = "System Alert: Number of unallocated virtual network public IPs is low in availablity zone " + dc.getName(); totalStr = Double.toString(totalCapacity); usedStr = Double.toString(usedCapacity); msgContent = "Number of unallocated public IPs is low, total: " + totalStr + ", allocated: " + usedStr + " (" + pctStr + "%)"; + alertType = ALERT_TYPE_VIRTUAL_NETWORK_PUBLIC_IP; break; case CapacityVO.CAPACITY_TYPE_DIRECT_ATTACHED_PUBLIC_IP: msgSubject = "System Alert: Number of unallocated direct attached public IPs is low in availablity zone " + dc.getName(); totalStr = Double.toString(totalCapacity); usedStr = Double.toString(usedCapacity); msgContent = "Number of unallocated direct attached public IPs is low, total: " + totalStr + ", allocated: " + usedStr + " (" + pctStr + "%)"; + alertType = ALERT_TYPE_DIRECT_ATTACHED_PUBLIC_IP; break; case CapacityVO.CAPACITY_TYPE_VLAN: msgSubject = "System Alert: Number of unallocated VLANs is low in availablity zone " + dc.getName(); totalStr = Double.toString(totalCapacity); usedStr = Double.toString(usedCapacity); msgContent = "Number of unallocated VLANs is low, total: " + totalStr + ", allocated: " + usedStr + " (" + pctStr + "%)"; + alertType = ALERT_TYPE_VLAN; break; } try { - _emailAlert.sendAlert(capacityType, dc.getId(), null, msgSubject, msgContent); + _emailAlert.sendAlert(alertType, dc.getId(), null, msgSubject, msgContent); } catch (Exception ex) { s_logger.error("Exception in CapacityChecker", ex); } diff --git a/server/src/com/cloud/api/ApiDBUtils.java b/server/src/com/cloud/api/ApiDBUtils.java index 047b7380f25..0aaff4a1937 100755 --- a/server/src/com/cloud/api/ApiDBUtils.java +++ b/server/src/com/cloud/api/ApiDBUtils.java @@ -350,7 +350,15 @@ public class ApiDBUtils { public static StorageStats getSecondaryStorageStatistics(long id) { return _statsCollector.getStorageStats(id); } + + public static CapacityVO getStoragePoolUsedStats(Long poolId, Long clusterId, Long podId, Long zoneId){ + return _storageMgr.getStoragePoolUsedStats(poolId, clusterId, podId, zoneId); + } + public static CapacityVO getSecondaryStorageUsedStats(Long hostId, Long zoneId){ + return _storageMgr.getSecondaryStorageUsedStats(hostId, zoneId); + } + // /////////////////////////////////////////////////////////// // Dao methods // // /////////////////////////////////////////////////////////// diff --git a/server/src/com/cloud/api/ApiResponseHelper.java b/server/src/com/cloud/api/ApiResponseHelper.java index ed28ae784b4..4df1c20904d 100755 --- a/server/src/com/cloud/api/ApiResponseHelper.java +++ b/server/src/com/cloud/api/ApiResponseHelper.java @@ -763,6 +763,8 @@ public class ApiResponseHelper implements ResponseGenerator { } capacityResponses.add(capacityResponse); } + // Do it for stats as well. + capacityResponses.addAll(getStatsCapacityresponse(null, null, pod.getId(), pod.getDataCenterId())); podResponse.setCapacitites(new ArrayList(capacityResponses)); } podResponse.setObjectName("pod"); @@ -810,6 +812,9 @@ public class ApiResponseHelper implements ResponseGenerator { } capacityResponses.add(capacityResponse); } + // Do it for stats as well. + capacityResponses.addAll(getStatsCapacityresponse(null, null, null, dataCenter.getId())); + zoneResponse.setCapacitites(new ArrayList(capacityResponses)); } @@ -823,6 +828,25 @@ public class ApiResponseHelper implements ResponseGenerator { return zoneResponse; } + private List getStatsCapacityresponse(Long poolId, Long clusterId, Long podId, Long zoneId){ + List capacities = new ArrayList(); + capacities.add(ApiDBUtils.getStoragePoolUsedStats(poolId, clusterId, podId, zoneId)); + if(clusterId == null && podId == null){ + capacities.add(ApiDBUtils.getSecondaryStorageUsedStats(poolId, zoneId)); + } + + List capacityResponses = new ArrayList(); + for (CapacityVO capacity : capacities){ + CapacityResponse capacityResponse = new CapacityResponse(); + capacityResponse.setCapacityType(capacity.getCapacityType()); + capacityResponse.setCapacityUsed(capacity.getUsedCapacity()); + capacityResponse.setCapacityTotal(capacity.getTotalCapacity()); + capacityResponses.add(capacityResponse); + } + + return capacityResponses; + } + @Override public VolumeResponse createVolumeResponse(Volume volume) { VolumeResponse volResponse = new VolumeResponse(); @@ -1012,6 +1036,8 @@ public class ApiResponseHelper implements ResponseGenerator { capacityResponses.add(capacityResponse); } + // Do it for stats as well. + capacityResponses.addAll(getStatsCapacityresponse(null, null, pod.getId(), pod.getDataCenterId())); clusterResponse.setCapacitites(new ArrayList(capacityResponses)); } clusterResponse.setObjectName("cluster"); @@ -1877,15 +1903,23 @@ public class ApiResponseHelper implements ResponseGenerator { capacityResponse.setCapacityUsed(summedCapacity.getUsedCapacity()); if (summedCapacity.getPodId() != null) { capacityResponse.setPodId(summedCapacity.getPodId()); - if (summedCapacity.getPodId() > 0) { - HostPodVO pod = ApiDBUtils.findPodById(summedCapacity.getPodId()); - if (pod != null) { - capacityResponse.setPodName(pod.getName()); - } - } else { - capacityResponse.setPodName("All"); + HostPodVO pod = ApiDBUtils.findPodById(summedCapacity.getPodId()); + if (pod != null) { + capacityResponse.setPodName(pod.getName()); } } + if (summedCapacity.getClusterId() != null) { + capacityResponse.setClusterId(summedCapacity.getClusterId()); + ClusterVO cluster = ApiDBUtils.findClusterById(summedCapacity.getClusterId()); + if (cluster != null) { + capacityResponse.setClusterName(cluster.getName()); + if (summedCapacity.getPodId() == null){ + long podId = cluster.getPodId(); + capacityResponse.setPodId(podId); + capacityResponse.setPodName(ApiDBUtils.findPodById(podId).getName()); + } + } + } capacityResponse.setZoneId(summedCapacity.getDataCenterId()); capacityResponse.setZoneName(ApiDBUtils.findZoneById(summedCapacity.getDataCenterId()).getName()); if (summedCapacity.getTotalCapacity() != 0) {