ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/client/alerters/EMail__Alerter.java
Revision: 1.15
Committed: Tue Mar 6 02:33:55 2001 UTC (23 years, 2 months ago) by ajm
Branch: MAIN
Changes since 1.14: +11 -3 lines
Log Message:
Now passes the time since the first alert for a problem occoured.

Also has support for formatting and displaying this information as obtained from the config

File Contents

# User Rev Content
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 ajm 1.15 import java.text.*;
13 tdb 1.2
14 tdb 1.1 /**
15 tdb 1.8 * This alerter delivers alerts using e-mail.
16 tdb 1.1 *
17 ajm 1.15 * @author $Author: tdb1 $
18     * @version $Id: EMail__Alerter.java,v 1.14 2001/03/05 23:13:22 tdb1 Exp $
19 tdb 1.1 */
20 tdb 1.2 public class EMail__Alerter implements PluginAlerter {
21 tdb 1.1
22     //---FINAL ATTRIBUTES---
23    
24     /**
25     * The current CVS revision of this class
26     */
27 ajm 1.15 public final String REVISION = "$Revision: 1.14 $";
28 tdb 1.1
29 tdb 1.8 public final String DESC = "Sends alerts over e-mail.";
30 tdb 1.1
31 tdb 1.14 public final String DEFAULT_LEVEL = Alert.alertLevels[0];
32    
33     public final String NOT_CONFIGURED = "<NOT CONFIGURED>";
34    
35 tdb 1.1 //---STATIC METHODS---
36    
37     //---CONSTRUCTORS---
38    
39 tdb 1.8 public EMail__Alerter() {
40 tdb 1.6 _logger.write(toString(), Logger.SYSINIT, "IRC Alerter started");
41 tdb 1.2 }
42    
43 tdb 1.1 //---PUBLIC METHODS---
44    
45     public void sendAlert(Alert alert) {
46 tdb 1.8 ConfigurationProxy cp = ConfigurationProxy.getInstance();
47 tdb 1.14 String levelName;
48     try {
49     levelName = cp.getProperty(_name, "Alerter.EMail.level");
50     } catch (PropertyNotFoundException e) {
51     levelName = DEFAULT_LEVEL;
52     _logger.write(toString(), Logger.WARNING, "Alerter.EMail.level value unavailable using default of " + levelName);
53     }
54 ajm 1.9 int level = StringUtils.getStringPos(levelName, Alert.alertLevels);
55 tdb 1.2 // only send if it's equal (or above) our level
56 ajm 1.13 if(((alert.getLevel() == 0) && (alert.getLastLevel() >= level)) || (alert.getLevel() >= level)) {
57 ajm 1.9 String alertType = Alert.alertLevels[alert.getLevel()];
58     String thresholdType = Alert.thresholdLevels[alert.getThreshold()];
59 ajm 1.15 String timeFirstSince = DateUtils.formatTime(System.currentTimeMillis() - alert.getInitialAlertTime(), "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs");
60     String timeFirstOccured = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(alert.getInitialAlertTime()));
61    
62 tdb 1.2 // sort out the subject
63 tdb 1.14 String subject;
64     try {
65     subject = cp.getProperty(_name, "Alerter.EMail.subject");
66     } catch (PropertyNotFoundException e) {
67     subject = NOT_CONFIGURED;
68     _logger.write(toString(), Logger.WARNING, "Alerter.EMail.subject value unavailable using default of " + subject);
69     }
70 tdb 1.7 subject = StringUtils.replaceText(subject, "%level%", alertType);
71 ajm 1.9 subject = StringUtils.replaceText(subject, "%threshold%", thresholdType);
72 tdb 1.4 subject = StringUtils.replaceText(subject, "%source%", alert.getSource());
73 tdb 1.3 subject = StringUtils.replaceText(subject, "%value%", alert.getValue());
74     subject = StringUtils.replaceText(subject, "%thresholdValue%", alert.getThresholdValue());
75     subject = StringUtils.replaceText(subject, "%attributeName%", alert.getAttributeName());
76 ajm 1.9 subject = StringUtils.replaceText(subject, "%timeTillNextAlert%", getTimeString(Long.parseLong(alert.getTimeTillNextAlert())));
77 ajm 1.15 subject = StringUtils.replaceText(subject, "%timeSinceFirstAlert%", timeFirstSince);
78     subject = StringUtils.replaceText(subject, "%timeOfFirstAlert%", timeFirstOccured);
79 ajm 1.9
80 tdb 1.2 // sort out the message body
81 tdb 1.14 String message;
82     try {
83     message = cp.getProperty(_name, "Alerter.EMail.message");
84     } catch (PropertyNotFoundException e) {
85     message = NOT_CONFIGURED;
86     _logger.write(toString(), Logger.WARNING, "Alerter.EMail.message value unavailable using default of " + message);
87     }
88 tdb 1.7 message = StringUtils.replaceText(message, "%level%", alertType);
89 ajm 1.9 message = StringUtils.replaceText(message, "%threshold%", thresholdType);
90 tdb 1.4 message = StringUtils.replaceText(message, "%source%", alert.getSource());
91 tdb 1.3 message = StringUtils.replaceText(message, "%value%", alert.getValue());
92     message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue());
93     message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName());
94 ajm 1.9 message = StringUtils.replaceText(message, "%timeTillNextAlert%", getTimeString(Long.parseLong(alert.getTimeTillNextAlert())));
95 ajm 1.15 message = StringUtils.replaceText(message, "%timeSinceFirstAlert%", timeFirstSince);
96     message = StringUtils.replaceText(message, "%timeOfFirstAlert%", timeFirstOccured);
97 ajm 1.9
98 tdb 1.2 try {
99     // create SMTP message
100 tdb 1.8 Smtp smtp = new Smtp(cp.getProperty(_name, "Alerter.EMail.smtpServer"));
101 tdb 1.2 // set our sender
102 tdb 1.8 smtp.setSender(cp.getProperty(_name, "Alerter.EMail.sender"));
103 tdb 1.2
104 tdb 1.11 // get the default destination list
105     String destList = cp.getProperty(_name, "Alerter.EMail.defaultDestList");
106     // check if the source we're alerting about has a specific destination
107     String sourceDestList = cp.getProperty("Host."+alert.getSource(), "Alerter.EMail.destList");
108     if(sourceDestList != null) {
109     // if there is a source destination list, use it
110     destList = sourceDestList;
111     }
112    
113     // set the to: list
114     StringTokenizer st = new StringTokenizer(destList, ";");
115 tdb 1.2 while (st.hasMoreTokens()) {
116     smtp.setTo(st.nextToken());
117     }
118    
119     // prepare to print the message
120     PrintWriter out = smtp.getOutputStream();
121     out.println("Subject: "+subject);
122     out.println();
123    
124     // send the message
125     out.println(message);
126     smtp.sendMessage();
127     smtp.close();
128 ajm 1.10 _logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level");
129 tdb 1.2 }
130     catch(IOException e) {
131     _logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e);
132 tdb 1.14 }
133     catch(PropertyNotFoundException e) {
134     _logger.write(toString(), Logger.ERROR, "Error obtaining essential configuration: "+e);
135 tdb 1.2 }
136     }
137 tdb 1.1 }
138    
139     /**
140     * Overrides the {@link java.lang.Object#toString() Object.toString()}
141     * method to provide clean logging (every class should have this).
142     *
143     * This uses the uk.ac.ukc.iscream.util.NameFormat class
144     * to format the toString()
145     *
146     * @return the name of this class and its CVS revision
147     */
148     public String toString() {
149     return FormatName.getName(
150     _name,
151     getClass().getName(),
152     REVISION);
153     }
154    
155     /**
156     * return the String representation of what the filter does
157     */
158     public String getDescription(){
159     return DESC;
160     }
161    
162     //---PRIVATE METHODS---
163    
164 ajm 1.9 private String getTimeString(long time) {
165     String timeString = null;
166     if (time >= 60) {
167     timeString = (time / 60) + " minutes";
168     } else if (time >= 3600) {
169     timeString = ((time/60) / 60) + " hours";
170     } else {
171     timeString = time + " seconds";
172     }
173     return timeString;
174     }
175    
176 tdb 1.1 //---ACCESSOR/MUTATOR METHODS---
177    
178     //---ATTRIBUTES---
179 tdb 1.2
180 tdb 1.1 /**
181     * This is the friendly identifier of the
182     * component this class is running in.
183     * eg, a Filter may be called "filter1",
184     * If this class does not have an owning
185     * component, a name from the configuration
186     * can be placed here. This name could also
187     * be changed to null for utility classes.
188     */
189 ajm 1.9 private String _name = "EMail Alert";
190 tdb 1.1
191     /**
192     * This holds a reference to the
193     * system logger that is being used.
194     */
195     private Logger _logger = ReferenceManager.getInstance().getLogger();
196    
197     //---STATIC ATTRIBUTES---
198    
199     }