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.22
Committed: Sun Aug 1 10:40:41 2004 UTC (19 years, 9 months ago) by tdb
Branch: MAIN
CVS Tags: HEAD
Changes since 1.21: +3 -3 lines
Log Message:
Catch a lot of old URL's and update them. Also remove a couple of old files
that aren't used.

File Contents

# User Rev Content
1 tdb 1.18 /*
2     * i-scream central monitoring system
3 tdb 1.22 * http://www.i-scream.org
4 tdb 1.18 * 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 ajm 1.1 //---PACKAGE DECLARATION---
22 tdb 1.17 package uk.org.iscream.cms.server.client;
23 ajm 1.1
24     //---IMPORTS---
25 tdb 1.20 import uk.org.iscream.cms.util.*;
26 tdb 1.17 import uk.org.iscream.cms.server.componentmanager.*;
27 ajm 1.1
28     /**
29 ajm 1.2 * The Register class holds theshold values,
30     * the last level of alert for each attribute for a monitor
31 ajm 1.11 * and the time of last alert sent for each alert level.
32 ajm 1.1 *
33 ajm 1.2 * 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 tdb 1.18 * @author $Author: tdb $
39 tdb 1.22 * @version $Id: Register.java,v 1.21 2004/01/15 13:41:47 tdb Exp $
40 ajm 1.1 */
41     public class Register {
42    
43     //---FINAL ATTRIBUTES---
44    
45     /**
46     * The current CVS revision of this class
47     */
48 tdb 1.22 public static final String REVISION = "$Revision: 1.21 $";
49 ajm 1.1
50     //---STATIC METHODS---
51    
52     //---CONSTRUCTORS---
53    
54     /**
55 ajm 1.2 * Construct a Register with the hostname and monitorName
56 ajm 1.11 * (for obtaining the threshold values).
57 ajm 1.14 * This constructs a generic register for a specific monitor.
58 ajm 1.1 *
59 ajm 1.2 * @param hostname the hostname this register is for
60     * @param monitorName the monitor this register is for
61 ajm 1.14 */
62     public Register(String hostname, String monitorName) {
63     this(hostname, monitorName, null);
64     }
65 ajm 1.11
66 ajm 1.14 /**
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 ajm 1.1 */
76 ajm 1.14 public Register(String hostname, String monitorName, String attributeName) {
77 ajm 1.2 _hostname = hostname;
78     _monitorName = monitorName;
79 ajm 1.14 _attributeName = attributeName;
80 ajm 1.11 _lastAlertLevel = 0;
81     _lastThresholdLevel = 0;
82     _initialAlertTime= 0;
83     _lastAlertTimeout = 0;
84     _times = new long[Alert.alertLevels.length];
85 ajm 1.2 // initialise the arrays to 0
86 ajm 1.11 for(int x = 0; x < Alert.alertLevels.length; x++) {
87     _times[x] = 0;
88 ajm 1.1 }
89     }
90 ajm 1.14
91 ajm 1.1
92     //---PUBLIC METHODS---
93    
94 tdb 1.21 /**
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 ajm 1.1 //---PRIVATE METHODS---
135    
136 ajm 1.14 /**
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 ajm 1.1 //---ACCESSOR/MUTATOR METHODS---
161    
162 ajm 1.2 /**
163 ajm 1.11 * Gets the last alert level
164 ajm 1.2 *
165     * @return the last alert level
166     */
167 ajm 1.11 public int getLastAlertLevel() {
168     return _lastAlertLevel;
169 ajm 1.2 }
170    
171     /**
172 ajm 1.11 * Sets the last threshold level
173 ajm 1.2 *
174     * @param level the new last threshold level
175     */
176 ajm 1.11 public void setLastThresholdLevel(int level) {
177     _lastThresholdLevel = level;
178 ajm 1.2 }
179    
180 ajm 1.11 /**
181     * Gets the last threshold level
182 ajm 1.2 *
183     * @return the last threshold level
184     */
185 ajm 1.11 public int getLastThresholdLevel() {
186     return _lastThresholdLevel;
187 ajm 1.2 }
188    
189     /**
190 ajm 1.11 * Sets the last alert level
191 ajm 1.2 *
192 ajm 1.5 * Note that if this is setting to an OK
193     * level alert, it resets _maxLevelCount.
194     *
195 ajm 1.6 * 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 ajm 1.2 * @param level the new last alert level
200     */
201 ajm 1.11 public void setLastAlertLevel(int level) {
202     _lastAlertLevel = level;
203 ajm 1.5 if (level == Alert.alertOK) {
204     _maxLevelCount = 0;
205 tdb 1.13 // 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 ajm 1.6 }
209     if (level == Alert.alertOK + 1) {
210 ajm 1.11 _initialAlertTime = System.currentTimeMillis();
211 ajm 1.5 }
212 ajm 1.2 }
213    
214     /**
215 ajm 1.11 * Gets the time an alert was sent at the given level.
216 ajm 1.2 *
217     * @param level the alert level to get
218     *
219     * @return the time last sent
220     */
221 ajm 1.11 public long getTimeLastSent() {
222     return _times[getLastAlertLevel()];
223 ajm 1.2 }
224    
225     /**
226 ajm 1.11 * Gets the time an alert was sent at the given level.
227 ajm 1.2 *
228     * @param level the alert level to get
229     * @param value the new time
230     */
231 ajm 1.11 public void setTimeLastSent(long value) {
232     _times[getLastAlertLevel()] = value;
233 ajm 1.2 }
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 tdb 1.4 // -1.0 means we don't use an alert level
246 ajm 1.2 double threshold = -1.0;
247 tdb 1.4 try {
248 ajm 1.15 String thresholdString = "";
249     try {
250     thresholdString = getThresholdConfig(level, _attributeName);
251     } catch (PropertyNotFoundException e) {
252     thresholdString = getThresholdConfig(level, null);
253     }
254 tdb 1.4 threshold = Double.parseDouble(thresholdString);
255     } catch (PropertyNotFoundException e) {
256     threshold = -1.0;
257     } catch (NumberFormatException e) {
258     threshold = -1.0;
259 ajm 1.2 }
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 ajm 1.16 * 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 ajm 1.2 * 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 ajm 1.11 public long getAlertTimeout(int level) {
283 tdb 1.4 // 0 means we don't use this value
284 ajm 1.2 long timeout = 0;
285 tdb 1.4 try {
286 ajm 1.16 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 ajm 1.11 int threshold = getLastThresholdLevel();
295 tdb 1.4 if (threshold > 0) {
296     timeout = (Long.parseLong(timeoutString) / threshold) * 1000;
297 ajm 1.2 }
298 tdb 1.4 } catch (PropertyNotFoundException e) {
299     timeout = 0;
300     } catch (NumberFormatException e) {
301     timeout = 0;
302 ajm 1.2 }
303     return timeout;
304 ajm 1.1 }
305    
306 ajm 1.2 /**
307     * Either advances to the next alert level, or if
308     * the maximum alert level has been reached, simply returns
309     * that.
310     *
311 ajm 1.5 * 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 ajm 1.2 */
316 ajm 1.11 public int getNextAlertLevel() {
317     if((getLastAlertLevel() + 1) > (Alert.alertLevels.length - 2)) {
318     return getLastAlertLevel();
319 ajm 1.2 }
320 ajm 1.11 return getLastAlertLevel() + 1;
321 ajm 1.2 }
322    
323     /**
324 ajm 1.3 * Gets the timeout value of the last alert
325     * sent
326     *
327     * @return the last timeout value
328     */
329 ajm 1.11 public long getLastAlertTimeout() {
330     return _lastAlertTimeout;
331 ajm 1.3 }
332    
333     /**
334     * Sets the timeout value of the last alert
335     * sent
336     *
337     * @param timeout the new value
338     */
339 ajm 1.11 public void setLastAlertTimeout(long timeout) {
340     _lastAlertTimeout = timeout;
341 ajm 1.3 }
342    
343     /**
344 ajm 1.6 * Returns the time that the first alert was sent
345     * for an attribute that has passed a threshold value
346     *
347     */
348 ajm 1.11 public long getInitialAlertTime() {
349     return _initialAlertTime;
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     }