1 |
tdb |
1.10 |
/* |
2 |
|
|
* i-scream central monitoring system |
3 |
tdb |
1.11 |
* http://www.i-scream.org.uk |
4 |
tdb |
1.10 |
* 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 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
22 |
tdb |
1.8 |
package uk.org.iscream.cms.server.client.monitors; |
23 |
tdb |
1.1 |
|
24 |
|
|
//---IMPORTS--- |
25 |
|
|
import java.util.HashMap; |
26 |
tdb |
1.2 |
import java.text.NumberFormat; |
27 |
tdb |
1.8 |
import uk.org.iscream.cms.server.client.*; |
28 |
|
|
import uk.org.iscream.cms.server.core.*; |
29 |
tdb |
1.13 |
import uk.org.iscream.cms.util.*; |
30 |
tdb |
1.8 |
import uk.org.iscream.cms.server.componentmanager.*; |
31 |
tdb |
1.1 |
|
32 |
|
|
/** |
33 |
|
|
* This Monitor watches the Memory for all machines |
34 |
|
|
* |
35 |
tdb |
1.10 |
* @author $Author: tdb $ |
36 |
tdb |
1.16 |
* @version $Id: Memory__Monitor.java,v 1.15 2003/03/10 08:42:57 tdb Exp $ |
37 |
tdb |
1.1 |
*/ |
38 |
|
|
public class Memory__Monitor extends MonitorSkeleton { |
39 |
|
|
|
40 |
|
|
//---FINAL ATTRIBUTES--- |
41 |
|
|
|
42 |
|
|
/** |
43 |
|
|
* The current CVS revision of this class |
44 |
|
|
*/ |
45 |
tdb |
1.16 |
public final String REVISION = "$Revision: 1.15 $"; |
46 |
tdb |
1.1 |
|
47 |
tdb |
1.7 |
/** |
48 |
|
|
* A description of this monitor |
49 |
|
|
*/ |
50 |
tdb |
1.1 |
public final String DESC = "Monitors Memory."; |
51 |
|
|
|
52 |
|
|
//---STATIC METHODS--- |
53 |
|
|
|
54 |
|
|
//---CONSTRUCTORS--- |
55 |
|
|
|
56 |
|
|
//---PUBLIC METHODS--- |
57 |
tdb |
1.7 |
|
58 |
|
|
/** |
59 |
|
|
* Analyse a packet of data, and generate an alert if |
60 |
|
|
* necessary. |
61 |
|
|
* |
62 |
|
|
* @param packet the XMLPacket to analyse |
63 |
|
|
*/ |
64 |
tdb |
1.1 |
public void analysePacket(XMLPacket packet) { |
65 |
ajm |
1.6 |
String source = packet.getParam("packet.attributes.machine_name"); |
66 |
|
|
if (!_hosts.containsKey(source)) { |
67 |
|
|
_hosts.put(source, new Register(source, _name)); |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
Register reg = (Register) _hosts.get(source); |
71 |
|
|
|
72 |
|
|
// get the packet data |
73 |
tdb |
1.12 |
double memoryTotal, memoryFree, memoryCache; |
74 |
ajm |
1.6 |
try { |
75 |
|
|
String total = packet.getParam("packet.memory.total"); |
76 |
|
|
String free = packet.getParam("packet.memory.free"); |
77 |
|
|
if(total==null || free==null) { |
78 |
tdb |
1.12 |
throw new NumberFormatException("Memory data invalid or not supplied"); |
79 |
tdb |
1.1 |
} |
80 |
ajm |
1.6 |
memoryTotal = Double.parseDouble(total); |
81 |
|
|
memoryFree = Double.parseDouble(free); |
82 |
tdb |
1.12 |
// get hold of the cache data.. default to 0 |
83 |
|
|
memoryCache = 0.0; |
84 |
|
|
String cache = packet.getParam("packet.memory.cache"); |
85 |
|
|
if(cache != null) { |
86 |
|
|
memoryCache = Double.parseDouble(cache); |
87 |
|
|
} |
88 |
ajm |
1.6 |
} catch (NumberFormatException e) { |
89 |
|
|
_logger.write(this.toString(), Logger.WARNING, "Received packet from "+source+" with bad memory information: "+e); |
90 |
|
|
// don't try to continue and process |
91 |
|
|
return; |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
boolean useValue = false; |
95 |
|
|
try { |
96 |
|
|
String option = _cp.getProperty("Host." + source, "Monitor." + _name + ".thresholdMeasure"); |
97 |
|
|
if (option.equals("VALUE")) { |
98 |
|
|
useValue = true; |
99 |
tdb |
1.12 |
} |
100 |
ajm |
1.6 |
} catch (PropertyNotFoundException e) { |
101 |
|
|
// we default to percentage |
102 |
|
|
} |
103 |
tdb |
1.12 |
|
104 |
|
|
boolean useCache = false; |
105 |
|
|
try { |
106 |
|
|
int useCacheAsFree = Integer.parseInt(_cp.getProperty("Host." + source, "Monitor." + _name + ".useCacheAsFree")); |
107 |
|
|
useCache = (useCacheAsFree == 1); |
108 |
|
|
} catch (PropertyNotFoundException e) { |
109 |
|
|
// we default to false |
110 |
|
|
} catch (NumberFormatException e) { |
111 |
|
|
// we default to false |
112 |
|
|
} |
113 |
|
|
|
114 |
tdb |
1.14 |
// this bit determines if the memory check is a % check |
115 |
|
|
// or a byte check |
116 |
tdb |
1.9 |
String type; |
117 |
|
|
double curValue; |
118 |
|
|
int newThreshold; |
119 |
ajm |
1.6 |
if(useValue) { |
120 |
tdb |
1.14 |
// bytes of memory available |
121 |
tdb |
1.12 |
if(useCache) { |
122 |
|
|
// NOTE: we take cache as being "probably free" for this check |
123 |
|
|
curValue = memoryFree + memoryCache; |
124 |
|
|
} else { |
125 |
|
|
curValue = memoryFree; |
126 |
|
|
} |
127 |
tdb |
1.9 |
// negate check |
128 |
|
|
newThreshold = checkAttributeThreshold(curValue, reg, true); |
129 |
tdb |
1.14 |
type = "bytes"; |
130 |
ajm |
1.6 |
} else { |
131 |
|
|
// % memory in use |
132 |
tdb |
1.12 |
if(useCache) { |
133 |
|
|
// NOTE: we take cache as being "probably free" for this check |
134 |
tdb |
1.15 |
curValue = (1 - (((double)memoryFree + (double)memoryCache) / (double)memoryTotal)) * 100; |
135 |
tdb |
1.12 |
} else { |
136 |
tdb |
1.15 |
curValue = (1 - ((double)memoryFree / (double)memoryTotal)) * 100; |
137 |
tdb |
1.12 |
} |
138 |
tdb |
1.9 |
// normal check |
139 |
|
|
newThreshold = checkAttributeThreshold(curValue, reg, false); |
140 |
|
|
type = "%"; |
141 |
tdb |
1.1 |
} |
142 |
ajm |
1.6 |
|
143 |
tdb |
1.16 |
// format the value to a String |
144 |
ajm |
1.6 |
NumberFormat nf = NumberFormat.getInstance(); |
145 |
|
|
nf.setMaximumFractionDigits(2); |
146 |
|
|
nf.setMinimumFractionDigits(2); |
147 |
tdb |
1.9 |
String strCurValue = nf.format(curValue); |
148 |
|
|
|
149 |
|
|
// set the attributeName nicely |
150 |
tdb |
1.16 |
String attributeName = type + " Memory in use " + type; |
151 |
tdb |
1.12 |
if(useCache) { |
152 |
|
|
attributeName += " (ex. cache)"; |
153 |
|
|
} |
154 |
tdb |
1.9 |
|
155 |
|
|
processAlert(newThreshold, attributeName, reg, source, strCurValue); |
156 |
tdb |
1.1 |
} |
157 |
|
|
|
158 |
|
|
/** |
159 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
160 |
|
|
* method to provide clean logging (every class should have this). |
161 |
|
|
* |
162 |
tdb |
1.13 |
* This uses the uk.org.iscream.cms.util.NameFormat class |
163 |
tdb |
1.1 |
* to format the toString() |
164 |
|
|
* |
165 |
|
|
* @return the name of this class and its CVS revision |
166 |
|
|
*/ |
167 |
|
|
public String toString() { |
168 |
|
|
return FormatName.getName( |
169 |
|
|
_name, |
170 |
|
|
getClass().getName(), |
171 |
|
|
REVISION); |
172 |
|
|
} |
173 |
|
|
|
174 |
|
|
/** |
175 |
|
|
* return the String representation of what the monitor does |
176 |
|
|
*/ |
177 |
|
|
public String getDescription(){ |
178 |
|
|
return DESC; |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
//---PRIVATE METHODS--- |
182 |
tdb |
1.7 |
|
183 |
|
|
/** |
184 |
|
|
* Checks a piece of current data, and returns the |
185 |
|
|
* threshold it breaches, if any. |
186 |
|
|
* |
187 |
tdb |
1.9 |
* The option to negate the check can be used in |
188 |
|
|
* situations where being *below* the threshold |
189 |
|
|
* is an 'alertable' situation. In this specific |
190 |
|
|
* case, we'd do this with kb disk checks. |
191 |
|
|
* |
192 |
|
|
* @param value the current value |
193 |
tdb |
1.7 |
* @param reg the Register for the host |
194 |
tdb |
1.9 |
* @param negateCheck whether to negate the check |
195 |
tdb |
1.7 |
* @return the threshold level breached, if any |
196 |
|
|
*/ |
197 |
tdb |
1.9 |
private int checkAttributeThreshold(double value, Register reg, boolean negateCheck) { |
198 |
tdb |
1.1 |
for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) { |
199 |
|
|
if (reg.getThreshold(thresholdLevel) != -1.0) { |
200 |
tdb |
1.9 |
if(!negateCheck) { |
201 |
|
|
// normal check - has the value gone *over* the threshold |
202 |
|
|
if(((double) reg.getThreshold(thresholdLevel)) < value) { |
203 |
|
|
return thresholdLevel; |
204 |
|
|
} |
205 |
|
|
} |
206 |
|
|
else { |
207 |
|
|
// negated check - has the value gone *under* the threshold |
208 |
|
|
if(((double) reg.getThreshold(thresholdLevel)) > value) { |
209 |
|
|
return thresholdLevel; |
210 |
|
|
} |
211 |
tdb |
1.1 |
} |
212 |
|
|
} |
213 |
|
|
} |
214 |
|
|
return Alert.thresholdNORMAL; |
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
218 |
tdb |
1.7 |
|
219 |
|
|
/** |
220 |
|
|
* Returns a reference to a specific Queue for this |
221 |
|
|
* monitor. This Queue returns only the data packets |
222 |
|
|
* (based on type) that we want too look at. |
223 |
|
|
* |
224 |
|
|
* @return a reference to a Queue |
225 |
|
|
*/ |
226 |
ajm |
1.6 |
protected Queue getQueue() { |
227 |
|
|
return MonitorManager.getInstance().getDataQueue(); |
228 |
|
|
} |
229 |
tdb |
1.1 |
|
230 |
|
|
//---ATTRIBUTES--- |
231 |
|
|
|
232 |
|
|
/** |
233 |
|
|
* This is the friendly identifier of the |
234 |
|
|
* component this class is running in. |
235 |
|
|
* eg, a Filter may be called "filter1", |
236 |
|
|
* If this class does not have an owning |
237 |
|
|
* component, a name from the configuration |
238 |
|
|
* can be placed here. This name could also |
239 |
|
|
* be changed to null for utility classes. |
240 |
|
|
*/ |
241 |
|
|
private String _name = "Memory"; |
242 |
|
|
|
243 |
|
|
/** |
244 |
|
|
* A reference to the configuration proxy in use |
245 |
|
|
*/ |
246 |
|
|
private ConfigurationProxy _cp = ConfigurationProxy.getInstance(); |
247 |
tdb |
1.7 |
|
248 |
|
|
/** |
249 |
|
|
* A HashMap of Registers (or groups of Registers), one |
250 |
|
|
* for each host we're monitoring. |
251 |
|
|
*/ |
252 |
tdb |
1.1 |
private HashMap _hosts = new HashMap(); |
253 |
|
|
|
254 |
|
|
//---STATIC ATTRIBUTES--- |
255 |
|
|
|
256 |
|
|
} |