1 |
+ |
/* |
2 |
+ |
* i-scream central monitoring system |
3 |
+ |
* http://www.i-scream.org |
4 |
+ |
* Copyright (C) 2000-2002 i-scream |
5 |
+ |
* |
6 |
+ |
* This program is free software; you can redistribute it and/or |
7 |
+ |
* modify it under the terms of the GNU General Public License |
8 |
+ |
* as published by the Free Software Foundation; either version 2 |
9 |
+ |
* of the License, or (at your option) any later version. |
10 |
+ |
* |
11 |
+ |
* This program is distributed in the hope that it will be useful, |
12 |
+ |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
+ |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
+ |
* GNU General Public License for more details. |
15 |
+ |
* |
16 |
+ |
* You should have received a copy of the GNU General Public License |
17 |
+ |
* along with this program; if not, write to the Free Software |
18 |
+ |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
+ |
*/ |
20 |
+ |
|
21 |
|
//---PACKAGE DECLARATION--- |
22 |
< |
package uk.org.iscream.client.alerters; |
22 |
> |
package uk.org.iscream.cms.server.client.alerters; |
23 |
|
|
24 |
|
//---IMPORTS--- |
25 |
< |
import uk.org.iscream.client.*; |
26 |
< |
import uk.org.iscream.core.*; |
27 |
< |
import uk.org.iscream.util.*; |
28 |
< |
import uk.org.iscream.componentmanager.*; |
9 |
< |
|
10 |
< |
import java.util.*; |
25 |
> |
import uk.org.iscream.cms.server.client.*; |
26 |
> |
import uk.org.iscream.cms.server.core.*; |
27 |
> |
import uk.org.iscream.cms.util.*; |
28 |
> |
import uk.org.iscream.cms.server.componentmanager.*; |
29 |
|
import java.io.*; |
30 |
< |
import java.text.*; |
30 |
> |
import java.util.StringTokenizer; |
31 |
|
|
32 |
+ |
|
33 |
|
/** |
34 |
|
* This alerter delivers alerts using e-mail. |
35 |
|
* |
36 |
|
* @author $Author$ |
37 |
|
* @version $Id$ |
38 |
|
*/ |
39 |
< |
public class EMail__Alerter extends Thread implements PluginAlerter { |
39 |
> |
public class EMail__Alerter extends AlerterSkeleton { |
40 |
|
|
41 |
|
//---FINAL ATTRIBUTES--- |
42 |
|
|
45 |
|
*/ |
46 |
|
public final String REVISION = "$Revision$"; |
47 |
|
|
48 |
+ |
/** |
49 |
+ |
* A description of this alerter |
50 |
+ |
*/ |
51 |
|
public final String DESC = "Sends alerts over e-mail."; |
52 |
|
|
31 |
– |
public final String DEFAULT_LEVEL = Alert.alertLevels[0]; |
32 |
– |
|
33 |
– |
public final String NOT_CONFIGURED = "<NOT CONFIGURED>"; |
34 |
– |
|
53 |
|
//---STATIC METHODS--- |
54 |
|
|
55 |
|
//---CONSTRUCTORS--- |
56 |
|
|
39 |
– |
public EMail__Alerter() { |
40 |
– |
_logger.write(toString(), Logger.SYSINIT, "IRC Alerter started"); |
41 |
– |
this.start(); |
42 |
– |
} |
43 |
– |
|
57 |
|
//---PUBLIC METHODS--- |
58 |
|
|
59 |
+ |
/** |
60 |
+ |
* Implements the abstract method from the skeleton class. |
61 |
+ |
* This method will attempt to send an alert |
62 |
+ |
* message using the configured email configuration. |
63 |
+ |
* |
64 |
+ |
* @param alert the alert to send |
65 |
+ |
*/ |
66 |
|
public void sendAlert(Alert alert) { |
67 |
< |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
68 |
< |
String levelName; |
67 |
> |
// get the subject and replace fields |
68 |
> |
String subject; |
69 |
|
try { |
70 |
< |
levelName = cp.getProperty(_name, "Alerter.EMail.level"); |
70 |
> |
subject = _cp.getProperty(_name, "Alerter.EMail.subject"); |
71 |
|
} catch (PropertyNotFoundException e) { |
72 |
< |
levelName = DEFAULT_LEVEL; |
73 |
< |
_logger.write(toString(), Logger.WARNING, "Alerter.EMail.level value unavailable using default of " + levelName); |
72 |
> |
subject = NOT_CONFIGURED; |
73 |
> |
_logger.write(toString(), Logger.WARNING, "Alerter.EMail.subject value unavailable using default of " + subject); |
74 |
|
} |
75 |
< |
int level = StringUtils.getStringPos(levelName, Alert.alertLevels); |
76 |
< |
// only send if it's equal (or above) our level |
77 |
< |
if(((alert.getLevel() == 0) && (alert.getLastLevel() >= level)) || (alert.getLevel() >= level)) { |
78 |
< |
String alertType = Alert.alertLevels[alert.getLevel()]; |
79 |
< |
String thresholdType = Alert.thresholdLevels[alert.getThreshold()]; |
80 |
< |
String timeFirstSince = DateUtils.formatTime((System.currentTimeMillis() - alert.getInitialAlertTime())/1000, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
81 |
< |
String timeFirstOccured = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(alert.getInitialAlertTime())); |
82 |
< |
|
83 |
< |
// sort out the subject |
84 |
< |
String subject; |
85 |
< |
try { |
86 |
< |
subject = cp.getProperty(_name, "Alerter.EMail.subject"); |
87 |
< |
} catch (PropertyNotFoundException e) { |
88 |
< |
subject = NOT_CONFIGURED; |
89 |
< |
_logger.write(toString(), Logger.WARNING, "Alerter.EMail.subject value unavailable using default of " + subject); |
75 |
> |
subject = processAlertMessage(subject, alert); |
76 |
> |
|
77 |
> |
// get the message body and replace fields |
78 |
> |
String message; |
79 |
> |
try { |
80 |
> |
message = _cp.getProperty(_name, "Alerter.EMail.message"); |
81 |
> |
} catch (PropertyNotFoundException e) { |
82 |
> |
message = NOT_CONFIGURED; |
83 |
> |
_logger.write(toString(), Logger.WARNING, "Alerter.EMail.message value unavailable using default of " + message); |
84 |
> |
} |
85 |
> |
message = processAlertMessage(message, alert); |
86 |
> |
|
87 |
> |
// attempt to send the actual message |
88 |
> |
try { |
89 |
> |
// create SMTP message |
90 |
> |
Smtp smtp = new Smtp(_cp.getProperty(_name, "Alerter.EMail.smtpServer")); |
91 |
> |
// set our sender |
92 |
> |
smtp.setSender(_cp.getProperty(_name, "Alerter.EMail.sender")); |
93 |
> |
|
94 |
> |
// list of destination addresses |
95 |
> |
String destList = _cp.getProperty("Host."+alert.getSource(), "Alerter.EMail.destList"); |
96 |
> |
|
97 |
> |
// set the to: list |
98 |
> |
StringTokenizer st = new StringTokenizer(destList, ";"); |
99 |
> |
while (st.hasMoreTokens()) { |
100 |
> |
smtp.setTo(st.nextToken()); |
101 |
|
} |
102 |
< |
subject = StringUtils.replaceText(subject, "%level%", alertType); |
103 |
< |
subject = StringUtils.replaceText(subject, "%threshold%", thresholdType); |
104 |
< |
subject = StringUtils.replaceText(subject, "%source%", alert.getSource()); |
105 |
< |
subject = StringUtils.replaceText(subject, "%value%", alert.getValue()); |
106 |
< |
subject = StringUtils.replaceText(subject, "%thresholdValue%", alert.getThresholdValue()); |
107 |
< |
subject = StringUtils.replaceText(subject, "%attributeName%", alert.getAttributeName()); |
108 |
< |
subject = StringUtils.replaceText(subject, "%timeTillNextAlert%", DateUtils.getTimeString(Long.parseLong(alert.getTimeTillNextAlert()))); |
109 |
< |
subject = StringUtils.replaceText(subject, "%timeSinceFirstAlert%", timeFirstSince); |
110 |
< |
subject = StringUtils.replaceText(subject, "%timeOfFirstAlert%", timeFirstOccured); |
111 |
< |
|
81 |
< |
// sort out the message body |
82 |
< |
String message; |
83 |
< |
try { |
84 |
< |
message = cp.getProperty(_name, "Alerter.EMail.message"); |
85 |
< |
} catch (PropertyNotFoundException e) { |
86 |
< |
message = NOT_CONFIGURED; |
87 |
< |
_logger.write(toString(), Logger.WARNING, "Alerter.EMail.message value unavailable using default of " + message); |
88 |
< |
} |
89 |
< |
message = StringUtils.replaceText(message, "%level%", alertType); |
90 |
< |
message = StringUtils.replaceText(message, "%threshold%", thresholdType); |
91 |
< |
message = StringUtils.replaceText(message, "%source%", alert.getSource()); |
92 |
< |
message = StringUtils.replaceText(message, "%value%", alert.getValue()); |
93 |
< |
message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue()); |
94 |
< |
message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName()); |
95 |
< |
message = StringUtils.replaceText(message, "%timeTillNextAlert%", DateUtils.getTimeString(Long.parseLong(alert.getTimeTillNextAlert()))); |
96 |
< |
message = StringUtils.replaceText(message, "%timeSinceFirstAlert%", timeFirstSince); |
97 |
< |
message = StringUtils.replaceText(message, "%timeOfFirstAlert%", timeFirstOccured); |
98 |
< |
|
99 |
< |
try { |
100 |
< |
// create SMTP message |
101 |
< |
Smtp smtp = new Smtp(cp.getProperty(_name, "Alerter.EMail.smtpServer")); |
102 |
< |
// set our sender |
103 |
< |
smtp.setSender(cp.getProperty(_name, "Alerter.EMail.sender")); |
104 |
< |
|
105 |
< |
String destList = cp.getProperty("Host."+alert.getSource(), "Alerter.EMail.destList"); |
106 |
< |
|
107 |
< |
// set the to: list |
108 |
< |
StringTokenizer st = new StringTokenizer(destList, ";"); |
109 |
< |
while (st.hasMoreTokens()) { |
110 |
< |
smtp.setTo(st.nextToken()); |
111 |
< |
} |
112 |
< |
|
113 |
< |
// prepare to print the message |
114 |
< |
PrintWriter out = smtp.getOutputStream(); |
115 |
< |
out.println("Subject: "+subject); |
116 |
< |
out.println(); |
117 |
< |
|
118 |
< |
// send the message |
119 |
< |
out.println(message); |
120 |
< |
smtp.sendMessage(); |
121 |
< |
smtp.close(); |
122 |
< |
_logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level"); |
123 |
< |
} |
124 |
< |
catch(IOException e) { |
125 |
< |
_logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e); |
126 |
< |
} |
127 |
< |
catch(PropertyNotFoundException e) { |
128 |
< |
_logger.write(toString(), Logger.ERROR, "Error obtaining essential configuration: "+e); |
129 |
< |
} |
102 |
> |
|
103 |
> |
// prepare to print the message |
104 |
> |
PrintWriter out = smtp.getOutputStream(); |
105 |
> |
out.println("Subject: "+subject+"\n"); |
106 |
> |
|
107 |
> |
// send the message |
108 |
> |
out.println(message); |
109 |
> |
smtp.sendMessage(); |
110 |
> |
smtp.close(); |
111 |
> |
_logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ Alert.alertLevels[alert.getLevel()] + " level"); |
112 |
|
} |
113 |
+ |
catch(IOException e) { |
114 |
+ |
_logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e); |
115 |
+ |
} |
116 |
+ |
catch(PropertyNotFoundException e) { |
117 |
+ |
_logger.write(toString(), Logger.ERROR, "Error obtaining essential configuration: "+e); |
118 |
+ |
} |
119 |
|
} |
120 |
|
|
121 |
|
/** |
122 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
123 |
|
* method to provide clean logging (every class should have this). |
124 |
|
* |
125 |
< |
* This uses the uk.org.iscream.util.NameFormat class |
125 |
> |
* This uses the uk.org.iscream.cms.util.NameFormat class |
126 |
|
* to format the toString() |
127 |
|
* |
128 |
|
* @return the name of this class and its CVS revision |
135 |
|
} |
136 |
|
|
137 |
|
/** |
138 |
< |
* return the String representation of what the filter does |
138 |
> |
* Return the String representation of what the alerter does |
139 |
> |
* |
140 |
> |
* @return the description |
141 |
|
*/ |
142 |
|
public String getDescription(){ |
143 |
|
return DESC; |
146 |
|
//---PRIVATE METHODS--- |
147 |
|
|
148 |
|
//---ACCESSOR/MUTATOR METHODS--- |
159 |
– |
|
160 |
– |
protected Queue getQueue() { |
161 |
– |
return AlerterManager.getInstance().getQueue(); |
162 |
– |
} |
149 |
|
|
150 |
< |
protected int getQueueId() { |
151 |
< |
if (_qID == -1) { |
152 |
< |
_qID = getQueue().getQueue(); |
153 |
< |
_logger.write(toString(), Logger.DEBUG, "Assigned Queue - " + _qID); |
154 |
< |
} |
155 |
< |
return _qID; |
150 |
> |
/** |
151 |
> |
* Returns the "friendly" name of this class. This |
152 |
> |
* is simply an accessor for _name, required due to |
153 |
> |
* inheritance issues with extending AlerterSkeleton. |
154 |
> |
* |
155 |
> |
* @return the friendly name |
156 |
> |
*/ |
157 |
> |
protected String getFName() { |
158 |
> |
return _name; |
159 |
|
} |
160 |
|
|
161 |
|
//---ATTRIBUTES--- |
169 |
|
* can be placed here. This name could also |
170 |
|
* be changed to null for utility classes. |
171 |
|
*/ |
172 |
< |
private String _name = "EMail Alert"; |
184 |
< |
|
185 |
< |
/** |
186 |
< |
* This holds a reference to the |
187 |
< |
* system logger that is being used. |
188 |
< |
*/ |
189 |
< |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
172 |
> |
protected String _name = "EMail"; |
173 |
|
|
191 |
– |
/** |
192 |
– |
* The queue id for this alerters queue in the alert queue |
193 |
– |
*/ |
194 |
– |
private int _qID = -1; |
195 |
– |
|
196 |
– |
/** |
197 |
– |
* The running status of the alerter |
198 |
– |
*/ |
199 |
– |
private boolean _running = true; |
200 |
– |
|
174 |
|
//---STATIC ATTRIBUTES--- |
175 |
|
|
176 |
|
} |