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.5 2001/03/02 01:29:22 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.5 $"; |
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 |
// sort out the subject |
61 |
String subject = _subject; |
62 |
subject = StringUtils.replaceText(subject, "%level%", String.valueOf(alert.getLevel())); |
63 |
subject = StringUtils.replaceText(subject, "%source%", alert.getSource()); |
64 |
subject = StringUtils.replaceText(subject, "%value%", alert.getValue()); |
65 |
subject = StringUtils.replaceText(subject, "%thresholdValue%", alert.getThresholdValue()); |
66 |
subject = StringUtils.replaceText(subject, "%attributeName%", alert.getAttributeName()); |
67 |
|
68 |
// sort out the message body |
69 |
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()); |
75 |
|
76 |
try { |
77 |
// create SMTP message |
78 |
Smtp smtp = new Smtp(_smtpServer); |
79 |
// 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())); |
98 |
} |
99 |
catch(IOException e) { |
100 |
_logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e); |
101 |
} |
102 |
} |
103 |
} |
104 |
|
105 |
/** |
106 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
107 |
* method to provide clean logging (every class should have this). |
108 |
* |
109 |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
110 |
* to format the toString() |
111 |
* |
112 |
* @return the name of this class and its CVS revision |
113 |
*/ |
114 |
public String toString() { |
115 |
return FormatName.getName( |
116 |
_name, |
117 |
getClass().getName(), |
118 |
REVISION); |
119 |
} |
120 |
|
121 |
/** |
122 |
* return the String representation of what the filter does |
123 |
*/ |
124 |
public String getDescription(){ |
125 |
return DESC; |
126 |
} |
127 |
|
128 |
//---PRIVATE METHODS--- |
129 |
|
130 |
//---ACCESSOR/MUTATOR METHODS--- |
131 |
|
132 |
//---ATTRIBUTES--- |
133 |
|
134 |
// an integer value |
135 |
private int _level; |
136 |
|
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 |
|
153 |
/** |
154 |
* This is the friendly identifier of the |
155 |
* component this class is running in. |
156 |
* eg, a Filter may be called "filter1", |
157 |
* If this class does not have an owning |
158 |
* component, a name from the configuration |
159 |
* can be placed here. This name could also |
160 |
* be changed to null for utility classes. |
161 |
*/ |
162 |
private String _name = ClientMain.NAME; |
163 |
|
164 |
/** |
165 |
* This holds a reference to the |
166 |
* system logger that is being used. |
167 |
*/ |
168 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
169 |
|
170 |
//---STATIC ATTRIBUTES--- |
171 |
|
172 |
} |