1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.ac.ukc.iscream.client.monitors; |
2 |
> |
package uk.org.iscream.client.monitors; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
< |
import uk.ac.ukc.iscream.client.*; |
6 |
< |
import uk.ac.ukc.iscream.core.*; |
7 |
< |
import uk.ac.ukc.iscream.util.*; |
8 |
< |
import uk.ac.ukc.iscream.componentmanager.*; |
5 |
> |
import java.util.HashMap; |
6 |
> |
import uk.org.iscream.client.*; |
7 |
> |
import uk.org.iscream.core.*; |
8 |
> |
import uk.org.iscream.util.*; |
9 |
> |
import uk.org.iscream.componentmanager.*; |
10 |
|
|
11 |
|
/** |
12 |
|
* This Monitor watches the CPU load for all machines |
14 |
|
* @author $Author$ |
15 |
|
* @version $Id$ |
16 |
|
*/ |
17 |
< |
public class CPU__Monitor implements Monitor { |
17 |
> |
public class CPU__Monitor extends MonitorSkeleton { |
18 |
|
|
19 |
|
//---FINAL ATTRIBUTES--- |
20 |
|
|
23 |
|
*/ |
24 |
|
public final String REVISION = "$Revision$"; |
25 |
|
|
26 |
+ |
/** |
27 |
+ |
* A description of this monitor |
28 |
+ |
*/ |
29 |
|
public final String DESC = "Monitors CPU."; |
30 |
|
|
31 |
|
//---STATIC METHODS--- |
32 |
|
|
33 |
|
//---CONSTRUCTORS--- |
34 |
|
|
31 |
– |
public CPU__Monitor() { |
32 |
– |
_alerterQueue = ClientMain._alerterQueue; |
33 |
– |
// get the configuration for this plug-in |
34 |
– |
Configuration config = _refman.getCM().getConfiguration(_name); |
35 |
– |
_levels = new double[(Alert.alerts).length]; |
36 |
– |
for (int x = 0; x < Alert.alerts.length; x++) { |
37 |
– |
try { |
38 |
– |
_levels[x] = Double.parseDouble(config.getProperty("Monitor.CPU.level." + x)); |
39 |
– |
} catch (NumberFormatException e) { |
40 |
– |
_levels[x] = -1; |
41 |
– |
} |
42 |
– |
} |
43 |
– |
} |
35 |
|
//---PUBLIC METHODS--- |
36 |
< |
|
36 |
> |
|
37 |
> |
/** |
38 |
> |
* Analyse a packet of data, and generate an alert if |
39 |
> |
* necessary. |
40 |
> |
* |
41 |
> |
* @param packet the XMLPacket to analyse |
42 |
> |
*/ |
43 |
|
public void analysePacket(XMLPacket packet) { |
44 |
< |
for(int x=0; x < _levels.length; x++) { |
45 |
< |
if (_levels[x] != -1) { |
46 |
< |
double idle = Double.parseDouble(packet.getParam("packet.cpu.idle")); |
47 |
< |
if (_levels[x] < idle) fireAlert(x, idle, "idle"); |
48 |
< |
double user = Double.parseDouble(packet.getParam("packet.cpu.user")); |
52 |
< |
if (_levels[x] < user) fireAlert(x, user, "user"); |
53 |
< |
double kernel = Double.parseDouble(packet.getParam("packet.cpu.kernel")); |
54 |
< |
if (_levels[x] < kernel) fireAlert(x, kernel, "kernel"); |
55 |
< |
double iowait = Double.parseDouble(packet.getParam("packet.cpu.iowait")); |
56 |
< |
if (_levels[x] < iowait) fireAlert(x, iowait, "iowait"); |
57 |
< |
double swap = Double.parseDouble(packet.getParam("packet.cpu.swap")); |
58 |
< |
if (_levels[x] < swap) fireAlert(x, swap, "swap"); |
59 |
< |
} |
44 |
> |
String source = packet.getParam("packet.attributes.machine_name"); |
45 |
> |
if (!_hosts.containsKey(source)) { |
46 |
> |
HashMap attributeRegisters = new HashMap(); |
47 |
> |
initAttributeRegsiters(source, attributeRegisters); |
48 |
> |
_hosts.put(source, attributeRegisters); |
49 |
|
} |
50 |
+ |
|
51 |
+ |
HashMap attributeRegisters = (HashMap) _hosts.get(source); |
52 |
+ |
for(int attributeNum = 0; attributeNum < _attributes.length; attributeNum++) { |
53 |
+ |
Register reg = (Register) attributeRegisters.get(_attributes[attributeNum]); |
54 |
+ |
// find out the threshold level we're at |
55 |
+ |
String attribute = _attributes[attributeNum]; |
56 |
+ |
String attributeName = _attributeNames[attributeNum]; |
57 |
+ |
String currentValue = packet.getParam(attribute); |
58 |
+ |
int newThreshold = checkAttributeThreshold(currentValue, reg); |
59 |
+ |
processAlert(newThreshold, attributeName, reg, source, currentValue); |
60 |
+ |
} |
61 |
|
} |
62 |
|
|
63 |
|
/** |
64 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
65 |
|
* method to provide clean logging (every class should have this). |
66 |
|
* |
67 |
< |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
67 |
> |
* This uses the uk.org.iscream.util.NameFormat class |
68 |
|
* to format the toString() |
69 |
|
* |
70 |
|
* @return the name of this class and its CVS revision |
77 |
|
} |
78 |
|
|
79 |
|
/** |
80 |
< |
* return the String representation of what the filter does |
80 |
> |
* return the String representation of what the monitor does |
81 |
|
*/ |
82 |
|
public String getDescription(){ |
83 |
|
return DESC; |
84 |
|
} |
85 |
|
|
86 |
|
//---PRIVATE METHODS--- |
87 |
< |
|
88 |
< |
private void fireAlert(int alertLevel, double currentValue, String type) { |
89 |
< |
String value = Double.toString(currentValue); |
90 |
< |
String thresholdValue = Double.toString(_levels[alertLevel]); |
91 |
< |
String attributeName = "CPU " + type; |
92 |
< |
Alert alert = new Alert(alertLevel, thresholdValue, value, attributeName); |
93 |
< |
_alerterQueue.add(alert); |
87 |
> |
|
88 |
> |
/** |
89 |
> |
* Checks a piece of current data, and returns the |
90 |
> |
* threshold it breaches, if any. |
91 |
> |
* |
92 |
> |
* @param attributeString a String representing the current data value |
93 |
> |
* @param reg the Register for the host |
94 |
> |
* @return the threshold level breached, if any |
95 |
> |
*/ |
96 |
> |
private int checkAttributeThreshold(String attributeString, Register reg) { |
97 |
> |
for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) { |
98 |
> |
if (reg.getThreshold(thresholdLevel) != -1.0) { |
99 |
> |
if(attributeString != null) { |
100 |
> |
try { |
101 |
> |
double attribute = Double.parseDouble(attributeString); |
102 |
> |
if (reg.getThreshold(thresholdLevel) < attribute) return thresholdLevel; |
103 |
> |
} catch (NumberFormatException e) { |
104 |
> |
// we got some duff data in the packet, but we shouldn't have |
105 |
> |
_logger.write(toString(), Logger.DEBUG, "possible errenous packet data, should be double value - " + attributeString); |
106 |
> |
} |
107 |
> |
} |
108 |
> |
} |
109 |
> |
} |
110 |
> |
return Alert.thresholdNORMAL; |
111 |
|
} |
112 |
+ |
|
113 |
+ |
/** |
114 |
+ |
* Initialises a HashMap of Registers with the current list |
115 |
+ |
* of attributes. This is only used if we are looking at more |
116 |
+ |
* than one distinct attribute. |
117 |
+ |
* |
118 |
+ |
* @param source the host we are looking at |
119 |
+ |
* @param attributeRegisters a HashMap to put the new Registers in |
120 |
+ |
*/ |
121 |
+ |
private void initAttributeRegsiters(String source, HashMap attributeRegisters) { |
122 |
+ |
for(int attributeNum = 0; attributeNum < _attributes.length; attributeNum++) { |
123 |
+ |
String attributeName = _attributes[attributeNum].substring(_attributes[attributeNum].lastIndexOf(".") + 1); |
124 |
+ |
attributeRegisters.put(_attributes[attributeNum], new Register(source, _name, attributeName)); |
125 |
+ |
} |
126 |
+ |
} |
127 |
|
|
128 |
|
//---ACCESSOR/MUTATOR METHODS--- |
129 |
+ |
|
130 |
+ |
/** |
131 |
+ |
* Returns a reference to a specific Queue for this |
132 |
+ |
* monitor. This Queue returns only the data packets |
133 |
+ |
* (based on type) that we want too look at. |
134 |
+ |
* |
135 |
+ |
* @return a reference to a Queue |
136 |
+ |
*/ |
137 |
+ |
protected Queue getQueue() { |
138 |
+ |
return MonitorManager.getInstance().getDataQueue(); |
139 |
+ |
} |
140 |
|
|
141 |
|
//---ATTRIBUTES--- |
142 |
|
|
149 |
|
* can be placed here. This name could also |
150 |
|
* be changed to null for utility classes. |
151 |
|
*/ |
152 |
< |
private String _name = ClientMain.NAME; |
153 |
< |
|
152 |
> |
private String _name = "CPU"; |
153 |
> |
|
154 |
|
/** |
155 |
< |
* This holds a reference to the |
156 |
< |
* system logger that is being used. |
155 |
> |
* A HashMap of Registers (or groups of Registers), one |
156 |
> |
* for each host we're monitoring. |
157 |
|
*/ |
158 |
< |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
158 |
> |
private HashMap _hosts = new HashMap(); |
159 |
|
|
160 |
< |
private double[] _levels; |
161 |
< |
|
162 |
< |
private Queue _alerterQueue; |
160 |
> |
/** |
161 |
> |
* An array of attributes which we will be checking. |
162 |
> |
*/ |
163 |
> |
private String[] _attributes = { "packet.cpu.user", "packet.cpu.kernel", "packet.cpu.iowait", "packet.cpu.swap" }; |
164 |
|
|
165 |
|
/** |
166 |
< |
* A reference to the reference manager in use |
166 |
> |
* An array of "nice names" for the attributes in _attributes. |
167 |
|
*/ |
168 |
< |
private ReferenceManager _refman = ReferenceManager.getInstance(); |
168 |
> |
private String[] _attributeNames = {"User CPU", "Kernel CPU", "I/O Wait CPU", "Swap CPU"}; |
169 |
|
|
170 |
|
//---STATIC ATTRIBUTES--- |
171 |
|
|