1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.ac.ukc.iscream.client.monitors; |
2 |
> |
package uk.org.iscream.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 uk.org.iscream.client.*; |
8 |
> |
import uk.org.iscream.core.*; |
9 |
> |
import uk.org.iscream.util.*; |
10 |
> |
import uk.org.iscream.componentmanager.*; |
11 |
|
|
12 |
|
/** |
13 |
|
* This Monitor watches heartbeats |
15 |
|
* @author $Author$ |
16 |
|
* @version $Id$ |
17 |
|
*/ |
18 |
< |
public class Heartbeat__Monitor extends MonitorSkeleton implements Runnable { |
18 |
> |
public class Heartbeat__Monitor extends MonitorSkeleton { |
19 |
|
|
20 |
|
//---FINAL ATTRIBUTES--- |
21 |
|
|
33 |
|
//---CONSTRUCTORS--- |
34 |
|
|
35 |
|
public Heartbeat__Monitor() { |
36 |
< |
new Thread(this).start(); |
36 |
> |
super(); |
37 |
> |
new HeartbeatWorker().start(); |
38 |
|
} |
39 |
|
|
40 |
|
//---PUBLIC METHODS--- |
41 |
|
|
42 |
< |
public void run() { |
43 |
< |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
44 |
< |
while(true) { |
44 |
< |
// this cycle period of this monitor's checks |
45 |
< |
int checkPeriod = 0; |
46 |
< |
try { |
47 |
< |
checkPeriod = Integer.parseInt(cp.getProperty(_name, "Monitor.Heartbeat.checkPeriod")); |
48 |
< |
} catch (PropertyNotFoundException e) { |
49 |
< |
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 |
< |
|
42 |
> |
public void analysePacket(XMLPacket packet) { |
43 |
> |
String source = packet.getParam("packet.attributes.machine_name"); |
44 |
> |
if (!_hosts.containsKey(source)) { |
45 |
|
synchronized(this) { |
46 |
< |
// perform the checks (use HB hash, although they *should* be the same) |
47 |
< |
Iterator i = _hosts.keySet().iterator(); |
48 |
< |
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 |
< |
} |
46 |
> |
HashMap registerHash = new HashMap(); |
47 |
> |
registerHash.put(source, new Register(source, _name)); |
48 |
> |
_hosts.put(source, new HeartbeatHolder(registerHash)); |
49 |
|
} |
69 |
– |
|
70 |
– |
// wait a while |
71 |
– |
try {Thread.sleep(checkPeriod * 1000);} catch (InterruptedException e) {} |
50 |
|
} |
51 |
+ |
HeartbeatHolder lastHeartbeat = (HeartbeatHolder) _hosts.get(source); |
52 |
+ |
lastHeartbeat.setLastHeartbeat(System.currentTimeMillis()/1000); |
53 |
|
} |
54 |
|
|
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 |
– |
|
55 |
|
/** |
56 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
57 |
|
* method to provide clean logging (every class should have this). |
58 |
|
* |
59 |
< |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
59 |
> |
* This uses the uk.org.iscream.util.NameFormat class |
60 |
|
* to format the toString() |
61 |
|
* |
62 |
|
* @return the name of this class and its CVS revision |
76 |
|
} |
77 |
|
|
78 |
|
//---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 |
– |
} |
79 |
|
|
80 |
|
private int checkAttributeThreshold(long timeSinceLastHB, Register reg) { |
81 |
|
for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) { |
90 |
|
|
91 |
|
//---ACCESSOR/MUTATOR METHODS--- |
92 |
|
|
93 |
+ |
protected Queue getQueue() { |
94 |
+ |
return MonitorManager.getInstance().getHeartbeatQueue(); |
95 |
+ |
} |
96 |
+ |
|
97 |
|
//---ATTRIBUTES--- |
98 |
|
|
99 |
|
/** |
114 |
|
|
115 |
|
private HashMap _hosts = new HashMap(); |
116 |
|
|
117 |
+ |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
118 |
+ |
|
119 |
|
//---STATIC ATTRIBUTES--- |
120 |
|
|
121 |
|
//---INNER CLASSES--- |
140 |
|
|
141 |
|
private long _lastHeartbeat; |
142 |
|
private HashMap _registerHash; |
143 |
< |
} |
144 |
< |
|
143 |
> |
} |
144 |
> |
|
145 |
> |
private class HeartbeatWorker extends Thread { |
146 |
> |
|
147 |
> |
public void run() { |
148 |
> |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
149 |
> |
while(true) { |
150 |
> |
// this cycle period of this monitor's checks |
151 |
> |
int checkPeriod = 0; |
152 |
> |
try { |
153 |
> |
checkPeriod = Integer.parseInt(cp.getProperty(_name, "Monitor.Heartbeat.checkPeriod")); |
154 |
> |
} catch (PropertyNotFoundException e) { |
155 |
> |
checkPeriod = DEFAULT_CHECK_PERIOD; |
156 |
> |
_logger.write(this.toString(), Logger.WARNING, "Monitor.Heartbeat.checkPeriod value unavailable using default of " + checkPeriod + " seconds"); |
157 |
> |
} catch (NumberFormatException e) { |
158 |
> |
checkPeriod = DEFAULT_CHECK_PERIOD; |
159 |
> |
_logger.write(this.toString(), Logger.WARNING, "Erronous Monitor.Heartbeat.checkPeriod value in configuration using default of " + checkPeriod + " seconds"); |
160 |
> |
} |
161 |
> |
|
162 |
> |
synchronized(this) { |
163 |
> |
// perform the checks (use HB hash, although they *should* be the same) |
164 |
> |
Iterator i = _hosts.keySet().iterator(); |
165 |
> |
while(i.hasNext()) { |
166 |
> |
// get host |
167 |
> |
String source = (String) i.next(); |
168 |
> |
// check it |
169 |
> |
boolean remove = analyseHB(source); |
170 |
> |
if(remove) { |
171 |
> |
i.remove(); |
172 |
> |
} |
173 |
> |
} |
174 |
> |
} |
175 |
> |
|
176 |
> |
// wait a while |
177 |
> |
try {Thread.sleep(checkPeriod * 1000);} catch (InterruptedException e) {} |
178 |
> |
} |
179 |
> |
} |
180 |
> |
|
181 |
> |
private boolean analyseHB(String source) { |
182 |
> |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
183 |
> |
HeartbeatHolder hbHolder = (HeartbeatHolder) _hosts.get(source); |
184 |
> |
Register reg = (Register) ((HashMap) hbHolder.getRegisterHash()).get(source); |
185 |
> |
|
186 |
> |
// get host's HB interval (seconds) |
187 |
> |
// this should always exist, thus we set to 0 |
188 |
> |
int hostHBinterval = 0; |
189 |
> |
try { |
190 |
> |
hostHBinterval = Integer.parseInt(cp.getProperty("Host."+source, "Host.TCPUpdateTime")); |
191 |
> |
} catch (PropertyNotFoundException e) { |
192 |
> |
hostHBinterval = 0; |
193 |
> |
_logger.write(this.toString(), Logger.WARNING, "TCPUpdateTime value unavailable using default of " + hostHBinterval + " seconds"); |
194 |
> |
} catch (NumberFormatException e) { |
195 |
> |
hostHBinterval = 0; |
196 |
> |
_logger.write(this.toString(), Logger.WARNING, "Erronous TCPUpdateTime value in configuration using default of " + hostHBinterval + " seconds"); |
197 |
> |
} |
198 |
> |
|
199 |
> |
// get host's last HB time (seconds) |
200 |
> |
long lastHeartbeat = hbHolder.getLastHeartbeat(); |
201 |
> |
// time since last heartbeat (seconds) |
202 |
> |
long timeSinceLastHB = (System.currentTimeMillis()/1000) - lastHeartbeat; |
203 |
> |
// time since (or until if negative) the expected heartbeat |
204 |
> |
long timeSinceExpectedHB = timeSinceLastHB - (long) hostHBinterval; |
205 |
> |
|
206 |
> |
// best do a check in case the expected heartbeat is in the future |
207 |
> |
if(timeSinceExpectedHB < 0) { |
208 |
> |
timeSinceExpectedHB = 0; |
209 |
> |
} |
210 |
> |
|
211 |
> |
// find out the threshold level we're at |
212 |
> |
int newThreshold = checkAttributeThreshold(timeSinceExpectedHB, reg); |
213 |
> |
|
214 |
> |
// process the alert |
215 |
> |
Heartbeat__Monitor.this.processAlert(newThreshold, "Heartbeat", reg, source, String.valueOf(timeSinceExpectedHB)); |
216 |
> |
|
217 |
> |
if(reg.getLastAlertLevel() == Alert.alertFINAL) { |
218 |
> |
return true; |
219 |
> |
} |
220 |
> |
return false; |
221 |
> |
} |
222 |
> |
} |
223 |
|
} |