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