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.3
Committed: Fri Mar 9 03:30:55 2001 UTC (23 years, 3 months ago) by ajm
Branch: MAIN
Changes since 1.2: +4 -4 lines
Log Message:
TOTALLY re-wrote the Register class and made appropriate changes thoughout.  It
is now much more obvious what is going on in many places.

The problem was probably caused by doing CPU as a first monitor and hard coding
the number of attributes a Register stores.  Now if a monitor wants to store
multiple attributes, it has to do that itself.  This makes alot of things
much more readable and inteligable as a result.

File Contents

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