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