1 |
/* |
2 |
* i-scream central monitoring system |
3 |
* http://www.i-scream.org.uk |
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.cms.server.client; |
23 |
|
24 |
//---IMPORTS--- |
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.text.*; |
30 |
import java.util.*; |
31 |
|
32 |
/** |
33 |
* Skeleton class for Alerters |
34 |
* This skeleton reads alerts from the alert queue designated |
35 |
* to the Alerter, it then feeds the alerts to the sendAlert |
36 |
* method, which the extending class should implement. The class |
37 |
* should then handle sending the message. This class also has a |
38 |
* method to allow formatting of messages. |
39 |
* |
40 |
* @author $Author: tdb $ |
41 |
* @version $Id: AlerterSkeleton.java,v 1.11 2002/05/21 16:47:16 tdb Exp $ |
42 |
*/ |
43 |
public abstract class AlerterSkeleton extends Thread implements PluginAlerter { |
44 |
|
45 |
//---FINAL ATTRIBUTES--- |
46 |
|
47 |
/** |
48 |
* The current CVS revision of this class |
49 |
*/ |
50 |
public final String REVISION = "$Revision: 1.11 $"; |
51 |
|
52 |
/** |
53 |
* The default level to send alerts at |
54 |
*/ |
55 |
public final String DEFAULT_LEVEL = Alert.alertLevels[0]; |
56 |
|
57 |
/** |
58 |
* If a configuration property is not |
59 |
* configured, this will be used instead. |
60 |
*/ |
61 |
public final String NOT_CONFIGURED = "<NOT CONFIGURED>"; |
62 |
|
63 |
//---STATIC METHODS--- |
64 |
|
65 |
//---CONSTRUCTORS--- |
66 |
|
67 |
/** |
68 |
* Constructs and starts the alerter reading alerts |
69 |
*/ |
70 |
public AlerterSkeleton() { |
71 |
_logger.write(toString(), Logger.SYSINIT, "started."); |
72 |
this.start(); |
73 |
} |
74 |
|
75 |
//---PUBLIC METHODS--- |
76 |
|
77 |
/** |
78 |
* Reads the next alert from the alerter queue for this |
79 |
* alerter. It then looks up Alerter.<alerter name>.level in |
80 |
* the configuration to determine the lowest level of alert |
81 |
* the extending alerter should send. If the alert is |
82 |
* above this level, or if the alert is an OK for an alert |
83 |
* escalation which did rise above this alerters level, it then |
84 |
* calls sendAlert to tell the alerter to send the alert. If it |
85 |
* isn't above this level, it ignores the alert. |
86 |
*/ |
87 |
public void run() { |
88 |
while(_running) { |
89 |
try { |
90 |
Alert alert = (Alert) getQueue().get(getQueueId()); |
91 |
String levelName; |
92 |
try { |
93 |
levelName = _cp.getProperty(_name, "Alerter." + getFName() + ".level"); |
94 |
} catch (PropertyNotFoundException e) { |
95 |
levelName = DEFAULT_LEVEL; |
96 |
_logger.write(toString(), Logger.WARNING, "Alerter." + getFName() + ".level value unavailable using default of " + levelName); |
97 |
} |
98 |
int level = StringUtils.getStringPos(levelName, Alert.alertLevels); |
99 |
// only send if it's equal (or above) our level |
100 |
if(((alert.getLevel() == 0) && (alert.getLastLevel() >= level)) || (alert.getLevel() >= level)) { |
101 |
sendAlert(alert); |
102 |
} |
103 |
} catch (InvalidQueueException e) { |
104 |
_logger.write(this.toString(), Logger.ERROR, "Unable to get queue."); |
105 |
} |
106 |
} |
107 |
} |
108 |
|
109 |
/** |
110 |
* Sends an alert object using whatever transport |
111 |
* mechanism the extending class is implementing. |
112 |
* |
113 |
* @param alert the Alert to send |
114 |
*/ |
115 |
protected abstract void sendAlert(Alert alert); |
116 |
|
117 |
/** |
118 |
* Processes a message, replace key fields (eg. %level%) with |
119 |
* representative text from the given Alert object. |
120 |
* |
121 |
* @param message The message to process |
122 |
* @param alert The Alert object to get data from |
123 |
* @return the processed message |
124 |
*/ |
125 |
protected String processAlertMessage(String message, Alert alert) { |
126 |
// get some values |
127 |
String alertType = Alert.alertLevels[alert.getLevel()]; |
128 |
String thresholdType = Alert.thresholdLevels[alert.getThreshold()]; |
129 |
String timeSinceFirst = DateUtils.formatTime((System.currentTimeMillis() - alert.getInitialAlertTime())/1000, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
130 |
String timeFirstOccured = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(alert.getInitialAlertTime())); |
131 |
|
132 |
// replace fields in message |
133 |
message = StringUtils.replaceText(message, "%level%", alertType); |
134 |
message = StringUtils.replaceText(message, "%threshold%", thresholdType); |
135 |
message = StringUtils.replaceText(message, "%source%", alert.getSource()); |
136 |
message = StringUtils.replaceText(message, "%value%", alert.getValue()); |
137 |
message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue()); |
138 |
message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName()); |
139 |
message = StringUtils.replaceText(message, "%timeTillNextAlert%", DateUtils.getTimeString(Long.parseLong(alert.getTimeTillNextAlert()))); |
140 |
message = StringUtils.replaceText(message, "%timeSinceFirstAlert%", timeSinceFirst); |
141 |
message = StringUtils.replaceText(message, "%timeOfFirstAlert%", timeFirstOccured); |
142 |
return message; |
143 |
} |
144 |
|
145 |
/** |
146 |
* Return the String representation of what the alerter does |
147 |
* |
148 |
* @return the description |
149 |
*/ |
150 |
public abstract String getDescription(); |
151 |
|
152 |
|
153 |
//---PRIVATE METHODS--- |
154 |
|
155 |
//---ACCESSOR/MUTATOR METHODS--- |
156 |
|
157 |
/** |
158 |
* Gets hold of the Alerts queue from the AlerterManager. |
159 |
* |
160 |
* @return The Alerts queue |
161 |
*/ |
162 |
protected Queue getQueue() { |
163 |
return AlerterManager.getInstance().getQueue(); |
164 |
} |
165 |
|
166 |
/** |
167 |
* Gets an id to an individual queue |
168 |
* |
169 |
* @return the id to the queue |
170 |
*/ |
171 |
protected int getQueueId() { |
172 |
if (_qID == -1) { |
173 |
_qID = getQueue().getQueue(); |
174 |
_logger.write(toString(), Logger.DEBUG, "Assigned Queue - " + _qID); |
175 |
} |
176 |
return _qID; |
177 |
} |
178 |
|
179 |
/** |
180 |
* Returns the "friendly" name of this class. This |
181 |
* is simply an accessor for _name, required due to |
182 |
* inheritance issues with extending AlerterSkeleton. |
183 |
* |
184 |
* @return the friendly name |
185 |
*/ |
186 |
protected abstract String getFName(); |
187 |
|
188 |
//---ATTRIBUTES--- |
189 |
|
190 |
/** |
191 |
* This is the friendly identifier of the |
192 |
* component this class is running in. |
193 |
* eg, a Filter may be called "filter1", |
194 |
* If this class does not have an owning |
195 |
* component, a name from the configuration |
196 |
* can be placed here. This name could also |
197 |
* be changed to null for utility classes. |
198 |
*/ |
199 |
protected String _name = "AlerterSkeleton"; |
200 |
|
201 |
/** |
202 |
* This holds a reference to the |
203 |
* system logger that is being used. |
204 |
*/ |
205 |
protected Logger _logger = ReferenceManager.getInstance().getLogger(); |
206 |
|
207 |
/** |
208 |
* A reference to the singleton configuration proxy |
209 |
* in use, through which alerter configuration can be obtained |
210 |
*/ |
211 |
protected ConfigurationProxy _cp = ConfigurationProxy.getInstance(); |
212 |
|
213 |
/** |
214 |
* The ID of the queue the alerter will use. |
215 |
* Initially -1, but initialised on first use. |
216 |
*/ |
217 |
protected int _qID = -1; |
218 |
|
219 |
/** |
220 |
* The state of the alerter thread |
221 |
*/ |
222 |
protected boolean _running = true; |
223 |
|
224 |
//---STATIC ATTRIBUTES--- |
225 |
|
226 |
} |