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.3
Committed: Wed Mar 14 23:25:29 2001 UTC (23 years, 2 months ago) by tdb
Branch: MAIN
Changes since 1.2: +8 -8 lines
Log Message:
The whole server package structure has been changed.
Old Package: uk.ac.ukc.iscream.*
New Package: uk.org.iscream.*

File Contents

# User Rev Content
1 ajm 1.1 //---PACKAGE DECLARATION---
2 tdb 1.3 package uk.org.iscream.client.monitors;
3 ajm 1.1
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 tdb 1.3 import uk.org.iscream.client.*;
11     import uk.org.iscream.core.*;
12     import uk.org.iscream.util.*;
13     import uk.org.iscream.componentmanager.*;
14 ajm 1.1
15     /**
16     * This Monitor watches the Disks for all machines
17     *
18 ajm 1.2 * @author $Author: ajm4 $
19 tdb 1.3 * @version $Id: Disk__Monitor.java,v 1.2 2001/03/09 03:30:55 ajm4 Exp $
20 ajm 1.1 */
21     public class Disk__Monitor extends MonitorSkeleton {
22    
23     //---FINAL ATTRIBUTES---
24    
25     /**
26     * The current CVS revision of this class
27     */
28 tdb 1.3 public final String REVISION = "$Revision: 1.2 $";
29 ajm 1.1
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 ajm 1.2 _hosts.put(source, new HashMap());
43 ajm 1.1 }
44    
45 ajm 1.2 HashMap diskRegisters = (HashMap) _hosts.get(source);
46 ajm 1.1
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 ajm 1.2 // 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 ajm 1.1 // 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     // percentage of memory in use
114     double diskInUse = (1 - (diskAvail / diskTotal)) * 100;
115     int newThreshold = checkAttributeThreshold(diskInUse, reg);
116    
117     // 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 ajm 1.2 processAlert(newThreshold, attributeName, reg, source, strDiskInUse);
127 ajm 1.1 }
128     }
129     }
130     }
131     }
132    
133     /**
134     * Overrides the {@link java.lang.Object#toString() Object.toString()}
135     * method to provide clean logging (every class should have this).
136     *
137 tdb 1.3 * This uses the uk.org.iscream.util.NameFormat class
138 ajm 1.1 * to format the toString()
139     *
140     * @return the name of this class and its CVS revision
141     */
142     public String toString() {
143     return FormatName.getName(
144     _name,
145     getClass().getName(),
146     REVISION);
147     }
148    
149     /**
150     * return the String representation of what the monitor does
151     */
152     public String getDescription(){
153     return DESC;
154     }
155    
156     //---PRIVATE METHODS---
157    
158     private int checkAttributeThreshold(double diskInUse, Register reg) {
159     for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) {
160     if (reg.getThreshold(thresholdLevel) != -1.0) {
161     if(((double) reg.getThreshold(thresholdLevel)) < diskInUse) {
162     return thresholdLevel;
163     }
164     }
165     }
166     return Alert.thresholdNORMAL;
167     }
168    
169     //---ACCESSOR/MUTATOR METHODS---
170    
171     //---ATTRIBUTES---
172    
173     /**
174     * This is the friendly identifier of the
175     * component this class is running in.
176     * eg, a Filter may be called "filter1",
177     * If this class does not have an owning
178     * component, a name from the configuration
179     * can be placed here. This name could also
180     * be changed to null for utility classes.
181     */
182     private String _name = "Disk";
183    
184     /**
185     * A reference to the configuration proxy in use
186     */
187     private ConfigurationProxy _cp = ConfigurationProxy.getInstance();
188    
189     private HashMap _hosts = new HashMap();
190    
191     //---STATIC ATTRIBUTES---
192    
193     }