Subversion Repositories SmartDukaan

Rev

Rev 5742 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5519 amar.kumar 1
package in.shop2020.alert.util;
2
 
3
import in.shop2020.alert.AlertType;
5674 amar.kumar 4
import in.shop2020.alert.AlertedEntity;
5519 amar.kumar 5
import in.shop2020.alert.EntityMonitoringStatus;
6
import in.shop2020.alert.EntityProcessedState;
7
import in.shop2020.alert.MonitoredEntity;
8
import in.shop2020.alert.AlertMapper;
5674 amar.kumar 9
import in.shop2020.alert.SearchFilter;
5519 amar.kumar 10
import in.shop2020.alert.handler.EntityHandler;
11
import in.shop2020.utils.GmailUtils;
12
 
13
import java.util.Date;
14
import java.util.List;
15
 
16
import javax.mail.MessagingException;
17
 
18
import org.apache.commons.logging.Log;
19
import org.apache.commons.logging.LogFactory;
20
import org.springframework.context.ApplicationContext;
21
import org.springframework.context.support.ClassPathXmlApplicationContext;
22
 
23
public class AlertTask {
5674 amar.kumar 24
	private static Log log = LogFactory.getLog(Class.class);
25
	private static Log logActive = LogFactory.getLog(AlertTask.class);
5519 amar.kumar 26
	private static EntityHandler      entityHandler;
27
 
5674 amar.kumar 28
    private static List<MonitoredEntity> activeAlertEntities;
5519 amar.kumar 29
 
30
    public AlertTask() {
31
    	ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
32
        entityHandler              = context.getBean(EntityHandler.class);
33
    }
34
 
35
    public static void main(String[] args) {
36
    	AlertTask alertTask = new AlertTask();
5674 amar.kumar 37
    	activeAlertEntities = entityHandler.getActiveAlertEntities();
5519 amar.kumar 38
 
5674 amar.kumar 39
    	for(MonitoredEntity entity : activeAlertEntities) {
5759 amar.kumar 40
    		if(entity.getCriticalExpiryTime()!=0 && entity.getCriticalExpiryTime() <= (new Date()).getTime()) {
5674 amar.kumar 41
    			AlertMapper alertMap;
42
		    	String 	delayedEventDescription = "UNKNOWN";
43
	    		alertMap = entityHandler.getAlertMapper(entity.getEntityType(), entity.getEventType());
44
	    		if(alertMap!=null) {
45
	    			delayedEventDescription = alertMap.getEventdesciption();
46
	    		}
5680 amar.kumar 47
    			logActive.error("\tEntityType=" + 
5679 amar.kumar 48
						entity.getEntityType() + " ; " + "\t" + entity.getEntityIdentifier() + 
49
						"\tEventType=" + delayedEventDescription + " ; " + "AlertSeverity=CRITICAL ; " + 
50
						"ExpiredTime=" + new Date(entity.getCriticalExpiryTime()));
5674 amar.kumar 51
    			if(entity.getEntityProcessedState() != EntityProcessedState.CRITICAL_SENT) {
52
	    		alert(entity, EntityProcessedState.CRITICAL_SENT);
53
	    		entityHandler.updateEntityProcessedState(entity.getId(), 
54
	    						EntityProcessedState.CRITICAL_SENT);
5519 amar.kumar 55
    			}
5759 amar.kumar 56
    		} else if(entity.getWarnExpiryTime()!=0 && entity.getWarnExpiryTime() <= (new Date()).getTime()) {
5674 amar.kumar 57
    			AlertMapper alertMap;
58
		    	String 	delayedEventDescription = "UNKNOWN";
59
	    		alertMap = entityHandler.getAlertMapper(entity.getEntityType(), entity.getEventType());
60
	    		if(alertMap!=null) {
61
	    			delayedEventDescription = alertMap.getEventdesciption();
62
	    		}
5680 amar.kumar 63
    			logActive.warn("\tEntityType=" + 
5679 amar.kumar 64
						entity.getEntityType() + " ; " + "\t" + entity.getEntityIdentifier() +
65
						"\tEventType=" + delayedEventDescription + " ; " + "AlertSeverity=WARN ; " +
66
						"ExpiredTime=" + new Date(entity.getWarnExpiryTime()));
5674 amar.kumar 67
    			if(entity.getEntityProcessedState() != EntityProcessedState.WARNING_SENT) {
68
    			alert(entity, EntityProcessedState.WARNING_SENT);
69
	    		entityHandler.updateEntityProcessedState(entity.getId(), 
70
	    						EntityProcessedState.WARNING_SENT);
5519 amar.kumar 71
    			}
72
    		}
73
    	}
5674 amar.kumar 74
 
75
    	//populateActiveAlertinLog();
5519 amar.kumar 76
    }
77
 
5674 amar.kumar 78
	private static void populateActiveAlertinLog() {
79
		SearchFilter searchFilter = new SearchFilter();
80
		List<AlertedEntity> alertedEntities = entityHandler.getAlertedEntities(searchFilter);
81
    	AlertMapper alertMap;
82
    	String 	delayedEventDescription = "UNKNOWN";
83
 
84
    	for(AlertedEntity entity : alertedEntities) {
85
    		alertMap = entityHandler.getAlertMapper(entity.getEntityType(), entity.getEventType());
86
    		if(alertMap!=null) {
87
    			delayedEventDescription = alertMap.getEventdesciption();
88
    		}
89
    		if(entity.getEntityProcessedState() == EntityProcessedState.CRITICAL_SENT) {
90
    				logActive.error(new Date(entity.getLastAlertedTime()) + "\tEntityType=" + 
91
    						entity.getEntityType() + " ; "+"\tEventType=" + delayedEventDescription + " ; " +
92
    						"AlertSeverity=CRITICAL ; " + "ExpiredTime=" + new Date(entity.getLastAlertedTime()));
93
    		} else if(entity.getEntityProcessedState() == EntityProcessedState.WARNING_SENT) {
94
    			logActive.warn(new Date(entity.getLastAlertedTime()) + "\tEntityType=" + 
95
						entity.getEntityType() + " ; "+"\tEventType=" + delayedEventDescription + " ; " +
96
						"AlertSeverity=WARN ; " + "ExpiredTime=" + new Date(entity.getLastAlertedTime()));
97
    		}
98
    	}
99
	}
100
 
5519 amar.kumar 101
	private static boolean alert(MonitoredEntity entity,
102
			EntityProcessedState state) {
103
		String delayedEventDescription = "UNKNOWN";
104
		String userIds = null;
105
		String[] sendTo = {};
106
		try {
107
			AlertMapper alertMap = entityHandler.getAlertMapper(entity.getEntityType(), entity.getEventType());
108
			if(alertMap!=null) {
109
				delayedEventDescription = alertMap.getEventdesciption();
110
				userIds = alertMap.getUserIds();
5742 amar.kumar 111
				if(userIds!=null)
112
					sendTo = userIds.split(";");
5519 amar.kumar 113
			} else {
5536 amar.kumar 114
				EntityMonitoringStatus entityMonitoringStatus = 
115
					entityHandler.getEntityMonitoringStatus(entity.getEntityType());
5563 amar.kumar 116
				sendTo = entityMonitoringStatus.getUserIds().split(";");
5519 amar.kumar 117
			}
5674 amar.kumar 118
			if(userIds!=null && userIds!="") {
5519 amar.kumar 119
 
120
				String emailFromAddress = "build@shop2020.in";
121
		        String password = "cafe@nes";
122
		        String emailSubjectTxt = null;
123
		        String emailMsgTxt = null;
124
		        System.out.println("check 1");
125
				if(state.equals(EntityProcessedState.WARNING_SENT)) {
126
					emailSubjectTxt = "Warning Alert for " + entity.getEntityType() 
127
					+ " : " + entity.getEntityIdentifier() + " in "+delayedEventDescription;
128
					emailMsgTxt = "Warning Time expired for "+ entity.getEntityType() 
129
					+ " : " + entity.getEntityIdentifier() + " in "+delayedEventDescription;
130
				} else if(state.equals(EntityProcessedState.CRITICAL_SENT)) {
131
					emailSubjectTxt = "Critical Alert for " + entity.getEntityType() 
132
					+ " : " + entity.getEntityIdentifier() + " in "+delayedEventDescription;
133
					emailMsgTxt = "Critical Time expired for "+ entity.getEntityType() 
134
					+ " : " + entity.getEntityIdentifier() + " in "+delayedEventDescription;
135
				}
5563 amar.kumar 136
				in.shop2020.alert.util.GmailUtils utils = new in.shop2020.alert.util.GmailUtils();
5519 amar.kumar 137
		        try {
138
		        	System.out.println("Just before mail");
5563 amar.kumar 139
		        	utils.sendSSLMessage(sendTo, emailSubjectTxt, emailMsgTxt, emailFromAddress, password);
5519 amar.kumar 140
				} catch (MessagingException e) {
141
					log.error("Unable to send "+state+" Alert for " + entity.getEntityType() 
142
							+ " : " + entity.getEntityIdentifier() + e);
143
					return false;
144
				}
145
				return true;
5674 amar.kumar 146
			}
5519 amar.kumar 147
		} catch(Exception e) {
5674 amar.kumar 148
			/*log.error("Unable to send Alert for " + entity.getEntityType() 
5519 amar.kumar 149
								+ " : " + entity.getEntityIdentifier() + e.getMessage() + e.getStackTrace() +
5674 amar.kumar 150
								e.getCause() + e.getLocalizedMessage());*/
5519 amar.kumar 151
		}
152
        return false;
153
	}
154
 
155
}