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.12
Committed: Mon Mar 5 12:09:28 2001 UTC (23 years, 3 months ago) by ajm
Branch: MAIN
Changes since 1.11: +4 -4 lines
Log Message:
Now responds to OK messages even if that isn't the level its monitoring.

Maybe not ideal just yet though, it should only get OK's if its level has been passed, but not sure how to do that yet.

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    
13 tdb 1.1 /**
14 tdb 1.8 * This alerter delivers alerts using e-mail.
15 tdb 1.1 *
16 ajm 1.12 * @author $Author: tdb1 $
17     * @version $Id: EMail__Alerter.java,v 1.11 2001/03/05 02:09:49 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 ajm 1.12 public final String REVISION = "$Revision: 1.11 $";
27 tdb 1.1
28 tdb 1.8 public final String DESC = "Sends alerts over e-mail.";
29 tdb 1.1
30     //---STATIC METHODS---
31    
32     //---CONSTRUCTORS---
33    
34 tdb 1.8 public EMail__Alerter() {
35 tdb 1.6 _logger.write(toString(), Logger.SYSINIT, "IRC Alerter started");
36 tdb 1.2 }
37    
38 tdb 1.1 //---PUBLIC METHODS---
39    
40     public void sendAlert(Alert alert) {
41 tdb 1.8 ConfigurationProxy cp = ConfigurationProxy.getInstance();
42 ajm 1.9 String levelName = cp.getProperty(_name, "Alerter.EMail.level");
43     int level = StringUtils.getStringPos(levelName, Alert.alertLevels);
44 tdb 1.2 // only send if it's equal (or above) our level
45 ajm 1.12 if((alert.getLevel() == 0) || (alert.getLevel() >= level)) {
46 ajm 1.9 String alertType = Alert.alertLevels[alert.getLevel()];
47     String thresholdType = Alert.thresholdLevels[alert.getThreshold()];
48 tdb 1.2 // sort out the subject
49 tdb 1.8 String subject = cp.getProperty(_name, "Alerter.EMail.subject");
50 tdb 1.7 subject = StringUtils.replaceText(subject, "%level%", alertType);
51 ajm 1.9 subject = StringUtils.replaceText(subject, "%threshold%", thresholdType);
52 tdb 1.4 subject = StringUtils.replaceText(subject, "%source%", alert.getSource());
53 tdb 1.3 subject = StringUtils.replaceText(subject, "%value%", alert.getValue());
54     subject = StringUtils.replaceText(subject, "%thresholdValue%", alert.getThresholdValue());
55     subject = StringUtils.replaceText(subject, "%attributeName%", alert.getAttributeName());
56 ajm 1.9 subject = StringUtils.replaceText(subject, "%timeTillNextAlert%", getTimeString(Long.parseLong(alert.getTimeTillNextAlert())));
57    
58 tdb 1.2 // sort out the message body
59 tdb 1.8 String message = cp.getProperty(_name, "Alerter.EMail.message");
60 tdb 1.7 message = StringUtils.replaceText(message, "%level%", alertType);
61 ajm 1.9 message = StringUtils.replaceText(message, "%threshold%", thresholdType);
62 tdb 1.4 message = StringUtils.replaceText(message, "%source%", alert.getSource());
63 tdb 1.3 message = StringUtils.replaceText(message, "%value%", alert.getValue());
64     message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue());
65     message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName());
66 ajm 1.9 message = StringUtils.replaceText(message, "%timeTillNextAlert%", getTimeString(Long.parseLong(alert.getTimeTillNextAlert())));
67    
68 tdb 1.2 try {
69     // create SMTP message
70 tdb 1.8 Smtp smtp = new Smtp(cp.getProperty(_name, "Alerter.EMail.smtpServer"));
71 tdb 1.2 // set our sender
72 tdb 1.8 smtp.setSender(cp.getProperty(_name, "Alerter.EMail.sender"));
73 tdb 1.2
74 tdb 1.11 // 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 tdb 1.2 while (st.hasMoreTokens()) {
86     smtp.setTo(st.nextToken());
87     }
88    
89     // prepare to print the message
90     PrintWriter out = smtp.getOutputStream();
91     out.println("Subject: "+subject);
92     out.println();
93    
94     // send the message
95     out.println(message);
96     smtp.sendMessage();
97     smtp.close();
98 ajm 1.10 _logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level");
99 tdb 1.2 }
100     catch(IOException e) {
101     _logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e);
102     }
103     }
104 tdb 1.1 }
105    
106     /**
107     * Overrides the {@link java.lang.Object#toString() Object.toString()}
108     * method to provide clean logging (every class should have this).
109     *
110     * This uses the uk.ac.ukc.iscream.util.NameFormat class
111     * to format the toString()
112     *
113     * @return the name of this class and its CVS revision
114     */
115     public String toString() {
116     return FormatName.getName(
117     _name,
118     getClass().getName(),
119     REVISION);
120     }
121    
122     /**
123     * return the String representation of what the filter does
124     */
125     public String getDescription(){
126     return DESC;
127     }
128    
129     //---PRIVATE METHODS---
130    
131 ajm 1.9 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 tdb 1.1 //---ACCESSOR/MUTATOR METHODS---
144    
145     //---ATTRIBUTES---
146 tdb 1.2
147 tdb 1.1 /**
148     * This is the friendly identifier of the
149     * component this class is running in.
150     * eg, a Filter may be called "filter1",
151     * If this class does not have an owning
152     * component, a name from the configuration
153     * can be placed here. This name could also
154     * be changed to null for utility classes.
155     */
156 ajm 1.9 private String _name = "EMail Alert";
157 tdb 1.1
158     /**
159     * This holds a reference to the
160     * system logger that is being used.
161     */
162     private Logger _logger = ReferenceManager.getInstance().getLogger();
163    
164     //---STATIC ATTRIBUTES---
165    
166     }