11 |
|
import java.io.*; |
12 |
|
|
13 |
|
/** |
14 |
< |
* This Alert sends by e-mail. |
14 |
> |
* This alerter delivers alerts using e-mail. |
15 |
|
* |
16 |
|
* @author $Author$ |
17 |
|
* @version $Id$ |
25 |
|
*/ |
26 |
|
public final String REVISION = "$Revision$"; |
27 |
|
|
28 |
< |
public final String DESC = "Sends alerts by e-mail"; |
28 |
> |
public final String DESC = "Sends alerts over 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"); |
34 |
> |
public EMail__Alerter() { |
35 |
> |
_logger.write(toString(), Logger.SYSINIT, "IRC Alerter started"); |
36 |
|
} |
37 |
|
|
38 |
|
//---PUBLIC METHODS--- |
39 |
|
|
40 |
|
public void sendAlert(Alert alert) { |
41 |
+ |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
42 |
+ |
String levelName = cp.getProperty(_name, "Alerter.EMail.level"); |
43 |
+ |
int level = StringUtils.getStringPos(levelName, Alert.alertLevels); |
44 |
|
// only send if it's equal (or above) our level |
45 |
< |
if(alert.getLevel() >= _level) { |
45 |
> |
if(alert.getLevel() >= level) { |
46 |
> |
String alertType = Alert.alertLevels[alert.getLevel()]; |
47 |
> |
String thresholdType = Alert.thresholdLevels[alert.getThreshold()]; |
48 |
|
// sort out the subject |
49 |
< |
String subject = _subject; |
50 |
< |
subject = StringUtils.replaceText(subject, "%level%", String.valueOf(alert.getLevel())); |
49 |
> |
String subject = cp.getProperty(_name, "Alerter.EMail.subject"); |
50 |
> |
subject = StringUtils.replaceText(subject, "%level%", alertType); |
51 |
> |
subject = StringUtils.replaceText(subject, "%threshold%", thresholdType); |
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()); |
56 |
< |
|
56 |
> |
subject = StringUtils.replaceText(subject, "%timeTillNextAlert%", getTimeString(Long.parseLong(alert.getTimeTillNextAlert()))); |
57 |
> |
|
58 |
|
// sort out the message body |
59 |
< |
String message = _message; |
60 |
< |
message = StringUtils.replaceText(message, "%level%", String.valueOf(alert.getLevel())); |
59 |
> |
String message = cp.getProperty(_name, "Alerter.EMail.message"); |
60 |
> |
message = StringUtils.replaceText(message, "%level%", alertType); |
61 |
> |
message = StringUtils.replaceText(message, "%threshold%", thresholdType); |
62 |
> |
message = StringUtils.replaceText(message, "%source%", alert.getSource()); |
63 |
|
message = StringUtils.replaceText(message, "%value%", alert.getValue()); |
64 |
|
message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue()); |
65 |
|
message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName()); |
66 |
< |
|
66 |
> |
message = StringUtils.replaceText(message, "%timeTillNextAlert%", getTimeString(Long.parseLong(alert.getTimeTillNextAlert()))); |
67 |
> |
|
68 |
|
try { |
69 |
|
// create SMTP message |
70 |
< |
Smtp smtp = new Smtp(_smtpServer); |
70 |
> |
Smtp smtp = new Smtp(cp.getProperty(_name, "Alerter.EMail.smtpServer")); |
71 |
|
// set our sender |
72 |
< |
smtp.setSender(_sender); |
72 |
> |
smtp.setSender(cp.getProperty(_name, "Alerter.EMail.sender")); |
73 |
|
|
74 |
< |
// set the to list |
75 |
< |
StringTokenizer st = new StringTokenizer(_destList, ";"); |
74 |
> |
// get the default destination list |
75 |
> |
String destList = cp.getProperty(_name, "Alerter.EMail.defaultDestList"); |
76 |
> |
// check if the source we're alerting about has a specific destination |
77 |
> |
String sourceDestList = cp.getProperty("Host."+alert.getSource(), "Alerter.EMail.destList"); |
78 |
> |
if(sourceDestList != null) { |
79 |
> |
// if there is a source destination list, use it |
80 |
> |
destList = sourceDestList; |
81 |
> |
} |
82 |
> |
|
83 |
> |
// set the to: list |
84 |
> |
StringTokenizer st = new StringTokenizer(destList, ";"); |
85 |
|
while (st.hasMoreTokens()) { |
86 |
|
smtp.setTo(st.nextToken()); |
87 |
|
} |
95 |
|
out.println(message); |
96 |
|
smtp.sendMessage(); |
97 |
|
smtp.close(); |
98 |
+ |
_logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level"); |
99 |
|
} |
100 |
|
catch(IOException e) { |
101 |
|
_logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e); |
128 |
|
|
129 |
|
//---PRIVATE METHODS--- |
130 |
|
|
131 |
+ |
private String getTimeString(long time) { |
132 |
+ |
String timeString = null; |
133 |
+ |
if (time >= 60) { |
134 |
+ |
timeString = (time / 60) + " minutes"; |
135 |
+ |
} else if (time >= 3600) { |
136 |
+ |
timeString = ((time/60) / 60) + " hours"; |
137 |
+ |
} else { |
138 |
+ |
timeString = time + " seconds"; |
139 |
+ |
} |
140 |
+ |
return timeString; |
141 |
+ |
} |
142 |
+ |
|
143 |
|
//---ACCESSOR/MUTATOR METHODS--- |
144 |
|
|
145 |
|
//---ATTRIBUTES--- |
128 |
– |
|
129 |
– |
// an integer value |
130 |
– |
private int _level; |
146 |
|
|
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 |
– |
|
147 |
|
/** |
148 |
|
* This is the friendly identifier of the |
149 |
|
* component this class is running in. |
153 |
|
* can be placed here. This name could also |
154 |
|
* be changed to null for utility classes. |
155 |
|
*/ |
156 |
< |
private String _name = ClientMain.NAME; |
156 |
> |
private String _name = "EMail Alert"; |
157 |
|
|
158 |
|
/** |
159 |
|
* This holds a reference to the |