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.*; |
47 |
|
|
48 |
|
} |
49 |
|
} |
50 |
+ |
|
51 |
|
//---PUBLIC METHODS--- |
52 |
|
|
53 |
|
public void analysePacket(XMLPacket packet) { |
54 |
|
if (packet.getParam("packet.attributes.type").equals("data")) { |
55 |
|
String source = packet.getParam("packet.attributes.machine_name"); |
56 |
< |
for(int x=0; x < _levels.length; x++) { |
57 |
< |
if (_levels[x] != -1) { |
56 |
< |
if(packet.getParam("packet.cpu.idle") != null) { |
57 |
< |
double idle = Double.parseDouble(packet.getParam("packet.cpu.idle")); |
58 |
< |
if (_levels[x] < idle) fireAlert(x, source, idle, "idle"); |
59 |
< |
} |
60 |
< |
|
61 |
< |
if(packet.getParam("packet.cpu.user") != null) { |
62 |
< |
double user = Double.parseDouble(packet.getParam("packet.cpu.user")); |
63 |
< |
if (_levels[x] < user) fireAlert(x, source, user, "user"); |
64 |
< |
} |
65 |
< |
|
66 |
< |
if(packet.getParam("packet.cpu.kernel") != null) { |
67 |
< |
double kernel = Double.parseDouble(packet.getParam("packet.cpu.kernel")); |
68 |
< |
if (_levels[x] < kernel) fireAlert(x, source, kernel, "kernel"); |
69 |
< |
} |
70 |
< |
|
71 |
< |
if(packet.getParam("packet.cpu.iowait") != null) { |
72 |
< |
double iowait = Double.parseDouble(packet.getParam("packet.cpu.iowait")); |
73 |
< |
if (_levels[x] < iowait) fireAlert(x, source, iowait, "iowait"); |
74 |
< |
} |
75 |
< |
|
76 |
< |
if(packet.getParam("packet.cpu.swap") != null) { |
77 |
< |
double swap = Double.parseDouble(packet.getParam("packet.cpu.swap")); |
78 |
< |
if (_levels[x] < swap) fireAlert(x, source, swap, "swap"); |
79 |
< |
} |
80 |
< |
} |
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, String source, 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; |
114 |
< |
Alert alert = new Alert(alertLevel, source, 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:" + type + " at:" + currentValue + " exceeding threshold:" + _levels[alertLevel]); |
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 |
|
|