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.18
Committed: Sat May 18 18:16:00 2002 UTC (22 years ago) by tdb
Branch: MAIN
Changes since 1.17: +22 -3 lines
Log Message:
i-scream is now licensed under the GPL. I've added the GPL headers to every
source file, and put a full copy of the license in the appropriate places.
I think I've covered everything. This is going to be a mad commit ;)

File Contents

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