1 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
tdb |
1.16 |
package uk.org.iscream.client.alerters; |
3 |
tdb |
1.1 |
|
4 |
|
|
//---IMPORTS--- |
5 |
tdb |
1.16 |
import uk.org.iscream.client.*; |
6 |
|
|
import uk.org.iscream.core.*; |
7 |
|
|
import uk.org.iscream.util.*; |
8 |
|
|
import uk.org.iscream.componentmanager.*; |
9 |
tdb |
1.1 |
|
10 |
tdb |
1.2 |
import java.util.*; |
11 |
|
|
import java.io.*; |
12 |
ajm |
1.15 |
import java.text.*; |
13 |
tdb |
1.2 |
|
14 |
tdb |
1.1 |
/** |
15 |
tdb |
1.8 |
* This alerter delivers alerts using e-mail. |
16 |
tdb |
1.1 |
* |
17 |
tdb |
1.17 |
* @author $Author: tdb1 $ |
18 |
ajm |
1.19 |
* @version $Id: EMail__Alerter.java,v 1.18 2001/03/16 17:13:49 tdb1 Exp $ |
19 |
tdb |
1.1 |
*/ |
20 |
ajm |
1.19 |
public class EMail__Alerter extends Thread implements PluginAlerter { |
21 |
tdb |
1.1 |
|
22 |
|
|
//---FINAL ATTRIBUTES--- |
23 |
|
|
|
24 |
|
|
/** |
25 |
|
|
* The current CVS revision of this class |
26 |
|
|
*/ |
27 |
ajm |
1.19 |
public final String REVISION = "$Revision: 1.18 $"; |
28 |
tdb |
1.1 |
|
29 |
tdb |
1.8 |
public final String DESC = "Sends alerts over e-mail."; |
30 |
tdb |
1.1 |
|
31 |
tdb |
1.14 |
public final String DEFAULT_LEVEL = Alert.alertLevels[0]; |
32 |
|
|
|
33 |
|
|
public final String NOT_CONFIGURED = "<NOT CONFIGURED>"; |
34 |
|
|
|
35 |
tdb |
1.1 |
//---STATIC METHODS--- |
36 |
|
|
|
37 |
|
|
//---CONSTRUCTORS--- |
38 |
|
|
|
39 |
tdb |
1.8 |
public EMail__Alerter() { |
40 |
tdb |
1.6 |
_logger.write(toString(), Logger.SYSINIT, "IRC Alerter started"); |
41 |
ajm |
1.19 |
this.start(); |
42 |
tdb |
1.2 |
} |
43 |
|
|
|
44 |
tdb |
1.1 |
//---PUBLIC METHODS--- |
45 |
|
|
|
46 |
|
|
public void sendAlert(Alert alert) { |
47 |
tdb |
1.8 |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
48 |
tdb |
1.14 |
String levelName; |
49 |
|
|
try { |
50 |
|
|
levelName = cp.getProperty(_name, "Alerter.EMail.level"); |
51 |
|
|
} catch (PropertyNotFoundException e) { |
52 |
|
|
levelName = DEFAULT_LEVEL; |
53 |
|
|
_logger.write(toString(), Logger.WARNING, "Alerter.EMail.level value unavailable using default of " + levelName); |
54 |
|
|
} |
55 |
ajm |
1.9 |
int level = StringUtils.getStringPos(levelName, Alert.alertLevels); |
56 |
tdb |
1.2 |
// only send if it's equal (or above) our level |
57 |
ajm |
1.13 |
if(((alert.getLevel() == 0) && (alert.getLastLevel() >= level)) || (alert.getLevel() >= level)) { |
58 |
ajm |
1.9 |
String alertType = Alert.alertLevels[alert.getLevel()]; |
59 |
|
|
String thresholdType = Alert.thresholdLevels[alert.getThreshold()]; |
60 |
tdb |
1.17 |
String timeFirstSince = DateUtils.formatTime((System.currentTimeMillis() - alert.getInitialAlertTime())/1000, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
61 |
ajm |
1.15 |
String timeFirstOccured = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(alert.getInitialAlertTime())); |
62 |
|
|
|
63 |
tdb |
1.2 |
// sort out the subject |
64 |
tdb |
1.14 |
String subject; |
65 |
|
|
try { |
66 |
|
|
subject = cp.getProperty(_name, "Alerter.EMail.subject"); |
67 |
|
|
} catch (PropertyNotFoundException e) { |
68 |
|
|
subject = NOT_CONFIGURED; |
69 |
|
|
_logger.write(toString(), Logger.WARNING, "Alerter.EMail.subject value unavailable using default of " + subject); |
70 |
|
|
} |
71 |
tdb |
1.7 |
subject = StringUtils.replaceText(subject, "%level%", alertType); |
72 |
ajm |
1.9 |
subject = StringUtils.replaceText(subject, "%threshold%", thresholdType); |
73 |
tdb |
1.4 |
subject = StringUtils.replaceText(subject, "%source%", alert.getSource()); |
74 |
tdb |
1.3 |
subject = StringUtils.replaceText(subject, "%value%", alert.getValue()); |
75 |
|
|
subject = StringUtils.replaceText(subject, "%thresholdValue%", alert.getThresholdValue()); |
76 |
|
|
subject = StringUtils.replaceText(subject, "%attributeName%", alert.getAttributeName()); |
77 |
tdb |
1.18 |
subject = StringUtils.replaceText(subject, "%timeTillNextAlert%", DateUtils.getTimeString(Long.parseLong(alert.getTimeTillNextAlert()))); |
78 |
ajm |
1.15 |
subject = StringUtils.replaceText(subject, "%timeSinceFirstAlert%", timeFirstSince); |
79 |
|
|
subject = StringUtils.replaceText(subject, "%timeOfFirstAlert%", timeFirstOccured); |
80 |
ajm |
1.9 |
|
81 |
tdb |
1.2 |
// sort out the message body |
82 |
tdb |
1.14 |
String message; |
83 |
|
|
try { |
84 |
|
|
message = cp.getProperty(_name, "Alerter.EMail.message"); |
85 |
|
|
} catch (PropertyNotFoundException e) { |
86 |
|
|
message = NOT_CONFIGURED; |
87 |
|
|
_logger.write(toString(), Logger.WARNING, "Alerter.EMail.message value unavailable using default of " + message); |
88 |
|
|
} |
89 |
tdb |
1.7 |
message = StringUtils.replaceText(message, "%level%", alertType); |
90 |
ajm |
1.9 |
message = StringUtils.replaceText(message, "%threshold%", thresholdType); |
91 |
tdb |
1.4 |
message = StringUtils.replaceText(message, "%source%", alert.getSource()); |
92 |
tdb |
1.3 |
message = StringUtils.replaceText(message, "%value%", alert.getValue()); |
93 |
|
|
message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue()); |
94 |
|
|
message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName()); |
95 |
tdb |
1.18 |
message = StringUtils.replaceText(message, "%timeTillNextAlert%", DateUtils.getTimeString(Long.parseLong(alert.getTimeTillNextAlert()))); |
96 |
ajm |
1.15 |
message = StringUtils.replaceText(message, "%timeSinceFirstAlert%", timeFirstSince); |
97 |
|
|
message = StringUtils.replaceText(message, "%timeOfFirstAlert%", timeFirstOccured); |
98 |
ajm |
1.9 |
|
99 |
tdb |
1.2 |
try { |
100 |
|
|
// create SMTP message |
101 |
tdb |
1.8 |
Smtp smtp = new Smtp(cp.getProperty(_name, "Alerter.EMail.smtpServer")); |
102 |
tdb |
1.2 |
// set our sender |
103 |
tdb |
1.8 |
smtp.setSender(cp.getProperty(_name, "Alerter.EMail.sender")); |
104 |
tdb |
1.2 |
|
105 |
tdb |
1.17 |
String destList = cp.getProperty("Host."+alert.getSource(), "Alerter.EMail.destList"); |
106 |
tdb |
1.11 |
|
107 |
|
|
// set the to: list |
108 |
|
|
StringTokenizer st = new StringTokenizer(destList, ";"); |
109 |
tdb |
1.2 |
while (st.hasMoreTokens()) { |
110 |
|
|
smtp.setTo(st.nextToken()); |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
// prepare to print the message |
114 |
|
|
PrintWriter out = smtp.getOutputStream(); |
115 |
|
|
out.println("Subject: "+subject); |
116 |
|
|
out.println(); |
117 |
|
|
|
118 |
|
|
// send the message |
119 |
|
|
out.println(message); |
120 |
|
|
smtp.sendMessage(); |
121 |
|
|
smtp.close(); |
122 |
ajm |
1.10 |
_logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level"); |
123 |
tdb |
1.2 |
} |
124 |
|
|
catch(IOException e) { |
125 |
|
|
_logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e); |
126 |
tdb |
1.14 |
} |
127 |
|
|
catch(PropertyNotFoundException e) { |
128 |
|
|
_logger.write(toString(), Logger.ERROR, "Error obtaining essential configuration: "+e); |
129 |
tdb |
1.2 |
} |
130 |
|
|
} |
131 |
tdb |
1.1 |
} |
132 |
|
|
|
133 |
|
|
/** |
134 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
135 |
|
|
* method to provide clean logging (every class should have this). |
136 |
|
|
* |
137 |
tdb |
1.16 |
* This uses the uk.org.iscream.util.NameFormat class |
138 |
tdb |
1.1 |
* to format the toString() |
139 |
|
|
* |
140 |
|
|
* @return the name of this class and its CVS revision |
141 |
|
|
*/ |
142 |
|
|
public String toString() { |
143 |
|
|
return FormatName.getName( |
144 |
|
|
_name, |
145 |
|
|
getClass().getName(), |
146 |
|
|
REVISION); |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
/** |
150 |
|
|
* return the String representation of what the filter does |
151 |
|
|
*/ |
152 |
|
|
public String getDescription(){ |
153 |
|
|
return DESC; |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
//---PRIVATE METHODS--- |
157 |
ajm |
1.9 |
|
158 |
tdb |
1.1 |
//---ACCESSOR/MUTATOR METHODS--- |
159 |
|
|
|
160 |
ajm |
1.19 |
protected Queue getQueue() { |
161 |
|
|
return AlerterManager.getInstance().getQueue(); |
162 |
|
|
} |
163 |
|
|
|
164 |
|
|
protected int getQueueId() { |
165 |
|
|
if (_qID == -1) { |
166 |
|
|
_qID = getQueue().getQueue(); |
167 |
|
|
_logger.write(toString(), Logger.DEBUG, "Assigned Queue - " + _qID); |
168 |
|
|
} |
169 |
|
|
return _qID; |
170 |
|
|
} |
171 |
|
|
|
172 |
tdb |
1.1 |
//---ATTRIBUTES--- |
173 |
tdb |
1.2 |
|
174 |
tdb |
1.1 |
/** |
175 |
|
|
* This is the friendly identifier of the |
176 |
|
|
* component this class is running in. |
177 |
|
|
* eg, a Filter may be called "filter1", |
178 |
|
|
* If this class does not have an owning |
179 |
|
|
* component, a name from the configuration |
180 |
|
|
* can be placed here. This name could also |
181 |
|
|
* be changed to null for utility classes. |
182 |
|
|
*/ |
183 |
ajm |
1.9 |
private String _name = "EMail Alert"; |
184 |
tdb |
1.1 |
|
185 |
|
|
/** |
186 |
|
|
* This holds a reference to the |
187 |
|
|
* system logger that is being used. |
188 |
|
|
*/ |
189 |
|
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
190 |
ajm |
1.19 |
|
191 |
|
|
/** |
192 |
|
|
* The queue id for this alerters queue in the alert queue |
193 |
|
|
*/ |
194 |
|
|
private int _qID = -1; |
195 |
|
|
|
196 |
|
|
/** |
197 |
|
|
* The running status of the alerter |
198 |
|
|
*/ |
199 |
|
|
private boolean _running = true; |
200 |
tdb |
1.1 |
|
201 |
|
|
//---STATIC ATTRIBUTES--- |
202 |
|
|
|
203 |
|
|
} |