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
(Generate patch)

Comparing projects/cms/source/server/uk/org/iscream/cms/server/client/alerters/EMail__Alerter.java (file contents):
Revision 1.6 by tdb, Fri Mar 2 01:32:09 2001 UTC vs.
Revision 1.15 by ajm, Tue Mar 6 02:33:55 2001 UTC

# Line 9 | Line 9 | import uk.ac.ukc.iscream.componentmanager.*;
9  
10   import java.util.*;
11   import java.io.*;
12 + import java.text.*;
13  
14   /**
15 < * This Alert sends by e-mail.
15 > * This alerter delivers alerts using e-mail.
16   *
17   * @author  $Author$
18   * @version $Id$
# Line 25 | Line 26 | public class EMail__Alerter implements PluginAlerter {
26       */
27      public final String REVISION = "$Revision$";
28      
29 <    public final String DESC = "Sends alerts by e-mail";
29 >    public final String DESC = "Sends alerts over e-mail.";
30      
31 +    public final String DEFAULT_LEVEL = Alert.alertLevels[0];
32 +    
33 +    public final String NOT_CONFIGURED = "<NOT CONFIGURED>";
34 +    
35   //---STATIC METHODS---
36  
37   //---CONSTRUCTORS---
38  
39 <    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");
51 <        
39 >    public EMail__Alerter() {      
40          _logger.write(toString(), Logger.SYSINIT, "IRC Alerter started");
41      }
42  
43   //---PUBLIC METHODS---
44  
45      public void sendAlert(Alert alert) {
46 +        ConfigurationProxy cp = ConfigurationProxy.getInstance();
47 +        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 +        int level = StringUtils.getStringPos(levelName, Alert.alertLevels);
55          // only send if it's equal (or above) our level
56 <        if(alert.getLevel() >= _level) {
56 >        if(((alert.getLevel() == 0) && (alert.getLastLevel() >= level)) || (alert.getLevel() >= level)) {
57 >            String alertType = Alert.alertLevels[alert.getLevel()];
58 >            String thresholdType = Alert.thresholdLevels[alert.getThreshold()];
59 >            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              // sort out the subject
63 <            String subject = _subject;
64 <            subject = StringUtils.replaceText(subject, "%level%", String.valueOf(alert.getLevel()));
63 >            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 >            subject = StringUtils.replaceText(subject, "%level%", alertType);
71 >            subject = StringUtils.replaceText(subject, "%threshold%", thresholdType);
72              subject = StringUtils.replaceText(subject, "%source%", alert.getSource());
73              subject = StringUtils.replaceText(subject, "%value%", alert.getValue());
74              subject = StringUtils.replaceText(subject, "%thresholdValue%", alert.getThresholdValue());
75              subject = StringUtils.replaceText(subject, "%attributeName%", alert.getAttributeName());
76 <            
76 >            subject = StringUtils.replaceText(subject, "%timeTillNextAlert%",  getTimeString(Long.parseLong(alert.getTimeTillNextAlert())));
77 >            subject = StringUtils.replaceText(subject, "%timeSinceFirstAlert%", timeFirstSince);
78 >            subject = StringUtils.replaceText(subject, "%timeOfFirstAlert%", timeFirstOccured);
79 >                        
80              // sort out the message body
81 <            String message = _message;
82 <            message = StringUtils.replaceText(message, "%level%", String.valueOf(alert.getLevel()));
81 >            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 >            message = StringUtils.replaceText(message, "%level%", alertType);
89 >            message = StringUtils.replaceText(message, "%threshold%", thresholdType);
90              message = StringUtils.replaceText(message, "%source%", alert.getSource());
91              message = StringUtils.replaceText(message, "%value%", alert.getValue());
92              message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue());
93              message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName());
94 <            
94 >            message = StringUtils.replaceText(message, "%timeTillNextAlert%",  getTimeString(Long.parseLong(alert.getTimeTillNextAlert())));
95 >            message = StringUtils.replaceText(message, "%timeSinceFirstAlert%", timeFirstSince);
96 >            message = StringUtils.replaceText(message, "%timeOfFirstAlert%", timeFirstOccured);
97 >                        
98              try {
99                  // create SMTP message
100 <                Smtp smtp = new Smtp(_smtpServer);
100 >                Smtp smtp = new Smtp(cp.getProperty(_name, "Alerter.EMail.smtpServer"));
101                  // set our sender
102 <                smtp.setSender(_sender);
102 >                smtp.setSender(cp.getProperty(_name, "Alerter.EMail.sender"));
103                  
104 <                // set the to list
105 <                StringTokenizer st = new StringTokenizer(_destList, ";");
104 >                // 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                  while (st.hasMoreTokens()) {
116                      smtp.setTo(st.nextToken());
117                  }
# Line 94 | Line 125 | public class EMail__Alerter implements PluginAlerter {
125                  out.println(message);
126                  smtp.sendMessage();
127                  smtp.close();
128 <                _logger.write(toString(), Logger.DEBUG, "Sending IRC Alert at level"+String.valueOf(alert.getLevel()));
128 >                _logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level");
129              }
130              catch(IOException e) {
131                  _logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e);
132              }
133 +            catch(PropertyNotFoundException e) {
134 +                _logger.write(toString(), Logger.ERROR, "Error obtaining essential configuration: "+e);
135 +            }
136          }
137      }
138  
# Line 127 | Line 161 | public class EMail__Alerter implements PluginAlerter {
161  
162   //---PRIVATE METHODS---
163  
164 +    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   //---ACCESSOR/MUTATOR METHODS---
177  
178   //---ATTRIBUTES---
133
134    // an integer value
135    private int _level;
179      
137    // a semi-colon seperated list of destination addresses
138    private String _destList;
139    
140    // a single sender address
141    private String _sender;
142    
143    // the fqdn of an smtp server
144    private String _smtpServer;
145    
146    // a subject with the following: %level% and %message%
147    // eg, "i-scream e-mail alert: level %level%, %message%"
148    private String _subject;
149
150    // a message body with the following: %level% and %message%
151    private String _message;
152
180      /**
181       * This is the friendly identifier of the
182       * component this class is running in.
# Line 159 | Line 186 | public class EMail__Alerter implements PluginAlerter {
186       * can be placed here.  This name could also
187       * be changed to null for utility classes.
188       */
189 <    private String _name = ClientMain.NAME;
189 >    private String _name = "EMail Alert";
190  
191      /**
192       * This holds a reference to the

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines