3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
|
import uk.ac.ukc.iscream.util.*; |
6 |
+ |
import uk.ac.ukc.iscream.componentmanager.ConfigurationProxy; |
7 |
|
|
8 |
|
/** |
9 |
< |
* Register class |
9 |
> |
* The Register class holds theshold values, |
10 |
> |
* the last level of alert for each attribute for a monitor |
11 |
> |
* and the time of last alert sent for each alert level for |
12 |
> |
* each attribute in this register. |
13 |
|
* |
14 |
+ |
* This class is used by monitor classes so they can determine |
15 |
+ |
* how often they should send alerts when breaching alert levels. |
16 |
+ |
* It also stores (and keeps uptodate (via the configuration proxy) |
17 |
+ |
* the threshold values for the monitor. |
18 |
+ |
* |
19 |
|
* @author $Author$ |
20 |
|
* @version $Id$ |
21 |
|
*/ |
33 |
|
//---CONSTRUCTORS--- |
34 |
|
|
35 |
|
/** |
36 |
< |
* Construct a Register |
36 |
> |
* Construct a Register with the hostname and monitorName |
37 |
> |
* (for obtaining the threshold values), and the number of |
38 |
> |
* attributes we are keeping track of. |
39 |
|
* |
40 |
< |
* @param size the size of the register |
40 |
> |
* @param hostname the hostname this register is for |
41 |
> |
* @param monitorName the monitor this register is for |
42 |
> |
* @param numAttributes the number of attributes to track |
43 |
|
*/ |
44 |
< |
public Register(int size) { |
45 |
< |
_register = new int[size]; |
46 |
< |
for (int x = 0; x < _register.length; x++) { |
47 |
< |
_register[x] = 0; |
44 |
> |
public Register(String hostname, String monitorName, int numAttributes) { |
45 |
> |
_hostname = hostname; |
46 |
> |
_monitorName = monitorName; |
47 |
> |
_lastAlertLevels = new int[numAttributes]; |
48 |
> |
_lastThresholdLevels = new int[numAttributes]; |
49 |
> |
_times = new long[numAttributes][Alert.alertLevels.length]; |
50 |
> |
// initialise the arrays to 0 |
51 |
> |
for (int x = 0; x < _lastAlertLevels.length; x++) { |
52 |
> |
_lastAlertLevels[x] = 0; |
53 |
> |
_lastThresholdLevels[x] = 0; |
54 |
> |
for(int y = 0; y < Alert.alertLevels.length; y++) { |
55 |
> |
_times[x][y] = 0; |
56 |
> |
} |
57 |
|
} |
58 |
|
} |
59 |
|
|
63 |
|
|
64 |
|
//---ACCESSOR/MUTATOR METHODS--- |
65 |
|
|
66 |
< |
public int getRegister(int register) { |
67 |
< |
return _register[register]; |
66 |
> |
/** |
67 |
> |
* Gets the last alert level for the |
68 |
> |
* given attribute. |
69 |
> |
* |
70 |
> |
* @param attributeNum the attribute to get |
71 |
> |
* |
72 |
> |
* @return the last alert level |
73 |
> |
*/ |
74 |
> |
public int getLastAlertLevel(int attributeNum) { |
75 |
> |
return _lastAlertLevels[attributeNum]; |
76 |
|
} |
77 |
|
|
78 |
< |
public void setRegister(int register, int value) { |
79 |
< |
_register[register] = value; |
78 |
> |
/** |
79 |
> |
* Sets the last threshold level for the |
80 |
> |
* given attribute. |
81 |
> |
* |
82 |
> |
* @param attributeNum the attribute to set |
83 |
> |
* @param level the new last threshold level |
84 |
> |
*/ |
85 |
> |
public void setLastThresholdLevel(int attributeNum, int level) { |
86 |
> |
_lastThresholdLevels[attributeNum] = level; |
87 |
|
} |
88 |
+ |
|
89 |
+ |
/** |
90 |
+ |
* Gets the last threshold level for the |
91 |
+ |
* given attribute. |
92 |
+ |
* |
93 |
+ |
* @param attributeNum the attribute to get |
94 |
+ |
* |
95 |
+ |
* @return the last threshold level |
96 |
+ |
*/ |
97 |
+ |
public int getLastThresholdLevel(int attributeNum) { |
98 |
+ |
return _lastThresholdLevels[attributeNum]; |
99 |
+ |
} |
100 |
+ |
|
101 |
+ |
/** |
102 |
+ |
* Sets the last alert level for the |
103 |
+ |
* given attribute. |
104 |
+ |
* |
105 |
+ |
* @param attributeNum the attribute to set |
106 |
+ |
* @param level the new last alert level |
107 |
+ |
*/ |
108 |
+ |
public void setLastAlertLevel(int attributeNum, int level) { |
109 |
+ |
_lastAlertLevels[attributeNum] = level; |
110 |
+ |
} |
111 |
+ |
|
112 |
+ |
/** |
113 |
+ |
* Gets the time an alert was sent for the |
114 |
+ |
* given attribute at the given level. |
115 |
+ |
* |
116 |
+ |
* @param attributeNum the attribute to get |
117 |
+ |
* @param level the alert level to get |
118 |
+ |
* |
119 |
+ |
* @return the time last sent |
120 |
+ |
*/ |
121 |
+ |
public long getTimeLastSent(int attributeNum) { |
122 |
+ |
return _times[attributeNum][getLastAlertLevel(attributeNum)]; |
123 |
+ |
} |
124 |
+ |
|
125 |
+ |
/** |
126 |
+ |
* Gets the time an alert was sent for the |
127 |
+ |
* given attribute at the given level. |
128 |
+ |
* |
129 |
+ |
* @param attributeNum the attribute to get |
130 |
+ |
* @param level the alert level to get |
131 |
+ |
* @param value the new time |
132 |
+ |
*/ |
133 |
+ |
public void setTimeLastSent(int attributeNum, long value) { |
134 |
+ |
_times[attributeNum][getLastAlertLevel(attributeNum)] = value; |
135 |
+ |
} |
136 |
+ |
|
137 |
+ |
/** |
138 |
+ |
* Gets the threshold value for the given |
139 |
+ |
* level of alert. |
140 |
+ |
* |
141 |
+ |
* If there is no alert threshold for a |
142 |
+ |
* given level, this returns -1.0 |
143 |
+ |
* |
144 |
+ |
* @param level the alert level |
145 |
+ |
*/ |
146 |
+ |
public double getThreshold(int level) { |
147 |
+ |
double threshold = -1.0; |
148 |
+ |
String thresholdString = _cp.getProperty("Host." + _hostname, "Monitor." + _monitorName + ".threshold." + Alert.thresholdLevels[level]); |
149 |
+ |
if(thresholdString != null) { |
150 |
+ |
try { |
151 |
+ |
threshold = Double.parseDouble(thresholdString); |
152 |
+ |
} catch (NumberFormatException e) { |
153 |
+ |
// -1.0 means we don't use an alert level |
154 |
+ |
threshold = -1.0; |
155 |
+ |
} |
156 |
+ |
} |
157 |
+ |
return threshold; |
158 |
+ |
} |
159 |
+ |
|
160 |
+ |
/** |
161 |
+ |
* Gets the alert timeout value for the given |
162 |
+ |
* level of alert. This value is in millis, |
163 |
+ |
* and is converted from the value in the config, |
164 |
+ |
* which should be seconds. |
165 |
+ |
* |
166 |
+ |
* Note that this is dependant on the threshold value |
167 |
+ |
* given, the timeout is obatined from the config, then |
168 |
+ |
* divided by the threshold value, this allows alerts to |
169 |
+ |
* progress faster should a higher threshold value be passed |
170 |
+ |
* |
171 |
+ |
* If there is no alert timeout for a |
172 |
+ |
* given level, this returns -1.0 |
173 |
+ |
* |
174 |
+ |
* @param level the alert level |
175 |
+ |
* @param thresholdLevel the threshold leve we are on |
176 |
+ |
*/ |
177 |
+ |
public long getAlertTimeout(int level, int attributeNum) { |
178 |
+ |
long timeout = 0; |
179 |
+ |
String timeoutString = _cp.getProperty("Host." + _hostname, "Monitor." + _monitorName + ".alertTimeout." + Alert.alertLevels[level]); |
180 |
+ |
if(timeoutString != null) { |
181 |
+ |
try { |
182 |
+ |
int threshold = getLastThresholdLevel(attributeNum); |
183 |
+ |
if (threshold > 0) { |
184 |
+ |
timeout = (Long.parseLong(timeoutString) / threshold) * 1000; |
185 |
+ |
} |
186 |
+ |
} catch (NumberFormatException e) { |
187 |
+ |
// -1.0 means we don't use this value |
188 |
+ |
timeout = 0; |
189 |
+ |
} |
190 |
+ |
} |
191 |
+ |
return timeout; |
192 |
+ |
} |
193 |
+ |
|
194 |
+ |
/** |
195 |
+ |
* Either advances to the next alert level, or if |
196 |
+ |
* the maximum alert level has been reached, simply returns |
197 |
+ |
* that. |
198 |
+ |
* |
199 |
+ |
* @param attributeNum the attribute to get next alert for |
200 |
+ |
*/ |
201 |
+ |
public int getNextAlertLevel(int attributeNum) { |
202 |
+ |
if((getLastAlertLevel(attributeNum) + 1) > (Alert.alertLevels.length - 1)) { |
203 |
+ |
return getLastAlertLevel(attributeNum); |
204 |
+ |
} |
205 |
+ |
return getLastAlertLevel(attributeNum) + 1; |
206 |
+ |
} |
207 |
|
|
208 |
+ |
/** |
209 |
+ |
* Advances the alert level to the next one up. |
210 |
+ |
* |
211 |
+ |
* @param attributeNum the attribute to advance the alert for |
212 |
+ |
*/ |
213 |
+ |
public void escalateAlert(int attributeNum) { |
214 |
+ |
setLastAlertLevel(attributeNum, getNextAlertLevel(attributeNum)); |
215 |
+ |
} |
216 |
+ |
|
217 |
|
//---ATTRIBUTES--- |
218 |
|
|
219 |
< |
private int[] _register; |
219 |
> |
/** |
220 |
> |
* The host this register is for |
221 |
> |
*/ |
222 |
> |
private String _hostname; |
223 |
> |
|
224 |
> |
/** |
225 |
> |
* The monitor this register is for |
226 |
> |
*/ |
227 |
> |
private String _monitorName; |
228 |
> |
|
229 |
> |
/** |
230 |
> |
* An array of last alert levels for |
231 |
> |
* each attribute this register is looking |
232 |
> |
* after. |
233 |
> |
*/ |
234 |
> |
private int[] _lastAlertLevels; |
235 |
> |
|
236 |
> |
/** |
237 |
> |
* An array of last threshold levels for |
238 |
> |
* each attribute this register is looking |
239 |
> |
* after. |
240 |
> |
*/ |
241 |
> |
private int[] _lastThresholdLevels; |
242 |
> |
|
243 |
> |
/** |
244 |
> |
* An array of arrays containing |
245 |
> |
* time an alert of each level |
246 |
> |
* was last sent for each attribute. |
247 |
> |
*/ |
248 |
> |
private long[][] _times; |
249 |
> |
|
250 |
> |
/** |
251 |
> |
* A reference to the configuration proxy in use |
252 |
> |
*/ |
253 |
> |
private ConfigurationProxy _cp = ConfigurationProxy.getInstance(); |
254 |
|
|
255 |
|
//---STATIC ATTRIBUTES--- |
256 |
|
|