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