1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.org.iscream.client.monitors; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import java.util.HashMap; |
6 |
import java.util.ArrayList; |
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.*; |
14 |
|
15 |
/** |
16 |
* This Monitor watches the Disks for all machines |
17 |
* |
18 |
* @author $Author: tdb1 $ |
19 |
* @version $Id: Disk__Monitor.java,v 1.3 2001/03/14 23:25:29 tdb1 Exp $ |
20 |
*/ |
21 |
public class Disk__Monitor extends MonitorSkeleton { |
22 |
|
23 |
//---FINAL ATTRIBUTES--- |
24 |
|
25 |
/** |
26 |
* The current CVS revision of this class |
27 |
*/ |
28 |
public final String REVISION = "$Revision: 1.3 $"; |
29 |
|
30 |
public final String DESC = "Monitors all host disks."; |
31 |
|
32 |
//---STATIC METHODS--- |
33 |
|
34 |
//---CONSTRUCTORS--- |
35 |
|
36 |
//---PUBLIC METHODS--- |
37 |
|
38 |
public void analysePacket(XMLPacket packet) { |
39 |
if (packet.getParam("packet.attributes.type").equals("data")) { |
40 |
String source = packet.getParam("packet.attributes.machine_name"); |
41 |
if (!_hosts.containsKey(source)) { |
42 |
_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"); |
78 |
|
79 |
// these next two will be used to calculate the %age |
80 |
// total |
81 |
disks.add("packet.disk.p" + diskNumber + ".attributes.kbytes"); |
82 |
String total = packet.getParam("packet.disk.p" + diskNumber + ".attributes.kbytes"); |
83 |
// available |
84 |
disks.add("packet.disk.p" + diskNumber + ".attributes.avail"); |
85 |
String avail = packet.getParam("packet.disk.p" + diskNumber + ".attributes.avail"); |
86 |
|
87 |
// *** now process this disk *** |
88 |
|
89 |
// check if we've seen this disk before on a previous run |
90 |
// if not, we need to create a register for it |
91 |
if(!diskRegisters.containsKey(diskNumber)) { |
92 |
diskRegisters.put(diskNumber, new Register(source, _name)); |
93 |
} |
94 |
|
95 |
// get the register for this disk |
96 |
Register reg = (Register) diskRegisters.get(diskNumber); |
97 |
|
98 |
// get the packet data |
99 |
double diskTotal, diskAvail; |
100 |
try { |
101 |
|
102 |
if(total==null || avail==null) { |
103 |
throw new NumberFormatException("Disk data invalid"); |
104 |
} |
105 |
diskTotal = Double.parseDouble(total); |
106 |
diskAvail = Double.parseDouble(avail); |
107 |
} catch (NumberFormatException e) { |
108 |
_logger.write(this.toString(), Logger.WARNING, "Received packet from "+source+" with bad disk information: "+e); |
109 |
// don't try to continue and process, try next disk |
110 |
break; |
111 |
} |
112 |
|
113 |
boolean useValue = false; |
114 |
try { |
115 |
String option = _cp.getProperty("Host." + source, "Monitor." + _name + ".thresholdMeasure"); |
116 |
if (option.equals("VALUE")) { |
117 |
useValue = true; |
118 |
} |
119 |
} catch (PropertyNotFoundException e) { |
120 |
// we default to percentage |
121 |
} |
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); |
148 |
} |
149 |
} |
150 |
} |
151 |
} |
152 |
} |
153 |
|
154 |
/** |
155 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
156 |
* method to provide clean logging (every class should have this). |
157 |
* |
158 |
* This uses the uk.org.iscream.util.NameFormat class |
159 |
* to format the toString() |
160 |
* |
161 |
* @return the name of this class and its CVS revision |
162 |
*/ |
163 |
public String toString() { |
164 |
return FormatName.getName( |
165 |
_name, |
166 |
getClass().getName(), |
167 |
REVISION); |
168 |
} |
169 |
|
170 |
/** |
171 |
* return the String representation of what the monitor does |
172 |
*/ |
173 |
public String getDescription(){ |
174 |
return DESC; |
175 |
} |
176 |
|
177 |
//---PRIVATE METHODS--- |
178 |
|
179 |
private int checkAttributeThreshold(double diskInUse, Register reg) { |
180 |
for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) { |
181 |
if (reg.getThreshold(thresholdLevel) != -1.0) { |
182 |
if(((double) reg.getThreshold(thresholdLevel)) < diskInUse) { |
183 |
return thresholdLevel; |
184 |
} |
185 |
} |
186 |
} |
187 |
return Alert.thresholdNORMAL; |
188 |
} |
189 |
|
190 |
//---ACCESSOR/MUTATOR METHODS--- |
191 |
|
192 |
//---ATTRIBUTES--- |
193 |
|
194 |
/** |
195 |
* This is the friendly identifier of the |
196 |
* component this class is running in. |
197 |
* eg, a Filter may be called "filter1", |
198 |
* If this class does not have an owning |
199 |
* component, a name from the configuration |
200 |
* can be placed here. This name could also |
201 |
* be changed to null for utility classes. |
202 |
*/ |
203 |
private String _name = "Disk"; |
204 |
|
205 |
/** |
206 |
* A reference to the configuration proxy in use |
207 |
*/ |
208 |
private ConfigurationProxy _cp = ConfigurationProxy.getInstance(); |
209 |
|
210 |
private HashMap _hosts = new HashMap(); |
211 |
|
212 |
//---STATIC ATTRIBUTES--- |
213 |
|
214 |
} |