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.*; |
14 |
|
* @author $Author$ |
15 |
|
* @version $Id$ |
16 |
|
*/ |
17 |
< |
public class CPU__Monitor implements Monitor { |
17 |
> |
public class CPU__Monitor implements PluginMonitor { |
18 |
|
|
19 |
|
//---FINAL ATTRIBUTES--- |
20 |
|
|
38 |
|
try { |
39 |
|
_levels[x] = Double.parseDouble(config.getProperty("Monitor.CPU.level." + x)); |
40 |
|
} catch (NumberFormatException e) { |
41 |
+ |
_logger.write(toString(), Logger.ERROR, "Invalid configured value for alert level " + x + " - ignoring level."); |
42 |
|
_levels[x] = -1; |
43 |
+ |
} catch (org.omg.CORBA.MARSHAL e2) { |
44 |
+ |
_logger.write(toString(), Logger.WARNING, "Alert level " + x + " unused."); |
45 |
+ |
_levels[x] = -1; |
46 |
|
} |
47 |
+ |
|
48 |
|
} |
49 |
|
} |
50 |
+ |
|
51 |
|
//---PUBLIC METHODS--- |
52 |
|
|
53 |
|
public void analysePacket(XMLPacket packet) { |
54 |
< |
for(int x=0; x < _levels.length; x++) { |
55 |
< |
if (_levels[x] != -1) { |
56 |
< |
double idle = Double.parseDouble(packet.getParam("packet.cpu.idle")); |
57 |
< |
if (_levels[x] < idle) fireAlert(x, idle, "idle"); |
51 |
< |
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"); |
54 |
> |
if (packet.getParam("packet.attributes.type").equals("data")) { |
55 |
> |
String source = packet.getParam("packet.attributes.machine_name"); |
56 |
> |
if (!_hosts.containsKey(source)) { |
57 |
> |
_hosts.put(source, new Register(_attributes.length)); |
58 |
|
} |
59 |
+ |
|
60 |
+ |
Register reg = (Register) _hosts.get(source); |
61 |
+ |
for(int x = 0; x < _attributes.length; x++) { |
62 |
+ |
int result = checkAttribute(packet, _attributes[x]); |
63 |
+ |
if (result != reg.getRegister(x)) { |
64 |
+ |
reg.setRegister(x, result); |
65 |
+ |
fireAlert(result, source, packet.getParam(_attributes[x]), _attributeNames[x]); |
66 |
+ |
} |
67 |
+ |
} |
68 |
|
} |
69 |
|
} |
70 |
|
|
93 |
|
|
94 |
|
//---PRIVATE METHODS--- |
95 |
|
|
96 |
< |
private void fireAlert(int alertLevel, double currentValue, String type) { |
97 |
< |
String value = Double.toString(currentValue); |
96 |
> |
private int checkAttribute(XMLPacket packet, String packetAttribute) { |
97 |
> |
for(int x= _levels.length; x > 0; x--) { |
98 |
> |
if (_levels[x] != -1) { |
99 |
> |
if(packet.getParam(packetAttribute) != null) { |
100 |
> |
double attribute = Double.parseDouble(packet.getParam(packetAttribute)); |
101 |
> |
if (_levels[x] < attribute) return x; |
102 |
> |
} |
103 |
> |
} |
104 |
> |
} |
105 |
> |
return 0; |
106 |
> |
} |
107 |
> |
|
108 |
> |
private void fireAlert(int alertLevel, String source, String currentValue, String attributeName) { |
109 |
|
String thresholdValue = Double.toString(_levels[alertLevel]); |
110 |
< |
String attributeName = "CPU " + type; |
92 |
< |
Alert alert = new Alert(alertLevel, thresholdValue, value, attributeName); |
110 |
> |
Alert alert = new Alert(alertLevel, source, thresholdValue, currentValue, attributeName); |
111 |
|
_alerterQueue.add(alert); |
112 |
+ |
_logger.write(toString(), Logger.DEBUG, "Fired alert for source:" + source + " at alert level:" + alertLevel + " on:" + attributeName + " at:" + currentValue + " exceeding threshold:" + _levels[alertLevel]); |
113 |
|
} |
114 |
|
|
115 |
|
//---ACCESSOR/MUTATOR METHODS--- |
141 |
|
* A reference to the reference manager in use |
142 |
|
*/ |
143 |
|
private ReferenceManager _refman = ReferenceManager.getInstance(); |
144 |
+ |
|
145 |
+ |
private HashMap _hosts = new HashMap(); |
146 |
+ |
|
147 |
+ |
private String[] _attributes = { "packet.cpu.user", "packet.cpu.kernel", "packet.cpu.iowait", "packet.cpu.swap" }; |
148 |
+ |
private String[] _attributeNames = {"User CPU", "Kernel CPU", "I/O Wait CPU", "Swap CPU"}; |
149 |
|
|
150 |
|
//---STATIC ATTRIBUTES--- |
151 |
|
|