1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.ac.ukc.iscream.client.monitors; |
2 |
> |
package uk.org.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.*; |
9 |
< |
import uk.ac.ukc.iscream.componentmanager.*; |
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 PluginMonitor { |
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--- |
33 |
|
//---CONSTRUCTORS--- |
34 |
|
|
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 |
< |
if (packet.getParam("packet.attributes.type").equals("data")) { |
45 |
< |
String source = packet.getParam("packet.attributes.machine_name"); |
46 |
< |
if (!_hosts.containsKey(source)) { |
47 |
< |
_hosts.put(source, new Register(source, _name, _attributes.length)); |
48 |
< |
} |
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 |
< |
int result = checkAttributeThreshold(packet.getParam(_attributes[attributeNum]), reg); |
45 |
< |
|
46 |
< |
// decide what threshold level we're on, if we've changed, record that |
47 |
< |
if (result != reg.getLastThresholdLevel(attributeNum)) { |
48 |
< |
reg.setLastThresholdLevel(attributeNum, result); |
49 |
< |
} |
50 |
< |
// as long as this isn't a normal level |
51 |
< |
if(reg.getLastThresholdLevel(attributeNum) != Alert.thresholdNORMAL) { |
52 |
< |
// if the time since the last alert is more than the time for |
53 |
< |
// its timeout, fire an alert, escalate the alert |
54 |
< |
long timeout = reg.getLastAlertTimeout(attributeNum); |
55 |
< |
if ((timeout > 0) && (reg.getTimeLastSent(attributeNum) > 0)) { |
56 |
< |
if((System.currentTimeMillis() - reg.getTimeLastSent(attributeNum)) > timeout) { |
57 |
< |
int lastAlert = reg.getLastAlertLevel(attributeNum); |
58 |
< |
reg.escalateAlert(attributeNum); |
59 |
< |
reg.setTimeLastSent(attributeNum, System.currentTimeMillis()); |
60 |
< |
reg.setLastAlertTimeout(attributeNum, reg.getAlertTimeout(reg.getLastAlertLevel(attributeNum), attributeNum)); |
61 |
< |
fireAlert(reg, lastAlert, packet, attributeNum); |
62 |
< |
} |
63 |
< |
// if we don't have a timeout configured...we got STRAIGHT to the next level |
64 |
< |
} else { |
65 |
< |
int lastAlert = reg.getLastAlertLevel(attributeNum); |
66 |
< |
reg.escalateAlert(attributeNum); |
67 |
< |
reg.setTimeLastSent(attributeNum, System.currentTimeMillis()); |
68 |
< |
reg.setLastAlertTimeout(attributeNum, reg.getAlertTimeout(reg.getLastAlertLevel(attributeNum), attributeNum)); |
69 |
< |
fireAlert(reg, lastAlert, packet, attributeNum); |
70 |
< |
} |
71 |
< |
|
72 |
< |
// we must be on ok, check the timeout value for this |
73 |
< |
} else { |
74 |
< |
// if we were on an OK alert before, then we don't do anything |
75 |
< |
// but if we weren't we only set OK, once the timout of the last |
76 |
< |
// alert has occourd |
77 |
< |
if (reg.getLastAlertLevel(attributeNum) != Alert.alertOK) { |
78 |
< |
long timeout = reg.getLastAlertTimeout(attributeNum); |
79 |
< |
if ((timeout > 0) && (reg.getTimeLastSent(attributeNum) > 0)) { |
80 |
< |
if ((System.currentTimeMillis() - reg.getTimeLastSent(attributeNum)) > timeout) { |
81 |
< |
int lastAlert = reg.getLastAlertLevel(attributeNum); |
82 |
< |
reg.setLastAlertLevel(attributeNum, Alert.alertOK); |
83 |
< |
reg.setTimeLastSent(attributeNum, System.currentTimeMillis()); |
84 |
< |
reg.setLastAlertTimeout(attributeNum, timeout); |
85 |
< |
fireAlert(reg, lastAlert, packet, attributeNum); |
86 |
< |
} |
87 |
< |
} |
88 |
< |
} |
89 |
< |
} |
90 |
< |
} |
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 |
84 |
|
} |
85 |
|
|
86 |
|
//---PRIVATE METHODS--- |
87 |
< |
|
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) { |
107 |
|
} |
108 |
|
} |
109 |
|
} |
110 |
< |
return 0; |
110 |
> |
return Alert.thresholdNORMAL; |
111 |
|
} |
112 |
< |
|
113 |
< |
private void fireAlert(Register reg, int lastAlert, XMLPacket packet, int attributeNum) { |
114 |
< |
int alertLevel = reg.getLastAlertLevel(attributeNum); |
115 |
< |
int thresholdLevel = reg.getLastThresholdLevel(attributeNum); |
116 |
< |
String source = packet.getParam("packet.attributes.machine_name"); |
117 |
< |
String currentValue = packet.getParam(_attributes[attributeNum]); |
118 |
< |
String attributeName = _attributeNames[attributeNum]; |
119 |
< |
String thresholdValue = Double.toString(reg.getThreshold(thresholdLevel)); |
120 |
< |
String time = Long.toString(reg.getAlertTimeout(reg.getLastAlertLevel(attributeNum), attributeNum) / 1000); |
121 |
< |
if (thresholdLevel == Alert.thresholdNORMAL) { |
122 |
< |
thresholdValue = "-"; |
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 |
|
} |
147 |
– |
if (alertLevel == Alert.alertOK) { |
148 |
– |
time = "0"; |
149 |
– |
} |
150 |
– |
Alert alert = new Alert(alertLevel, lastAlert, thresholdLevel, source, thresholdValue, currentValue, attributeName, time); |
151 |
– |
_alerterQueue.add(alert); |
152 |
– |
_logger.write(toString(), Logger.DEBUG, "Fired alert for source:" + source + " at alert level:" + Alert.alertLevels[alertLevel] + " on:" + attributeName + " for threshold level:" + Alert.thresholdLevels[thresholdLevel] + " at:" + currentValue + " exceeding threshold of:" +thresholdValue + " next alert sent in:" + time + "secs"); |
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 |
|
|
150 |
|
* be changed to null for utility classes. |
151 |
|
*/ |
152 |
|
private String _name = "CPU"; |
153 |
< |
|
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 |
|
|
176 |
– |
private Queue _alerterQueue = ClientMain._alerterQueue; |
177 |
– |
|
160 |
|
/** |
161 |
< |
* A reference to the configuration proxy in use |
161 |
> |
* An array of attributes which we will be checking. |
162 |
|
*/ |
181 |
– |
private ConfigurationProxy _cp = ConfigurationProxy.getInstance(); |
182 |
– |
|
183 |
– |
private HashMap _hosts = new HashMap(); |
184 |
– |
|
163 |
|
private String[] _attributes = { "packet.cpu.user", "packet.cpu.kernel", "packet.cpu.iowait", "packet.cpu.swap" }; |
164 |
+ |
|
165 |
+ |
/** |
166 |
+ |
* An array of "nice names" for the attributes in _attributes. |
167 |
+ |
*/ |
168 |
|
private String[] _attributeNames = {"User CPU", "Kernel CPU", "I/O Wait CPU", "Swap CPU"}; |
169 |
|
|
170 |
|
//---STATIC ATTRIBUTES--- |