1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.org.iscream.client.monitors; |
2 |
> |
package uk.org.iscream.cms.server.client.monitors; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
|
import java.util.HashMap; |
7 |
|
import java.util.Set; |
8 |
|
import java.util.Iterator; |
9 |
|
import java.text.NumberFormat; |
10 |
< |
import uk.org.iscream.client.*; |
11 |
< |
import uk.org.iscream.core.*; |
12 |
< |
import uk.org.iscream.util.*; |
13 |
< |
import uk.org.iscream.componentmanager.*; |
10 |
> |
import uk.org.iscream.cms.server.client.*; |
11 |
> |
import uk.org.iscream.cms.server.core.*; |
12 |
> |
import uk.org.iscream.cms.server.util.*; |
13 |
> |
import uk.org.iscream.cms.server.componentmanager.*; |
14 |
|
|
15 |
|
/** |
16 |
|
* This Monitor watches the Disks for all machines |
27 |
|
*/ |
28 |
|
public final String REVISION = "$Revision$"; |
29 |
|
|
30 |
+ |
/** |
31 |
+ |
* A description of this monitor |
32 |
+ |
*/ |
33 |
|
public final String DESC = "Monitors all host disks."; |
34 |
|
|
35 |
|
//---STATIC METHODS--- |
37 |
|
//---CONSTRUCTORS--- |
38 |
|
|
39 |
|
//---PUBLIC METHODS--- |
40 |
< |
|
40 |
> |
|
41 |
> |
/** |
42 |
> |
* Analyse a packet of data, and generate an alert if |
43 |
> |
* necessary. |
44 |
> |
* |
45 |
> |
* @param packet the XMLPacket to analyse |
46 |
> |
*/ |
47 |
|
public void analysePacket(XMLPacket packet) { |
48 |
< |
if (packet.getParam("packet.attributes.type").equals("data")) { |
49 |
< |
String source = packet.getParam("packet.attributes.machine_name"); |
50 |
< |
if (!_hosts.containsKey(source)) { |
51 |
< |
_hosts.put(source, new HashMap()); |
43 |
< |
} |
44 |
< |
|
45 |
< |
HashMap diskRegisters = (HashMap) _hosts.get(source); |
46 |
< |
|
47 |
< |
// a tempory holder for all the disk attributes we find |
48 |
< |
ArrayList disks = new ArrayList(); |
49 |
< |
|
50 |
< |
// unfortunatly we need to check the whole packet |
51 |
< |
// to find the disks, and then get the data attributes |
52 |
< |
Set packetSet = packet.getSet(); |
53 |
< |
Iterator i = packetSet.iterator(); |
54 |
< |
while (i.hasNext()) { |
55 |
< |
String dataKey = (String) i.next(); |
56 |
< |
if(dataKey.startsWith("packet.disk.p")) { |
57 |
< |
if(!disks.contains(dataKey)) { |
58 |
< |
String diskNumber = ""; |
59 |
< |
|
60 |
< |
// pos is after "packet.disk.p" |
61 |
< |
int pos = 13; |
62 |
< |
while (dataKey.charAt(pos) != '.') { |
63 |
< |
diskNumber = diskNumber + dataKey.charAt(pos); |
64 |
< |
pos++; |
65 |
< |
} |
66 |
< |
|
67 |
< |
// add the disk to our list, with the packet data |
68 |
< |
|
69 |
< |
// used (we won't use this value - but we need it to forget about it ;) |
70 |
< |
disks.add("packet.disk.p" + diskNumber + ".attributes.used"); |
71 |
< |
|
72 |
< |
// device |
73 |
< |
disks.add("packet.disk.p" + diskNumber + ".attributes.name"); |
74 |
< |
String device = packet.getParam("packet.disk.p" + diskNumber + ".attributes.name"); |
75 |
< |
// mount point |
76 |
< |
disks.add("packet.disk.p" + diskNumber + ".attributes.mount"); |
77 |
< |
String mount = packet.getParam("packet.disk.p" + diskNumber + ".attributes.mount"); |
48 |
> |
String source = packet.getParam("packet.attributes.machine_name"); |
49 |
> |
if (!_hosts.containsKey(source)) { |
50 |
> |
_hosts.put(source, new HashMap()); |
51 |
> |
} |
52 |
|
|
53 |
< |
// these next two will be used to calculate the %age |
54 |
< |
// total |
55 |
< |
disks.add("packet.disk.p" + diskNumber + ".attributes.kbytes"); |
56 |
< |
String total = packet.getParam("packet.disk.p" + diskNumber + ".attributes.kbytes"); |
57 |
< |
// available |
58 |
< |
disks.add("packet.disk.p" + diskNumber + ".attributes.avail"); |
59 |
< |
String avail = packet.getParam("packet.disk.p" + diskNumber + ".attributes.avail"); |
53 |
> |
HashMap diskRegisters = (HashMap) _hosts.get(source); |
54 |
> |
|
55 |
> |
// key prefix |
56 |
> |
String keyPrefix = "packet.disk.p"; |
57 |
> |
|
58 |
> |
// a temporary holder for all the disk attributes we find |
59 |
> |
ArrayList disks = new ArrayList(); |
60 |
|
|
61 |
< |
// *** now process this disk *** |
61 |
> |
// unfortunatly we need to check the whole packet |
62 |
> |
// to find the disks, and then get the data attributes |
63 |
> |
Set packetSet = packet.getSet(); |
64 |
> |
Iterator i = packetSet.iterator(); |
65 |
> |
while (i.hasNext()) { |
66 |
> |
String dataKey = (String) i.next(); |
67 |
> |
if(dataKey.startsWith(keyPrefix)) { |
68 |
> |
if(!disks.contains(dataKey)) { |
69 |
> |
String diskNumber = ""; |
70 |
|
|
71 |
< |
// check if we've seen this disk before on a previous run |
72 |
< |
// if not, we need to create a register for it |
73 |
< |
if(!diskRegisters.containsKey(diskNumber)) { |
74 |
< |
diskRegisters.put(diskNumber, new Register(source, _name)); |
75 |
< |
} |
71 |
> |
// pos is after "packet.disk.p" |
72 |
> |
int pos = keyPrefix.length(); |
73 |
> |
while (dataKey.charAt(pos) != '.') { |
74 |
> |
diskNumber = diskNumber + dataKey.charAt(pos); |
75 |
> |
pos++; |
76 |
> |
} |
77 |
|
|
78 |
< |
// get the register for this disk |
96 |
< |
Register reg = (Register) diskRegisters.get(diskNumber); |
78 |
> |
// add the disk to our list, with the packet data |
79 |
|
|
80 |
< |
// get the packet data |
81 |
< |
double diskTotal, diskAvail; |
82 |
< |
try { |
83 |
< |
|
84 |
< |
if(total==null || avail==null) { |
85 |
< |
throw new NumberFormatException("Disk data invalid"); |
86 |
< |
} |
87 |
< |
diskTotal = Double.parseDouble(total); |
88 |
< |
diskAvail = Double.parseDouble(avail); |
89 |
< |
} catch (NumberFormatException e) { |
90 |
< |
_logger.write(this.toString(), Logger.WARNING, "Received packet from "+source+" with bad disk information: "+e); |
91 |
< |
// don't try to continue and process, try next disk |
92 |
< |
break; |
80 |
> |
// used (we won't use this value - but we need it to forget about it ;) |
81 |
> |
disks.add(keyPrefix + diskNumber + ".attributes.used"); |
82 |
> |
|
83 |
> |
// device |
84 |
> |
disks.add(keyPrefix + diskNumber + ".attributes.name"); |
85 |
> |
String device = packet.getParam(keyPrefix + diskNumber + ".attributes.name"); |
86 |
> |
// mount point |
87 |
> |
disks.add(keyPrefix + diskNumber + ".attributes.mount"); |
88 |
> |
String mount = packet.getParam(keyPrefix + diskNumber + ".attributes.mount"); |
89 |
> |
|
90 |
> |
// these next two will be used to calculate the %age |
91 |
> |
// total |
92 |
> |
disks.add(keyPrefix + diskNumber + ".attributes.kbytes"); |
93 |
> |
String total = packet.getParam(keyPrefix + diskNumber + ".attributes.kbytes"); |
94 |
> |
// available |
95 |
> |
disks.add(keyPrefix + diskNumber + ".attributes.avail"); |
96 |
> |
String avail = packet.getParam(keyPrefix + diskNumber + ".attributes.avail"); |
97 |
> |
|
98 |
> |
// *** now process this disk *** |
99 |
> |
|
100 |
> |
// check if we've seen this disk before on a previous run |
101 |
> |
// if not, we need to create a register for it |
102 |
> |
// nb. use the device as the key as this is unlikely to change, |
103 |
> |
// unlike diskNumber which could easily change |
104 |
> |
// (diskNumber is based on the order of df's output!) |
105 |
> |
if(!diskRegisters.containsKey(device)) { |
106 |
> |
diskRegisters.put(device, new Register(source, _name, mount)); |
107 |
> |
} |
108 |
> |
|
109 |
> |
// get the register for this disk |
110 |
> |
Register reg = (Register) diskRegisters.get(device); |
111 |
> |
|
112 |
> |
// get the packet data |
113 |
> |
double diskTotal, diskAvail; |
114 |
> |
try { |
115 |
> |
|
116 |
> |
if(total==null || avail==null) { |
117 |
> |
throw new NumberFormatException("Disk data invalid"); |
118 |
|
} |
119 |
< |
|
120 |
< |
boolean useValue = false; |
119 |
> |
diskTotal = Double.parseDouble(total); |
120 |
> |
diskAvail = Double.parseDouble(avail); |
121 |
> |
} catch (NumberFormatException e) { |
122 |
> |
_logger.write(this.toString(), Logger.WARNING, "Received packet from "+source+" with bad disk information: "+e); |
123 |
> |
// don't try to continue and process, try next disk |
124 |
> |
break; |
125 |
> |
} |
126 |
> |
|
127 |
> |
boolean useValue = false; |
128 |
> |
try { |
129 |
> |
// try looking for a mount-point specific thresholdMeasure first |
130 |
> |
String option = _cp.getProperty("Host." + source, "Monitor." + _name + "." + mount + ".thresholdMeasure"); |
131 |
> |
if (option.equals("VALUE")) { |
132 |
> |
useValue = true; |
133 |
> |
} |
134 |
> |
} catch (PropertyNotFoundException e) { |
135 |
|
try { |
136 |
+ |
// now look for a more general thresholdMeasure |
137 |
|
String option = _cp.getProperty("Host." + source, "Monitor." + _name + ".thresholdMeasure"); |
138 |
|
if (option.equals("VALUE")) { |
139 |
|
useValue = true; |
140 |
< |
} |
141 |
< |
} catch (PropertyNotFoundException e) { |
142 |
< |
// we default to percentage |
140 |
> |
} |
141 |
> |
} catch (PropertyNotFoundException f) { |
142 |
> |
// we default to percentage in any case |
143 |
|
} |
122 |
– |
|
123 |
– |
// this bit determines if the disk check is a % check |
124 |
– |
// or a kb check |
125 |
– |
double diskInUse; |
126 |
– |
if(useValue) { |
127 |
– |
// kb disk in use |
128 |
– |
diskInUse = diskTotal - diskAvail; |
129 |
– |
} else { |
130 |
– |
// kb disk in use |
131 |
– |
diskInUse = (1 - (diskAvail / diskTotal)) * 100; |
132 |
– |
} |
133 |
– |
|
134 |
– |
|
135 |
– |
|
136 |
– |
int newThreshold = checkAttributeThreshold(diskInUse, reg); |
137 |
– |
|
138 |
– |
// format the memoryInUse to a String |
139 |
– |
NumberFormat nf = NumberFormat.getInstance(); |
140 |
– |
nf.setMaximumFractionDigits(2); |
141 |
– |
nf.setMinimumFractionDigits(2); |
142 |
– |
String strDiskInUse = nf.format(diskInUse); |
143 |
– |
|
144 |
– |
// say which disk had the problem |
145 |
– |
String attributeName = "Disk in use % on " + mount + " (" + device + ")"; |
146 |
– |
|
147 |
– |
processAlert(newThreshold, attributeName, reg, source, strDiskInUse); |
144 |
|
} |
145 |
+ |
|
146 |
+ |
// this bit determines if the disk check is a % check |
147 |
+ |
// or a kb check |
148 |
+ |
String type; |
149 |
+ |
double curValue; |
150 |
+ |
int newThreshold; |
151 |
+ |
if(useValue) { |
152 |
+ |
// kb disk available |
153 |
+ |
curValue = diskAvail; |
154 |
+ |
// negate check |
155 |
+ |
newThreshold = checkAttributeThreshold(curValue, reg, true); |
156 |
+ |
type = "kb"; |
157 |
+ |
} else { |
158 |
+ |
// % disk in use |
159 |
+ |
curValue = (1 - (diskAvail / diskTotal)) * 100; |
160 |
+ |
// normal check |
161 |
+ |
newThreshold = checkAttributeThreshold(curValue, reg, false); |
162 |
+ |
type = "%"; |
163 |
+ |
} |
164 |
+ |
|
165 |
+ |
// format the diskInUse to a String |
166 |
+ |
NumberFormat nf = NumberFormat.getInstance(); |
167 |
+ |
nf.setMaximumFractionDigits(2); |
168 |
+ |
nf.setMinimumFractionDigits(2); |
169 |
+ |
String strCurValue = nf.format(curValue); |
170 |
+ |
|
171 |
+ |
// say which disk had the problem |
172 |
+ |
String attributeName = "Disk in use " + type + " on " + mount + " (" + device + ")"; |
173 |
+ |
|
174 |
+ |
processAlert(newThreshold, attributeName, reg, source, strCurValue); |
175 |
|
} |
176 |
|
} |
177 |
|
} |
181 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
182 |
|
* method to provide clean logging (every class should have this). |
183 |
|
* |
184 |
< |
* This uses the uk.org.iscream.util.NameFormat class |
184 |
> |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
185 |
|
* to format the toString() |
186 |
|
* |
187 |
|
* @return the name of this class and its CVS revision |
201 |
|
} |
202 |
|
|
203 |
|
//---PRIVATE METHODS--- |
204 |
< |
|
205 |
< |
private int checkAttributeThreshold(double diskInUse, Register reg) { |
204 |
> |
|
205 |
> |
/** |
206 |
> |
* Checks a piece of current data, and returns the |
207 |
> |
* threshold it breaches, if any. |
208 |
> |
* |
209 |
> |
* The option to negate the check can be used in |
210 |
> |
* situations where being *below* the threshold |
211 |
> |
* is an 'alertable' situation. In this specific |
212 |
> |
* case, we'd do this with kb disk checks. |
213 |
> |
* |
214 |
> |
* @param value the current value |
215 |
> |
* @param reg the Register for the host |
216 |
> |
* @param negateCheck whether to negate the check |
217 |
> |
* @return the threshold level breached, if any |
218 |
> |
*/ |
219 |
> |
private int checkAttributeThreshold(double value, Register reg, boolean negateCheck) { |
220 |
|
for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) { |
221 |
|
if (reg.getThreshold(thresholdLevel) != -1.0) { |
222 |
< |
if(((double) reg.getThreshold(thresholdLevel)) < diskInUse) { |
223 |
< |
return thresholdLevel; |
222 |
> |
if(!negateCheck) { |
223 |
> |
// normal check - has the value gone *over* the threshold |
224 |
> |
if(((double) reg.getThreshold(thresholdLevel)) < value) { |
225 |
> |
return thresholdLevel; |
226 |
> |
} |
227 |
|
} |
228 |
+ |
else { |
229 |
+ |
// negated check - has the value gone *under* the threshold |
230 |
+ |
if(((double) reg.getThreshold(thresholdLevel)) > value) { |
231 |
+ |
return thresholdLevel; |
232 |
+ |
} |
233 |
+ |
} |
234 |
|
} |
235 |
|
} |
236 |
|
return Alert.thresholdNORMAL; |
237 |
|
} |
238 |
|
|
239 |
|
//---ACCESSOR/MUTATOR METHODS--- |
240 |
< |
|
240 |
> |
|
241 |
> |
/** |
242 |
> |
* Returns a reference to a specific Queue for this |
243 |
> |
* monitor. This Queue returns only the data packets |
244 |
> |
* (based on type) that we want too look at. |
245 |
> |
* |
246 |
> |
* @return a reference to a Queue |
247 |
> |
*/ |
248 |
> |
protected Queue getQueue() { |
249 |
> |
return MonitorManager.getInstance().getDataQueue(); |
250 |
> |
} |
251 |
> |
|
252 |
|
//---ATTRIBUTES--- |
253 |
|
|
254 |
|
/** |
266 |
|
* A reference to the configuration proxy in use |
267 |
|
*/ |
268 |
|
private ConfigurationProxy _cp = ConfigurationProxy.getInstance(); |
269 |
< |
|
269 |
> |
|
270 |
> |
/** |
271 |
> |
* A HashMap of Registers (or groups of Registers), one |
272 |
> |
* for each host we're monitoring. |
273 |
> |
*/ |
274 |
|
private HashMap _hosts = new HashMap(); |
275 |
|
|
276 |
|
//---STATIC ATTRIBUTES--- |