ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/client/monitors/Disk__Monitor.java
Revision: 1.6
Committed: Thu Mar 22 01:51:27 2001 UTC (23 years, 1 month ago) by ajm
Branch: MAIN
Changes since 1.5: +3 -3 lines
Log Message:
modified old comment

File Contents

# Content
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.5 2001/03/22 00:59:13 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.5 $";
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, mount));
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 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 diskInUse 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);
151 }
152 }
153 }
154 }
155 }
156
157 /**
158 * Overrides the {@link java.lang.Object#toString() Object.toString()}
159 * method to provide clean logging (every class should have this).
160 *
161 * This uses the uk.org.iscream.util.NameFormat class
162 * to format the toString()
163 *
164 * @return the name of this class and its CVS revision
165 */
166 public String toString() {
167 return FormatName.getName(
168 _name,
169 getClass().getName(),
170 REVISION);
171 }
172
173 /**
174 * return the String representation of what the monitor does
175 */
176 public String getDescription(){
177 return DESC;
178 }
179
180 //---PRIVATE METHODS---
181
182 private int checkAttributeThreshold(double diskInUse, Register reg) {
183 for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) {
184 if (reg.getThreshold(thresholdLevel) != -1.0) {
185 if(((double) reg.getThreshold(thresholdLevel)) < diskInUse) {
186 return thresholdLevel;
187 }
188 }
189 }
190 return Alert.thresholdNORMAL;
191 }
192
193 //---ACCESSOR/MUTATOR METHODS---
194
195 //---ATTRIBUTES---
196
197 /**
198 * This is the friendly identifier of the
199 * component this class is running in.
200 * eg, a Filter may be called "filter1",
201 * If this class does not have an owning
202 * component, a name from the configuration
203 * can be placed here. This name could also
204 * be changed to null for utility classes.
205 */
206 private String _name = "Disk";
207
208 /**
209 * A reference to the configuration proxy in use
210 */
211 private ConfigurationProxy _cp = ConfigurationProxy.getInstance();
212
213 private HashMap _hosts = new HashMap();
214
215 //---STATIC ATTRIBUTES---
216
217 }