1 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
|
|
package uk.ac.ukc.iscream.client.alerters; |
3 |
|
|
|
4 |
|
|
//---IMPORTS--- |
5 |
|
|
import uk.ac.ukc.iscream.client.*; |
6 |
|
|
import uk.ac.ukc.iscream.core.*; |
7 |
|
|
import uk.ac.ukc.iscream.util.*; |
8 |
|
|
import uk.ac.ukc.iscream.componentmanager.*; |
9 |
|
|
|
10 |
tdb |
1.2 |
import java.util.*; |
11 |
|
|
import java.io.*; |
12 |
|
|
|
13 |
tdb |
1.1 |
/** |
14 |
tdb |
1.8 |
* This alerter delivers alerts using e-mail. |
15 |
tdb |
1.1 |
* |
16 |
ajm |
1.10 |
* @author $Author: ajm4 $ |
17 |
|
|
* @version $Id: EMail__Alerter.java,v 1.9 2001/03/04 02:41:16 ajm4 Exp $ |
18 |
tdb |
1.1 |
*/ |
19 |
tdb |
1.2 |
public class EMail__Alerter implements PluginAlerter { |
20 |
tdb |
1.1 |
|
21 |
|
|
//---FINAL ATTRIBUTES--- |
22 |
|
|
|
23 |
|
|
/** |
24 |
|
|
* The current CVS revision of this class |
25 |
|
|
*/ |
26 |
ajm |
1.10 |
public final String REVISION = "$Revision: 1.9 $"; |
27 |
tdb |
1.1 |
|
28 |
tdb |
1.8 |
public final String DESC = "Sends alerts over e-mail."; |
29 |
tdb |
1.1 |
|
30 |
|
|
//---STATIC METHODS--- |
31 |
|
|
|
32 |
|
|
//---CONSTRUCTORS--- |
33 |
|
|
|
34 |
tdb |
1.8 |
public EMail__Alerter() { |
35 |
tdb |
1.6 |
_logger.write(toString(), Logger.SYSINIT, "IRC Alerter started"); |
36 |
tdb |
1.2 |
} |
37 |
|
|
|
38 |
tdb |
1.1 |
//---PUBLIC METHODS--- |
39 |
|
|
|
40 |
|
|
public void sendAlert(Alert alert) { |
41 |
tdb |
1.8 |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
42 |
ajm |
1.9 |
String levelName = cp.getProperty(_name, "Alerter.EMail.level"); |
43 |
|
|
int level = StringUtils.getStringPos(levelName, Alert.alertLevels); |
44 |
tdb |
1.2 |
// only send if it's equal (or above) our level |
45 |
tdb |
1.8 |
if(alert.getLevel() >= level) { |
46 |
ajm |
1.9 |
String alertType = Alert.alertLevels[alert.getLevel()]; |
47 |
|
|
String thresholdType = Alert.thresholdLevels[alert.getThreshold()]; |
48 |
tdb |
1.2 |
// sort out the subject |
49 |
tdb |
1.8 |
String subject = cp.getProperty(_name, "Alerter.EMail.subject"); |
50 |
tdb |
1.7 |
subject = StringUtils.replaceText(subject, "%level%", alertType); |
51 |
ajm |
1.9 |
subject = StringUtils.replaceText(subject, "%threshold%", thresholdType); |
52 |
tdb |
1.4 |
subject = StringUtils.replaceText(subject, "%source%", alert.getSource()); |
53 |
tdb |
1.3 |
subject = StringUtils.replaceText(subject, "%value%", alert.getValue()); |
54 |
|
|
subject = StringUtils.replaceText(subject, "%thresholdValue%", alert.getThresholdValue()); |
55 |
|
|
subject = StringUtils.replaceText(subject, "%attributeName%", alert.getAttributeName()); |
56 |
ajm |
1.9 |
subject = StringUtils.replaceText(subject, "%timeTillNextAlert%", getTimeString(Long.parseLong(alert.getTimeTillNextAlert()))); |
57 |
|
|
|
58 |
tdb |
1.2 |
// sort out the message body |
59 |
tdb |
1.8 |
String message = cp.getProperty(_name, "Alerter.EMail.message"); |
60 |
tdb |
1.7 |
message = StringUtils.replaceText(message, "%level%", alertType); |
61 |
ajm |
1.9 |
message = StringUtils.replaceText(message, "%threshold%", thresholdType); |
62 |
tdb |
1.4 |
message = StringUtils.replaceText(message, "%source%", alert.getSource()); |
63 |
tdb |
1.3 |
message = StringUtils.replaceText(message, "%value%", alert.getValue()); |
64 |
|
|
message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue()); |
65 |
|
|
message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName()); |
66 |
ajm |
1.9 |
message = StringUtils.replaceText(message, "%timeTillNextAlert%", getTimeString(Long.parseLong(alert.getTimeTillNextAlert()))); |
67 |
|
|
|
68 |
tdb |
1.2 |
try { |
69 |
|
|
// create SMTP message |
70 |
tdb |
1.8 |
Smtp smtp = new Smtp(cp.getProperty(_name, "Alerter.EMail.smtpServer")); |
71 |
tdb |
1.2 |
// set our sender |
72 |
tdb |
1.8 |
smtp.setSender(cp.getProperty(_name, "Alerter.EMail.sender")); |
73 |
tdb |
1.2 |
|
74 |
|
|
// set the to list |
75 |
tdb |
1.8 |
StringTokenizer st = new StringTokenizer(cp.getProperty(_name, "Alerter.EMail.destList"), ";"); |
76 |
tdb |
1.2 |
while (st.hasMoreTokens()) { |
77 |
|
|
smtp.setTo(st.nextToken()); |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
// prepare to print the message |
81 |
|
|
PrintWriter out = smtp.getOutputStream(); |
82 |
|
|
out.println("Subject: "+subject); |
83 |
|
|
out.println(); |
84 |
|
|
|
85 |
|
|
// send the message |
86 |
|
|
out.println(message); |
87 |
|
|
smtp.sendMessage(); |
88 |
|
|
smtp.close(); |
89 |
ajm |
1.10 |
_logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level"); |
90 |
tdb |
1.2 |
} |
91 |
|
|
catch(IOException e) { |
92 |
|
|
_logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e); |
93 |
|
|
} |
94 |
|
|
} |
95 |
tdb |
1.1 |
} |
96 |
|
|
|
97 |
|
|
/** |
98 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
99 |
|
|
* method to provide clean logging (every class should have this). |
100 |
|
|
* |
101 |
|
|
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
102 |
|
|
* to format the toString() |
103 |
|
|
* |
104 |
|
|
* @return the name of this class and its CVS revision |
105 |
|
|
*/ |
106 |
|
|
public String toString() { |
107 |
|
|
return FormatName.getName( |
108 |
|
|
_name, |
109 |
|
|
getClass().getName(), |
110 |
|
|
REVISION); |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
/** |
114 |
|
|
* return the String representation of what the filter does |
115 |
|
|
*/ |
116 |
|
|
public String getDescription(){ |
117 |
|
|
return DESC; |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
//---PRIVATE METHODS--- |
121 |
|
|
|
122 |
ajm |
1.9 |
private String getTimeString(long time) { |
123 |
|
|
String timeString = null; |
124 |
|
|
if (time >= 60) { |
125 |
|
|
timeString = (time / 60) + " minutes"; |
126 |
|
|
} else if (time >= 3600) { |
127 |
|
|
timeString = ((time/60) / 60) + " hours"; |
128 |
|
|
} else { |
129 |
|
|
timeString = time + " seconds"; |
130 |
|
|
} |
131 |
|
|
return timeString; |
132 |
|
|
} |
133 |
|
|
|
134 |
tdb |
1.1 |
//---ACCESSOR/MUTATOR METHODS--- |
135 |
|
|
|
136 |
|
|
//---ATTRIBUTES--- |
137 |
tdb |
1.2 |
|
138 |
tdb |
1.1 |
/** |
139 |
|
|
* This is the friendly identifier of the |
140 |
|
|
* component this class is running in. |
141 |
|
|
* eg, a Filter may be called "filter1", |
142 |
|
|
* If this class does not have an owning |
143 |
|
|
* component, a name from the configuration |
144 |
|
|
* can be placed here. This name could also |
145 |
|
|
* be changed to null for utility classes. |
146 |
|
|
*/ |
147 |
ajm |
1.9 |
private String _name = "EMail Alert"; |
148 |
tdb |
1.1 |
|
149 |
|
|
/** |
150 |
|
|
* This holds a reference to the |
151 |
|
|
* system logger that is being used. |
152 |
|
|
*/ |
153 |
|
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
154 |
|
|
|
155 |
|
|
//---STATIC ATTRIBUTES--- |
156 |
|
|
|
157 |
|
|
} |