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/Swap__Monitor.java
Revision: 1.8
Committed: Tue Mar 19 14:51:06 2002 UTC (22 years, 2 months ago) by tdb
Branch: MAIN
Changes since 1.7: +4 -4 lines
Log Message:
Minor change to log message.

File Contents

# User Rev Content
1 tdb 1.1 //---PACKAGE DECLARATION---
2 tdb 1.7 package uk.org.iscream.cms.server.client.monitors;
3 tdb 1.1
4     //---IMPORTS---
5     import java.util.HashMap;
6 tdb 1.2 import java.text.NumberFormat;
7 tdb 1.7 import uk.org.iscream.cms.server.client.*;
8     import uk.org.iscream.cms.server.core.*;
9     import uk.org.iscream.cms.server.util.*;
10     import uk.org.iscream.cms.server.componentmanager.*;
11 tdb 1.1
12     /**
13     * This Monitor watches the Swap for all machines
14     *
15 tdb 1.8 * @author $Author: tdb $
16     * @version $Id: Swap__Monitor.java,v 1.7 2001/05/29 17:02:34 tdb Exp $
17 tdb 1.1 */
18     public class Swap__Monitor extends MonitorSkeleton {
19    
20     //---FINAL ATTRIBUTES---
21    
22     /**
23     * The current CVS revision of this class
24     */
25 tdb 1.8 public final String REVISION = "$Revision: 1.7 $";
26 tdb 1.1
27 tdb 1.6 /**
28     * A description of this monitor
29     */
30 tdb 1.1 public final String DESC = "Monitors Swap.";
31    
32     //---STATIC METHODS---
33    
34     //---CONSTRUCTORS---
35    
36     //---PUBLIC METHODS---
37 tdb 1.6
38     /**
39     * Analyse a packet of data, and generate an alert if
40     * necessary.
41     *
42     * @param packet the XMLPacket to analyse
43     */
44 tdb 1.1 public void analysePacket(XMLPacket packet) {
45 ajm 1.5 String source = packet.getParam("packet.attributes.machine_name");
46     if (!_hosts.containsKey(source)) {
47     _hosts.put(source, new Register(source, _name));
48     }
49    
50     Register reg = (Register) _hosts.get(source);
51    
52     // find out the threshold level we're at
53     String attributeName = "Swap In Use %";
54    
55     // get the packet data
56     double swapTotal, swapFree;
57     try {
58     String total = packet.getParam("packet.swap.total");
59     String free = packet.getParam("packet.swap.free");
60     if(total==null || free==null) {
61     throw new NumberFormatException("Memory data invalid");
62 tdb 1.1 }
63 ajm 1.5 swapTotal = Double.parseDouble(total);
64     swapFree = Double.parseDouble(free);
65     } catch (NumberFormatException e) {
66 tdb 1.8 _logger.write(this.toString(), Logger.WARNING, "Received packet from "+source+" with bad swap information: "+e);
67 ajm 1.5 // don't try to continue and process
68     return;
69     }
70    
71     // percentage of memory in use
72     double swapInUse = (1-(swapFree / swapTotal)) * 100;
73     int newThreshold = checkAttributeThreshold(swapInUse, reg);
74    
75     // format the memoryInUse to a String
76     NumberFormat nf = NumberFormat.getInstance();
77     nf.setMaximumFractionDigits(2);
78     nf.setMinimumFractionDigits(2);
79     String strSwapInUse = nf.format(swapInUse);
80    
81     processAlert(newThreshold, attributeName, reg, source, strSwapInUse);
82 tdb 1.1
83     }
84    
85     /**
86     * Overrides the {@link java.lang.Object#toString() Object.toString()}
87     * method to provide clean logging (every class should have this).
88     *
89 tdb 1.7 * This uses the uk.org.iscream.cms.server.util.NameFormat class
90 tdb 1.1 * to format the toString()
91     *
92     * @return the name of this class and its CVS revision
93     */
94     public String toString() {
95     return FormatName.getName(
96     _name,
97     getClass().getName(),
98     REVISION);
99     }
100    
101     /**
102     * return the String representation of what the monitor does
103     */
104     public String getDescription(){
105     return DESC;
106     }
107    
108     //---PRIVATE METHODS---
109 tdb 1.6
110     /**
111     * Checks a piece of current data, and returns the
112     * threshold it breaches, if any.
113     *
114     * @param attributeString a String representing the current data value
115     * @param reg the Register for the host
116     * @return the threshold level breached, if any
117     */
118 tdb 1.1 private int checkAttributeThreshold(double swapInUse, Register reg) {
119     for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) {
120     if (reg.getThreshold(thresholdLevel) != -1.0) {
121     if(((double) reg.getThreshold(thresholdLevel)) < swapInUse) {
122     return thresholdLevel;
123     }
124     }
125     }
126     return Alert.thresholdNORMAL;
127     }
128    
129     //---ACCESSOR/MUTATOR METHODS---
130 tdb 1.6
131     /**
132     * Returns a reference to a specific Queue for this
133     * monitor. This Queue returns only the data packets
134     * (based on type) that we want too look at.
135     *
136     * @return a reference to a Queue
137     */
138 ajm 1.5 protected Queue getQueue() {
139     return MonitorManager.getInstance().getDataQueue();
140     }
141    
142 tdb 1.1
143     //---ATTRIBUTES---
144    
145     /**
146     * This is the friendly identifier of the
147     * component this class is running in.
148     * eg, a Filter may be called "filter1",
149     * If this class does not have an owning
150     * component, a name from the configuration
151     * can be placed here. This name could also
152     * be changed to null for utility classes.
153     */
154     private String _name = "Swap";
155    
156     /**
157 tdb 1.6 * A HashMap of Registers (or groups of Registers), one
158     * for each host we're monitoring.
159 tdb 1.1 */
160     private HashMap _hosts = new HashMap();
161    
162     //---STATIC ATTRIBUTES---
163    
164     }