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: ajm4 $ |
19 |
* @version $Id: Disk__Monitor.java,v 1.6 2001/03/22 01:51:27 ajm4 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.6 $"; |
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 |
String source = packet.getParam("packet.attributes.machine_name"); |
40 |
if (!_hosts.containsKey(source)) { |
41 |
_hosts.put(source, new HashMap()); |
42 |
} |
43 |
|
44 |
HashMap diskRegisters = (HashMap) _hosts.get(source); |
45 |
|
46 |
// a tempory holder for all the disk attributes we find |
47 |
ArrayList disks = new ArrayList(); |
48 |
|
49 |
// unfortunatly we need to check the whole packet |
50 |
// to find the disks, and then get the data attributes |
51 |
Set packetSet = packet.getSet(); |
52 |
Iterator i = packetSet.iterator(); |
53 |
while (i.hasNext()) { |
54 |
String dataKey = (String) i.next(); |
55 |
if(dataKey.startsWith("packet.disk.p")) { |
56 |
if(!disks.contains(dataKey)) { |
57 |
String diskNumber = ""; |
58 |
|
59 |
// pos is after "packet.disk.p" |
60 |
int pos = 13; |
61 |
while (dataKey.charAt(pos) != '.') { |
62 |
diskNumber = diskNumber + dataKey.charAt(pos); |
63 |
pos++; |
64 |
} |
65 |
|
66 |
// add the disk to our list, with the packet data |
67 |
|
68 |
// used (we won't use this value - but we need it to forget about it ;) |
69 |
disks.add("packet.disk.p" + diskNumber + ".attributes.used"); |
70 |
|
71 |
// device |
72 |
disks.add("packet.disk.p" + diskNumber + ".attributes.name"); |
73 |
String device = packet.getParam("packet.disk.p" + diskNumber + ".attributes.name"); |
74 |
// mount point |
75 |
disks.add("packet.disk.p" + diskNumber + ".attributes.mount"); |
76 |
String mount = packet.getParam("packet.disk.p" + diskNumber + ".attributes.mount"); |
77 |
|
78 |
// these next two will be used to calculate the %age |
79 |
// total |
80 |
disks.add("packet.disk.p" + diskNumber + ".attributes.kbytes"); |
81 |
String total = packet.getParam("packet.disk.p" + diskNumber + ".attributes.kbytes"); |
82 |
// available |
83 |
disks.add("packet.disk.p" + diskNumber + ".attributes.avail"); |
84 |
String avail = packet.getParam("packet.disk.p" + diskNumber + ".attributes.avail"); |
85 |
|
86 |
// *** now process this disk *** |
87 |
|
88 |
// check if we've seen this disk before on a previous run |
89 |
// if not, we need to create a register for it |
90 |
if(!diskRegisters.containsKey(diskNumber)) { |
91 |
diskRegisters.put(diskNumber, new Register(source, _name, mount)); |
92 |
} |
93 |
|
94 |
// get the register for this disk |
95 |
Register reg = (Register) diskRegisters.get(diskNumber); |
96 |
|
97 |
// get the packet data |
98 |
double diskTotal, diskAvail; |
99 |
try { |
100 |
|
101 |
if(total==null || avail==null) { |
102 |
throw new NumberFormatException("Disk data invalid"); |
103 |
} |
104 |
diskTotal = Double.parseDouble(total); |
105 |
diskAvail = Double.parseDouble(avail); |
106 |
} catch (NumberFormatException e) { |
107 |
_logger.write(this.toString(), Logger.WARNING, "Received packet from "+source+" with bad disk information: "+e); |
108 |
// don't try to continue and process, try next disk |
109 |
break; |
110 |
} |
111 |
|
112 |
boolean useValue = false; |
113 |
try { |
114 |
String option = _cp.getProperty("Host." + source, "Monitor." + _name + ".thresholdMeasure"); |
115 |
if (option.equals("VALUE")) { |
116 |
useValue = true; |
117 |
} |
118 |
} catch (PropertyNotFoundException e) { |
119 |
// we default to percentage |
120 |
} |
121 |
|
122 |
// this bit determines if the disk check is a % check |
123 |
// or a kb check |
124 |
double diskInUse; |
125 |
String type; |
126 |
if(useValue) { |
127 |
// kb disk in use |
128 |
diskInUse = diskTotal - diskAvail; |
129 |
type = "kb"; |
130 |
} else { |
131 |
// % disk in use |
132 |
diskInUse = (1 - (diskAvail / diskTotal)) * 100; |
133 |
type = "%"; |
134 |
} |
135 |
|
136 |
|
137 |
|
138 |
int newThreshold = checkAttributeThreshold(diskInUse, reg); |
139 |
|
140 |
// format the diskInUse to a String |
141 |
NumberFormat nf = NumberFormat.getInstance(); |
142 |
nf.setMaximumFractionDigits(2); |
143 |
nf.setMinimumFractionDigits(2); |
144 |
String strDiskInUse = nf.format(diskInUse); |
145 |
|
146 |
// say which disk had the problem |
147 |
String attributeName = "Disk in use " + type + " on " + mount + " (" + device + ")"; |
148 |
|
149 |
processAlert(newThreshold, attributeName, reg, source, strDiskInUse); |
150 |
} |
151 |
} |
152 |
} |
153 |
} |
154 |
|
155 |
/** |
156 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
157 |
* method to provide clean logging (every class should have this). |
158 |
* |
159 |
* This uses the uk.org.iscream.util.NameFormat class |
160 |
* to format the toString() |
161 |
* |
162 |
* @return the name of this class and its CVS revision |
163 |
*/ |
164 |
public String toString() { |
165 |
return FormatName.getName( |
166 |
_name, |
167 |
getClass().getName(), |
168 |
REVISION); |
169 |
} |
170 |
|
171 |
/** |
172 |
* return the String representation of what the monitor does |
173 |
*/ |
174 |
public String getDescription(){ |
175 |
return DESC; |
176 |
} |
177 |
|
178 |
//---PRIVATE METHODS--- |
179 |
|
180 |
private int checkAttributeThreshold(double diskInUse, Register reg) { |
181 |
for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) { |
182 |
if (reg.getThreshold(thresholdLevel) != -1.0) { |
183 |
if(((double) reg.getThreshold(thresholdLevel)) < diskInUse) { |
184 |
return thresholdLevel; |
185 |
} |
186 |
} |
187 |
} |
188 |
return Alert.thresholdNORMAL; |
189 |
} |
190 |
|
191 |
//---ACCESSOR/MUTATOR METHODS--- |
192 |
|
193 |
protected Queue getQueue() { |
194 |
return MonitorManager.getInstance().getDataQueue(); |
195 |
} |
196 |
|
197 |
//---ATTRIBUTES--- |
198 |
|
199 |
/** |
200 |
* This is the friendly identifier of the |
201 |
* component this class is running in. |
202 |
* eg, a Filter may be called "filter1", |
203 |
* If this class does not have an owning |
204 |
* component, a name from the configuration |
205 |
* can be placed here. This name could also |
206 |
* be changed to null for utility classes. |
207 |
*/ |
208 |
private String _name = "Disk"; |
209 |
|
210 |
/** |
211 |
* A reference to the configuration proxy in use |
212 |
*/ |
213 |
private ConfigurationProxy _cp = ConfigurationProxy.getInstance(); |
214 |
|
215 |
private HashMap _hosts = new HashMap(); |
216 |
|
217 |
//---STATIC ATTRIBUTES--- |
218 |
|
219 |
} |