Skip to content

Commit 15b68b4

Browse files
hsato03Henrique Sato
authored andcommitted
Refactor alert email generation method (apache#8831)
Co-authored-by: Henrique Sato <[email protected]>
1 parent f2db8bd commit 15b68b4

File tree

1 file changed

+67
-93
lines changed

1 file changed

+67
-93
lines changed

server/src/main/java/com/cloud/alert/AlertManagerImpl.java

Lines changed: 67 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -627,107 +627,81 @@ private void generateEmailAlert(DataCenterVO dc, HostPodVO pod, ClusterVO cluste
627627

628628
String msgSubject = null;
629629
String msgContent = null;
630-
String totalStr;
631-
String usedStr;
632-
String pctStr = formatPercent(usedCapacity / totalCapacity);
630+
String percentual = formatPercent(usedCapacity / totalCapacity);
631+
String totalInMB = formatBytesToMegabytes(totalCapacity);
632+
String usedInMB = formatBytesToMegabytes(usedCapacity);
633+
String totalInString = String.valueOf(totalCapacity);
634+
String usedInString = String.valueOf(usedCapacity);
633635
AlertType alertType = null;
634636
Long podId = pod == null ? null : pod.getId();
635637
Long clusterId = cluster == null ? null : cluster.getId();
638+
String clusterName = cluster == null ? null : cluster.getName();
639+
String podName = pod == null ? null : pod.getName();
640+
String dataCenterName = dc.getName();
636641

637642
switch (capacityType) {
638-
639-
//Cluster Level
640-
case Capacity.CAPACITY_TYPE_MEMORY:
641-
msgSubject = "System Alert: Low Available Memory in cluster " + cluster.getName() + " pod " + pod.getName() + " of availability zone " + dc.getName();
642-
totalStr = formatBytesToMegabytes(totalCapacity);
643-
usedStr = formatBytesToMegabytes(usedCapacity);
644-
msgContent = "System memory is low, total: " + totalStr + " MB, used: " + usedStr + " MB (" + pctStr + "%)";
645-
alertType = AlertManager.AlertType.ALERT_TYPE_MEMORY;
646-
break;
647-
case Capacity.CAPACITY_TYPE_CPU:
648-
msgSubject = "System Alert: Low Unallocated CPU in cluster " + cluster.getName() + " pod " + pod.getName() + " of availability zone " + dc.getName();
649-
totalStr = DfWhole.format(totalCapacity);
650-
usedStr = DfWhole.format(usedCapacity);
651-
msgContent = "Unallocated CPU is low, total: " + totalStr + " Mhz, used: " + usedStr + " Mhz (" + pctStr + "%)";
652-
alertType = AlertManager.AlertType.ALERT_TYPE_CPU;
653-
break;
654-
case Capacity.CAPACITY_TYPE_STORAGE:
655-
msgSubject = "System Alert: Low Available Storage in cluster " + cluster.getName() + " pod " + pod.getName() + " of availability zone " + dc.getName();
656-
totalStr = formatBytesToMegabytes(totalCapacity);
657-
usedStr = formatBytesToMegabytes(usedCapacity);
658-
msgContent = "Available storage space is low, total: " + totalStr + " MB, used: " + usedStr + " MB (" + pctStr + "%)";
659-
alertType = AlertManager.AlertType.ALERT_TYPE_STORAGE;
660-
break;
661-
case Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED:
662-
msgSubject = "System Alert: Remaining unallocated Storage is low in cluster " + cluster.getName() + " pod " + pod.getName() + " of availability zone " +
663-
dc.getName();
664-
totalStr = formatBytesToMegabytes(totalCapacity);
665-
usedStr = formatBytesToMegabytes(usedCapacity);
666-
msgContent = "Unallocated storage space is low, total: " + totalStr + " MB, allocated: " + usedStr + " MB (" + pctStr + "%)";
667-
alertType = AlertManager.AlertType.ALERT_TYPE_STORAGE_ALLOCATED;
668-
break;
669-
case Capacity.CAPACITY_TYPE_LOCAL_STORAGE:
670-
msgSubject = "System Alert: Remaining unallocated Local Storage is low in cluster " + cluster.getName() + " pod " + pod.getName() + " of availability zone " +
671-
dc.getName();
672-
totalStr = formatBytesToMegabytes(totalCapacity);
673-
usedStr = formatBytesToMegabytes(usedCapacity);
674-
msgContent = "Unallocated storage space is low, total: " + totalStr + " MB, allocated: " + usedStr + " MB (" + pctStr + "%)";
675-
alertType = AlertManager.AlertType.ALERT_TYPE_LOCAL_STORAGE;
676-
break;
677-
678-
//Pod Level
679-
case Capacity.CAPACITY_TYPE_PRIVATE_IP:
680-
msgSubject = "System Alert: Number of unallocated private IPs is low in pod " + pod.getName() + " of availability zone " + dc.getName();
681-
totalStr = Double.toString(totalCapacity);
682-
usedStr = Double.toString(usedCapacity);
683-
msgContent = "Number of unallocated private IPs is low, total: " + totalStr + ", allocated: " + usedStr + " (" + pctStr + "%)";
684-
alertType = AlertManager.AlertType.ALERT_TYPE_PRIVATE_IP;
685-
break;
686-
687-
//Zone Level
688-
case Capacity.CAPACITY_TYPE_SECONDARY_STORAGE:
689-
msgSubject = "System Alert: Low Available Secondary Storage in availability zone " + dc.getName();
690-
totalStr = formatBytesToMegabytes(totalCapacity);
691-
usedStr = formatBytesToMegabytes(usedCapacity);
692-
msgContent = "Available secondary storage space is low, total: " + totalStr + " MB, used: " + usedStr + " MB (" + pctStr + "%)";
693-
alertType = AlertManager.AlertType.ALERT_TYPE_SECONDARY_STORAGE;
694-
break;
695-
case Capacity.CAPACITY_TYPE_VIRTUAL_NETWORK_PUBLIC_IP:
696-
msgSubject = "System Alert: Number of unallocated virtual network public IPs is low in availability zone " + dc.getName();
697-
totalStr = Double.toString(totalCapacity);
698-
usedStr = Double.toString(usedCapacity);
699-
msgContent = "Number of unallocated public IPs is low, total: " + totalStr + ", allocated: " + usedStr + " (" + pctStr + "%)";
700-
alertType = AlertManager.AlertType.ALERT_TYPE_VIRTUAL_NETWORK_PUBLIC_IP;
701-
break;
702-
case Capacity.CAPACITY_TYPE_DIRECT_ATTACHED_PUBLIC_IP:
703-
msgSubject = "System Alert: Number of unallocated shared network IPs is low in availability zone " + dc.getName();
704-
totalStr = Double.toString(totalCapacity);
705-
usedStr = Double.toString(usedCapacity);
706-
msgContent = "Number of unallocated shared network IPs is low, total: " + totalStr + ", allocated: " + usedStr + " (" + pctStr + "%)";
707-
alertType = AlertManager.AlertType.ALERT_TYPE_DIRECT_ATTACHED_PUBLIC_IP;
708-
break;
709-
case Capacity.CAPACITY_TYPE_VLAN:
710-
msgSubject = "System Alert: Number of unallocated VLANs is low in availability zone " + dc.getName();
711-
totalStr = Double.toString(totalCapacity);
712-
usedStr = Double.toString(usedCapacity);
713-
msgContent = "Number of unallocated VLANs is low, total: " + totalStr + ", allocated: " + usedStr + " (" + pctStr + "%)";
714-
alertType = AlertManager.AlertType.ALERT_TYPE_VLAN;
715-
break;
716-
case Capacity.CAPACITY_TYPE_VIRTUAL_NETWORK_IPV6_SUBNET:
717-
msgSubject = "System Alert: Number of unallocated virtual network guest IPv6 subnets is low in availability zone " + dc.getName();
718-
totalStr = Double.toString(totalCapacity);
719-
usedStr = Double.toString(usedCapacity);
720-
msgContent = "Number of unallocated virtual network guest IPv6 subnets is low, total: " + totalStr + ", allocated: " + usedStr + " (" + pctStr + "%)";
721-
alertType = AlertManager.AlertType.ALERT_TYPE_VIRTUAL_NETWORK_IPV6_SUBNET;
722-
break;
643+
case Capacity.CAPACITY_TYPE_MEMORY:
644+
msgSubject = String.format("System Alert: Low Available Memory in cluster [%s] pod [%s] of availability zone [%s].", clusterName, podName, dataCenterName);
645+
msgContent = String.format("System memory is low, total: %s MB, used: %s MB (%s%%).", totalInMB, usedInMB, percentual);
646+
alertType = AlertManager.AlertType.ALERT_TYPE_MEMORY;
647+
break;
648+
case Capacity.CAPACITY_TYPE_CPU:
649+
msgSubject = String.format("System Alert: Low Unallocated CPU in cluster [%s] pod [%s] of availability zone [%s].", clusterName, podName, dataCenterName);
650+
msgContent = String.format("Unallocated CPU is low, total: %s Mhz, used: %s Mhz (%s%%).", DfWhole.format(totalCapacity), DfWhole.format(usedCapacity), percentual);
651+
alertType = AlertManager.AlertType.ALERT_TYPE_CPU;
652+
break;
653+
case Capacity.CAPACITY_TYPE_STORAGE:
654+
msgSubject = String.format("System Alert: Low Available Storage in cluster [%s] pod [%s] of availability zone [%s].", clusterName, podName, dataCenterName);
655+
msgContent = String.format("Available storage space is low, total: %s MB, used: %s MB (%s%%).", totalInMB, usedInMB, percentual);
656+
alertType = AlertManager.AlertType.ALERT_TYPE_STORAGE;
657+
break;
658+
case Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED:
659+
msgSubject = String.format("System Alert: Remaining unallocated Storage is low in cluster [%s] pod [%s] of availability zone [%s].", clusterName, podName,
660+
dataCenterName);
661+
msgContent = String.format("Unallocated storage space is low, total: %s MB, allocated: %s MB (%s%%)", totalInMB, usedInMB, percentual);
662+
alertType = AlertManager.AlertType.ALERT_TYPE_STORAGE_ALLOCATED;
663+
break;
664+
case Capacity.CAPACITY_TYPE_LOCAL_STORAGE:
665+
msgSubject = String.format("System Alert: Remaining unallocated Local Storage is low in cluster [%s] pod [%s] of availability zone [%s].", clusterName, podName,
666+
dataCenterName);
667+
msgContent = String.format("Unallocated storage space is low, total: %s MB, allocated: %s MB (%s%%)", totalInMB, usedInMB, percentual);
668+
alertType = AlertManager.AlertType.ALERT_TYPE_LOCAL_STORAGE;
669+
break;
670+
case Capacity.CAPACITY_TYPE_PRIVATE_IP:
671+
msgSubject = String.format("System Alert: Number of unallocated private IPs is low in pod %s of availability zone [%s].", podName, dataCenterName);
672+
msgContent = String.format("Number of unallocated private IPs is low, total: %s, allocated: %s (%s%%)", totalInString, usedInString, percentual);
673+
alertType = AlertManager.AlertType.ALERT_TYPE_PRIVATE_IP;
674+
break;
675+
case Capacity.CAPACITY_TYPE_SECONDARY_STORAGE:
676+
msgSubject = String.format("System Alert: Low Available Secondary Storage in availability zone [%s].", dataCenterName);
677+
msgContent = String.format("Available secondary storage space is low, total: %s MB, used: %s MB (%s%%).", totalInMB, usedInMB, percentual);
678+
alertType = AlertManager.AlertType.ALERT_TYPE_SECONDARY_STORAGE;
679+
break;
680+
case Capacity.CAPACITY_TYPE_VIRTUAL_NETWORK_PUBLIC_IP:
681+
msgSubject = String.format("System Alert: Number of unallocated virtual network public IPs is low in availability zone [%s].", dataCenterName);
682+
msgContent = String.format("Number of unallocated public IPs is low, total: %s, allocated: %s (%s%%).", totalInString, usedInString, percentual);
683+
alertType = AlertManager.AlertType.ALERT_TYPE_VIRTUAL_NETWORK_PUBLIC_IP;
684+
break;
685+
case Capacity.CAPACITY_TYPE_DIRECT_ATTACHED_PUBLIC_IP:
686+
msgSubject = String.format("System Alert: Number of unallocated shared network IPs is low in availability zone [%s].", dataCenterName);
687+
msgContent = String.format("Number of unallocated shared network IPs is low, total: %s, allocated: %s (%s%%).", totalInString, usedInString, percentual);
688+
alertType = AlertManager.AlertType.ALERT_TYPE_DIRECT_ATTACHED_PUBLIC_IP;
689+
break;
690+
case Capacity.CAPACITY_TYPE_VLAN:
691+
msgSubject = String.format("System Alert: Number of unallocated VLANs is low in availability zone [%s].", dataCenterName);
692+
msgContent = String.format("Number of unallocated VLANs is low, total: %s, allocated: %s (%s%%).", totalInString, usedInString, percentual);
693+
alertType = AlertManager.AlertType.ALERT_TYPE_VLAN;
694+
break;
695+
case Capacity.CAPACITY_TYPE_VIRTUAL_NETWORK_IPV6_SUBNET:
696+
msgSubject = String.format("System Alert: Number of unallocated virtual network guest IPv6 subnets is low in availability zone [%s].", dc.getName());
697+
msgContent = String.format("Number of unallocated virtual network guest IPv6 subnets is low, total: [%s], allocated: [%s] (%s%%).", totalInString, usedInString, percentual);
698+
alertType = AlertManager.AlertType.ALERT_TYPE_VIRTUAL_NETWORK_IPV6_SUBNET;
699+
break;
723700
}
724701

725702
try {
726-
if (logger.isDebugEnabled()) {
727-
logger.debug(msgSubject);
728-
logger.debug(msgContent);
729-
}
730-
sendAlert(alertType, dc, pod, cluster, msgSubject, msgContent);
703+
logger.debug("Sending alert with subject [{}] and content [{}].", msgSubject, msgContent);
704+
sendAlert(alertType, dc.getId(), podId, clusterId, msgSubject, msgContent);
731705
} catch (Exception ex) {
732706
logger.error("Exception in CapacityChecker", ex);
733707
}

0 commit comments

Comments
 (0)