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