ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/client/monitors/Heartbeat__Monitor.java
Revision: 1.2
Committed: Mon Mar 5 15:47:48 2001 UTC (23 years, 2 months ago) by tdb
Branch: MAIN
Changes since 1.1: +26 -13 lines
Log Message:
Fixed to work.
Had a bug with using int's instead of long's.
Didn't actually start the thread.
Needed a bit or error catching.

File Contents

# Content
1 //---PACKAGE DECLARATION---
2 package uk.ac.ukc.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.*;
11
12 /**
13 * This Monitor watches heartbeats
14 *
15 * @author $Author: tdb1 $
16 * @version $Id: Heartbeat__Monitor.java,v 1.1 2001/03/05 13:30:34 tdb1 Exp $
17 */
18 public class Heartbeat__Monitor extends Thread implements PluginMonitor {
19
20 //---FINAL ATTRIBUTES---
21
22 /**
23 * The current CVS revision of this class
24 */
25 public final String REVISION = "$Revision: 1.1 $";
26
27 public final String DESC = "Monitors Heartbeats.";
28
29 //---STATIC METHODS---
30
31 //---CONSTRUCTORS---
32
33 public Heartbeat__Monitor() {
34 this.start();
35 }
36
37 //---PUBLIC METHODS---
38
39 public void run() {
40 ConfigurationProxy cp = ConfigurationProxy.getInstance();
41 while(true) {
42 // this cycle period could be done better, maybe ?
43 int checkPeriod = Integer.parseInt(cp.getProperty(_name, "Monitor.Heartbeat.checkPeriod"));
44
45 // perform the checks (use HB hash, although they *should* be the same)
46 Iterator i = _hostsHB.keySet().iterator();
47 while(i.hasNext()) {
48 // get host
49 String source = (String) i.next();
50 // check it
51 analyseHB(source);
52 }
53
54 // wait a while
55 try {Thread.sleep(checkPeriod * 1000);} catch (InterruptedException e) {}
56 }
57 }
58
59 // only use attribute num 0 :)
60 public void analyseHB(String source) {
61 ConfigurationProxy cp = ConfigurationProxy.getInstance();
62 Register reg = (Register) _hostsReg.get(source);
63
64 // get host's HB interval (seconds)
65 // this should always exist
66 String HBint = cp.getProperty("Host."+source, "TCPUpdateTime");
67 int hostHBinterval = 0;
68 if(HBint != null) {
69 hostHBinterval = Integer.parseInt(cp.getProperty("Host."+source, "TCPUpdateTime"));
70 }
71 else {
72 hostHBinterval = 60;
73 }
74
75 // get host's last HB time (seconds)
76 long lastHeartbeat = ((HeartbeatHolder) _hostsHB.get(source)).getLastHeartbeat();
77 // time since last heartbeat (seconds)
78 long timeSinceLastHB = (System.currentTimeMillis()/1000) - lastHeartbeat;
79
80 // find out the threshold level we're at
81 int result = checkAttributeThreshold(timeSinceLastHB, reg);
82
83 // decide what threshold level we're on, if we've changed, record that
84 if (result != reg.getLastThresholdLevel(0)) {
85 reg.setLastThresholdLevel(0, result);
86 }
87
88 // as long as this isn't a normal level
89 if(reg.getLastThresholdLevel(0) != Alert.thresholdNORMAL) {
90 // if the time since the last alert is more than the time for
91 // its timeout, fire an alert, escalate the alert
92 long timeout = reg.getLastAlertTimeout(0);
93 if ((timeout > 0) && (reg.getTimeLastSent(0) > 0)) {
94 if((System.currentTimeMillis() - reg.getTimeLastSent(0)) > timeout) {
95 int lastAlert = reg.getLastAlertLevel(0);
96 reg.escalateAlert(0);
97 reg.setTimeLastSent(0, System.currentTimeMillis());
98 reg.setLastAlertTimeout(0, reg.getAlertTimeout(reg.getLastAlertLevel(0), 0));
99 // -- SEND
100 fireAlert(source, timeSinceLastHB, reg, lastAlert);
101 }
102 // if we don't have a timeout configured...we got STRAIGHT to the next level
103 } else {
104 int lastAlert = reg.getLastAlertLevel(0);
105 reg.escalateAlert(0);
106 reg.setTimeLastSent(0, System.currentTimeMillis());
107 reg.setLastAlertTimeout(0, reg.getAlertTimeout(reg.getLastAlertLevel(0), 0));
108 // -- SEND
109 fireAlert(source, timeSinceLastHB, reg, lastAlert);
110 }
111
112 // we must be on ok, check the timeout value for this
113 } else {
114 // if we were on an OK alert before, then we don't do anything
115 // but if we weren't we only set OK, once the timeout of the last
116 // alert has occourd
117 if (reg.getLastAlertLevel(0) != Alert.alertOK) {
118 long timeout = reg.getLastAlertTimeout(0);
119 if ((timeout > 0) && (reg.getTimeLastSent(0) > 0)) {
120 if ((System.currentTimeMillis() - reg.getTimeLastSent(0)) > timeout) {
121 int lastAlert = reg.getLastAlertLevel(0);
122 reg.setLastAlertLevel(0, Alert.alertOK);
123 reg.setTimeLastSent(0, System.currentTimeMillis());
124 reg.setLastAlertTimeout(0, timeout);
125 // -- SEND
126 fireAlert(source, timeSinceLastHB, reg, lastAlert);
127 }
128 }
129 }
130 }
131 }
132
133 public void analysePacket(XMLPacket packet) {
134 if (packet.getParam("packet.attributes.type").equals("heartbeat")) {
135 String source = packet.getParam("packet.attributes.machine_name");
136 if (!_hostsHB.containsKey(source)) {
137 _hostsReg.put(source, new Register(source, _name, 1));
138 _hostsHB.put(source, new HeartbeatHolder());
139 }
140 HeartbeatHolder lastHeartbeat = (HeartbeatHolder) _hostsHB.get(source);
141 lastHeartbeat.setLastHeartbeat(System.currentTimeMillis()/1000);
142 }
143 }
144
145 /**
146 * Overrides the {@link java.lang.Object#toString() Object.toString()}
147 * method to provide clean logging (every class should have this).
148 *
149 * This uses the uk.ac.ukc.iscream.util.NameFormat class
150 * to format the toString()
151 *
152 * @return the name of this class and its CVS revision
153 */
154 public String toString() {
155 return FormatName.getName(
156 _name,
157 getClass().getName(),
158 REVISION);
159 }
160
161 /**
162 * return the String representation of what the monitor does
163 */
164 public String getDescription(){
165 return DESC;
166 }
167
168 //---PRIVATE METHODS---
169
170 private int checkAttributeThreshold(long timeSinceLastHB, Register reg) {
171 for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) {
172 if (reg.getThreshold(thresholdLevel) != -1.0) {
173 if (((long) reg.getThreshold(thresholdLevel)) < timeSinceLastHB) {
174 return thresholdLevel;
175 }
176 }
177 }
178 return 0;
179 }
180
181 private void fireAlert(String source, long timeSinceLastHB, Register reg, int lastAlert) {
182 int alertLevel = reg.getLastAlertLevel(0);
183 int thresholdLevel = reg.getLastThresholdLevel(0);
184 String currentValue = String.valueOf(timeSinceLastHB);
185 String attributeName = "Heartbeat";
186 String thresholdValue = String.valueOf(reg.getThreshold(thresholdLevel));
187 String time = Long.toString(reg.getAlertTimeout(reg.getLastAlertLevel(0), 0) / 1000);
188 if (thresholdLevel == Alert.thresholdNORMAL) {
189 thresholdValue = "-";
190 }
191 if (alertLevel == Alert.alertOK) {
192 time = "0";
193 }
194 Alert alert = new Alert(alertLevel, lastAlert, thresholdLevel, source, thresholdValue, currentValue, attributeName, time);
195 _alerterQueue.add(alert);
196 _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");
197 }
198
199 //---ACCESSOR/MUTATOR METHODS---
200
201 //---ATTRIBUTES---
202
203 /**
204 * This is the friendly identifier of the
205 * component this class is running in.
206 * eg, a Filter may be called "filter1",
207 * If this class does not have an owning
208 * component, a name from the configuration
209 * can be placed here. This name could also
210 * be changed to null for utility classes.
211 */
212 private String _name = "Heartbeat";
213
214 /**
215 * This holds a reference to the
216 * system logger that is being used.
217 */
218 private Logger _logger = ReferenceManager.getInstance().getLogger();
219
220 private Queue _alerterQueue = ClientMain._alerterQueue;
221
222 /**
223 * A reference to the configuration proxy in use
224 */
225 private ConfigurationProxy _cp = ConfigurationProxy.getInstance();
226
227 private HashMap _hostsHB = new HashMap();
228 private HashMap _hostsReg = new HashMap();
229
230 //---STATIC ATTRIBUTES---
231
232 //---INNER CLASSES---
233
234 private class HeartbeatHolder {
235
236 public void setLastHeartbeat(long lastHeartbeat) {
237 _lastHeartbeat = lastHeartbeat;
238 }
239
240 public long getLastHeartbeat() {
241 return _lastHeartbeat;
242 }
243
244 private long _lastHeartbeat;
245 }
246
247 }