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
(Generate patch)

Comparing projects/cms/source/server/uk/org/iscream/cms/server/client/monitors/Disk__Monitor.java (file contents):
Revision 1.2 by ajm, Fri Mar 9 03:30:55 2001 UTC vs.
Revision 1.9 by tdb, Tue May 29 17:02:34 2001 UTC

# Line 1 | Line 1
1   //---PACKAGE DECLARATION---
2 < package uk.ac.ukc.iscream.client.monitors;
2 > package uk.org.iscream.cms.server.client.monitors;
3  
4   //---IMPORTS---
5   import java.util.HashMap;
# Line 7 | Line 7 | 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.*;
10 > import uk.org.iscream.cms.server.client.*;
11 > import uk.org.iscream.cms.server.core.*;
12 > import uk.org.iscream.cms.server.util.*;
13 > import uk.org.iscream.cms.server.componentmanager.*;
14  
15   /**
16   * This Monitor watches the Disks for all machines
# Line 27 | Line 27 | public class Disk__Monitor extends MonitorSkeleton {
27       */
28      public final String REVISION = "$Revision$";
29      
30 +    /**
31 +     * A description of this monitor
32 +     */
33      public final String DESC = "Monitors all host disks.";
34      
35   //---STATIC METHODS---
# Line 34 | Line 37 | public class Disk__Monitor extends MonitorSkeleton {
37   //---CONSTRUCTORS---
38  
39   //---PUBLIC METHODS---
40 <
40 >    
41 >    /**
42 >     * Analyse a packet of data, and generate an alert if
43 >     * necessary.
44 >     *
45 >     * @param packet the XMLPacket to analyse
46 >     */
47      public void analysePacket(XMLPacket packet) {
48 <        if (packet.getParam("packet.attributes.type").equals("data")) {
49 <            String source = packet.getParam("packet.attributes.machine_name");
50 <            if (!_hosts.containsKey(source)) {
51 <                _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");
48 >        String source = packet.getParam("packet.attributes.machine_name");
49 >        if (!_hosts.containsKey(source)) {
50 >            _hosts.put(source, new HashMap());
51 >        }
52  
53 <                        // these next two will be used to calculate the %age
54 <                        // total
55 <                        disks.add("packet.disk.p" + diskNumber + ".attributes.kbytes");
56 <                        String total = packet.getParam("packet.disk.p" + diskNumber + ".attributes.kbytes");
57 <                        // available
58 <                        disks.add("packet.disk.p" + diskNumber + ".attributes.avail");
59 <                        String avail = packet.getParam("packet.disk.p" + diskNumber + ".attributes.avail");
53 >        HashMap diskRegisters = (HashMap) _hosts.get(source);
54 >        
55 >        // key prefix
56 >        String keyPrefix = "packet.disk.p";
57 >        
58 >        // a tempory holder for all the disk attributes we find
59 >        ArrayList disks = new ArrayList();
60  
61 <                        // *** now process this disk ***
61 >        // unfortunatly we need to check the whole packet
62 >        // to find the disks, and then get the data attributes
63 >        Set packetSet = packet.getSet();
64 >        Iterator i = packetSet.iterator();
65 >        while (i.hasNext()) {
66 >            String dataKey = (String) i.next();
67 >            if(dataKey.startsWith(keyPrefix)) {
68 >                if(!disks.contains(dataKey)) {
69 >                    String diskNumber = "";
70  
71 <                        // check if we've seen this disk before on a previous run
72 <                        // if not, we need to create a register for it
73 <                        if(!diskRegisters.containsKey(diskNumber)) {
74 <                            diskRegisters.put(diskNumber, new Register(source, _name));
75 <                        }
71 >                    // pos is after "packet.disk.p"
72 >                    int pos = keyPrefix.length();
73 >                    while (dataKey.charAt(pos) != '.') {
74 >                        diskNumber = diskNumber + dataKey.charAt(pos);
75 >                        pos++;
76 >                    }
77  
78 <                        // get the register for this disk
96 <                        Register reg = (Register) diskRegisters.get(diskNumber);
78 >                    // add the disk to our list, with the packet data
79  
80 <                        // get the packet data
81 <                        double diskTotal, diskAvail;
82 <                        try {
83 <                
84 <                            if(total==null || avail==null) {
85 <                                throw new NumberFormatException("Disk data invalid");
86 <                            }
87 <                            diskTotal = Double.parseDouble(total);
88 <                            diskAvail = Double.parseDouble(avail);
89 <                        } catch (NumberFormatException e) {
90 <                            _logger.write(this.toString(), Logger.WARNING, "Received packet from "+source+" with bad disk information: "+e);
91 <                            // don't try to continue and process, try next disk
92 <                            break;
80 >                    // used (we won't use this value - but we need it to forget about it ;)
81 >                    disks.add(keyPrefix + diskNumber + ".attributes.used");
82 >
83 >                    // device
84 >                    disks.add(keyPrefix + diskNumber + ".attributes.name");
85 >                    String device = packet.getParam(keyPrefix + diskNumber + ".attributes.name");
86 >                    // mount point
87 >                    disks.add(keyPrefix + diskNumber + ".attributes.mount");
88 >                    String mount = packet.getParam(keyPrefix + diskNumber + ".attributes.mount");
89 >
90 >                    // these next two will be used to calculate the %age
91 >                    // total
92 >                    disks.add(keyPrefix + diskNumber + ".attributes.kbytes");
93 >                    String total = packet.getParam(keyPrefix + diskNumber + ".attributes.kbytes");
94 >                    // available
95 >                    disks.add(keyPrefix + diskNumber + ".attributes.avail");
96 >                    String avail = packet.getParam(keyPrefix + diskNumber + ".attributes.avail");
97 >
98 >                    // *** now process this disk ***
99 >
100 >                    // check if we've seen this disk before on a previous run
101 >                    // if not, we need to create a register for it
102 >                    if(!diskRegisters.containsKey(diskNumber)) {
103 >                        diskRegisters.put(diskNumber, new Register(source, _name, mount));
104 >                    }
105 >
106 >                    // get the register for this disk
107 >                    Register reg = (Register) diskRegisters.get(diskNumber);
108 >
109 >                    // get the packet data
110 >                    double diskTotal, diskAvail;
111 >                    try {
112 >
113 >                        if(total==null || avail==null) {
114 >                            throw new NumberFormatException("Disk data invalid");
115                          }
116 <            
117 <                        // percentage of memory in use
118 <                        double diskInUse = (1 - (diskAvail / diskTotal)) * 100;
119 <                        int newThreshold = checkAttributeThreshold(diskInUse, reg);
120 <            
121 <                        // format the memoryInUse to a String
118 <                        NumberFormat nf = NumberFormat.getInstance();
119 <                        nf.setMaximumFractionDigits(2);
120 <                        nf.setMinimumFractionDigits(2);
121 <                        String strDiskInUse = nf.format(diskInUse);
122 <  
123 <                        // say which disk had the problem
124 <                        String attributeName = "Disk in use % on " + mount + " (" + device + ")";
125 <                                                
126 <                        processAlert(newThreshold, attributeName, reg, source, strDiskInUse);
116 >                        diskTotal = Double.parseDouble(total);
117 >                        diskAvail = Double.parseDouble(avail);
118 >                    } catch (NumberFormatException e) {
119 >                        _logger.write(this.toString(), Logger.WARNING, "Received packet from "+source+" with bad disk information: "+e);
120 >                        // don't try to continue and process, try next disk
121 >                        break;
122                      }
123 +
124 +                    boolean useValue = false;
125 +                    try {
126 +                        String option = _cp.getProperty("Host." + source, "Monitor." + _name + ".thresholdMeasure");
127 +                        if (option.equals("VALUE")) {
128 +                            useValue = true;
129 +                        }                            
130 +                    } catch (PropertyNotFoundException e) {
131 +                        // we default to percentage
132 +                    }
133 +
134 +                    // this  bit determines if the disk check is a % check
135 +                    // or a kb check
136 +                    double diskInUse;
137 +                    String type;
138 +                    if(useValue) {
139 +                        // kb disk in use
140 +                        diskInUse = diskTotal - diskAvail;
141 +                        type = "kb";
142 +                    } else {
143 +                        // % disk in use
144 +                        diskInUse = (1 - (diskAvail / diskTotal)) * 100;
145 +                        type = "%";
146 +                    }
147 +
148 +
149 +
150 +                    int newThreshold = checkAttributeThreshold(diskInUse, reg);
151 +
152 +                    // format the diskInUse to a String
153 +                    NumberFormat nf = NumberFormat.getInstance();
154 +                    nf.setMaximumFractionDigits(2);
155 +                    nf.setMinimumFractionDigits(2);
156 +                    String strDiskInUse = nf.format(diskInUse);
157 +
158 +                    // say which disk had the problem
159 +                    String attributeName = "Disk in use " + type + " on " + mount + " (" + device + ")";
160 +
161 +                    processAlert(newThreshold, attributeName, reg, source, strDiskInUse);
162                  }
163              }
164          }
# Line 134 | Line 168 | public class Disk__Monitor extends MonitorSkeleton {
168       * Overrides the {@link java.lang.Object#toString() Object.toString()}
169       * method to provide clean logging (every class should have this).
170       *
171 <     * This uses the uk.ac.ukc.iscream.util.NameFormat class
171 >     * This uses the uk.org.iscream.cms.server.util.NameFormat class
172       * to format the toString()
173       *
174       * @return the name of this class and its CVS revision
# Line 154 | Line 188 | public class Disk__Monitor extends MonitorSkeleton {
188      }
189  
190   //---PRIVATE METHODS---
191 <
191 >    
192 >    /**
193 >     * Checks a piece of current data, and returns the
194 >     * threshold it breaches, if any.
195 >     *
196 >     * @param diskInUse the amount of space in use
197 >     * @param reg the Register for the host
198 >     * @return the threshold level breached, if any
199 >     */
200      private int checkAttributeThreshold(double diskInUse, Register reg) {
201          for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) {
202              if (reg.getThreshold(thresholdLevel) != -1.0) {
# Line 167 | Line 209 | public class Disk__Monitor extends MonitorSkeleton {
209      }
210  
211   //---ACCESSOR/MUTATOR METHODS---
212 <
212 >    
213 >    /**
214 >     * Returns a reference to a specific Queue for this
215 >     * monitor. This Queue returns only the data packets
216 >     * (based on type) that we want too look at.
217 >     *
218 >     * @return a reference to a Queue
219 >     */
220 >    protected Queue getQueue() {
221 >        return MonitorManager.getInstance().getDataQueue();
222 >    }
223 >    
224   //---ATTRIBUTES---
225  
226      /**
# Line 185 | Line 238 | public class Disk__Monitor extends MonitorSkeleton {
238       * A reference to the configuration proxy in use
239       */
240      private ConfigurationProxy _cp = ConfigurationProxy.getInstance();
241 <
241 >    
242 >    /**
243 >     * A HashMap of Registers (or groups of Registers), one
244 >     * for each host we're monitoring.
245 >     */
246      private HashMap _hosts = new HashMap();
247  
248   //---STATIC ATTRIBUTES---

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines