| 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 |
|
|
| 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 |
– |
_levels[x] = -1; |
| 41 |
– |
} catch (org.omg.CORBA.MARSHAL e2) { |
| 42 |
– |
_levels[x] = -1; |
| 43 |
– |
} |
| 44 |
– |
|
| 45 |
– |
} |
| 46 |
– |
} |
| 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")); |
| 54 |
< |
if (_levels[x] < idle) fireAlert(x, source, idle, "idle"); |
| 55 |
< |
double user = Double.parseDouble(packet.getParam("packet.cpu.user")); |
| 56 |
< |
if (_levels[x] < user) fireAlert(x, source, user, "user"); |
| 57 |
< |
double kernel = Double.parseDouble(packet.getParam("packet.cpu.kernel")); |
| 58 |
< |
if (_levels[x] < kernel) fireAlert(x, source, kernel, "kernel"); |
| 59 |
< |
double iowait = Double.parseDouble(packet.getParam("packet.cpu.iowait")); |
| 60 |
< |
if (_levels[x] < iowait) fireAlert(x, source, iowait, "iowait"); |
| 61 |
< |
double swap = Double.parseDouble(packet.getParam("packet.cpu.swap")); |
| 62 |
< |
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 |
+ |
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.getAlertTimeout(reg.getLastAlertLevel(attributeNum), attributeNum); |
| 55 |
+ |
if ((timeout > 0) && (reg.getTimeLastSent(attributeNum) > 0)) { |
| 56 |
+ |
if((System.currentTimeMillis() - reg.getTimeLastSent(attributeNum)) > timeout) { |
| 57 |
+ |
reg.escalateAlert(attributeNum); |
| 58 |
+ |
reg.setTimeLastSent(attributeNum, System.currentTimeMillis()); |
| 59 |
+ |
fireAlert(reg, packet, attributeNum); |
| 60 |
+ |
} |
| 61 |
+ |
// if we don't have a timeout configured...we got STRAIGHT to the next level |
| 62 |
+ |
} else { |
| 63 |
+ |
reg.escalateAlert(attributeNum); |
| 64 |
+ |
reg.setTimeLastSent(attributeNum, System.currentTimeMillis()); |
| 65 |
+ |
fireAlert(reg, packet, attributeNum); |
| 66 |
+ |
} |
| 67 |
+ |
|
| 68 |
+ |
// we must be on ok, check the timeout value for this |
| 69 |
+ |
} else { |
| 70 |
+ |
// if we were on an OK alert before, then we don't do anything |
| 71 |
+ |
// but if we weren't we only set OK, once the timout of the last |
| 72 |
+ |
// alert has occourd |
| 73 |
+ |
if (reg.getLastAlertLevel(attributeNum) != Alert.alertOK) { |
| 74 |
+ |
long timeout = reg.getAlertTimeout(reg.getLastAlertLevel(attributeNum), attributeNum); |
| 75 |
+ |
if ((timeout > 0) && (reg.getTimeLastSent(attributeNum) > 0)) { |
| 76 |
+ |
if ((System.currentTimeMillis() - reg.getTimeLastSent(attributeNum)) > timeout) { |
| 77 |
+ |
reg.setLastAlertLevel(attributeNum, Alert.alertOK); |
| 78 |
+ |
reg.setTimeLastSent(attributeNum, System.currentTimeMillis()); |
| 79 |
+ |
fireAlert(reg, packet, attributeNum); |
| 80 |
+ |
} |
| 81 |
+ |
} else { |
| 82 |
+ |
reg.setLastAlertLevel(attributeNum, Alert.alertOK); |
| 83 |
+ |
reg.setTimeLastSent(attributeNum, System.currentTimeMillis()); |
| 84 |
+ |
fireAlert(reg, packet, attributeNum); |
| 85 |
+ |
} |
| 86 |
+ |
} |
| 87 |
+ |
} |
| 88 |
+ |
} |
| 89 |
|
} |
| 90 |
|
} |
| 91 |
|
|
| 106 |
|
} |
| 107 |
|
|
| 108 |
|
/** |
| 109 |
< |
* return the String representation of what the filter does |
| 109 |
> |
* return the String representation of what the monitor does |
| 110 |
|
*/ |
| 111 |
|
public String getDescription(){ |
| 112 |
|
return DESC; |
| 114 |
|
|
| 115 |
|
//---PRIVATE METHODS--- |
| 116 |
|
|
| 117 |
< |
private void fireAlert(int alertLevel, String source, double currentValue, String type) { |
| 118 |
< |
String value = Double.toString(currentValue); |
| 119 |
< |
String thresholdValue = Double.toString(_levels[alertLevel]); |
| 120 |
< |
String attributeName = "CPU " + type; |
| 121 |
< |
Alert alert = new Alert(alertLevel, source, thresholdValue, value, attributeName); |
| 117 |
> |
private int checkAttributeThreshold(String attributeString, Register reg) { |
| 118 |
> |
for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) { |
| 119 |
> |
if (reg.getThreshold(thresholdLevel) != -1.0) { |
| 120 |
> |
if(attributeString != null) { |
| 121 |
> |
try { |
| 122 |
> |
double attribute = Double.parseDouble(attributeString); |
| 123 |
> |
if (reg.getThreshold(thresholdLevel) < attribute) return thresholdLevel; |
| 124 |
> |
} catch (NumberFormatException e) { |
| 125 |
> |
// we got some duff data in the packet, but we shouldn't have |
| 126 |
> |
_logger.write(toString(), Logger.DEBUG, "possible errenous packet data, should be double value - " + attributeString); |
| 127 |
> |
} |
| 128 |
> |
} |
| 129 |
> |
} |
| 130 |
> |
} |
| 131 |
> |
return 0; |
| 132 |
> |
} |
| 133 |
> |
|
| 134 |
> |
private void fireAlert(Register reg, XMLPacket packet, int attributeNum) { |
| 135 |
> |
int alertLevel = reg.getLastAlertLevel(attributeNum); |
| 136 |
> |
int thresholdLevel = reg.getLastThresholdLevel(attributeNum); |
| 137 |
> |
String source = packet.getParam("packet.attributes.machine_name"); |
| 138 |
> |
String currentValue = packet.getParam(_attributes[attributeNum]); |
| 139 |
> |
String attributeName = _attributeNames[attributeNum]; |
| 140 |
> |
String thresholdValue = Double.toString(reg.getThreshold(thresholdLevel)); |
| 141 |
> |
String time = Long.toString(reg.getAlertTimeout(reg.getLastAlertLevel(attributeNum), attributeNum) / 1000); |
| 142 |
> |
if (thresholdLevel == Alert.thresholdNORMAL) { |
| 143 |
> |
thresholdValue = "-"; |
| 144 |
> |
} |
| 145 |
> |
if (alertLevel == Alert.alertOK) { |
| 146 |
> |
time = "0"; |
| 147 |
> |
} |
| 148 |
> |
Alert alert = new Alert(alertLevel, thresholdLevel, source, thresholdValue, currentValue, attributeName, time); |
| 149 |
|
_alerterQueue.add(alert); |
| 150 |
+ |
_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"); |
| 151 |
|
} |
| 152 |
|
|
| 153 |
|
//---ACCESSOR/MUTATOR METHODS--- |
| 163 |
|
* can be placed here. This name could also |
| 164 |
|
* be changed to null for utility classes. |
| 165 |
|
*/ |
| 166 |
< |
private String _name = ClientMain.NAME; |
| 166 |
> |
private String _name = "CPU"; |
| 167 |
|
|
| 168 |
|
/** |
| 169 |
|
* This holds a reference to the |
| 171 |
|
*/ |
| 172 |
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
| 173 |
|
|
| 174 |
< |
private double[] _levels; |
| 122 |
< |
|
| 123 |
< |
private Queue _alerterQueue; |
| 174 |
> |
private Queue _alerterQueue = ClientMain._alerterQueue; |
| 175 |
|
|
| 176 |
|
/** |
| 177 |
< |
* A reference to the reference manager in use |
| 177 |
> |
* A reference to the configuration proxy in use |
| 178 |
|
*/ |
| 179 |
< |
private ReferenceManager _refman = ReferenceManager.getInstance(); |
| 179 |
> |
private ConfigurationProxy _cp = ConfigurationProxy.getInstance(); |
| 180 |
> |
|
| 181 |
> |
private HashMap _hosts = new HashMap(); |
| 182 |
> |
|
| 183 |
> |
private String[] _attributes = { "packet.cpu.user", "packet.cpu.kernel", "packet.cpu.iowait", "packet.cpu.swap" }; |
| 184 |
> |
private String[] _attributeNames = {"User CPU", "Kernel CPU", "I/O Wait CPU", "Swap CPU"}; |
| 185 |
|
|
| 186 |
|
//---STATIC ATTRIBUTES--- |
| 187 |
|
|