Subversion Repositories SmartDukaan

Rev

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