1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.ac.ukc.iscream.client.alerters; |
2 |
> |
package uk.org.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 |
< |
import java.util.*; |
5 |
> |
import uk.org.iscream.client.*; |
6 |
> |
import uk.org.iscream.core.*; |
7 |
> |
import uk.org.iscream.util.*; |
8 |
> |
import uk.org.iscream.componentmanager.*; |
9 |
|
import java.io.*; |
10 |
+ |
import java.util.StringTokenizer; |
11 |
|
|
12 |
+ |
|
13 |
|
/** |
14 |
< |
* This Alert sends by e-mail. |
14 |
> |
* This alerter delivers alerts using e-mail. |
15 |
|
* |
16 |
|
* @author $Author$ |
17 |
|
* @version $Id$ |
18 |
|
*/ |
19 |
< |
public class EMail__Alerter implements PluginAlerter { |
19 |
> |
public class EMail__Alerter extends AlerterSkeleton { |
20 |
|
|
21 |
|
//---FINAL ATTRIBUTES--- |
22 |
|
|
25 |
|
*/ |
26 |
|
public final String REVISION = "$Revision$"; |
27 |
|
|
28 |
< |
public final String DESC = "Sends alerts by e-mail"; |
28 |
> |
/** |
29 |
> |
* A description of this alerter |
30 |
> |
*/ |
31 |
> |
public final String DESC = "Sends alerts over e-mail."; |
32 |
|
|
33 |
|
//---STATIC METHODS--- |
34 |
|
|
35 |
|
//---CONSTRUCTORS--- |
36 |
|
|
34 |
– |
public EMail__Alerter() { |
35 |
– |
// get the configuration for this alerter |
36 |
– |
Configuration config = ReferenceManager.getInstance().getCM().getConfiguration(_name); |
37 |
– |
|
38 |
– |
// an integer value |
39 |
– |
_level = Integer.parseInt(config.getProperty("Alerter.EMail.level")); |
40 |
– |
// a semi-colon seperated list of destination addresses |
41 |
– |
_destList = config.getProperty("Alerter.EMail.destList"); |
42 |
– |
// a single from address |
43 |
– |
_sender = config.getProperty("Alerter.EMail.sender"); |
44 |
– |
// the fqdn of an smtp server |
45 |
– |
_smtpServer = config.getProperty("Alerter.EMail.smtpServer"); |
46 |
– |
// a subject with the following: %level% and %message% |
47 |
– |
// eg, "i-scream e-mail alert: level %level%, %message%" |
48 |
– |
_subject = config.getProperty("Alerter.EMail.subject"); |
49 |
– |
// a message body with the following: %level% and %message% |
50 |
– |
_message = config.getProperty("Alerter.EMail.message"); |
51 |
– |
|
52 |
– |
_logger.write(toString(), Logger.SYSINIT, "IRC Alerter started"); |
53 |
– |
} |
54 |
– |
|
37 |
|
//---PUBLIC METHODS--- |
38 |
|
|
39 |
+ |
/** |
40 |
+ |
* Implements the abstract method from the skeleton class. |
41 |
+ |
* This method will attempt to send an alert |
42 |
+ |
* message using the configured email configuration. |
43 |
+ |
* |
44 |
+ |
* @param alert the alert to send |
45 |
+ |
*/ |
46 |
|
public void sendAlert(Alert alert) { |
47 |
< |
// only send if it's equal (or above) our level |
48 |
< |
if(alert.getLevel() >= _level) { |
49 |
< |
// sort out the subject |
50 |
< |
String subject = _subject; |
51 |
< |
subject = StringUtils.replaceText(subject, "%level%", String.valueOf(alert.getLevel())); |
52 |
< |
subject = StringUtils.replaceText(subject, "%source%", alert.getSource()); |
53 |
< |
subject = StringUtils.replaceText(subject, "%value%", alert.getValue()); |
54 |
< |
subject = StringUtils.replaceText(subject, "%thresholdValue%", alert.getThresholdValue()); |
55 |
< |
subject = StringUtils.replaceText(subject, "%attributeName%", alert.getAttributeName()); |
47 |
> |
// get the subject and replace fields |
48 |
> |
String subject; |
49 |
> |
try { |
50 |
> |
subject = _cp.getProperty(_name, "Alerter.EMail.subject"); |
51 |
> |
} catch (PropertyNotFoundException e) { |
52 |
> |
subject = NOT_CONFIGURED; |
53 |
> |
_logger.write(toString(), Logger.WARNING, "Alerter.EMail.subject value unavailable using default of " + subject); |
54 |
> |
} |
55 |
> |
subject = processAlertMessage(subject, alert); |
56 |
> |
|
57 |
> |
// get the message body and replace fields |
58 |
> |
String message; |
59 |
> |
try { |
60 |
> |
message = _cp.getProperty(_name, "Alerter.EMail.message"); |
61 |
> |
} catch (PropertyNotFoundException e) { |
62 |
> |
message = NOT_CONFIGURED; |
63 |
> |
_logger.write(toString(), Logger.WARNING, "Alerter.EMail.message value unavailable using default of " + message); |
64 |
> |
} |
65 |
> |
message = processAlertMessage(message, alert); |
66 |
> |
|
67 |
> |
// attempt to send the actual message |
68 |
> |
try { |
69 |
> |
// create SMTP message |
70 |
> |
Smtp smtp = new Smtp(_cp.getProperty(_name, "Alerter.EMail.smtpServer")); |
71 |
> |
// set our sender |
72 |
> |
smtp.setSender(_cp.getProperty(_name, "Alerter.EMail.sender")); |
73 |
|
|
74 |
< |
// sort out the message body |
75 |
< |
String message = _message; |
70 |
< |
message = StringUtils.replaceText(message, "%level%", String.valueOf(alert.getLevel())); |
71 |
< |
message = StringUtils.replaceText(message, "%source%", alert.getSource()); |
72 |
< |
message = StringUtils.replaceText(message, "%value%", alert.getValue()); |
73 |
< |
message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue()); |
74 |
< |
message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName()); |
74 |
> |
// list of destination addresses |
75 |
> |
String destList = _cp.getProperty("Host."+alert.getSource(), "Alerter.EMail.destList"); |
76 |
|
|
77 |
< |
try { |
78 |
< |
// create SMTP message |
79 |
< |
Smtp smtp = new Smtp(_smtpServer); |
80 |
< |
// set our sender |
80 |
< |
smtp.setSender(_sender); |
81 |
< |
|
82 |
< |
// set the to list |
83 |
< |
StringTokenizer st = new StringTokenizer(_destList, ";"); |
84 |
< |
while (st.hasMoreTokens()) { |
85 |
< |
smtp.setTo(st.nextToken()); |
86 |
< |
} |
87 |
< |
|
88 |
< |
// prepare to print the message |
89 |
< |
PrintWriter out = smtp.getOutputStream(); |
90 |
< |
out.println("Subject: "+subject); |
91 |
< |
out.println(); |
92 |
< |
|
93 |
< |
// send the message |
94 |
< |
out.println(message); |
95 |
< |
smtp.sendMessage(); |
96 |
< |
smtp.close(); |
97 |
< |
_logger.write(toString(), Logger.DEBUG, "Sending IRC Alert at level"+String.valueOf(alert.getLevel())); |
77 |
> |
// set the to: list |
78 |
> |
StringTokenizer st = new StringTokenizer(destList, ";"); |
79 |
> |
while (st.hasMoreTokens()) { |
80 |
> |
smtp.setTo(st.nextToken()); |
81 |
|
} |
82 |
< |
catch(IOException e) { |
83 |
< |
_logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e); |
84 |
< |
} |
82 |
> |
|
83 |
> |
// prepare to print the message |
84 |
> |
PrintWriter out = smtp.getOutputStream(); |
85 |
> |
out.println("Subject: "+subject+"\n"); |
86 |
> |
|
87 |
> |
// send the message |
88 |
> |
out.println(message); |
89 |
> |
smtp.sendMessage(); |
90 |
> |
smtp.close(); |
91 |
> |
_logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ Alert.alertLevels[alert.getLevel()] + " level"); |
92 |
|
} |
93 |
+ |
catch(IOException e) { |
94 |
+ |
_logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e); |
95 |
+ |
} |
96 |
+ |
catch(PropertyNotFoundException e) { |
97 |
+ |
_logger.write(toString(), Logger.ERROR, "Error obtaining essential configuration: "+e); |
98 |
+ |
} |
99 |
|
} |
100 |
|
|
101 |
|
/** |
102 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
103 |
|
* method to provide clean logging (every class should have this). |
104 |
|
* |
105 |
< |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
105 |
> |
* This uses the uk.org.iscream.util.NameFormat class |
106 |
|
* to format the toString() |
107 |
|
* |
108 |
|
* @return the name of this class and its CVS revision |
115 |
|
} |
116 |
|
|
117 |
|
/** |
118 |
< |
* return the String representation of what the filter does |
118 |
> |
* Return the String representation of what the alerter does |
119 |
> |
* |
120 |
> |
* @return the description |
121 |
|
*/ |
122 |
|
public String getDescription(){ |
123 |
|
return DESC; |
124 |
|
} |
125 |
|
|
126 |
|
//---PRIVATE METHODS--- |
127 |
< |
|
127 |
> |
|
128 |
|
//---ACCESSOR/MUTATOR METHODS--- |
129 |
|
|
130 |
|
//---ATTRIBUTES--- |
133 |
– |
|
134 |
– |
// an integer value |
135 |
– |
private int _level; |
131 |
|
|
137 |
– |
// a semi-colon seperated list of destination addresses |
138 |
– |
private String _destList; |
139 |
– |
|
140 |
– |
// a single sender address |
141 |
– |
private String _sender; |
142 |
– |
|
143 |
– |
// the fqdn of an smtp server |
144 |
– |
private String _smtpServer; |
145 |
– |
|
146 |
– |
// a subject with the following: %level% and %message% |
147 |
– |
// eg, "i-scream e-mail alert: level %level%, %message%" |
148 |
– |
private String _subject; |
149 |
– |
|
150 |
– |
// a message body with the following: %level% and %message% |
151 |
– |
private String _message; |
152 |
– |
|
132 |
|
/** |
133 |
|
* This is the friendly identifier of the |
134 |
|
* component this class is running in. |
138 |
|
* can be placed here. This name could also |
139 |
|
* be changed to null for utility classes. |
140 |
|
*/ |
141 |
< |
private String _name = ClientMain.NAME; |
142 |
< |
|
164 |
< |
/** |
165 |
< |
* This holds a reference to the |
166 |
< |
* system logger that is being used. |
167 |
< |
*/ |
168 |
< |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
169 |
< |
|
141 |
> |
protected String _name = "EMail"; |
142 |
> |
|
143 |
|
//---STATIC ATTRIBUTES--- |
144 |
|
|
145 |
|
} |