Subversion Repositories SmartDukaan

Rev

Rev 5563 | Rev 5679 | Go to most recent revision | 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
//    	entitiesToBeAlerted = entityHandler.getEntitiesToBeAlerted();
38
    	activeAlertEntities = entityHandler.getActiveAlertEntities();
5519 amar.kumar 39
 
5674 amar.kumar 40
    	for(MonitoredEntity entity : activeAlertEntities) {
5563 amar.kumar 41
    		if(entity.getCriticalExpiryTime() <= (new Date()).getTime()) {
5674 amar.kumar 42
    			AlertMapper alertMap;
43
		    	String 	delayedEventDescription = "UNKNOWN";
44
	    		alertMap = entityHandler.getAlertMapper(entity.getEntityType(), entity.getEventType());
45
	    		if(alertMap!=null) {
46
	    			delayedEventDescription = alertMap.getEventdesciption();
47
	    		}
48
    			logActive.error(new Date(entity.getCriticalExpiryTime()) + "\tEntityType=" + 
49
						entity.getEntityType() + " ; "+"\tEventType=" + delayedEventDescription + " ; " +
50
						"AlertSeverity=CRITICAL ; " + "ExpiredTime=" + new Date(entity.getCriticalExpiryTime()));
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
    			}
5563 amar.kumar 56
    		} else if(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
	    		}
63
    			logActive.warn(new Date(entity.getCriticalExpiryTime()) + "\tEntityType=" + 
64
						entity.getEntityType() + " ; "+"\tEventType=" + delayedEventDescription + " ; " +
65
						"AlertSeverity=WARN ; " + "ExpiredTime=" + new Date(entity.getCriticalExpiryTime()));
66
    			if(entity.getEntityProcessedState() != EntityProcessedState.WARNING_SENT) {
67
    			alert(entity, EntityProcessedState.WARNING_SENT);
68
	    		entityHandler.updateEntityProcessedState(entity.getId(), 
69
	    						EntityProcessedState.WARNING_SENT);
5519 amar.kumar 70
    			}
71
    		}
72
    	}
5674 amar.kumar 73
 
74
    	//populateActiveAlertinLog();
5519 amar.kumar 75
    }
76
 
5674 amar.kumar 77
	private static void populateActiveAlertinLog() {
78
		SearchFilter searchFilter = new SearchFilter();
79
		List<AlertedEntity> alertedEntities = entityHandler.getAlertedEntities(searchFilter);
80
    	AlertMapper alertMap;
81
    	String 	delayedEventDescription = "UNKNOWN";
82
 
83
    	for(AlertedEntity entity : alertedEntities) {
84
    		alertMap = entityHandler.getAlertMapper(entity.getEntityType(), entity.getEventType());
85
    		if(alertMap!=null) {
86
    			delayedEventDescription = alertMap.getEventdesciption();
87
    		}
88
    		if(entity.getEntityProcessedState() == EntityProcessedState.CRITICAL_SENT) {
89
    				logActive.error(new Date(entity.getLastAlertedTime()) + "\tEntityType=" + 
90
    						entity.getEntityType() + " ; "+"\tEventType=" + delayedEventDescription + " ; " +
91
    						"AlertSeverity=CRITICAL ; " + "ExpiredTime=" + new Date(entity.getLastAlertedTime()));
92
    		} else if(entity.getEntityProcessedState() == EntityProcessedState.WARNING_SENT) {
93
    			logActive.warn(new Date(entity.getLastAlertedTime()) + "\tEntityType=" + 
94
						entity.getEntityType() + " ; "+"\tEventType=" + delayedEventDescription + " ; " +
95
						"AlertSeverity=WARN ; " + "ExpiredTime=" + new Date(entity.getLastAlertedTime()));
96
    		}
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();
111
				sendTo = userIds.split(";");
112
			} else {
5536 amar.kumar 113
				EntityMonitoringStatus entityMonitoringStatus = 
114
					entityHandler.getEntityMonitoringStatus(entity.getEntityType());
5563 amar.kumar 115
				sendTo = entityMonitoringStatus.getUserIds().split(";");
116
				//sendTo[0] = "amar.kumar@shop2020.in";
5519 amar.kumar 117
			}
118
			System.out.println(sendTo);
5674 amar.kumar 119
			if(userIds!=null && userIds!="") {
5519 amar.kumar 120
 
121
				String emailFromAddress = "build@shop2020.in";
122
		        String password = "cafe@nes";
123
		        String emailSubjectTxt = null;
124
		        String emailMsgTxt = null;
125
		        System.out.println("check 1");
126
				if(state.equals(EntityProcessedState.WARNING_SENT)) {
127
					emailSubjectTxt = "Warning Alert for " + entity.getEntityType() 
128
					+ " : " + entity.getEntityIdentifier() + " in "+delayedEventDescription;
129
					emailMsgTxt = "Warning Time expired for "+ entity.getEntityType() 
130
					+ " : " + entity.getEntityIdentifier() + " in "+delayedEventDescription;
131
				} else if(state.equals(EntityProcessedState.CRITICAL_SENT)) {
132
					emailSubjectTxt = "Critical Alert for " + entity.getEntityType() 
133
					+ " : " + entity.getEntityIdentifier() + " in "+delayedEventDescription;
134
					emailMsgTxt = "Critical Time expired for "+ entity.getEntityType() 
135
					+ " : " + entity.getEntityIdentifier() + " in "+delayedEventDescription;
136
				}
5563 amar.kumar 137
				in.shop2020.alert.util.GmailUtils utils = new in.shop2020.alert.util.GmailUtils();
5519 amar.kumar 138
		        try {
139
		        	System.out.println("Just before mail");
5563 amar.kumar 140
		        	utils.sendSSLMessage(sendTo, emailSubjectTxt, emailMsgTxt, emailFromAddress, password);
5519 amar.kumar 141
				} catch (MessagingException e) {
142
					log.error("Unable to send "+state+" Alert for " + entity.getEntityType() 
143
							+ " : " + entity.getEntityIdentifier() + e);
144
					return false;
145
				}
146
				return true;
5674 amar.kumar 147
			}
5519 amar.kumar 148
		} catch(Exception e) {
5674 amar.kumar 149
			/*log.error("Unable to send Alert for " + entity.getEntityType() 
5519 amar.kumar 150
								+ " : " + entity.getEntityIdentifier() + e.getMessage() + e.getStackTrace() +
5674 amar.kumar 151
								e.getCause() + e.getLocalizedMessage());*/
5519 amar.kumar 152
		}
153
        return false;
154
	}
155
 
156
}