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 extends MonitorSkeleton { |
18 |
|
|
19 |
|
//---FINAL ATTRIBUTES--- |
20 |
|
|
29 |
|
|
30 |
|
//---CONSTRUCTORS--- |
31 |
|
|
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 |
– |
_logger.write(toString(), Logger.ERROR, "Invalid configured value for alert level " + x + " - ignoring level."); |
41 |
– |
_levels[x] = -1; |
42 |
– |
} catch (org.omg.CORBA.MARSHAL e2) { |
43 |
– |
_logger.write(toString(), Logger.WARNING, "Alert level " + x + " unused."); |
44 |
– |
_levels[x] = -1; |
45 |
– |
} |
46 |
– |
|
47 |
– |
} |
48 |
– |
} |
32 |
|
//---PUBLIC METHODS--- |
33 |
|
|
34 |
|
public void analysePacket(XMLPacket packet) { |
35 |
< |
String source = packet.getParam("packet.attributes.hostname"); |
36 |
< |
for(int x=0; x < _levels.length; x++) { |
37 |
< |
if (_levels[x] != -1) { |
38 |
< |
double idle = Double.parseDouble(packet.getParam("packet.cpu.idle")); |
56 |
< |
if (_levels[x] < idle) fireAlert(x, source, idle, "idle"); |
57 |
< |
double user = Double.parseDouble(packet.getParam("packet.cpu.user")); |
58 |
< |
if (_levels[x] < user) fireAlert(x, source, user, "user"); |
59 |
< |
double kernel = Double.parseDouble(packet.getParam("packet.cpu.kernel")); |
60 |
< |
if (_levels[x] < kernel) fireAlert(x, source, kernel, "kernel"); |
61 |
< |
double iowait = Double.parseDouble(packet.getParam("packet.cpu.iowait")); |
62 |
< |
if (_levels[x] < iowait) fireAlert(x, source, iowait, "iowait"); |
63 |
< |
double swap = Double.parseDouble(packet.getParam("packet.cpu.swap")); |
64 |
< |
if (_levels[x] < swap) fireAlert(x, source, swap, "swap"); |
35 |
> |
if (packet.getParam("packet.attributes.type").equals("data")) { |
36 |
> |
String source = packet.getParam("packet.attributes.machine_name"); |
37 |
> |
if (!_hosts.containsKey(source)) { |
38 |
> |
_hosts.put(source, new Register(source, _name, _attributes.length)); |
39 |
|
} |
40 |
+ |
|
41 |
+ |
Register reg = (Register) _hosts.get(source); |
42 |
+ |
for(int attributeNum = 0; attributeNum < _attributes.length; attributeNum++) { |
43 |
+ |
// find out the threshold level we're at |
44 |
+ |
String attribute = _attributes[attributeNum]; |
45 |
+ |
String attributeName = _attributeNames[attributeNum]; |
46 |
+ |
String currentValue = packet.getParam(attribute); |
47 |
+ |
int newThreshold = checkAttributeThreshold(currentValue, reg); |
48 |
+ |
processAlert(newThreshold, attributeNum, attributeName, reg, source, currentValue); |
49 |
+ |
} |
50 |
|
} |
51 |
|
} |
52 |
|
|
67 |
|
} |
68 |
|
|
69 |
|
/** |
70 |
< |
* return the String representation of what the filter does |
70 |
> |
* return the String representation of what the monitor does |
71 |
|
*/ |
72 |
|
public String getDescription(){ |
73 |
|
return DESC; |
75 |
|
|
76 |
|
//---PRIVATE METHODS--- |
77 |
|
|
78 |
< |
private void fireAlert(int alertLevel, String source, double currentValue, String type) { |
79 |
< |
String value = Double.toString(currentValue); |
80 |
< |
String thresholdValue = Double.toString(_levels[alertLevel]); |
81 |
< |
String attributeName = "CPU " + type; |
82 |
< |
Alert alert = new Alert(alertLevel, source, thresholdValue, value, attributeName); |
83 |
< |
_alerterQueue.add(alert); |
78 |
> |
private int checkAttributeThreshold(String attributeString, Register reg) { |
79 |
> |
for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) { |
80 |
> |
if (reg.getThreshold(thresholdLevel) != -1.0) { |
81 |
> |
if(attributeString != null) { |
82 |
> |
try { |
83 |
> |
double attribute = Double.parseDouble(attributeString); |
84 |
> |
if (reg.getThreshold(thresholdLevel) < attribute) return thresholdLevel; |
85 |
> |
} catch (NumberFormatException e) { |
86 |
> |
// we got some duff data in the packet, but we shouldn't have |
87 |
> |
_logger.write(toString(), Logger.DEBUG, "possible errenous packet data, should be double value - " + attributeString); |
88 |
> |
} |
89 |
> |
} |
90 |
> |
} |
91 |
> |
} |
92 |
> |
return Alert.thresholdNORMAL; |
93 |
|
} |
94 |
|
|
95 |
|
//---ACCESSOR/MUTATOR METHODS--- |
105 |
|
* can be placed here. This name could also |
106 |
|
* be changed to null for utility classes. |
107 |
|
*/ |
108 |
< |
private String _name = ClientMain.NAME; |
116 |
< |
|
117 |
< |
/** |
118 |
< |
* This holds a reference to the |
119 |
< |
* system logger that is being used. |
120 |
< |
*/ |
121 |
< |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
108 |
> |
private String _name = "CPU"; |
109 |
|
|
123 |
– |
private double[] _levels; |
124 |
– |
|
125 |
– |
private Queue _alerterQueue; |
126 |
– |
|
110 |
|
/** |
111 |
< |
* A reference to the reference manager in use |
111 |
> |
* A reference to the configuration proxy in use |
112 |
|
*/ |
113 |
< |
private ReferenceManager _refman = ReferenceManager.getInstance(); |
113 |
> |
private ConfigurationProxy _cp = ConfigurationProxy.getInstance(); |
114 |
> |
|
115 |
> |
private HashMap _hosts = new HashMap(); |
116 |
> |
|
117 |
> |
private String[] _attributes = { "packet.cpu.user", "packet.cpu.kernel", "packet.cpu.iowait", "packet.cpu.swap" }; |
118 |
> |
private String[] _attributeNames = {"User CPU", "Kernel CPU", "I/O Wait CPU", "Swap CPU"}; |
119 |
|
|
120 |
|
//---STATIC ATTRIBUTES--- |
121 |
|
|