1 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
tdb |
1.4 |
package uk.org.iscream.client.monitors; |
3 |
tdb |
1.1 |
|
4 |
|
|
//---IMPORTS--- |
5 |
|
|
import java.util.HashMap; |
6 |
tdb |
1.2 |
import java.text.NumberFormat; |
7 |
tdb |
1.4 |
import uk.org.iscream.client.*; |
8 |
|
|
import uk.org.iscream.core.*; |
9 |
|
|
import uk.org.iscream.util.*; |
10 |
|
|
import uk.org.iscream.componentmanager.*; |
11 |
tdb |
1.1 |
|
12 |
|
|
/** |
13 |
|
|
* This Monitor watches the Memory for all machines |
14 |
|
|
* |
15 |
ajm |
1.6 |
* @author $Author: ajm4 $ |
16 |
|
|
* @version $Id: Memory__Monitor.java,v 1.5 2001/03/18 00:54:04 ajm4 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 |
ajm |
1.6 |
public final String REVISION = "$Revision: 1.5 $"; |
26 |
tdb |
1.1 |
|
27 |
|
|
public final String DESC = "Monitors Memory."; |
28 |
|
|
|
29 |
|
|
//---STATIC METHODS--- |
30 |
|
|
|
31 |
|
|
//---CONSTRUCTORS--- |
32 |
|
|
|
33 |
|
|
//---PUBLIC METHODS--- |
34 |
|
|
|
35 |
|
|
public void analysePacket(XMLPacket packet) { |
36 |
ajm |
1.6 |
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 = "Memory In Use %"; |
45 |
|
|
|
46 |
|
|
// get the packet data |
47 |
|
|
double memoryTotal, memoryFree; |
48 |
|
|
try { |
49 |
|
|
String total = packet.getParam("packet.memory.total"); |
50 |
|
|
String free = packet.getParam("packet.memory.free"); |
51 |
|
|
if(total==null || free==null) { |
52 |
|
|
throw new NumberFormatException("Memory data invalid"); |
53 |
tdb |
1.1 |
} |
54 |
ajm |
1.6 |
memoryTotal = Double.parseDouble(total); |
55 |
|
|
memoryFree = Double.parseDouble(free); |
56 |
|
|
} catch (NumberFormatException e) { |
57 |
|
|
_logger.write(this.toString(), Logger.WARNING, "Received packet from "+source+" with bad memory information: "+e); |
58 |
|
|
// don't try to continue and process |
59 |
|
|
return; |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
boolean useValue = false; |
63 |
|
|
try { |
64 |
|
|
String option = _cp.getProperty("Host." + source, "Monitor." + _name + ".thresholdMeasure"); |
65 |
|
|
if (option.equals("VALUE")) { |
66 |
|
|
useValue = true; |
67 |
|
|
} |
68 |
|
|
} catch (PropertyNotFoundException e) { |
69 |
|
|
// we default to percentage |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
// this bit determines if the memory check is a % check |
73 |
|
|
// or a kb check |
74 |
|
|
double memoryInUse; |
75 |
|
|
if(useValue) { |
76 |
|
|
// kb memory in use |
77 |
|
|
memoryInUse = memoryTotal - memoryFree; |
78 |
|
|
} else { |
79 |
|
|
// % memory in use |
80 |
|
|
memoryInUse = (1-(memoryFree / memoryTotal)) * 100; |
81 |
tdb |
1.1 |
} |
82 |
ajm |
1.6 |
|
83 |
|
|
int newThreshold = checkAttributeThreshold(memoryInUse, reg); |
84 |
|
|
|
85 |
|
|
// format the memoryInUse to a String |
86 |
|
|
NumberFormat nf = NumberFormat.getInstance(); |
87 |
|
|
nf.setMaximumFractionDigits(2); |
88 |
|
|
nf.setMinimumFractionDigits(2); |
89 |
|
|
String strMemoryInUse = nf.format(memoryInUse); |
90 |
|
|
|
91 |
|
|
processAlert(newThreshold, attributeName, reg, source, strMemoryInUse); |
92 |
tdb |
1.1 |
} |
93 |
|
|
|
94 |
|
|
/** |
95 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
96 |
|
|
* method to provide clean logging (every class should have this). |
97 |
|
|
* |
98 |
tdb |
1.4 |
* This uses the uk.org.iscream.util.NameFormat class |
99 |
tdb |
1.1 |
* to format the toString() |
100 |
|
|
* |
101 |
|
|
* @return the name of this class and its CVS revision |
102 |
|
|
*/ |
103 |
|
|
public String toString() { |
104 |
|
|
return FormatName.getName( |
105 |
|
|
_name, |
106 |
|
|
getClass().getName(), |
107 |
|
|
REVISION); |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
/** |
111 |
|
|
* return the String representation of what the monitor does |
112 |
|
|
*/ |
113 |
|
|
public String getDescription(){ |
114 |
|
|
return DESC; |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
//---PRIVATE METHODS--- |
118 |
|
|
|
119 |
|
|
private int checkAttributeThreshold(double memoryInUse, Register reg) { |
120 |
|
|
for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) { |
121 |
|
|
if (reg.getThreshold(thresholdLevel) != -1.0) { |
122 |
|
|
if(((double) reg.getThreshold(thresholdLevel)) < memoryInUse) { |
123 |
|
|
return thresholdLevel; |
124 |
|
|
} |
125 |
|
|
} |
126 |
|
|
} |
127 |
|
|
return Alert.thresholdNORMAL; |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
131 |
ajm |
1.6 |
|
132 |
|
|
protected Queue getQueue() { |
133 |
|
|
return MonitorManager.getInstance().getDataQueue(); |
134 |
|
|
} |
135 |
tdb |
1.1 |
|
136 |
|
|
//---ATTRIBUTES--- |
137 |
|
|
|
138 |
|
|
/** |
139 |
|
|
* This is the friendly identifier of the |
140 |
|
|
* component this class is running in. |
141 |
|
|
* eg, a Filter may be called "filter1", |
142 |
|
|
* If this class does not have an owning |
143 |
|
|
* component, a name from the configuration |
144 |
|
|
* can be placed here. This name could also |
145 |
|
|
* be changed to null for utility classes. |
146 |
|
|
*/ |
147 |
|
|
private String _name = "Memory"; |
148 |
|
|
|
149 |
|
|
/** |
150 |
|
|
* A reference to the configuration proxy in use |
151 |
|
|
*/ |
152 |
|
|
private ConfigurationProxy _cp = ConfigurationProxy.getInstance(); |
153 |
|
|
|
154 |
|
|
private HashMap _hosts = new HashMap(); |
155 |
|
|
|
156 |
|
|
//---STATIC ATTRIBUTES--- |
157 |
|
|
|
158 |
|
|
} |