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 |
import java.util.*; |
11 |
import java.io.*; |
12 |
|
13 |
/** |
14 |
* This Alert sends by e-mail. |
15 |
* |
16 |
* @author $Author: tdb1 $ |
17 |
* @version $Id: EMail__Alerter.java,v 1.6 2001/03/02 01:32:09 tdb1 Exp $ |
18 |
*/ |
19 |
public class EMail__Alerter implements PluginAlerter { |
20 |
|
21 |
//---FINAL ATTRIBUTES--- |
22 |
|
23 |
/** |
24 |
* The current CVS revision of this class |
25 |
*/ |
26 |
public final String REVISION = "$Revision: 1.6 $"; |
27 |
|
28 |
public final String DESC = "Sends alerts by e-mail"; |
29 |
|
30 |
//---STATIC METHODS--- |
31 |
|
32 |
//---CONSTRUCTORS--- |
33 |
|
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 |
|
55 |
//---PUBLIC METHODS--- |
56 |
|
57 |
public void sendAlert(Alert alert) { |
58 |
// only send if it's equal (or above) our level |
59 |
if(alert.getLevel() >= _level) { |
60 |
String alertType = Alert.alerts[alert.getLevel()]+"["+String.valueOf(alert.getLevel())+"]"; |
61 |
// sort out the subject |
62 |
String subject = _subject; |
63 |
subject = StringUtils.replaceText(subject, "%level%", alertType); |
64 |
subject = StringUtils.replaceText(subject, "%source%", alert.getSource()); |
65 |
subject = StringUtils.replaceText(subject, "%value%", alert.getValue()); |
66 |
subject = StringUtils.replaceText(subject, "%thresholdValue%", alert.getThresholdValue()); |
67 |
subject = StringUtils.replaceText(subject, "%attributeName%", alert.getAttributeName()); |
68 |
|
69 |
// sort out the message body |
70 |
String message = _message; |
71 |
message = StringUtils.replaceText(message, "%level%", alertType); |
72 |
message = StringUtils.replaceText(message, "%source%", alert.getSource()); |
73 |
message = StringUtils.replaceText(message, "%value%", alert.getValue()); |
74 |
message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue()); |
75 |
message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName()); |
76 |
|
77 |
try { |
78 |
// create SMTP message |
79 |
Smtp smtp = new Smtp(_smtpServer); |
80 |
// set our sender |
81 |
smtp.setSender(_sender); |
82 |
|
83 |
// set the to list |
84 |
StringTokenizer st = new StringTokenizer(_destList, ";"); |
85 |
while (st.hasMoreTokens()) { |
86 |
smtp.setTo(st.nextToken()); |
87 |
} |
88 |
|
89 |
// prepare to print the message |
90 |
PrintWriter out = smtp.getOutputStream(); |
91 |
out.println("Subject: "+subject); |
92 |
out.println(); |
93 |
|
94 |
// send the message |
95 |
out.println(message); |
96 |
smtp.sendMessage(); |
97 |
smtp.close(); |
98 |
_logger.write(toString(), Logger.DEBUG, "Sending IRC Alert at level"+String.valueOf(alert.getLevel())); |
99 |
} |
100 |
catch(IOException e) { |
101 |
_logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e); |
102 |
} |
103 |
} |
104 |
} |
105 |
|
106 |
/** |
107 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
108 |
* method to provide clean logging (every class should have this). |
109 |
* |
110 |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
111 |
* to format the toString() |
112 |
* |
113 |
* @return the name of this class and its CVS revision |
114 |
*/ |
115 |
public String toString() { |
116 |
return FormatName.getName( |
117 |
_name, |
118 |
getClass().getName(), |
119 |
REVISION); |
120 |
} |
121 |
|
122 |
/** |
123 |
* return the String representation of what the filter does |
124 |
*/ |
125 |
public String getDescription(){ |
126 |
return DESC; |
127 |
} |
128 |
|
129 |
//---PRIVATE METHODS--- |
130 |
|
131 |
//---ACCESSOR/MUTATOR METHODS--- |
132 |
|
133 |
//---ATTRIBUTES--- |
134 |
|
135 |
// an integer value |
136 |
private int _level; |
137 |
|
138 |
// a semi-colon seperated list of destination addresses |
139 |
private String _destList; |
140 |
|
141 |
// a single sender address |
142 |
private String _sender; |
143 |
|
144 |
// the fqdn of an smtp server |
145 |
private String _smtpServer; |
146 |
|
147 |
// a subject with the following: %level% and %message% |
148 |
// eg, "i-scream e-mail alert: level %level%, %message%" |
149 |
private String _subject; |
150 |
|
151 |
// a message body with the following: %level% and %message% |
152 |
private String _message; |
153 |
|
154 |
/** |
155 |
* This is the friendly identifier of the |
156 |
* component this class is running in. |
157 |
* eg, a Filter may be called "filter1", |
158 |
* If this class does not have an owning |
159 |
* component, a name from the configuration |
160 |
* can be placed here. This name could also |
161 |
* be changed to null for utility classes. |
162 |
*/ |
163 |
private String _name = ClientMain.NAME; |
164 |
|
165 |
/** |
166 |
* This holds a reference to the |
167 |
* system logger that is being used. |
168 |
*/ |
169 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
170 |
|
171 |
//---STATIC ATTRIBUTES--- |
172 |
|
173 |
} |