1 |
//---PACKAGE DECLARATION--- |
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.*; |
9 |
import uk.ac.ukc.iscream.componentmanager.*; |
10 |
|
11 |
/** |
12 |
* This Monitor watches the CPU load for all machines |
13 |
* |
14 |
* @author $Author: ajm4 $ |
15 |
* @version $Id: CPU__Monitor.java,v 1.14 2001/03/05 12:45:51 ajm4 Exp $ |
16 |
*/ |
17 |
public class CPU__Monitor implements PluginMonitor { |
18 |
|
19 |
//---FINAL ATTRIBUTES--- |
20 |
|
21 |
/** |
22 |
* The current CVS revision of this class |
23 |
*/ |
24 |
public final String REVISION = "$Revision: 1.14 $"; |
25 |
|
26 |
public final String DESC = "Monitors CPU."; |
27 |
|
28 |
//---STATIC METHODS--- |
29 |
|
30 |
//---CONSTRUCTORS--- |
31 |
|
32 |
//---PUBLIC METHODS--- |
33 |
|
34 |
public void analysePacket(XMLPacket packet) { |
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 |
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 |
} |
91 |
} |
92 |
} |
93 |
|
94 |
/** |
95 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
96 |
* method to provide clean logging (every class should have this). |
97 |
* |
98 |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
99 |
* to format the toString() |
100 |
* |
101 |
* @return the name of this class and its CVS revision |
102 |
*/ |
103 |
public String toString() { |
104 |
return FormatName.getName( |
105 |
_name, |
106 |
getClass().getName(), |
107 |
REVISION); |
108 |
} |
109 |
|
110 |
/** |
111 |
* return the String representation of what the monitor does |
112 |
*/ |
113 |
public String getDescription(){ |
114 |
return DESC; |
115 |
} |
116 |
|
117 |
//---PRIVATE METHODS--- |
118 |
|
119 |
private int checkAttributeThreshold(String attributeString, Register reg) { |
120 |
for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) { |
121 |
if (reg.getThreshold(thresholdLevel) != -1.0) { |
122 |
if(attributeString != null) { |
123 |
try { |
124 |
double attribute = Double.parseDouble(attributeString); |
125 |
if (reg.getThreshold(thresholdLevel) < attribute) return thresholdLevel; |
126 |
} catch (NumberFormatException e) { |
127 |
// we got some duff data in the packet, but we shouldn't have |
128 |
_logger.write(toString(), Logger.DEBUG, "possible errenous packet data, should be double value - " + attributeString); |
129 |
} |
130 |
} |
131 |
} |
132 |
} |
133 |
return 0; |
134 |
} |
135 |
|
136 |
private void fireAlert(Register reg, int lastAlert, XMLPacket packet, int attributeNum) { |
137 |
int alertLevel = reg.getLastAlertLevel(attributeNum); |
138 |
int thresholdLevel = reg.getLastThresholdLevel(attributeNum); |
139 |
String source = packet.getParam("packet.attributes.machine_name"); |
140 |
String currentValue = packet.getParam(_attributes[attributeNum]); |
141 |
String attributeName = _attributeNames[attributeNum]; |
142 |
String thresholdValue = Double.toString(reg.getThreshold(thresholdLevel)); |
143 |
String timeout = Long.toString(reg.getAlertTimeout(reg.getLastAlertLevel(attributeNum), attributeNum) / 1000); |
144 |
if (thresholdLevel == Alert.thresholdNORMAL) { |
145 |
thresholdValue = "-"; |
146 |
} |
147 |
if (alertLevel == Alert.alertOK) { |
148 |
timeout = "0"; |
149 |
} |
150 |
Alert alert = new Alert(alertLevel, lastAlert, thresholdLevel, source, thresholdValue, currentValue, attributeName, timeout, reg.getInitialAlertTime(attributeNum)); |
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:" + timeout + "secs"); |
153 |
} |
154 |
|
155 |
//---ACCESSOR/MUTATOR METHODS--- |
156 |
|
157 |
//---ATTRIBUTES--- |
158 |
|
159 |
/** |
160 |
* This is the friendly identifier of the |
161 |
* component this class is running in. |
162 |
* eg, a Filter may be called "filter1", |
163 |
* If this class does not have an owning |
164 |
* component, a name from the configuration |
165 |
* can be placed here. This name could also |
166 |
* be changed to null for utility classes. |
167 |
*/ |
168 |
private String _name = "CPU"; |
169 |
|
170 |
/** |
171 |
* This holds a reference to the |
172 |
* system logger that is being used. |
173 |
*/ |
174 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
175 |
|
176 |
private Queue _alerterQueue = ClientMain._alerterQueue; |
177 |
|
178 |
/** |
179 |
* A reference to the configuration proxy in use |
180 |
*/ |
181 |
private ConfigurationProxy _cp = ConfigurationProxy.getInstance(); |
182 |
|
183 |
private HashMap _hosts = new HashMap(); |
184 |
|
185 |
private String[] _attributes = { "packet.cpu.user", "packet.cpu.kernel", "packet.cpu.iowait", "packet.cpu.swap" }; |
186 |
private String[] _attributeNames = {"User CPU", "Kernel CPU", "I/O Wait CPU", "Swap CPU"}; |
187 |
|
188 |
//---STATIC ATTRIBUTES--- |
189 |
|
190 |
} |