Notification Plugin should provide interface which accepts the Eagle Alert Entity and return Status of the same.
Today in Eagle there is no Notification Plugin Framework where users can implement the Notification Plugin Interface and upload their own library. This Plugin allows user to implement their own and also provides below default implementation
- Sending Email Alert
- Persisting Message to Kafka
- Writing Entities to Eagle Service
Notification Interface:
At the time of Eagle Topology starts , Code should scan and register the Notification Type ( Custom Notification / Email / Kafka Message etc.. ). Since Eagle allows users to implement their own Notification Implementation , Our Topology Init code should automatically detects and register which type of Notification needs to be used .
public class MessageNotification implements NotificationPlugin {
}
When users deploys their code with Eagle Service , Our Topology Initializer have to detect MessageNotification and Register it automatically.
How to select Notification When defining Policy ?
Eagle should allow users to select the Notification Type at the time of Policy Creation.. For that we need to persist the detected policies in HBase and Provide API on Top of It to Query.
To ensure the consistency always delete the Notification Table and Recreate with detected Notifications.
Persisting Message to Kafka:
Email Alert Notification:
Email Notification allows us to send email alert/ sms to the configured email id.
@Resource(name = "Email Notification" , description = " Email Notification API to trigger email/sms ") public class EmailNotification implements NotificationPlugin { private static final Logger LOG = LoggerFactory.getLogger(EmailNotification.class); private Map<String, AlertDefinitionAPIEntity> activeAlerts = new ConcurrentHashMap<String, AlertDefinitionAPIEntity>(); static Map<String, List<EmailGenerator>> emailGenerators = new ConcurrentHashMap<String, List<EmailGenerator>>(); private Config config; private AlertDefinitionDAO alertDefinitionDao; private NotificationStatus status ; /* initialize Email Notification related Objects Properly */ public void _init() throws Exception { // Get Config Object config = EagleConfigFactory.load().getConfig(); String site = config.getString("eagleProps.site"); String dataSource = config.getString("eagleProps.dataSource"); activeAlerts.clear(); emailGenerators.clear(); // find out all policies and its notification Config alertDefinitionDao = new AlertDefinitionDAOImpl(new EagleServiceConnector(config.getString("eagleProps.eagleService.host"), config.getInt("eagleProps.eagleService.port"))); try{ activeAlerts = alertDefinitionDao.findActiveAlertDefsByNotification( site , dataSource ,"Email Notification"); }catch (Exception ex ){ LOG.error(ex.getMessage()); throw new Exception(" Email Notification Cannot be initialized . Reason : "+ex.getMessage()); } // Create Email Set<String> policies = activeAlerts.keySet(); for( String policyId : policies ){ AlertDefinitionAPIEntity alertDef = activeAlerts.get(policyId); List<EmailGenerator> tmpList = createEmailGenerator(alertDef); this.emailGenerators.put(policyId , tmpList); } } @Override public void onAlert(AlertAPIEntity alertEntity) { status = new NotificationStatus(); String policyId = alertEntity.getTags().get(AlertConstants.POLICY_ID); System.out.println(" Email Notification "); List<EmailGenerator> generatorList = this.emailGenerators.get(policyId); boolean isSuccess = false; for( EmailGenerator gen : generatorList ) { isSuccess = gen.sendAlertEmail(alertEntity); if( !isSuccess ) { status.setMessage(" Failed to send email "); status.setNotificationSuccess(false); }else status.setNotificationSuccess(true); } } }
PersistAlertToEagle Service:
If user selects this Notification Plugin , all alerts will be persisted into Eagle Store.
/** * Responsible to persist Alerts to Eagle Storage */ @Resource(name = "Eagle Store" , description = "Persist Alert Entity to Eagle Store") public class PersistToEagleStore implements NotificationPlugin { private static final Logger LOG = LoggerFactory.getLogger(PersistToEagleStore.class); private NotificationStatus status; private Config config; private EagleAlertPersist persist; @Override public void _init() throws Exception { config = EagleConfigFactory.load().getConfig(); this.persist = new EagleAlertPersist(config.getString("eagleProps.eagleService.host"), config.getInt("eagleProps.eagleService.port"), config.getString("eagleProps.eagleService.username"), config.getString("eagleProps.eagleService.password")); } @Override public NotificationStatus getStatus() { return this.status; } @Override public void onAlert(AlertAPIEntity alertEntity) { try{ status = new NotificationStatus(); List<AlertAPIEntity> list = new ArrayList<AlertAPIEntity>(); list.add(alertEntity); persist.doPersist( list ); status.setNotificationSuccess(true); }catch (Exception ex ){ status.setMessage(ex.getMessage()); LOG.error(" Exception when Posting Alert Entity to Eagle Service Topic. Reason : "+ex.getMessage()); } } }