1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.ac.ukc.iscream.client.monitors; |
2 |
> |
package uk.org.iscream.cms.server.client.monitors; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
|
import java.util.HashMap; |
6 |
|
import java.util.Iterator; |
7 |
< |
import uk.ac.ukc.iscream.client.*; |
8 |
< |
import uk.ac.ukc.iscream.core.*; |
9 |
< |
import uk.ac.ukc.iscream.util.*; |
10 |
< |
import uk.ac.ukc.iscream.componentmanager.*; |
7 |
> |
import java.util.StringTokenizer; |
8 |
> |
import uk.org.iscream.cms.server.client.*; |
9 |
> |
import uk.org.iscream.cms.server.core.*; |
10 |
> |
import uk.org.iscream.cms.server.util.*; |
11 |
> |
import uk.org.iscream.cms.server.componentmanager.*; |
12 |
|
|
13 |
|
/** |
14 |
< |
* This Monitor watches heartbeats |
14 |
> |
* This Monitor watches heartbeats. |
15 |
> |
* It generates an alert when a heartbeat that is expected |
16 |
> |
* does not arrive. Unlike all the other monitors, this one |
17 |
> |
* is driven by an event *not* occuring, rather than an |
18 |
> |
* event occuring. This means it must be actively checking |
19 |
> |
* for missing heartbeat's, and thus has an extra inner class |
20 |
> |
* thread. |
21 |
|
* |
22 |
|
* @author $Author$ |
23 |
|
* @version $Id$ |
24 |
|
*/ |
25 |
< |
public class Heartbeat__Monitor extends MonitorSkeleton implements Runnable { |
25 |
> |
public class Heartbeat__Monitor extends MonitorSkeleton { |
26 |
|
|
27 |
|
//---FINAL ATTRIBUTES--- |
28 |
|
|
31 |
|
*/ |
32 |
|
public final String REVISION = "$Revision$"; |
33 |
|
|
34 |
+ |
/** |
35 |
+ |
* A description of this monitor |
36 |
+ |
*/ |
37 |
|
public final String DESC = "Monitors Heartbeats."; |
38 |
|
|
39 |
+ |
/** |
40 |
+ |
* The default (used if not configured) period at |
41 |
+ |
* which to check for old heartbeats. (in seconds) |
42 |
+ |
*/ |
43 |
|
public final int DEFAULT_CHECK_PERIOD = 60; |
44 |
|
|
45 |
|
//---STATIC METHODS--- |
46 |
|
|
47 |
|
//---CONSTRUCTORS--- |
48 |
< |
|
48 |
> |
|
49 |
> |
/** |
50 |
> |
* Constructs a new Heartbeat monitor, and starts off |
51 |
> |
* the worker thread. |
52 |
> |
*/ |
53 |
|
public Heartbeat__Monitor() { |
54 |
< |
new Thread(this).start(); |
54 |
> |
super(); |
55 |
> |
createInitialHosts(); |
56 |
> |
new HeartbeatWorker().start(); |
57 |
|
} |
58 |
|
|
59 |
|
//---PUBLIC METHODS--- |
60 |
|
|
61 |
< |
public void run() { |
62 |
< |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
63 |
< |
while(true) { |
64 |
< |
// this cycle period of this monitor's checks |
65 |
< |
int checkPeriod = 0; |
66 |
< |
try { |
67 |
< |
checkPeriod = Integer.parseInt(cp.getProperty(_name, "Monitor.Heartbeat.checkPeriod")); |
68 |
< |
} catch (PropertyNotFoundException e) { |
69 |
< |
checkPeriod = DEFAULT_CHECK_PERIOD; |
50 |
< |
_logger.write(toString(), Logger.WARNING, "Monitor.Heartbeat.checkPeriod value unavailable using default of " + checkPeriod + " seconds"); |
51 |
< |
} catch (NumberFormatException e) { |
52 |
< |
checkPeriod = DEFAULT_CHECK_PERIOD; |
53 |
< |
_logger.write(toString(), Logger.WARNING, "Erronous Monitor.Heartbeat.checkPeriod value in configuration using default of " + checkPeriod + " seconds"); |
54 |
< |
} |
55 |
< |
|
61 |
> |
/** |
62 |
> |
* Analyse a packet of data. In this case, this will just |
63 |
> |
* register the fact that a heartbeat has arrived. |
64 |
> |
* |
65 |
> |
* @param packet The packet of data to analyse |
66 |
> |
*/ |
67 |
> |
public void analysePacket(XMLPacket packet) { |
68 |
> |
String source = packet.getParam("packet.attributes.machine_name"); |
69 |
> |
if (!_hosts.containsKey(source)) { |
70 |
|
synchronized(this) { |
71 |
< |
// perform the checks (use HB hash, although they *should* be the same) |
58 |
< |
Iterator i = _hosts.keySet().iterator(); |
59 |
< |
while(i.hasNext()) { |
60 |
< |
// get host |
61 |
< |
String source = (String) i.next(); |
62 |
< |
// check it |
63 |
< |
boolean remove = analyseHB(source); |
64 |
< |
if(remove) { |
65 |
< |
i.remove(); |
66 |
< |
} |
67 |
< |
} |
71 |
> |
_hosts.put(source, new HeartbeatHolder(new Register(source, _name))); |
72 |
|
} |
69 |
– |
|
70 |
– |
// wait a while |
71 |
– |
try {Thread.sleep(checkPeriod * 1000);} catch (InterruptedException e) {} |
73 |
|
} |
74 |
+ |
HeartbeatHolder lastHeartbeat = (HeartbeatHolder) _hosts.get(source); |
75 |
+ |
lastHeartbeat.setLastHeartbeat(System.currentTimeMillis()/1000); |
76 |
|
} |
77 |
|
|
75 |
– |
public void analysePacket(XMLPacket packet) { |
76 |
– |
if (packet.getParam("packet.attributes.type").equals("heartbeat")) { |
77 |
– |
String source = packet.getParam("packet.attributes.machine_name"); |
78 |
– |
if (!_hosts.containsKey(source)) { |
79 |
– |
synchronized(this) { |
80 |
– |
HashMap registerHash = new HashMap(); |
81 |
– |
registerHash.put(source, new Register(source, _name, 1)); |
82 |
– |
_hosts.put(source, new HeartbeatHolder(registerHash)); |
83 |
– |
} |
84 |
– |
} |
85 |
– |
HeartbeatHolder lastHeartbeat = (HeartbeatHolder) _hosts.get(source); |
86 |
– |
lastHeartbeat.setLastHeartbeat(System.currentTimeMillis()/1000); |
87 |
– |
} |
88 |
– |
} |
89 |
– |
|
78 |
|
/** |
79 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
80 |
|
* method to provide clean logging (every class should have this). |
81 |
|
* |
82 |
< |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
82 |
> |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
83 |
|
* to format the toString() |
84 |
|
* |
85 |
|
* @return the name of this class and its CVS revision |
99 |
|
} |
100 |
|
|
101 |
|
//---PRIVATE METHODS--- |
114 |
– |
|
115 |
– |
private boolean analyseHB(String source) { |
116 |
– |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
117 |
– |
HeartbeatHolder hbHolder = (HeartbeatHolder) _hosts.get(source); |
118 |
– |
Register reg = (Register) ((HashMap) hbHolder.getRegisterHash()).get(source); |
119 |
– |
|
120 |
– |
// get host's HB interval (seconds) |
121 |
– |
// this should always exist, thus we set to 0 |
122 |
– |
int hostHBinterval = 0; |
123 |
– |
try { |
124 |
– |
hostHBinterval = Integer.parseInt(cp.getProperty("Host."+source, "Host.TCPUpdateTime")); |
125 |
– |
} catch (PropertyNotFoundException e) { |
126 |
– |
hostHBinterval = 0; |
127 |
– |
_logger.write(toString(), Logger.WARNING, "TCPUpdateTime value unavailable using default of " + hostHBinterval + " seconds"); |
128 |
– |
} catch (NumberFormatException e) { |
129 |
– |
hostHBinterval = 0; |
130 |
– |
_logger.write(toString(), Logger.WARNING, "Erronous TCPUpdateTime value in configuration using default of " + hostHBinterval + " seconds"); |
131 |
– |
} |
132 |
– |
|
133 |
– |
// get host's last HB time (seconds) |
134 |
– |
long lastHeartbeat = hbHolder.getLastHeartbeat(); |
135 |
– |
// time since last heartbeat (seconds) |
136 |
– |
long timeSinceLastHB = (System.currentTimeMillis()/1000) - lastHeartbeat; |
137 |
– |
// time since (or until if negative) the expected heartbeat |
138 |
– |
long timeSinceExpectedHB = timeSinceLastHB - (long) hostHBinterval; |
139 |
– |
|
140 |
– |
// best do a check in case the expected heartbeat is in the future |
141 |
– |
if(timeSinceExpectedHB < 0) { |
142 |
– |
timeSinceExpectedHB = 0; |
143 |
– |
} |
144 |
– |
|
145 |
– |
// find out the threshold level we're at |
146 |
– |
int newThreshold = checkAttributeThreshold(timeSinceExpectedHB, reg); |
147 |
– |
|
148 |
– |
// process the alert |
149 |
– |
processAlert(newThreshold, 0, "Heartbeat", reg, source, String.valueOf(timeSinceExpectedHB)); |
150 |
– |
|
151 |
– |
if(reg.getLastAlertLevel(0) == Alert.alertFINAL) { |
152 |
– |
return true; |
153 |
– |
} |
154 |
– |
return false; |
155 |
– |
} |
102 |
|
|
103 |
+ |
/** |
104 |
+ |
* Checks whether the time since the last heartbeat |
105 |
+ |
* is beyond the threshold(s). |
106 |
+ |
* |
107 |
+ |
* @param timeSinceLastHB a long time since the last heartbeat arrived |
108 |
+ |
* @param reg the Register for this host |
109 |
+ |
* @return the level which has been breached, if any |
110 |
+ |
*/ |
111 |
|
private int checkAttributeThreshold(long timeSinceLastHB, Register reg) { |
112 |
|
for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) { |
113 |
|
if (reg.getThreshold(thresholdLevel) != -1.0) { |
118 |
|
} |
119 |
|
return Alert.thresholdNORMAL; |
120 |
|
} |
121 |
+ |
|
122 |
+ |
/** |
123 |
+ |
* Gets an initial list of hosts from the config |
124 |
+ |
* and adds a fake set of heartbeats for them. |
125 |
+ |
* If the hosts don't respond within the timeout |
126 |
+ |
* period an alert will be raised. |
127 |
+ |
* |
128 |
+ |
* The effect of this is to allow us to know about |
129 |
+ |
* hosts which weren't on when we started up, and |
130 |
+ |
* will thus never have generated a heartbeat - yet |
131 |
+ |
* will still want to know they're not responding. |
132 |
+ |
*/ |
133 |
+ |
private void createInitialHosts() { |
134 |
+ |
// get the initial list of hosts from the config |
135 |
+ |
String initialHosts = ""; |
136 |
+ |
try { |
137 |
+ |
initialHosts = _cp.getProperty(_name, "Monitor.Heartbeat.initialHosts"); |
138 |
+ |
} catch (PropertyNotFoundException e) { |
139 |
+ |
// just leave initialHosts empty |
140 |
+ |
_logger.write(Heartbeat__Monitor.this.toString(), Logger.DEBUG, "No initial list of hosts set, defaulting to none."); |
141 |
+ |
} |
142 |
+ |
|
143 |
+ |
// parse through the initial hosts adding them |
144 |
+ |
StringTokenizer st = new StringTokenizer(initialHosts, ";"); |
145 |
+ |
while (st.hasMoreTokens()) { |
146 |
+ |
String source = st.nextToken(); |
147 |
+ |
// check if they already exist, don't want to add them twice |
148 |
+ |
if (!_hosts.containsKey(source)) { |
149 |
+ |
synchronized(this) { |
150 |
+ |
_hosts.put(source, new HeartbeatHolder(new Register(source, _name))); |
151 |
+ |
} |
152 |
+ |
} |
153 |
+ |
HeartbeatHolder lastHeartbeat = (HeartbeatHolder) _hosts.get(source); |
154 |
+ |
// set a "fake" heartbeat |
155 |
+ |
lastHeartbeat.setLastHeartbeat(System.currentTimeMillis()/1000); |
156 |
+ |
} |
157 |
+ |
} |
158 |
|
|
159 |
|
//---ACCESSOR/MUTATOR METHODS--- |
160 |
< |
|
160 |
> |
|
161 |
> |
/** |
162 |
> |
* Returns a reference to the Queue we're getting data |
163 |
> |
* from. This is specific to this monitor. |
164 |
> |
* |
165 |
> |
* @return a reference to a Queue to get data from |
166 |
> |
*/ |
167 |
> |
protected Queue getQueue() { |
168 |
> |
return MonitorManager.getInstance().getHeartbeatQueue(); |
169 |
> |
} |
170 |
> |
|
171 |
|
//---ATTRIBUTES--- |
172 |
|
|
173 |
|
/** |
185 |
|
* A reference to the configuration proxy in use |
186 |
|
*/ |
187 |
|
private ConfigurationProxy _cp = ConfigurationProxy.getInstance(); |
188 |
< |
|
188 |
> |
|
189 |
> |
/** |
190 |
> |
* A HashMap of hosts, with associated HeartbeatHolder's. |
191 |
> |
*/ |
192 |
|
private HashMap _hosts = new HashMap(); |
193 |
+ |
|
194 |
+ |
/** |
195 |
+ |
* A reference to the system logger. |
196 |
+ |
*/ |
197 |
+ |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
198 |
|
|
199 |
|
//---STATIC ATTRIBUTES--- |
200 |
|
|
201 |
|
//---INNER CLASSES--- |
202 |
< |
|
202 |
> |
|
203 |
> |
/** |
204 |
> |
* This inner class simply holding some information |
205 |
> |
* about a specific host. |
206 |
> |
*/ |
207 |
|
private class HeartbeatHolder { |
208 |
|
|
209 |
< |
public HeartbeatHolder(HashMap registerHash) { |
210 |
< |
_registerHash = registerHash; |
209 |
> |
/** |
210 |
> |
* Construct a new HeartbeatHolder. |
211 |
> |
*/ |
212 |
> |
public HeartbeatHolder(Register register) { |
213 |
> |
_register = register; |
214 |
|
} |
215 |
|
|
216 |
+ |
/** |
217 |
+ |
* Set the time of the last heartbeat |
218 |
+ |
*/ |
219 |
|
public void setLastHeartbeat(long lastHeartbeat) { |
220 |
|
_lastHeartbeat = lastHeartbeat; |
221 |
|
} |
222 |
|
|
223 |
+ |
/** |
224 |
+ |
* Get the time of the last heartbeat |
225 |
+ |
*/ |
226 |
|
public long getLastHeartbeat() { |
227 |
|
return _lastHeartbeat; |
228 |
|
} |
229 |
|
|
230 |
< |
public HashMap getRegisterHash() { |
231 |
< |
return _registerHash; |
230 |
> |
/** |
231 |
> |
* Get the Register |
232 |
> |
*/ |
233 |
> |
public Register getRegister() { |
234 |
> |
return _register; |
235 |
|
} |
236 |
|
|
237 |
+ |
/** |
238 |
+ |
* last heartbeat time |
239 |
+ |
*/ |
240 |
|
private long _lastHeartbeat; |
241 |
< |
private HashMap _registerHash; |
242 |
< |
} |
243 |
< |
|
241 |
> |
|
242 |
> |
/** |
243 |
> |
* register ref |
244 |
> |
*/ |
245 |
> |
private Register _register; |
246 |
> |
} |
247 |
> |
|
248 |
> |
/** |
249 |
> |
* This worker thread just checks all the hosts and then |
250 |
> |
* waits a period of time before doing it again. It sends |
251 |
> |
* Alerts as required. |
252 |
> |
*/ |
253 |
> |
private class HeartbeatWorker extends Thread { |
254 |
> |
|
255 |
> |
/** |
256 |
> |
* The main run method of this worker thread. It simply |
257 |
> |
* checks through all the hosts it has stored, running |
258 |
> |
* the analyseHB method on each. It then removes any |
259 |
> |
* that have passed a FINAL, and waits a (configured) |
260 |
> |
* length of time before doing it again. |
261 |
> |
*/ |
262 |
> |
public void run() { |
263 |
> |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
264 |
> |
while(true) { |
265 |
> |
// this cycle period of this monitor's checks |
266 |
> |
int checkPeriod = 0; |
267 |
> |
try { |
268 |
> |
checkPeriod = Integer.parseInt(cp.getProperty(_name, "Monitor.Heartbeat.checkPeriod")); |
269 |
> |
} catch (PropertyNotFoundException e) { |
270 |
> |
checkPeriod = DEFAULT_CHECK_PERIOD; |
271 |
> |
_logger.write(Heartbeat__Monitor.this.toString(), Logger.WARNING, "Monitor.Heartbeat.checkPeriod value unavailable using default of " + checkPeriod + " seconds"); |
272 |
> |
} catch (NumberFormatException e) { |
273 |
> |
checkPeriod = DEFAULT_CHECK_PERIOD; |
274 |
> |
_logger.write(Heartbeat__Monitor.this.toString(), Logger.WARNING, "Erronous Monitor.Heartbeat.checkPeriod value in configuration using default of " + checkPeriod + " seconds"); |
275 |
> |
} |
276 |
> |
|
277 |
> |
synchronized(Heartbeat__Monitor.this) { |
278 |
> |
// perform the checks (use HB hash, although they *should* be the same) |
279 |
> |
Iterator i = _hosts.keySet().iterator(); |
280 |
> |
while(i.hasNext()) { |
281 |
> |
// get host |
282 |
> |
String source = (String) i.next(); |
283 |
> |
// check it |
284 |
> |
boolean remove = analyseHB(source); |
285 |
> |
// remove it if it's passed a FINAL |
286 |
> |
if(remove) { |
287 |
> |
i.remove(); |
288 |
> |
} |
289 |
> |
} |
290 |
> |
} |
291 |
> |
|
292 |
> |
// wait a while |
293 |
> |
try {Thread.sleep(checkPeriod * 1000);} catch (InterruptedException e) {} |
294 |
> |
} |
295 |
> |
} |
296 |
> |
|
297 |
> |
/** |
298 |
> |
* Analyses a given host's state, and if need be generates |
299 |
> |
* a relevant Alert. Note that it also checks if the last |
300 |
> |
* alert sent is FINAL, in which case it returns true to |
301 |
> |
* indicate removal of this host. |
302 |
> |
* |
303 |
> |
* @param source the host to check |
304 |
> |
* @return whether this host can be deleted |
305 |
> |
*/ |
306 |
> |
private boolean analyseHB(String source) { |
307 |
> |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
308 |
> |
HeartbeatHolder hbHolder = (HeartbeatHolder) _hosts.get(source); |
309 |
> |
Register reg = hbHolder.getRegister(); |
310 |
> |
|
311 |
> |
// get host's HB interval (seconds) |
312 |
> |
// this should always exist, thus we set to 0 |
313 |
> |
int hostHBinterval = 0; |
314 |
> |
try { |
315 |
> |
hostHBinterval = Integer.parseInt(cp.getProperty("Host."+source, "Host.TCPUpdateTime")); |
316 |
> |
} catch (PropertyNotFoundException e) { |
317 |
> |
hostHBinterval = 0; |
318 |
> |
_logger.write(Heartbeat__Monitor.this.toString(), Logger.WARNING, "TCPUpdateTime value unavailable using default of " + hostHBinterval + " seconds"); |
319 |
> |
} catch (NumberFormatException e) { |
320 |
> |
hostHBinterval = 0; |
321 |
> |
_logger.write(Heartbeat__Monitor.this.toString(), Logger.WARNING, "Erronous TCPUpdateTime value in configuration using default of " + hostHBinterval + " seconds"); |
322 |
> |
} |
323 |
> |
|
324 |
> |
// get host's last HB time (seconds) |
325 |
> |
long lastHeartbeat = hbHolder.getLastHeartbeat(); |
326 |
> |
// time since last heartbeat (seconds) |
327 |
> |
long timeSinceLastHB = (System.currentTimeMillis()/1000) - lastHeartbeat; |
328 |
> |
// time since (or until if negative) the expected heartbeat |
329 |
> |
long timeSinceExpectedHB = timeSinceLastHB - (long) hostHBinterval; |
330 |
> |
|
331 |
> |
// best do a check in case the expected heartbeat is in the future |
332 |
> |
if(timeSinceExpectedHB < 0) { |
333 |
> |
timeSinceExpectedHB = 0; |
334 |
> |
} |
335 |
> |
|
336 |
> |
// find out the threshold level we're at |
337 |
> |
int newThreshold = checkAttributeThreshold(timeSinceExpectedHB, reg); |
338 |
> |
|
339 |
> |
// process the alert |
340 |
> |
Heartbeat__Monitor.this.processAlert(newThreshold, "Heartbeat", reg, source, String.valueOf(timeSinceExpectedHB)); |
341 |
> |
|
342 |
> |
if(reg.getLastAlertLevel() == Alert.alertFINAL) { |
343 |
> |
return true; |
344 |
> |
} |
345 |
> |
return false; |
346 |
> |
} |
347 |
> |
} |
348 |
|
} |