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.server.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.18 2002/05/18 18:16:00 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.18 $"; |
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 |
//---PRIVATE METHODS--- |
95 |
|
96 |
/** |
97 |
* Obtains a threshold value from the configuration for a given level. |
98 |
* The attribute name specifies which property to get. |
99 |
* eg, attributeName = idle it will look for |
100 |
* Monitor.<monitor name>.idle.threshold.<alert level> |
101 |
* eg, attributeName = null |
102 |
* Monitor.<monitor name>.threshold.<alert level> |
103 |
* |
104 |
* Note that if its null, this will get the threshold for the monitor |
105 |
* as a whole, not the specific attribute. |
106 |
* |
107 |
* @param level the alert level to get the attribute for |
108 |
* @param attributeName the attribute to get the threshold for |
109 |
* |
110 |
* @return the threshold obtained |
111 |
*/ |
112 |
private String getThresholdConfig(int level, String attributeName) throws PropertyNotFoundException { |
113 |
String temp = ""; |
114 |
if (attributeName != null) { |
115 |
temp = "." + attributeName; |
116 |
} |
117 |
return _cp.getProperty("Host." + _hostname, "Monitor." + _monitorName + temp + ".threshold." + Alert.thresholdLevels[level]); |
118 |
} |
119 |
|
120 |
//---ACCESSOR/MUTATOR METHODS--- |
121 |
|
122 |
/** |
123 |
* Gets the last alert level |
124 |
* |
125 |
* @return the last alert level |
126 |
*/ |
127 |
public int getLastAlertLevel() { |
128 |
return _lastAlertLevel; |
129 |
} |
130 |
|
131 |
/** |
132 |
* Sets the last threshold level |
133 |
* |
134 |
* @param level the new last threshold level |
135 |
*/ |
136 |
public void setLastThresholdLevel(int level) { |
137 |
_lastThresholdLevel = level; |
138 |
} |
139 |
|
140 |
/** |
141 |
* Gets the last threshold level |
142 |
* |
143 |
* @return the last threshold level |
144 |
*/ |
145 |
public int getLastThresholdLevel() { |
146 |
return _lastThresholdLevel; |
147 |
} |
148 |
|
149 |
/** |
150 |
* Sets the last alert level |
151 |
* |
152 |
* Note that if this is setting to an OK |
153 |
* level alert, it resets _maxLevelCount. |
154 |
* |
155 |
* This also sets the "initialAlertTime" |
156 |
* if the next alert after OK is set. |
157 |
* And resets it to 0 if it IS an OK. |
158 |
* |
159 |
* @param level the new last alert level |
160 |
*/ |
161 |
public void setLastAlertLevel(int level) { |
162 |
_lastAlertLevel = level; |
163 |
if (level == Alert.alertOK) { |
164 |
_maxLevelCount = 0; |
165 |
// we won't do this, so OK's still have the initialAlertTime |
166 |
// of the original alert they're OK'ing |
167 |
//_initialAlertTime = 0; |
168 |
} |
169 |
if (level == Alert.alertOK + 1) { |
170 |
_initialAlertTime = System.currentTimeMillis(); |
171 |
} |
172 |
} |
173 |
|
174 |
/** |
175 |
* Gets the time an alert was sent at the given level. |
176 |
* |
177 |
* @param level the alert level to get |
178 |
* |
179 |
* @return the time last sent |
180 |
*/ |
181 |
public long getTimeLastSent() { |
182 |
return _times[getLastAlertLevel()]; |
183 |
} |
184 |
|
185 |
/** |
186 |
* Gets the time an alert was sent at the given level. |
187 |
* |
188 |
* @param level the alert level to get |
189 |
* @param value the new time |
190 |
*/ |
191 |
public void setTimeLastSent(long value) { |
192 |
_times[getLastAlertLevel()] = value; |
193 |
} |
194 |
|
195 |
/** |
196 |
* Gets the threshold value for the given |
197 |
* level of alert. |
198 |
* |
199 |
* If there is no alert threshold for a |
200 |
* given level, this returns -1.0 |
201 |
* |
202 |
* @param level the alert level |
203 |
*/ |
204 |
public double getThreshold(int level) { |
205 |
// -1.0 means we don't use an alert level |
206 |
double threshold = -1.0; |
207 |
try { |
208 |
String thresholdString = ""; |
209 |
try { |
210 |
thresholdString = getThresholdConfig(level, _attributeName); |
211 |
} catch (PropertyNotFoundException e) { |
212 |
thresholdString = getThresholdConfig(level, null); |
213 |
} |
214 |
threshold = Double.parseDouble(thresholdString); |
215 |
} catch (PropertyNotFoundException e) { |
216 |
threshold = -1.0; |
217 |
} catch (NumberFormatException e) { |
218 |
threshold = -1.0; |
219 |
} |
220 |
return threshold; |
221 |
} |
222 |
|
223 |
/** |
224 |
* Gets the alert timeout value for the given |
225 |
* level of alert. This value is in millis, |
226 |
* and is converted from the value in the config, |
227 |
* which should be seconds. |
228 |
* |
229 |
* Note that if the alert timeout for the current monitor |
230 |
* is not configured, it will try to obtain the default |
231 |
* timeout for all Monitor's. If there is no alert timeout |
232 |
* for either the monitor or a default setting this returns 0. |
233 |
* |
234 |
* Note that this is dependant on the threshold value |
235 |
* given, the timeout is obatined from the config, then |
236 |
* divided by the threshold value, this allows alerts to |
237 |
* progress faster should a higher threshold value be passed |
238 |
* |
239 |
* @param level the alert level |
240 |
* @param thresholdLevel the threshold leve we are on |
241 |
*/ |
242 |
public long getAlertTimeout(int level) { |
243 |
// 0 means we don't use this value |
244 |
long timeout = 0; |
245 |
try { |
246 |
String timeoutString; |
247 |
try { |
248 |
timeoutString = _cp.getProperty("Host." + _hostname, "Monitor." + _monitorName + ".alertTimeout." + Alert.alertLevels[level]); |
249 |
} catch (PropertyNotFoundException e) { |
250 |
// if there is no timeout for the monitor |
251 |
// check for a default |
252 |
timeoutString = _cp.getProperty("Host." + _hostname, "Monitor.alertTimeout." + Alert.alertLevels[level]); |
253 |
} |
254 |
int threshold = getLastThresholdLevel(); |
255 |
if (threshold > 0) { |
256 |
timeout = (Long.parseLong(timeoutString) / threshold) * 1000; |
257 |
} |
258 |
} catch (PropertyNotFoundException e) { |
259 |
timeout = 0; |
260 |
} catch (NumberFormatException e) { |
261 |
timeout = 0; |
262 |
} |
263 |
return timeout; |
264 |
} |
265 |
|
266 |
/** |
267 |
* Either advances to the next alert level, or if |
268 |
* the maximum alert level has been reached, simply returns |
269 |
* that. |
270 |
* |
271 |
* Note this method will NEVER reach the last alert level |
272 |
* in the list, this is assumed to be FINAL, and special |
273 |
* logic is in place to handle that. |
274 |
* |
275 |
*/ |
276 |
public int getNextAlertLevel() { |
277 |
if((getLastAlertLevel() + 1) > (Alert.alertLevels.length - 2)) { |
278 |
return getLastAlertLevel(); |
279 |
} |
280 |
return getLastAlertLevel() + 1; |
281 |
} |
282 |
|
283 |
/** |
284 |
* Gets the timeout value of the last alert |
285 |
* sent |
286 |
* |
287 |
* @return the last timeout value |
288 |
*/ |
289 |
public long getLastAlertTimeout() { |
290 |
return _lastAlertTimeout; |
291 |
} |
292 |
|
293 |
/** |
294 |
* Sets the timeout value of the last alert |
295 |
* sent |
296 |
* |
297 |
* @param timeout the new value |
298 |
*/ |
299 |
public void setLastAlertTimeout(long timeout) { |
300 |
_lastAlertTimeout = timeout; |
301 |
} |
302 |
|
303 |
/** |
304 |
* Returns the time that the first alert was sent |
305 |
* for an attribute that has passed a threshold value |
306 |
* |
307 |
*/ |
308 |
public long getInitialAlertTime() { |
309 |
return _initialAlertTime; |
310 |
} |
311 |
|
312 |
/** |
313 |
* Advances the alert level to the next one up. |
314 |
* |
315 |
* This keeps track of the number of the number |
316 |
* of times the highest NON-FINAL alert level |
317 |
* has been reached. Note this isn't a specific |
318 |
* level, just the highest, so you could configure |
319 |
* more levels and not affect this. |
320 |
* |
321 |
* If the count exceeds the number of times |
322 |
* set in the configuration, it escalates to |
323 |
* a FINAL alert if we're using FINAL's. |
324 |
* |
325 |
* It determines if to use FINAL's from the config |
326 |
* entry reachFINALcount, when the count exceeds |
327 |
* this value, it escalates to a FINAL. If that attribute |
328 |
* is mis-read or is not configured. It will NEVER reach |
329 |
* a FINAL. |
330 |
* |
331 |
*/ |
332 |
public void escalateAlert() { |
333 |
// don't escalate if we're already on the last alert |
334 |
if(getLastAlertLevel() != Alert.alertLevels.length -1) { |
335 |
setLastAlertLevel(getNextAlertLevel()); |
336 |
} |
337 |
try { |
338 |
// note if we fail to get this value, we won't process the res of this |
339 |
int reachFINALcount = Integer.parseInt(_cp.getProperty("Host." + _hostname, "Monitor." + _monitorName + ".reachFINALcount")); |
340 |
if (getLastAlertLevel() == Alert.alertLevels.length - 2) { |
341 |
_maxLevelCount++; |
342 |
if(_maxLevelCount > reachFINALcount) { |
343 |
setLastAlertLevel(Alert.alertFINAL); |
344 |
} |
345 |
} |
346 |
} catch (PropertyNotFoundException e) { |
347 |
// we NEVER reach FINAL in this case |
348 |
} catch (NumberFormatException e) { |
349 |
// we NEVER reach FINAL in this case |
350 |
} |
351 |
} |
352 |
|
353 |
//---ATTRIBUTES--- |
354 |
|
355 |
/** |
356 |
* The host this register is for |
357 |
*/ |
358 |
private String _hostname; |
359 |
|
360 |
/** |
361 |
* The monitor this register is for |
362 |
*/ |
363 |
private String _monitorName; |
364 |
|
365 |
/** |
366 |
* The attribute name, as obtained from |
367 |
* the configuration. |
368 |
* eg, idle or /var |
369 |
*/ |
370 |
private String _attributeName; |
371 |
|
372 |
/** |
373 |
* An array of last alert levels for |
374 |
* each attribute this register is looking |
375 |
* after. |
376 |
*/ |
377 |
private int _lastAlertLevel; |
378 |
|
379 |
/** |
380 |
* An array of last threshold levels for |
381 |
* each attribute this register is looking |
382 |
* after. |
383 |
*/ |
384 |
private int _lastThresholdLevel; |
385 |
|
386 |
/** |
387 |
* An array of last alert timeout levels for |
388 |
* each attribute this register is looking |
389 |
* after. |
390 |
*/ |
391 |
private long _lastAlertTimeout; |
392 |
|
393 |
/** |
394 |
* An array of arrays containing |
395 |
* time an alert of each level |
396 |
* was last sent for each attribute. |
397 |
*/ |
398 |
private long[] _times; |
399 |
|
400 |
/** |
401 |
* Initial times that an alert was first |
402 |
* raised. |
403 |
*/ |
404 |
private long _initialAlertTime; |
405 |
|
406 |
/** |
407 |
* A reference to the configuration proxy in use |
408 |
*/ |
409 |
private ConfigurationProxy _cp = ConfigurationProxy.getInstance(); |
410 |
|
411 |
/** |
412 |
* The number of times the maximum level alert |
413 |
* has occured IN A ROW, this is escalated by |
414 |
* the escalate(int) method, and reset by the |
415 |
* setLastAlertLevel(int, int) method |
416 |
*/ |
417 |
private int _maxLevelCount = 0; |
418 |
|
419 |
//---STATIC ATTRIBUTES--- |
420 |
|
421 |
} |