1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.org.iscream.cms.server.client.monitors; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import java.util.HashMap; |
6 |
import java.text.NumberFormat; |
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 |
|
12 |
/** |
13 |
* This Monitor watches the Swap for all machines |
14 |
* |
15 |
* @author $Author: tdb1 $ |
16 |
* @version $Id: Swap__Monitor.java,v 1.6 2001/03/23 02:32:49 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.6 $"; |
26 |
|
27 |
/** |
28 |
* A description of this monitor |
29 |
*/ |
30 |
public final String DESC = "Monitors Swap."; |
31 |
|
32 |
//---STATIC METHODS--- |
33 |
|
34 |
//---CONSTRUCTORS--- |
35 |
|
36 |
//---PUBLIC METHODS--- |
37 |
|
38 |
/** |
39 |
* Analyse a packet of data, and generate an alert if |
40 |
* necessary. |
41 |
* |
42 |
* @param packet the XMLPacket to analyse |
43 |
*/ |
44 |
public void analysePacket(XMLPacket packet) { |
45 |
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 |
} |
63 |
swapTotal = Double.parseDouble(total); |
64 |
swapFree = Double.parseDouble(free); |
65 |
} catch (NumberFormatException e) { |
66 |
_logger.write(this.toString(), Logger.WARNING, "Received packet from "+source+" with bad swap information"+e); |
67 |
// 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 |
|
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 |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
90 |
* 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 |
|
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 |
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 |
|
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 |
protected Queue getQueue() { |
139 |
return MonitorManager.getInstance().getDataQueue(); |
140 |
} |
141 |
|
142 |
|
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 |
* A HashMap of Registers (or groups of Registers), one |
158 |
* for each host we're monitoring. |
159 |
*/ |
160 |
private HashMap _hosts = new HashMap(); |
161 |
|
162 |
//---STATIC ATTRIBUTES--- |
163 |
|
164 |
} |