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