Rev 5742 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.alert.util;import in.shop2020.alert.AlertType;import in.shop2020.alert.AlertedEntity;import in.shop2020.alert.EntityMonitoringStatus;import in.shop2020.alert.EntityProcessedState;import in.shop2020.alert.MonitoredEntity;import in.shop2020.alert.AlertMapper;import in.shop2020.alert.SearchFilter;import in.shop2020.alert.handler.EntityHandler;import in.shop2020.utils.GmailUtils;import java.util.Date;import java.util.List;import javax.mail.MessagingException;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class AlertTask {private static Log log = LogFactory.getLog(Class.class);private static Log logActive = LogFactory.getLog(AlertTask.class);private static EntityHandler entityHandler;private static List<MonitoredEntity> activeAlertEntities;public AlertTask() {ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");entityHandler = context.getBean(EntityHandler.class);}public static void main(String[] args) {AlertTask alertTask = new AlertTask();activeAlertEntities = entityHandler.getActiveAlertEntities();for(MonitoredEntity entity : activeAlertEntities) {if(entity.getCriticalExpiryTime()!=0 && entity.getCriticalExpiryTime() <= (new Date()).getTime()) {AlertMapper alertMap;String delayedEventDescription = "UNKNOWN";alertMap = entityHandler.getAlertMapper(entity.getEntityType(), entity.getEventType());if(alertMap!=null) {delayedEventDescription = alertMap.getEventdesciption();}logActive.error("\tEntityType=" +entity.getEntityType() + " ; " + "\t" + entity.getEntityIdentifier() +"\tEventType=" + delayedEventDescription + " ; " + "AlertSeverity=CRITICAL ; " +"ExpiredTime=" + new Date(entity.getCriticalExpiryTime()));if(entity.getEntityProcessedState() != EntityProcessedState.CRITICAL_SENT) {alert(entity, EntityProcessedState.CRITICAL_SENT);entityHandler.updateEntityProcessedState(entity.getId(),EntityProcessedState.CRITICAL_SENT);}} else if(entity.getWarnExpiryTime()!=0 && entity.getWarnExpiryTime() <= (new Date()).getTime()) {AlertMapper alertMap;String delayedEventDescription = "UNKNOWN";alertMap = entityHandler.getAlertMapper(entity.getEntityType(), entity.getEventType());if(alertMap!=null) {delayedEventDescription = alertMap.getEventdesciption();}logActive.warn("\tEntityType=" +entity.getEntityType() + " ; " + "\t" + entity.getEntityIdentifier() +"\tEventType=" + delayedEventDescription + " ; " + "AlertSeverity=WARN ; " +"ExpiredTime=" + new Date(entity.getWarnExpiryTime()));if(entity.getEntityProcessedState() != EntityProcessedState.WARNING_SENT) {alert(entity, EntityProcessedState.WARNING_SENT);entityHandler.updateEntityProcessedState(entity.getId(),EntityProcessedState.WARNING_SENT);}}}//populateActiveAlertinLog();}private static void populateActiveAlertinLog() {SearchFilter searchFilter = new SearchFilter();List<AlertedEntity> alertedEntities = entityHandler.getAlertedEntities(searchFilter);AlertMapper alertMap;String delayedEventDescription = "UNKNOWN";for(AlertedEntity entity : alertedEntities) {alertMap = entityHandler.getAlertMapper(entity.getEntityType(), entity.getEventType());if(alertMap!=null) {delayedEventDescription = alertMap.getEventdesciption();}if(entity.getEntityProcessedState() == EntityProcessedState.CRITICAL_SENT) {logActive.error(new Date(entity.getLastAlertedTime()) + "\tEntityType=" +entity.getEntityType() + " ; "+"\tEventType=" + delayedEventDescription + " ; " +"AlertSeverity=CRITICAL ; " + "ExpiredTime=" + new Date(entity.getLastAlertedTime()));} else if(entity.getEntityProcessedState() == EntityProcessedState.WARNING_SENT) {logActive.warn(new Date(entity.getLastAlertedTime()) + "\tEntityType=" +entity.getEntityType() + " ; "+"\tEventType=" + delayedEventDescription + " ; " +"AlertSeverity=WARN ; " + "ExpiredTime=" + new Date(entity.getLastAlertedTime()));}}}private static boolean alert(MonitoredEntity entity,EntityProcessedState state) {String delayedEventDescription = "UNKNOWN";String userIds = null;String[] sendTo = {};try {AlertMapper alertMap = entityHandler.getAlertMapper(entity.getEntityType(), entity.getEventType());if(alertMap!=null) {delayedEventDescription = alertMap.getEventdesciption();userIds = alertMap.getUserIds();if(userIds!=null)sendTo = userIds.split(";");} else {EntityMonitoringStatus entityMonitoringStatus =entityHandler.getEntityMonitoringStatus(entity.getEntityType());sendTo = entityMonitoringStatus.getUserIds().split(";");}if(userIds!=null && userIds!="") {String emailFromAddress = "build@shop2020.in";String password = "cafe@nes";String emailSubjectTxt = null;String emailMsgTxt = null;System.out.println("check 1");if(state.equals(EntityProcessedState.WARNING_SENT)) {emailSubjectTxt = "Warning Alert for " + entity.getEntityType()+ " : " + entity.getEntityIdentifier() + " in "+delayedEventDescription;emailMsgTxt = "Warning Time expired for "+ entity.getEntityType()+ " : " + entity.getEntityIdentifier() + " in "+delayedEventDescription;} else if(state.equals(EntityProcessedState.CRITICAL_SENT)) {emailSubjectTxt = "Critical Alert for " + entity.getEntityType()+ " : " + entity.getEntityIdentifier() + " in "+delayedEventDescription;emailMsgTxt = "Critical Time expired for "+ entity.getEntityType()+ " : " + entity.getEntityIdentifier() + " in "+delayedEventDescription;}in.shop2020.alert.util.GmailUtils utils = new in.shop2020.alert.util.GmailUtils();try {System.out.println("Just before mail");utils.sendSSLMessage(sendTo, emailSubjectTxt, emailMsgTxt, emailFromAddress, password);} catch (MessagingException e) {log.error("Unable to send "+state+" Alert for " + entity.getEntityType()+ " : " + entity.getEntityIdentifier() + e);return false;}return true;}} catch(Exception e) {/*log.error("Unable to send Alert for " + entity.getEntityType()+ " : " + entity.getEntityIdentifier() + e.getMessage() + e.getStackTrace() +e.getCause() + e.getLocalizedMessage());*/}return false;}}