1919import java .io .UnsupportedEncodingException ;
2020import java .text .DecimalFormat ;
2121import java .util .ArrayList ;
22- import java .util .Arrays ;
2322import java .util .Date ;
2423import java .util .HashMap ;
2524import java .util .HashSet ;
3736import javax .mail .MessagingException ;
3837import javax .naming .ConfigurationException ;
3938
40- import com .cloud .dc .DataCenter ;
41- import com .cloud .dc .Pod ;
42- import com .cloud .org .Cluster ;
43-
4439import org .apache .cloudstack .backup .BackupManager ;
4540import org .apache .cloudstack .framework .config .ConfigDepot ;
4641import org .apache .cloudstack .framework .config .ConfigKey ;
4742import org .apache .cloudstack .framework .config .Configurable ;
4843import org .apache .cloudstack .framework .config .dao .ConfigurationDao ;
44+ import org .apache .cloudstack .framework .messagebus .MessageBus ;
4945import org .apache .cloudstack .managed .context .ManagedContextTimerTask ;
5046import org .apache .cloudstack .storage .datastore .db .ObjectStoreDao ;
5147import org .apache .cloudstack .storage .datastore .db .PrimaryDataStoreDao ;
5450import org .apache .cloudstack .utils .mailing .SMTPMailProperties ;
5551import org .apache .cloudstack .utils .mailing .SMTPMailSender ;
5652import org .apache .commons .lang3 .ArrayUtils ;
53+ import org .apache .commons .lang3 .StringUtils ;
5754import org .apache .commons .lang3 .math .NumberUtils ;
5855import org .apache .logging .log4j .LogManager ;
5956import org .apache .logging .log4j .Logger ;
7067import com .cloud .configuration .Config ;
7168import com .cloud .configuration .ConfigurationManager ;
7269import com .cloud .dc .ClusterVO ;
70+ import com .cloud .dc .DataCenter ;
7371import com .cloud .dc .DataCenter .NetworkType ;
7472import com .cloud .dc .DataCenterVO ;
7573import com .cloud .dc .HostPodVO ;
74+ import com .cloud .dc .Pod ;
7675import com .cloud .dc .Vlan .VlanType ;
7776import com .cloud .dc .dao .ClusterDao ;
7877import com .cloud .dc .dao .DataCenterDao ;
8685import com .cloud .host .dao .HostDao ;
8786import com .cloud .network .Ipv6Service ;
8887import com .cloud .network .dao .IPAddressDao ;
88+ import com .cloud .org .Cluster ;
8989import com .cloud .org .Grouping .AllocationState ;
9090import com .cloud .resource .ResourceManager ;
9191import com .cloud .storage .StorageManager ;
9292import com .cloud .utils .Pair ;
93+ import com .cloud .utils .Ternary ;
9394import com .cloud .utils .component .ManagerBase ;
9495import com .cloud .utils .concurrency .NamedThreadFactory ;
9596import com .cloud .utils .db .SearchCriteria ;
100101public class AlertManagerImpl extends ManagerBase implements AlertManager , Configurable {
101102 protected Logger logger = LogManager .getLogger (AlertManagerImpl .class .getName ());
102103
103- public static final List <AlertType > ALERTS = Arrays .asList (AlertType .ALERT_TYPE_HOST
104- , AlertType .ALERT_TYPE_USERVM
105- , AlertType .ALERT_TYPE_DOMAIN_ROUTER
106- , AlertType .ALERT_TYPE_CONSOLE_PROXY
107- , AlertType .ALERT_TYPE_SSVM
108- , AlertType .ALERT_TYPE_STORAGE_MISC
109- , AlertType .ALERT_TYPE_MANAGEMENT_NODE
110- , AlertType .ALERT_TYPE_RESOURCE_LIMIT_EXCEEDED
111- , AlertType .ALERT_TYPE_UPLOAD_FAILED
112- , AlertType .ALERT_TYPE_OOBM_AUTH_ERROR
113- , AlertType .ALERT_TYPE_HA_ACTION
114- , AlertType .ALERT_TYPE_CA_CERT
115- , AlertType .ALERT_TYPE_EXTENSION_PATH_NOT_READY
116- , AlertType .ALERT_TYPE_VPN_GATEWAY_OBSOLETE_PARAMETERS );
117-
118104 private static final long INITIAL_CAPACITY_CHECK_DELAY = 30L * 1000L ; // Thirty seconds expressed in milliseconds.
119105
120106 private static final DecimalFormat DfPct = new DecimalFormat ("###.##" );
@@ -156,6 +142,8 @@ public class AlertManagerImpl extends ManagerBase implements AlertManager, Confi
156142 Ipv6Service ipv6Service ;
157143 @ Inject
158144 HostDao hostDao ;
145+ @ Inject
146+ MessageBus messageBus ;
159147
160148 private Timer _timer = null ;
161149 private long _capacityCheckPeriod = 60L * 60L * 1000L ; // One hour by default.
@@ -175,6 +163,8 @@ public class AlertManagerImpl extends ManagerBase implements AlertManager, Confi
175163 protected String [] recipients = null ;
176164 protected String senderAddress = null ;
177165
166+ private final List <String > allowedRepetitiveAlertTypeNames = new ArrayList <>();
167+
178168 public AlertManagerImpl () {
179169 _executor = Executors .newCachedThreadPool (new NamedThreadFactory ("Email-Alerts-Sender" ));
180170 }
@@ -254,12 +244,32 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
254244 _capacityCheckPeriod = Long .parseLong (Config .CapacityCheckPeriod .getDefaultValue ());
255245 }
256246 }
247+ initMessageBusListener ();
248+ setupRepetitiveAlertTypes ();
257249
258250 _timer = new Timer ("CapacityChecker" );
259251
260252 return true ;
261253 }
262254
255+ protected void setupRepetitiveAlertTypes () {
256+ allowedRepetitiveAlertTypeNames .clear ();
257+ String allowedRepetitiveAlertsStr = AllowedRepetitiveAlertTypes .value ();
258+ logger .trace ("Allowed repetitive alert types specified by {}: {} " , AllowedRepetitiveAlertTypes .key (),
259+ allowedRepetitiveAlertsStr );
260+ if (StringUtils .isBlank (allowedRepetitiveAlertsStr )) {
261+ return ;
262+ }
263+ String [] allowedRepetitiveAlertTypesArray = allowedRepetitiveAlertsStr .split ("," );
264+ for (String allowedTypeName : allowedRepetitiveAlertTypesArray ) {
265+ if (StringUtils .isBlank (allowedTypeName )) {
266+ continue ;
267+ }
268+ allowedRepetitiveAlertTypeNames .add (allowedTypeName .toLowerCase ());
269+ }
270+ logger .trace ("{} alert types specified for repetitive alerts" , allowedRepetitiveAlertTypeNames .size ());
271+ }
272+
263273 @ Override
264274 public boolean start () {
265275 _timer .schedule (new CapacityChecker (), INITIAL_CAPACITY_CHECK_DELAY , _capacityCheckPeriod );
@@ -850,11 +860,11 @@ public void sendAlert(AlertType alertType, DataCenter dataCenter, Pod pod, Clust
850860
851861 @ Nullable
852862 private AlertVO getAlertForTrivialAlertType (AlertType alertType , long dataCenterId , Long podId , Long clusterId ) {
853- AlertVO alert = null ;
854- if (! ALERTS .contains (alertType )) {
855- alert = _alertDao . getLastAlert ( alertType . getType (), dataCenterId , podId , clusterId ) ;
863+ if ( alertType . isRepetitionAllowed () || ( StringUtils . isNotBlank ( alertType . getName ()) &&
864+ allowedRepetitiveAlertTypeNames .contains (alertType . getName (). toLowerCase ()) )) {
865+ return null ;
856866 }
857- return alert ;
867+ return _alertDao . getLastAlert ( alertType . getType (), dataCenterId , podId , clusterId ) ;
858868 }
859869
860870 protected void sendMessage (SMTPMailProperties mailProps ) {
@@ -883,7 +893,7 @@ public String getConfigComponentName() {
883893 @ Override
884894 public ConfigKey <?>[] getConfigKeys () {
885895 return new ConfigKey <?>[] {CPUCapacityThreshold , MemoryCapacityThreshold , StorageAllocatedCapacityThreshold , StorageCapacityThreshold , AlertSmtpEnabledSecurityProtocols ,
886- AlertSmtpUseStartTLS , Ipv6SubnetCapacityThreshold , AlertSmtpUseAuth };
896+ AlertSmtpUseStartTLS , Ipv6SubnetCapacityThreshold , AlertSmtpUseAuth , AllowedRepetitiveAlertTypes };
887897 }
888898
889899 @ Override
@@ -897,4 +907,16 @@ public boolean generateAlert(AlertType alertType, long dataCenterId, Long podId,
897907 return false ;
898908 }
899909 }
910+
911+ @ SuppressWarnings ("unchecked" )
912+ protected void initMessageBusListener () {
913+ messageBus .subscribe (EventTypes .EVENT_CONFIGURATION_VALUE_EDIT , (senderAddress , subject , args ) -> {
914+ Ternary <String , ConfigKey .Scope , Long > updatedSetting = (Ternary <String , ConfigKey .Scope , Long >) args ;
915+ String updatedSettingName = updatedSetting .first ();
916+ if (!AllowedRepetitiveAlertTypes .key ().equals (updatedSettingName )) {
917+ return ;
918+ }
919+ setupRepetitiveAlertTypes ();
920+ });
921+ }
900922}
0 commit comments