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/Register.java
Revision: 1.21
Committed: Thu Jan 15 13:41:47 2004 UTC (20 years, 4 months ago) by tdb
Branch: MAIN
Changes since 1.20: +42 -43 lines
Log Message:
Assuming I can still program in Java, these changes allow monitoring to
be disabled at a per-host level or a per-host-per-monitor level.

File Contents

# Content
1 /*
2 * i-scream central monitoring system
3 * http://www.i-scream.org.uk
4 * Copyright (C) 2000-2002 i-scream
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 //---PACKAGE DECLARATION---
22 package uk.org.iscream.cms.server.client;
23
24 //---IMPORTS---
25 import uk.org.iscream.cms.util.*;
26 import uk.org.iscream.cms.server.componentmanager.*;
27
28 /**
29 * The Register class holds theshold values,
30 * the last level of alert for each attribute for a monitor
31 * and the time of last alert sent for each alert level.
32 *
33 * This class is used by monitor classes so they can determine
34 * how often they should send alerts when breaching alert levels.
35 * It also stores (and keeps uptodate (via the configuration proxy)
36 * the threshold values for the monitor.
37 *
38 * @author $Author: tdb $
39 * @version $Id: Register.java,v 1.20 2003/02/05 16:43:45 tdb Exp $
40 */
41 public class Register {
42
43 //---FINAL ATTRIBUTES---
44
45 /**
46 * The current CVS revision of this class
47 */
48 public static final String REVISION = "$Revision: 1.20 $";
49
50 //---STATIC METHODS---
51
52 //---CONSTRUCTORS---
53
54 /**
55 * Construct a Register with the hostname and monitorName
56 * (for obtaining the threshold values).
57 * This constructs a generic register for a specific monitor.
58 *
59 * @param hostname the hostname this register is for
60 * @param monitorName the monitor this register is for
61 */
62 public Register(String hostname, String monitorName) {
63 this(hostname, monitorName, null);
64 }
65
66 /**
67 * Construct a Register with the hostname and monitorName
68 * (for obtaining the threshold values).
69 * This constructs a register for a specific attribute check
70 * by a monitor.
71 *
72 * @param hostname the hostname this register is for
73 * @param monitorName the monitor this register is for
74 * @param attributeName the specific attribute this register is for
75 */
76 public Register(String hostname, String monitorName, String attributeName) {
77 _hostname = hostname;
78 _monitorName = monitorName;
79 _attributeName = attributeName;
80 _lastAlertLevel = 0;
81 _lastThresholdLevel = 0;
82 _initialAlertTime= 0;
83 _lastAlertTimeout = 0;
84 _times = new long[Alert.alertLevels.length];
85 // initialise the arrays to 0
86 for(int x = 0; x < Alert.alertLevels.length; x++) {
87 _times[x] = 0;
88 }
89 }
90
91
92 //---PUBLIC METHODS---
93
94 /**
95 * Advances the alert level to the next one up.
96 *
97 * This keeps track of the number of the number
98 * of times the highest NON-FINAL alert level
99 * has been reached. Note this isn't a specific
100 * level, just the highest, so you could configure
101 * more levels and not affect this.
102 *
103 * If the count exceeds the number of times
104 * set in the configuration, it escalates to
105 * a FINAL alert if we're using FINAL's.
106 *
107 * It determines if to use FINAL's from the config
108 * entry reachFINALcount, when the count exceeds
109 * this value, it escalates to a FINAL. If that attribute
110 * is mis-read or is not configured. It will NEVER reach
111 * a FINAL.
112 */
113 public void escalateAlert() {
114 // don't escalate if we're already on the last alert
115 if(getLastAlertLevel() != Alert.alertLevels.length -1) {
116 setLastAlertLevel(getNextAlertLevel());
117 }
118 try {
119 // note if we fail to get this value, we won't process the res of this
120 int reachFINALcount = Integer.parseInt(_cp.getProperty("Host." + _hostname, "Monitor." + _monitorName + ".reachFINALcount"));
121 if (getLastAlertLevel() == Alert.alertLevels.length - 2) {
122 _maxLevelCount++;
123 if(_maxLevelCount > reachFINALcount) {
124 setLastAlertLevel(Alert.alertFINAL);
125 }
126 }
127 } catch (PropertyNotFoundException e) {
128 // we NEVER reach FINAL in this case
129 } catch (NumberFormatException e) {
130 // we NEVER reach FINAL in this case
131 }
132 }
133
134 //---PRIVATE METHODS---
135
136 /**
137 * Obtains a threshold value from the configuration for a given level.
138 * The attribute name specifies which property to get.
139 * eg, attributeName = idle it will look for
140 * Monitor.<monitor name>.idle.threshold.<alert level>
141 * eg, attributeName = null
142 * Monitor.<monitor name>.threshold.<alert level>
143 *
144 * Note that if its null, this will get the threshold for the monitor
145 * as a whole, not the specific attribute.
146 *
147 * @param level the alert level to get the attribute for
148 * @param attributeName the attribute to get the threshold for
149 *
150 * @return the threshold obtained
151 */
152 private String getThresholdConfig(int level, String attributeName) throws PropertyNotFoundException {
153 String temp = "";
154 if (attributeName != null) {
155 temp = "." + attributeName;
156 }
157 return _cp.getProperty("Host." + _hostname, "Monitor." + _monitorName + temp + ".threshold." + Alert.thresholdLevels[level]);
158 }
159
160 //---ACCESSOR/MUTATOR METHODS---
161
162 /**
163 * Gets the last alert level
164 *
165 * @return the last alert level
166 */
167 public int getLastAlertLevel() {
168 return _lastAlertLevel;
169 }
170
171 /**
172 * Sets the last threshold level
173 *
174 * @param level the new last threshold level
175 */
176 public void setLastThresholdLevel(int level) {
177 _lastThresholdLevel = level;
178 }
179
180 /**
181 * Gets the last threshold level
182 *
183 * @return the last threshold level
184 */
185 public int getLastThresholdLevel() {
186 return _lastThresholdLevel;
187 }
188
189 /**
190 * Sets the last alert level
191 *
192 * Note that if this is setting to an OK
193 * level alert, it resets _maxLevelCount.
194 *
195 * This also sets the "initialAlertTime"
196 * if the next alert after OK is set.
197 * And resets it to 0 if it IS an OK.
198 *
199 * @param level the new last alert level
200 */
201 public void setLastAlertLevel(int level) {
202 _lastAlertLevel = level;
203 if (level == Alert.alertOK) {
204 _maxLevelCount = 0;
205 // we won't do this, so OK's still have the initialAlertTime
206 // of the original alert they're OK'ing
207 //_initialAlertTime = 0;
208 }
209 if (level == Alert.alertOK + 1) {
210 _initialAlertTime = System.currentTimeMillis();
211 }
212 }
213
214 /**
215 * Gets the time an alert was sent at the given level.
216 *
217 * @param level the alert level to get
218 *
219 * @return the time last sent
220 */
221 public long getTimeLastSent() {
222 return _times[getLastAlertLevel()];
223 }
224
225 /**
226 * Gets the time an alert was sent at the given level.
227 *
228 * @param level the alert level to get
229 * @param value the new time
230 */
231 public void setTimeLastSent(long value) {
232 _times[getLastAlertLevel()] = value;
233 }
234
235 /**
236 * Gets the threshold value for the given
237 * level of alert.
238 *
239 * If there is no alert threshold for a
240 * given level, this returns -1.0
241 *
242 * @param level the alert level
243 */
244 public double getThreshold(int level) {
245 // -1.0 means we don't use an alert level
246 double threshold = -1.0;
247 try {
248 String thresholdString = "";
249 try {
250 thresholdString = getThresholdConfig(level, _attributeName);
251 } catch (PropertyNotFoundException e) {
252 thresholdString = getThresholdConfig(level, null);
253 }
254 threshold = Double.parseDouble(thresholdString);
255 } catch (PropertyNotFoundException e) {
256 threshold = -1.0;
257 } catch (NumberFormatException e) {
258 threshold = -1.0;
259 }
260 return threshold;
261 }
262
263 /**
264 * Gets the alert timeout value for the given
265 * level of alert. This value is in millis,
266 * and is converted from the value in the config,
267 * which should be seconds.
268 *
269 * Note that if the alert timeout for the current monitor
270 * is not configured, it will try to obtain the default
271 * timeout for all Monitor's. If there is no alert timeout
272 * for either the monitor or a default setting this returns 0.
273 *
274 * Note that this is dependant on the threshold value
275 * given, the timeout is obatined from the config, then
276 * divided by the threshold value, this allows alerts to
277 * progress faster should a higher threshold value be passed
278 *
279 * @param level the alert level
280 * @param thresholdLevel the threshold leve we are on
281 */
282 public long getAlertTimeout(int level) {
283 // 0 means we don't use this value
284 long timeout = 0;
285 try {
286 String timeoutString;
287 try {
288 timeoutString = _cp.getProperty("Host." + _hostname, "Monitor." + _monitorName + ".alertTimeout." + Alert.alertLevels[level]);
289 } catch (PropertyNotFoundException e) {
290 // if there is no timeout for the monitor
291 // check for a default
292 timeoutString = _cp.getProperty("Host." + _hostname, "Monitor.alertTimeout." + Alert.alertLevels[level]);
293 }
294 int threshold = getLastThresholdLevel();
295 if (threshold > 0) {
296 timeout = (Long.parseLong(timeoutString) / threshold) * 1000;
297 }
298 } catch (PropertyNotFoundException e) {
299 timeout = 0;
300 } catch (NumberFormatException e) {
301 timeout = 0;
302 }
303 return timeout;
304 }
305
306 /**
307 * Either advances to the next alert level, or if
308 * the maximum alert level has been reached, simply returns
309 * that.
310 *
311 * Note this method will NEVER reach the last alert level
312 * in the list, this is assumed to be FINAL, and special
313 * logic is in place to handle that.
314 *
315 */
316 public int getNextAlertLevel() {
317 if((getLastAlertLevel() + 1) > (Alert.alertLevels.length - 2)) {
318 return getLastAlertLevel();
319 }
320 return getLastAlertLevel() + 1;
321 }
322
323 /**
324 * Gets the timeout value of the last alert
325 * sent
326 *
327 * @return the last timeout value
328 */
329 public long getLastAlertTimeout() {
330 return _lastAlertTimeout;
331 }
332
333 /**
334 * Sets the timeout value of the last alert
335 * sent
336 *
337 * @param timeout the new value
338 */
339 public void setLastAlertTimeout(long timeout) {
340 _lastAlertTimeout = timeout;
341 }
342
343 /**
344 * Returns the time that the first alert was sent
345 * for an attribute that has passed a threshold value
346 *
347 */
348 public long getInitialAlertTime() {
349 return _initialAlertTime;
350 }
351
352 //---ATTRIBUTES---
353
354 /**
355 * The host this register is for
356 */
357 private String _hostname;
358
359 /**
360 * The monitor this register is for
361 */
362 private String _monitorName;
363
364 /**
365 * The attribute name, as obtained from
366 * the configuration.
367 * eg, idle or /var
368 */
369 private String _attributeName;
370
371 /**
372 * An array of last alert levels for
373 * each attribute this register is looking
374 * after.
375 */
376 private int _lastAlertLevel;
377
378 /**
379 * An array of last threshold levels for
380 * each attribute this register is looking
381 * after.
382 */
383 private int _lastThresholdLevel;
384
385 /**
386 * An array of last alert timeout levels for
387 * each attribute this register is looking
388 * after.
389 */
390 private long _lastAlertTimeout;
391
392 /**
393 * An array of arrays containing
394 * time an alert of each level
395 * was last sent for each attribute.
396 */
397 private long[] _times;
398
399 /**
400 * Initial times that an alert was first
401 * raised.
402 */
403 private long _initialAlertTime;
404
405 /**
406 * A reference to the configuration proxy in use
407 */
408 private ConfigurationProxy _cp = ConfigurationProxy.getInstance();
409
410 /**
411 * The number of times the maximum level alert
412 * has occured IN A ROW, this is escalated by
413 * the escalate(int) method, and reset by the
414 * setLastAlertLevel(int, int) method
415 */
416 private int _maxLevelCount = 0;
417
418 //---STATIC ATTRIBUTES---
419
420 }