1 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
tdb |
1.8 |
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.8 |
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 Memory for all machines |
14 |
|
|
* |
15 |
tdb |
1.8 |
* @author $Author: tdb1 $ |
16 |
|
|
* @version $Id: Memory__Monitor.java,v 1.7 2001/03/23 02:32:49 tdb1 Exp $ |
17 |
tdb |
1.1 |
*/ |
18 |
|
|
public class Memory__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.7 |
/** |
28 |
|
|
* A description of this monitor |
29 |
|
|
*/ |
30 |
tdb |
1.1 |
public final String DESC = "Monitors Memory."; |
31 |
|
|
|
32 |
|
|
//---STATIC METHODS--- |
33 |
|
|
|
34 |
|
|
//---CONSTRUCTORS--- |
35 |
|
|
|
36 |
|
|
//---PUBLIC METHODS--- |
37 |
tdb |
1.7 |
|
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.6 |
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 = "Memory In Use %"; |
54 |
|
|
|
55 |
|
|
// get the packet data |
56 |
|
|
double memoryTotal, memoryFree; |
57 |
|
|
try { |
58 |
|
|
String total = packet.getParam("packet.memory.total"); |
59 |
|
|
String free = packet.getParam("packet.memory.free"); |
60 |
|
|
if(total==null || free==null) { |
61 |
|
|
throw new NumberFormatException("Memory data invalid"); |
62 |
tdb |
1.1 |
} |
63 |
ajm |
1.6 |
memoryTotal = Double.parseDouble(total); |
64 |
|
|
memoryFree = Double.parseDouble(free); |
65 |
|
|
} catch (NumberFormatException e) { |
66 |
|
|
_logger.write(this.toString(), Logger.WARNING, "Received packet from "+source+" with bad memory information: "+e); |
67 |
|
|
// don't try to continue and process |
68 |
|
|
return; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
boolean useValue = false; |
72 |
|
|
try { |
73 |
|
|
String option = _cp.getProperty("Host." + source, "Monitor." + _name + ".thresholdMeasure"); |
74 |
|
|
if (option.equals("VALUE")) { |
75 |
|
|
useValue = true; |
76 |
|
|
} |
77 |
|
|
} catch (PropertyNotFoundException e) { |
78 |
|
|
// we default to percentage |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
// this bit determines if the memory check is a % check |
82 |
|
|
// or a kb check |
83 |
|
|
double memoryInUse; |
84 |
|
|
if(useValue) { |
85 |
|
|
// kb memory in use |
86 |
|
|
memoryInUse = memoryTotal - memoryFree; |
87 |
|
|
} else { |
88 |
|
|
// % memory in use |
89 |
|
|
memoryInUse = (1-(memoryFree / memoryTotal)) * 100; |
90 |
tdb |
1.1 |
} |
91 |
ajm |
1.6 |
|
92 |
|
|
int newThreshold = checkAttributeThreshold(memoryInUse, reg); |
93 |
|
|
|
94 |
|
|
// format the memoryInUse to a String |
95 |
|
|
NumberFormat nf = NumberFormat.getInstance(); |
96 |
|
|
nf.setMaximumFractionDigits(2); |
97 |
|
|
nf.setMinimumFractionDigits(2); |
98 |
|
|
String strMemoryInUse = nf.format(memoryInUse); |
99 |
|
|
|
100 |
|
|
processAlert(newThreshold, attributeName, reg, source, strMemoryInUse); |
101 |
tdb |
1.1 |
} |
102 |
|
|
|
103 |
|
|
/** |
104 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
105 |
|
|
* method to provide clean logging (every class should have this). |
106 |
|
|
* |
107 |
tdb |
1.8 |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
108 |
tdb |
1.1 |
* to format the toString() |
109 |
|
|
* |
110 |
|
|
* @return the name of this class and its CVS revision |
111 |
|
|
*/ |
112 |
|
|
public String toString() { |
113 |
|
|
return FormatName.getName( |
114 |
|
|
_name, |
115 |
|
|
getClass().getName(), |
116 |
|
|
REVISION); |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
/** |
120 |
|
|
* return the String representation of what the monitor does |
121 |
|
|
*/ |
122 |
|
|
public String getDescription(){ |
123 |
|
|
return DESC; |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
//---PRIVATE METHODS--- |
127 |
tdb |
1.7 |
|
128 |
|
|
/** |
129 |
|
|
* Checks a piece of current data, and returns the |
130 |
|
|
* threshold it breaches, if any. |
131 |
|
|
* |
132 |
|
|
* @param attributeString a String representing the current data value |
133 |
|
|
* @param reg the Register for the host |
134 |
|
|
* @return the threshold level breached, if any |
135 |
|
|
*/ |
136 |
tdb |
1.1 |
private int checkAttributeThreshold(double memoryInUse, Register reg) { |
137 |
|
|
for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) { |
138 |
|
|
if (reg.getThreshold(thresholdLevel) != -1.0) { |
139 |
|
|
if(((double) reg.getThreshold(thresholdLevel)) < memoryInUse) { |
140 |
|
|
return thresholdLevel; |
141 |
|
|
} |
142 |
|
|
} |
143 |
|
|
} |
144 |
|
|
return Alert.thresholdNORMAL; |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
148 |
tdb |
1.7 |
|
149 |
|
|
/** |
150 |
|
|
* Returns a reference to a specific Queue for this |
151 |
|
|
* monitor. This Queue returns only the data packets |
152 |
|
|
* (based on type) that we want too look at. |
153 |
|
|
* |
154 |
|
|
* @return a reference to a Queue |
155 |
|
|
*/ |
156 |
ajm |
1.6 |
protected Queue getQueue() { |
157 |
|
|
return MonitorManager.getInstance().getDataQueue(); |
158 |
|
|
} |
159 |
tdb |
1.1 |
|
160 |
|
|
//---ATTRIBUTES--- |
161 |
|
|
|
162 |
|
|
/** |
163 |
|
|
* This is the friendly identifier of the |
164 |
|
|
* component this class is running in. |
165 |
|
|
* eg, a Filter may be called "filter1", |
166 |
|
|
* If this class does not have an owning |
167 |
|
|
* component, a name from the configuration |
168 |
|
|
* can be placed here. This name could also |
169 |
|
|
* be changed to null for utility classes. |
170 |
|
|
*/ |
171 |
|
|
private String _name = "Memory"; |
172 |
|
|
|
173 |
|
|
/** |
174 |
|
|
* A reference to the configuration proxy in use |
175 |
|
|
*/ |
176 |
|
|
private ConfigurationProxy _cp = ConfigurationProxy.getInstance(); |
177 |
tdb |
1.7 |
|
178 |
|
|
/** |
179 |
|
|
* A HashMap of Registers (or groups of Registers), one |
180 |
|
|
* for each host we're monitoring. |
181 |
|
|
*/ |
182 |
tdb |
1.1 |
private HashMap _hosts = new HashMap(); |
183 |
|
|
|
184 |
|
|
//---STATIC ATTRIBUTES--- |
185 |
|
|
|
186 |
|
|
} |