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.15
Committed: Thu Mar 22 00:31:24 2001 UTC (23 years, 2 months ago) by ajm
Branch: MAIN
Changes since 1.14: +9 -4 lines
Log Message:
Now checks both attribute and default if the config isn't there for the attribute.

File Contents

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