1 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
tdb |
1.13 |
package uk.org.iscream.client.monitors; |
3 |
tdb |
1.1 |
|
4 |
|
|
//---IMPORTS--- |
5 |
|
|
import java.util.HashMap; |
6 |
|
|
import java.util.Iterator; |
7 |
tdb |
1.13 |
import uk.org.iscream.client.*; |
8 |
|
|
import uk.org.iscream.core.*; |
9 |
|
|
import uk.org.iscream.util.*; |
10 |
|
|
import uk.org.iscream.componentmanager.*; |
11 |
tdb |
1.1 |
|
12 |
|
|
/** |
13 |
|
|
* This Monitor watches heartbeats |
14 |
|
|
* |
15 |
ajm |
1.14 |
* @author $Author: tdb1 $ |
16 |
|
|
* @version $Id: Heartbeat__Monitor.java,v 1.13 2001/03/14 23:25:29 tdb1 Exp $ |
17 |
tdb |
1.1 |
*/ |
18 |
ajm |
1.14 |
public class Heartbeat__Monitor extends MonitorSkeleton { |
19 |
tdb |
1.1 |
|
20 |
|
|
//---FINAL ATTRIBUTES--- |
21 |
|
|
|
22 |
|
|
/** |
23 |
|
|
* The current CVS revision of this class |
24 |
|
|
*/ |
25 |
ajm |
1.14 |
public final String REVISION = "$Revision: 1.13 $"; |
26 |
tdb |
1.1 |
|
27 |
|
|
public final String DESC = "Monitors Heartbeats."; |
28 |
|
|
|
29 |
tdb |
1.3 |
public final int DEFAULT_CHECK_PERIOD = 60; |
30 |
|
|
|
31 |
tdb |
1.1 |
//---STATIC METHODS--- |
32 |
|
|
|
33 |
|
|
//---CONSTRUCTORS--- |
34 |
|
|
|
35 |
tdb |
1.2 |
public Heartbeat__Monitor() { |
36 |
tdb |
1.3 |
new Thread(this).start(); |
37 |
ajm |
1.14 |
new HeartbeatWorker().start(); |
38 |
tdb |
1.2 |
} |
39 |
|
|
|
40 |
tdb |
1.1 |
//---PUBLIC METHODS--- |
41 |
|
|
|
42 |
ajm |
1.14 |
public void analysePacket(XMLPacket packet) { |
43 |
|
|
String source = packet.getParam("packet.attributes.machine_name"); |
44 |
|
|
if (!_hosts.containsKey(source)) { |
45 |
tdb |
1.9 |
synchronized(this) { |
46 |
ajm |
1.14 |
HashMap registerHash = new HashMap(); |
47 |
|
|
registerHash.put(source, new Register(source, _name)); |
48 |
|
|
_hosts.put(source, new HeartbeatHolder(registerHash)); |
49 |
tdb |
1.1 |
} |
50 |
|
|
} |
51 |
ajm |
1.14 |
HeartbeatHolder lastHeartbeat = (HeartbeatHolder) _hosts.get(source); |
52 |
|
|
lastHeartbeat.setLastHeartbeat(System.currentTimeMillis()/1000); |
53 |
tdb |
1.1 |
} |
54 |
|
|
|
55 |
|
|
/** |
56 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
57 |
|
|
* method to provide clean logging (every class should have this). |
58 |
|
|
* |
59 |
tdb |
1.13 |
* This uses the uk.org.iscream.util.NameFormat class |
60 |
tdb |
1.1 |
* to format the toString() |
61 |
|
|
* |
62 |
|
|
* @return the name of this class and its CVS revision |
63 |
|
|
*/ |
64 |
|
|
public String toString() { |
65 |
|
|
return FormatName.getName( |
66 |
|
|
_name, |
67 |
|
|
getClass().getName(), |
68 |
|
|
REVISION); |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
/** |
72 |
|
|
* return the String representation of what the monitor does |
73 |
|
|
*/ |
74 |
|
|
public String getDescription(){ |
75 |
|
|
return DESC; |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
//---PRIVATE METHODS--- |
79 |
tdb |
1.7 |
|
80 |
|
|
private boolean analyseHB(String source) { |
81 |
|
|
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
82 |
|
|
HeartbeatHolder hbHolder = (HeartbeatHolder) _hosts.get(source); |
83 |
|
|
Register reg = (Register) ((HashMap) hbHolder.getRegisterHash()).get(source); |
84 |
|
|
|
85 |
|
|
// get host's HB interval (seconds) |
86 |
|
|
// this should always exist, thus we set to 0 |
87 |
|
|
int hostHBinterval = 0; |
88 |
|
|
try { |
89 |
|
|
hostHBinterval = Integer.parseInt(cp.getProperty("Host."+source, "Host.TCPUpdateTime")); |
90 |
|
|
} catch (PropertyNotFoundException e) { |
91 |
|
|
hostHBinterval = 0; |
92 |
|
|
_logger.write(toString(), Logger.WARNING, "TCPUpdateTime value unavailable using default of " + hostHBinterval + " seconds"); |
93 |
|
|
} catch (NumberFormatException e) { |
94 |
|
|
hostHBinterval = 0; |
95 |
|
|
_logger.write(toString(), Logger.WARNING, "Erronous TCPUpdateTime value in configuration using default of " + hostHBinterval + " seconds"); |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
// get host's last HB time (seconds) |
99 |
|
|
long lastHeartbeat = hbHolder.getLastHeartbeat(); |
100 |
|
|
// time since last heartbeat (seconds) |
101 |
|
|
long timeSinceLastHB = (System.currentTimeMillis()/1000) - lastHeartbeat; |
102 |
tdb |
1.8 |
// time since (or until if negative) the expected heartbeat |
103 |
tdb |
1.11 |
long timeSinceExpectedHB = timeSinceLastHB - (long) hostHBinterval; |
104 |
tdb |
1.8 |
|
105 |
|
|
// best do a check in case the expected heartbeat is in the future |
106 |
|
|
if(timeSinceExpectedHB < 0) { |
107 |
|
|
timeSinceExpectedHB = 0; |
108 |
|
|
} |
109 |
tdb |
1.7 |
|
110 |
|
|
// find out the threshold level we're at |
111 |
tdb |
1.8 |
int newThreshold = checkAttributeThreshold(timeSinceExpectedHB, reg); |
112 |
tdb |
1.7 |
|
113 |
|
|
// process the alert |
114 |
ajm |
1.12 |
processAlert(newThreshold, "Heartbeat", reg, source, String.valueOf(timeSinceExpectedHB)); |
115 |
tdb |
1.7 |
|
116 |
ajm |
1.12 |
if(reg.getLastAlertLevel() == Alert.alertFINAL) { |
117 |
tdb |
1.7 |
return true; |
118 |
|
|
} |
119 |
|
|
return false; |
120 |
|
|
} |
121 |
tdb |
1.1 |
|
122 |
tdb |
1.2 |
private int checkAttributeThreshold(long timeSinceLastHB, Register reg) { |
123 |
tdb |
1.1 |
for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) { |
124 |
|
|
if (reg.getThreshold(thresholdLevel) != -1.0) { |
125 |
tdb |
1.2 |
if (((long) reg.getThreshold(thresholdLevel)) < timeSinceLastHB) { |
126 |
tdb |
1.1 |
return thresholdLevel; |
127 |
|
|
} |
128 |
|
|
} |
129 |
|
|
} |
130 |
tdb |
1.7 |
return Alert.thresholdNORMAL; |
131 |
tdb |
1.1 |
} |
132 |
|
|
|
133 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
134 |
|
|
|
135 |
ajm |
1.14 |
protected Queue getQueue() { |
136 |
|
|
return MonitorManager.getInstance().getHeartbeatQueue(); |
137 |
|
|
} |
138 |
|
|
|
139 |
tdb |
1.1 |
//---ATTRIBUTES--- |
140 |
|
|
|
141 |
|
|
/** |
142 |
|
|
* This is the friendly identifier of the |
143 |
|
|
* component this class is running in. |
144 |
|
|
* eg, a Filter may be called "filter1", |
145 |
|
|
* If this class does not have an owning |
146 |
|
|
* component, a name from the configuration |
147 |
|
|
* can be placed here. This name could also |
148 |
|
|
* be changed to null for utility classes. |
149 |
|
|
*/ |
150 |
|
|
private String _name = "Heartbeat"; |
151 |
|
|
|
152 |
|
|
/** |
153 |
|
|
* A reference to the configuration proxy in use |
154 |
|
|
*/ |
155 |
|
|
private ConfigurationProxy _cp = ConfigurationProxy.getInstance(); |
156 |
|
|
|
157 |
tdb |
1.6 |
private HashMap _hosts = new HashMap(); |
158 |
tdb |
1.1 |
|
159 |
|
|
//---STATIC ATTRIBUTES--- |
160 |
|
|
|
161 |
|
|
//---INNER CLASSES--- |
162 |
|
|
|
163 |
|
|
private class HeartbeatHolder { |
164 |
|
|
|
165 |
tdb |
1.6 |
public HeartbeatHolder(HashMap registerHash) { |
166 |
|
|
_registerHash = registerHash; |
167 |
|
|
} |
168 |
|
|
|
169 |
tdb |
1.2 |
public void setLastHeartbeat(long lastHeartbeat) { |
170 |
tdb |
1.1 |
_lastHeartbeat = lastHeartbeat; |
171 |
|
|
} |
172 |
|
|
|
173 |
tdb |
1.2 |
public long getLastHeartbeat() { |
174 |
tdb |
1.1 |
return _lastHeartbeat; |
175 |
|
|
} |
176 |
|
|
|
177 |
tdb |
1.6 |
public HashMap getRegisterHash() { |
178 |
|
|
return _registerHash; |
179 |
|
|
} |
180 |
|
|
|
181 |
tdb |
1.2 |
private long _lastHeartbeat; |
182 |
tdb |
1.6 |
private HashMap _registerHash; |
183 |
ajm |
1.14 |
} |
184 |
|
|
|
185 |
|
|
private class HeartbeatWorker extends Thread { |
186 |
|
|
|
187 |
|
|
public void run() { |
188 |
|
|
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
189 |
|
|
while(true) { |
190 |
|
|
// this cycle period of this monitor's checks |
191 |
|
|
int checkPeriod = 0; |
192 |
|
|
try { |
193 |
|
|
checkPeriod = Integer.parseInt(cp.getProperty(_name, "Monitor.Heartbeat.checkPeriod")); |
194 |
|
|
} catch (PropertyNotFoundException e) { |
195 |
|
|
checkPeriod = DEFAULT_CHECK_PERIOD; |
196 |
|
|
_logger.write(this.toString(), Logger.WARNING, "Monitor.Heartbeat.checkPeriod value unavailable using default of " + checkPeriod + " seconds"); |
197 |
|
|
} catch (NumberFormatException e) { |
198 |
|
|
checkPeriod = DEFAULT_CHECK_PERIOD; |
199 |
|
|
_logger.write(this.toString(), Logger.WARNING, "Erronous Monitor.Heartbeat.checkPeriod value in configuration using default of " + checkPeriod + " seconds"); |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
synchronized(this) { |
203 |
|
|
// perform the checks (use HB hash, although they *should* be the same) |
204 |
|
|
Iterator i = _hosts.keySet().iterator(); |
205 |
|
|
while(i.hasNext()) { |
206 |
|
|
// get host |
207 |
|
|
String source = (String) i.next(); |
208 |
|
|
// check it |
209 |
|
|
boolean remove = analyseHB(source); |
210 |
|
|
if(remove) { |
211 |
|
|
i.remove(); |
212 |
|
|
} |
213 |
|
|
} |
214 |
|
|
} |
215 |
|
|
|
216 |
|
|
// wait a while |
217 |
|
|
try {Thread.sleep(checkPeriod * 1000);} catch (InterruptedException e) {} |
218 |
|
|
} |
219 |
|
|
} |
220 |
|
|
} |
221 |
tdb |
1.1 |
|
222 |
|
|
} |