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