15 |
|
* @author $Author$ |
16 |
|
* @version $Id$ |
17 |
|
*/ |
18 |
< |
public class Heartbeat__Monitor extends Thread implements PluginMonitor { |
18 |
> |
public class Heartbeat__Monitor implements PluginMonitor, Runnable { |
19 |
|
|
20 |
|
//---FINAL ATTRIBUTES--- |
21 |
|
|
26 |
|
|
27 |
|
public final String DESC = "Monitors Heartbeats."; |
28 |
|
|
29 |
+ |
public final int DEFAULT_CHECK_PERIOD = 60; |
30 |
+ |
|
31 |
|
//---STATIC METHODS--- |
32 |
|
|
33 |
|
//---CONSTRUCTORS--- |
34 |
|
|
35 |
+ |
public Heartbeat__Monitor() { |
36 |
+ |
new Thread(this).start(); |
37 |
+ |
} |
38 |
+ |
|
39 |
|
//---PUBLIC METHODS--- |
40 |
|
|
41 |
|
public void run() { |
42 |
|
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
43 |
|
while(true) { |
44 |
< |
// this cycle period could be done better, maybe ? |
45 |
< |
int checkPeriod = Integer.parseInt(cp.getProperty(_name, "Monitor.Heartbeat.checkPeriod")); |
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 |
|
|
56 |
|
// perform the checks (use HB hash, although they *should* be the same) |
57 |
< |
Iterator i = _hostsHB.keySet().iterator(); |
57 |
> |
Iterator i = _hosts.keySet().iterator(); |
58 |
|
while(i.hasNext()) { |
59 |
|
// get host |
60 |
|
String source = (String) i.next(); |
61 |
|
// check it |
62 |
< |
analyseHB(source); |
62 |
> |
boolean remove = analyseHB(source); |
63 |
> |
if(remove) { |
64 |
> |
i.remove(); |
65 |
> |
} |
66 |
|
} |
67 |
|
|
68 |
|
// wait a while |
71 |
|
} |
72 |
|
|
73 |
|
// only use attribute num 0 :) |
74 |
< |
public void analyseHB(String source) { |
74 |
> |
public boolean analyseHB(String source) { |
75 |
|
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
76 |
< |
Register reg = (Register) _hostsReg.get(source); |
76 |
> |
HeartbeatHolder hbHolder = (HeartbeatHolder) _hosts.get(source); |
77 |
> |
Register reg = (Register) ((HashMap) hbHolder.getRegisterHash()).get(source); |
78 |
|
|
79 |
|
// get host's HB interval (seconds) |
80 |
< |
int hostHBinterval = Integer.parseInt(cp.getProperty("Host."+source, "TCPUpdateTime")); |
80 |
> |
// this should always exist, thus we set to 0 |
81 |
> |
int hostHBinterval = 0; |
82 |
> |
try { |
83 |
> |
hostHBinterval = Integer.parseInt(cp.getProperty("Host."+source, "Host.TCPUpdateTime")); |
84 |
> |
} catch (PropertyNotFoundException e) { |
85 |
> |
hostHBinterval = 0; |
86 |
> |
_logger.write(toString(), Logger.WARNING, "TCPUpdateTime value unavailable using default of " + hostHBinterval + " seconds"); |
87 |
> |
} catch (NumberFormatException e) { |
88 |
> |
hostHBinterval = 0; |
89 |
> |
_logger.write(toString(), Logger.WARNING, "Erronous TCPUpdateTime value in configuration using default of " + hostHBinterval + " seconds"); |
90 |
> |
} |
91 |
> |
|
92 |
|
// get host's last HB time (seconds) |
93 |
< |
int lastHeartbeat = ((HeartbeatHolder) _hostsHB.get(source)).getLastHeartbeat(); |
93 |
> |
long lastHeartbeat = hbHolder.getLastHeartbeat(); |
94 |
|
// time since last heartbeat (seconds) |
95 |
< |
int timeSinceLastHB = ((int) (System.currentTimeMillis()/1000)) - lastHeartbeat; |
95 |
> |
long timeSinceLastHB = (System.currentTimeMillis()/1000) - lastHeartbeat; |
96 |
|
|
97 |
|
// find out the threshold level we're at |
98 |
|
int result = checkAttributeThreshold(timeSinceLastHB, reg); |
145 |
|
} |
146 |
|
} |
147 |
|
} |
148 |
+ |
if(reg.getLastAlertLevel(0) == Alert.alertFINAL) { |
149 |
+ |
return true; |
150 |
+ |
} |
151 |
+ |
return false; |
152 |
|
} |
153 |
|
|
154 |
|
public void analysePacket(XMLPacket packet) { |
155 |
|
if (packet.getParam("packet.attributes.type").equals("heartbeat")) { |
156 |
|
String source = packet.getParam("packet.attributes.machine_name"); |
157 |
< |
if (!_hostsHB.containsKey(source)) { |
158 |
< |
_hostsReg.put(source, new Register(source, _name, 1)); |
159 |
< |
_hostsHB.put(source, new HeartbeatHolder()); |
157 |
> |
if (!_hosts.containsKey(source)) { |
158 |
> |
HashMap registerHash = new HashMap(); |
159 |
> |
registerHash.put(source, new Register(source, _name, 1)); |
160 |
> |
_hosts.put(source, new HeartbeatHolder(registerHash)); |
161 |
|
} |
162 |
< |
HeartbeatHolder lastHeartbeat = (HeartbeatHolder) _hostsReg.get(source); |
163 |
< |
lastHeartbeat.setLastHeartbeat((int)System.currentTimeMillis()/1000); |
162 |
> |
HeartbeatHolder lastHeartbeat = (HeartbeatHolder) _hosts.get(source); |
163 |
> |
lastHeartbeat.setLastHeartbeat(System.currentTimeMillis()/1000); |
164 |
|
} |
165 |
|
} |
166 |
|
|
189 |
|
|
190 |
|
//---PRIVATE METHODS--- |
191 |
|
|
192 |
< |
private int checkAttributeThreshold(int timeSinceLastHB, Register reg) { |
192 |
> |
private int checkAttributeThreshold(long timeSinceLastHB, Register reg) { |
193 |
|
for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) { |
194 |
|
if (reg.getThreshold(thresholdLevel) != -1.0) { |
195 |
< |
if (reg.getThreshold(thresholdLevel) < timeSinceLastHB) { |
195 |
> |
if (((long) reg.getThreshold(thresholdLevel)) < timeSinceLastHB) { |
196 |
|
return thresholdLevel; |
197 |
|
} |
198 |
|
} |
200 |
|
return 0; |
201 |
|
} |
202 |
|
|
203 |
< |
private void fireAlert(String source, int timeSinceLastHB, Register reg, int lastAlert) { |
203 |
> |
private void fireAlert(String source, long timeSinceLastHB, Register reg, int lastAlert) { |
204 |
|
int alertLevel = reg.getLastAlertLevel(0); |
205 |
|
int thresholdLevel = reg.getLastThresholdLevel(0); |
206 |
|
String currentValue = String.valueOf(timeSinceLastHB); |
207 |
|
String attributeName = "Heartbeat"; |
208 |
|
String thresholdValue = String.valueOf(reg.getThreshold(thresholdLevel)); |
209 |
< |
String time = Long.toString(reg.getAlertTimeout(reg.getLastAlertLevel(0), 0) / 1000); |
209 |
> |
String timeout = Long.toString(reg.getAlertTimeout(reg.getLastAlertLevel(0), 0) / 1000); |
210 |
|
if (thresholdLevel == Alert.thresholdNORMAL) { |
211 |
|
thresholdValue = "-"; |
212 |
|
} |
213 |
|
if (alertLevel == Alert.alertOK) { |
214 |
< |
time = "0"; |
214 |
> |
timeout = "0"; |
215 |
|
} |
216 |
< |
Alert alert = new Alert(alertLevel, lastAlert, thresholdLevel, source, thresholdValue, currentValue, attributeName, time); |
216 |
> |
Alert alert = new Alert(alertLevel, lastAlert, thresholdLevel, source, thresholdValue, currentValue, attributeName, timeout, reg.getInitialAlertTime(0)); |
217 |
|
_alerterQueue.add(alert); |
218 |
< |
_logger.write(toString(), Logger.DEBUG, "Fired alert for source:" + source + " at alert level:" + Alert.alertLevels[alertLevel] + " on:" + attributeName + " for threshold level:" + Alert.thresholdLevels[thresholdLevel] + " at:" + currentValue + " exceeding threshold of:" +thresholdValue + " next alert sent in:" + time + "secs"); |
218 |
> |
_logger.write(toString(), Logger.DEBUG, "Fired alert for source:" + source + " at alert level:" + Alert.alertLevels[alertLevel] + " on:" + attributeName + " for threshold level:" + Alert.thresholdLevels[thresholdLevel] + " at:" + currentValue + " exceeding threshold of:" +thresholdValue + " next alert sent in:" + timeout + "secs"); |
219 |
|
} |
220 |
|
|
221 |
|
//---ACCESSOR/MUTATOR METHODS--- |
246 |
|
*/ |
247 |
|
private ConfigurationProxy _cp = ConfigurationProxy.getInstance(); |
248 |
|
|
249 |
< |
private HashMap _hostsHB = new HashMap(); |
215 |
< |
private HashMap _hostsReg = new HashMap(); |
249 |
> |
private HashMap _hosts = new HashMap(); |
250 |
|
|
251 |
|
//---STATIC ATTRIBUTES--- |
252 |
|
|
254 |
|
|
255 |
|
private class HeartbeatHolder { |
256 |
|
|
257 |
< |
public void setLastHeartbeat(int lastHeartbeat) { |
257 |
> |
public HeartbeatHolder(HashMap registerHash) { |
258 |
> |
_registerHash = registerHash; |
259 |
> |
} |
260 |
> |
|
261 |
> |
public void setLastHeartbeat(long lastHeartbeat) { |
262 |
|
_lastHeartbeat = lastHeartbeat; |
263 |
|
} |
264 |
|
|
265 |
< |
public int getLastHeartbeat() { |
265 |
> |
public long getLastHeartbeat() { |
266 |
|
return _lastHeartbeat; |
267 |
|
} |
268 |
|
|
269 |
< |
private int _lastHeartbeat; |
269 |
> |
public HashMap getRegisterHash() { |
270 |
> |
return _registerHash; |
271 |
> |
} |
272 |
> |
|
273 |
> |
private long _lastHeartbeat; |
274 |
> |
private HashMap _registerHash; |
275 |
|
} |
276 |
|
|
277 |
|
} |