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.7
Committed: Tue Mar 6 22:35:01 2001 UTC (23 years, 3 months ago) by tdb
Branch: MAIN
Changes since 1.6: +40 -112 lines
Log Message:
Changed the existing monitor's to use the skeleton class.

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.6 2001/03/06 20:26:01 tdb1 Exp $
17 */
18 public class Heartbeat__Monitor extends MonitorSkeleton implements Runnable {
19
20 //---FINAL ATTRIBUTES---
21
22 /**
23 * The current CVS revision of this class
24 */
25 public final String REVISION = "$Revision: 1.6 $";
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 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 = _hosts.keySet().iterator();
58 while(i.hasNext()) {
59 // get host
60 String source = (String) i.next();
61 // check it
62 boolean remove = analyseHB(source);
63 if(remove) {
64 i.remove();
65 }
66 }
67
68 // wait a while
69 try {Thread.sleep(checkPeriod * 1000);} catch (InterruptedException e) {}
70 }
71 }
72
73 public void analysePacket(XMLPacket packet) {
74 if (packet.getParam("packet.attributes.type").equals("heartbeat")) {
75 String source = packet.getParam("packet.attributes.machine_name");
76 if (!_hosts.containsKey(source)) {
77 HashMap registerHash = new HashMap();
78 registerHash.put(source, new Register(source, _name, 1));
79 _hosts.put(source, new HeartbeatHolder(registerHash));
80 }
81 HeartbeatHolder lastHeartbeat = (HeartbeatHolder) _hosts.get(source);
82 lastHeartbeat.setLastHeartbeat(System.currentTimeMillis()/1000);
83 }
84 }
85
86 /**
87 * Overrides the {@link java.lang.Object#toString() Object.toString()}
88 * method to provide clean logging (every class should have this).
89 *
90 * This uses the uk.ac.ukc.iscream.util.NameFormat class
91 * to format the toString()
92 *
93 * @return the name of this class and its CVS revision
94 */
95 public String toString() {
96 return FormatName.getName(
97 _name,
98 getClass().getName(),
99 REVISION);
100 }
101
102 /**
103 * return the String representation of what the monitor does
104 */
105 public String getDescription(){
106 return DESC;
107 }
108
109 //---PRIVATE METHODS---
110
111 private boolean analyseHB(String source) {
112 ConfigurationProxy cp = ConfigurationProxy.getInstance();
113 HeartbeatHolder hbHolder = (HeartbeatHolder) _hosts.get(source);
114 Register reg = (Register) ((HashMap) hbHolder.getRegisterHash()).get(source);
115
116 // get host's HB interval (seconds)
117 // this should always exist, thus we set to 0
118 int hostHBinterval = 0;
119 try {
120 hostHBinterval = Integer.parseInt(cp.getProperty("Host."+source, "Host.TCPUpdateTime"));
121 } catch (PropertyNotFoundException e) {
122 hostHBinterval = 0;
123 _logger.write(toString(), Logger.WARNING, "TCPUpdateTime value unavailable using default of " + hostHBinterval + " seconds");
124 } catch (NumberFormatException e) {
125 hostHBinterval = 0;
126 _logger.write(toString(), Logger.WARNING, "Erronous TCPUpdateTime value in configuration using default of " + hostHBinterval + " seconds");
127 }
128
129 // get host's last HB time (seconds)
130 long lastHeartbeat = hbHolder.getLastHeartbeat();
131 // time since last heartbeat (seconds)
132 long timeSinceLastHB = (System.currentTimeMillis()/1000) - lastHeartbeat;
133
134 // find out the threshold level we're at
135 int newThreshold = checkAttributeThreshold(timeSinceLastHB, reg);
136
137 // process the alert
138 processAlert(newThreshold, 0, "Heartbeat", reg, source, String.valueOf(timeSinceLastHB));
139
140 if(reg.getLastAlertLevel(0) == Alert.alertFINAL) {
141 return true;
142 }
143 return false;
144 }
145
146 private int checkAttributeThreshold(long timeSinceLastHB, Register reg) {
147 for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) {
148 if (reg.getThreshold(thresholdLevel) != -1.0) {
149 if (((long) reg.getThreshold(thresholdLevel)) < timeSinceLastHB) {
150 return thresholdLevel;
151 }
152 }
153 }
154 return Alert.thresholdNORMAL;
155 }
156
157 //---ACCESSOR/MUTATOR METHODS---
158
159 //---ATTRIBUTES---
160
161 /**
162 * This is the friendly identifier of the
163 * component this class is running in.
164 * eg, a Filter may be called "filter1",
165 * If this class does not have an owning
166 * component, a name from the configuration
167 * can be placed here. This name could also
168 * be changed to null for utility classes.
169 */
170 private String _name = "Heartbeat";
171
172 /**
173 * A reference to the configuration proxy in use
174 */
175 private ConfigurationProxy _cp = ConfigurationProxy.getInstance();
176
177 private HashMap _hosts = new HashMap();
178
179 //---STATIC ATTRIBUTES---
180
181 //---INNER CLASSES---
182
183 private class HeartbeatHolder {
184
185 public HeartbeatHolder(HashMap registerHash) {
186 _registerHash = registerHash;
187 }
188
189 public void setLastHeartbeat(long lastHeartbeat) {
190 _lastHeartbeat = lastHeartbeat;
191 }
192
193 public long getLastHeartbeat() {
194 return _lastHeartbeat;
195 }
196
197 public HashMap getRegisterHash() {
198 return _registerHash;
199 }
200
201 private long _lastHeartbeat;
202 private HashMap _registerHash;
203 }
204
205 }