1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.ac.ukc.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.ac.ukc.iscream.client.*; |
11 |
import uk.ac.ukc.iscream.core.*; |
12 |
import uk.ac.ukc.iscream.util.*; |
13 |
import uk.ac.ukc.iscream.componentmanager.*; |
14 |
|
15 |
/** |
16 |
* This Monitor watches the Disks for all machines |
17 |
* |
18 |
* @author $Author: tdb1 $ |
19 |
* @version $Id: Memory__Monitor.java,v 1.2 2001/03/07 00:38:05 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.2 $"; |
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 Register(source, _name, 1)); |
43 |
} |
44 |
|
45 |
Register reg = (Register) _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 |
// get the packet data |
90 |
double diskTotal, diskAvail; |
91 |
try { |
92 |
|
93 |
if(total==null || avail==null) { |
94 |
throw new NumberFormatException("Disk data invalid"); |
95 |
} |
96 |
diskTotal = Double.parseDouble(total); |
97 |
diskAvail = Double.parseDouble(avail); |
98 |
} catch (NumberFormatException e) { |
99 |
_logger.write(this.toString(), Logger.WARNING, "Received packet from "+source+" with bad disk information: "+e); |
100 |
// don't try to continue and process, try next disk |
101 |
break; |
102 |
} |
103 |
|
104 |
// percentage of memory in use |
105 |
double diskInUse = (1 - (diskAvail / diskTotal)) * 100; |
106 |
int newThreshold = checkAttributeThreshold(diskInUse, reg); |
107 |
|
108 |
// format the memoryInUse to a String |
109 |
NumberFormat nf = NumberFormat.getInstance(); |
110 |
nf.setMaximumFractionDigits(2); |
111 |
nf.setMinimumFractionDigits(2); |
112 |
String strDiskInUse = nf.format(diskInUse); |
113 |
|
114 |
// say which disk had the problem |
115 |
String attributeName = "Disk in use % on " + mount + " (" + device + ")"; |
116 |
|
117 |
processAlert(newThreshold, 0, attributeName, reg, source, strDiskInUse); |
118 |
} |
119 |
} |
120 |
} |
121 |
} |
122 |
} |
123 |
|
124 |
/** |
125 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
126 |
* method to provide clean logging (every class should have this). |
127 |
* |
128 |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
129 |
* to format the toString() |
130 |
* |
131 |
* @return the name of this class and its CVS revision |
132 |
*/ |
133 |
public String toString() { |
134 |
return FormatName.getName( |
135 |
_name, |
136 |
getClass().getName(), |
137 |
REVISION); |
138 |
} |
139 |
|
140 |
/** |
141 |
* return the String representation of what the monitor does |
142 |
*/ |
143 |
public String getDescription(){ |
144 |
return DESC; |
145 |
} |
146 |
|
147 |
//---PRIVATE METHODS--- |
148 |
|
149 |
private int checkAttributeThreshold(double diskInUse, Register reg) { |
150 |
for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) { |
151 |
if (reg.getThreshold(thresholdLevel) != -1.0) { |
152 |
if(((double) reg.getThreshold(thresholdLevel)) < diskInUse) { |
153 |
return thresholdLevel; |
154 |
} |
155 |
} |
156 |
} |
157 |
return Alert.thresholdNORMAL; |
158 |
} |
159 |
|
160 |
//---ACCESSOR/MUTATOR METHODS--- |
161 |
|
162 |
//---ATTRIBUTES--- |
163 |
|
164 |
/** |
165 |
* This is the friendly identifier of the |
166 |
* component this class is running in. |
167 |
* eg, a Filter may be called "filter1", |
168 |
* If this class does not have an owning |
169 |
* component, a name from the configuration |
170 |
* can be placed here. This name could also |
171 |
* be changed to null for utility classes. |
172 |
*/ |
173 |
private String _name = "Disk"; |
174 |
|
175 |
/** |
176 |
* A reference to the configuration proxy in use |
177 |
*/ |
178 |
private ConfigurationProxy _cp = ConfigurationProxy.getInstance(); |
179 |
|
180 |
private HashMap _hosts = new HashMap(); |
181 |
|
182 |
//---STATIC ATTRIBUTES--- |
183 |
|
184 |
} |