Subversion Repositories SmartDukaan

Rev

Rev 5563 | Rev 5679 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5563 Rev 5674
Line 1... Line 1...
1
package in.shop2020.alert.util;
1
package in.shop2020.alert.util;
2
 
2
 
3
import in.shop2020.alert.AlertType;
3
import in.shop2020.alert.AlertType;
-
 
4
import in.shop2020.alert.AlertedEntity;
4
import in.shop2020.alert.EntityMonitoringStatus;
5
import in.shop2020.alert.EntityMonitoringStatus;
5
import in.shop2020.alert.EntityProcessedState;
6
import in.shop2020.alert.EntityProcessedState;
6
import in.shop2020.alert.MonitoredEntity;
7
import in.shop2020.alert.MonitoredEntity;
7
import in.shop2020.alert.AlertMapper;
8
import in.shop2020.alert.AlertMapper;
-
 
9
import in.shop2020.alert.SearchFilter;
8
import in.shop2020.alert.handler.EntityHandler;
10
import in.shop2020.alert.handler.EntityHandler;
9
import in.shop2020.utils.GmailUtils;
11
import in.shop2020.utils.GmailUtils;
10
 
12
 
11
import java.util.Date;
13
import java.util.Date;
12
import java.util.List;
14
import java.util.List;
Line 17... Line 19...
17
import org.apache.commons.logging.LogFactory;
19
import org.apache.commons.logging.LogFactory;
18
import org.springframework.context.ApplicationContext;
20
import org.springframework.context.ApplicationContext;
19
import org.springframework.context.support.ClassPathXmlApplicationContext;
21
import org.springframework.context.support.ClassPathXmlApplicationContext;
20
 
22
 
21
public class AlertTask {
23
public class AlertTask {
22
	
-
 
-
 
24
	private static Log log = LogFactory.getLog(Class.class);
23
	private static Log log = LogFactory.getLog(AlertTask.class);
25
	private static Log logActive = LogFactory.getLog(AlertTask.class);
24
	private static EntityHandler      entityHandler;
26
	private static EntityHandler      entityHandler;
25
	
27
	
26
    private static List<MonitoredEntity> entitiesToBeAlerted;
28
    private static List<MonitoredEntity> activeAlertEntities;
27
    
29
    
28
    public AlertTask() {
30
    public AlertTask() {
29
    	ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
31
    	ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
30
        entityHandler              = context.getBean(EntityHandler.class);
32
        entityHandler              = context.getBean(EntityHandler.class);
31
    }
33
    }
32
    
34
    
33
    public static void main(String[] args) {
35
    public static void main(String[] args) {
34
    	AlertTask alertTask = new AlertTask();
36
    	AlertTask alertTask = new AlertTask();
35
    	entitiesToBeAlerted = entityHandler.getEntitiesToBeAlerted();
37
//    	entitiesToBeAlerted = entityHandler.getEntitiesToBeAlerted();
-
 
38
    	activeAlertEntities = entityHandler.getActiveAlertEntities();
36
    	
39
    	
37
    	for(MonitoredEntity entity : entitiesToBeAlerted) {
40
    	for(MonitoredEntity entity : activeAlertEntities) {
38
    		if(entity.getCriticalExpiryTime() <= (new Date()).getTime()) {
41
    		if(entity.getCriticalExpiryTime() <= (new Date()).getTime()) {
-
 
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) {
39
    			if(alert(entity, EntityProcessedState.CRITICAL_SENT)) {
52
	    		alert(entity, EntityProcessedState.CRITICAL_SENT);
40
    				entityHandler.updateEntityProcessedState(entity.getId(), 
53
	    		entityHandler.updateEntityProcessedState(entity.getId(), 
41
    						EntityProcessedState.CRITICAL_SENT);
54
	    						EntityProcessedState.CRITICAL_SENT);
42
    			}
55
    			}
43
    		} else if(entity.getWarnExpiryTime() <= (new Date()).getTime()) {
56
    		} else if(entity.getWarnExpiryTime() <= (new Date()).getTime()) {
-
 
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) {
44
    			if(alert(entity, EntityProcessedState.WARNING_SENT)) {
67
    			alert(entity, EntityProcessedState.WARNING_SENT);
45
    				entityHandler.updateEntityProcessedState(entity.getId(), 
68
	    		entityHandler.updateEntityProcessedState(entity.getId(), 
46
    						EntityProcessedState.WARNING_SENT);
69
	    						EntityProcessedState.WARNING_SENT);
47
    			}
70
    			}
48
    		}
71
    		}
49
    	}
72
    	}
-
 
73
    	
-
 
74
    	//populateActiveAlertinLog();
50
    }
75
    }
51
 
76
 
-
 
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
 
52
	private static boolean alert(MonitoredEntity entity,
101
	private static boolean alert(MonitoredEntity entity,
53
			EntityProcessedState state) {
102
			EntityProcessedState state) {
54
		String delayedEventDescription = "UNKNOWN";
103
		String delayedEventDescription = "UNKNOWN";
55
		String userIds = null;
104
		String userIds = null;
56
		String[] sendTo = {};
105
		String[] sendTo = {};
Line 65... Line 114...
65
					entityHandler.getEntityMonitoringStatus(entity.getEntityType());
114
					entityHandler.getEntityMonitoringStatus(entity.getEntityType());
66
				sendTo = entityMonitoringStatus.getUserIds().split(";");
115
				sendTo = entityMonitoringStatus.getUserIds().split(";");
67
				//sendTo[0] = "amar.kumar@shop2020.in";
116
				//sendTo[0] = "amar.kumar@shop2020.in";
68
			}
117
			}
69
			System.out.println(sendTo);
118
			System.out.println(sendTo);
70
			//if(userIds!=null && userIds!="") {
119
			if(userIds!=null && userIds!="") {
71
				
120
				
72
				String emailFromAddress = "build@shop2020.in";
121
				String emailFromAddress = "build@shop2020.in";
73
		        String password = "cafe@nes";
122
		        String password = "cafe@nes";
74
		        String emailSubjectTxt = null;
123
		        String emailSubjectTxt = null;
75
		        String emailMsgTxt = null;
124
		        String emailMsgTxt = null;
Line 93... Line 142...
93
					log.error("Unable to send "+state+" Alert for " + entity.getEntityType() 
142
					log.error("Unable to send "+state+" Alert for " + entity.getEntityType() 
94
							+ " : " + entity.getEntityIdentifier() + e);
143
							+ " : " + entity.getEntityIdentifier() + e);
95
					return false;
144
					return false;
96
				}
145
				}
97
				return true;
146
				return true;
98
			//}
147
			}
99
		} catch(Exception e) {
148
		} catch(Exception e) {
100
			log.error("Unable to send Alert for " + entity.getEntityType() 
149
			/*log.error("Unable to send Alert for " + entity.getEntityType() 
101
								+ " : " + entity.getEntityIdentifier() + e.getMessage() + e.getStackTrace() +
150
								+ " : " + entity.getEntityIdentifier() + e.getMessage() + e.getStackTrace() +
102
								e.getCause() + e.getLocalizedMessage());
151
								e.getCause() + e.getLocalizedMessage());*/
103
		}
152
		}
104
        return false;
153
        return false;
105
	}
154
	}
106
	
155
	
107
}
156
}