1 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
|
|
package uk.ac.ukc.iscream.client.monitors; |
3 |
|
|
|
4 |
|
|
//---IMPORTS--- |
5 |
ajm |
1.9 |
import java.util.HashMap; |
6 |
tdb |
1.1 |
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 |
ajm |
1.17 |
* @author $Author: tdb1 $ |
15 |
|
|
* @version $Id: CPU__Monitor.java,v 1.16 2001/03/06 22:35:01 tdb1 Exp $ |
16 |
tdb |
1.1 |
*/ |
17 |
tdb |
1.16 |
public class CPU__Monitor extends MonitorSkeleton { |
18 |
tdb |
1.1 |
|
19 |
|
|
//---FINAL ATTRIBUTES--- |
20 |
|
|
|
21 |
|
|
/** |
22 |
|
|
* The current CVS revision of this class |
23 |
|
|
*/ |
24 |
ajm |
1.17 |
public final String REVISION = "$Revision: 1.16 $"; |
25 |
tdb |
1.1 |
|
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 |
ajm |
1.7 |
if (packet.getParam("packet.attributes.type").equals("data")) { |
36 |
tdb |
1.8 |
String source = packet.getParam("packet.attributes.machine_name"); |
37 |
ajm |
1.9 |
if (!_hosts.containsKey(source)) { |
38 |
ajm |
1.11 |
_hosts.put(source, new Register(source, _name, _attributes.length)); |
39 |
ajm |
1.2 |
} |
40 |
ajm |
1.9 |
|
41 |
|
|
Register reg = (Register) _hosts.get(source); |
42 |
ajm |
1.11 |
for(int attributeNum = 0; attributeNum < _attributes.length; attributeNum++) { |
43 |
|
|
// find out the threshold level we're at |
44 |
ajm |
1.17 |
String attribute = _attributes[attributeNum]; |
45 |
|
|
String attributeName = _attributeNames[attributeNum]; |
46 |
|
|
String currentValue = packet.getParam(attribute); |
47 |
tdb |
1.16 |
int newThreshold = checkAttributeThreshold(currentValue, reg); |
48 |
|
|
processAlert(newThreshold, attributeNum, attributeName, reg, source, currentValue); |
49 |
ajm |
1.11 |
} |
50 |
ajm |
1.2 |
} |
51 |
tdb |
1.1 |
} |
52 |
|
|
|
53 |
|
|
/** |
54 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
55 |
|
|
* method to provide clean logging (every class should have this). |
56 |
|
|
* |
57 |
|
|
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
58 |
|
|
* to format the toString() |
59 |
|
|
* |
60 |
|
|
* @return the name of this class and its CVS revision |
61 |
|
|
*/ |
62 |
|
|
public String toString() { |
63 |
|
|
return FormatName.getName( |
64 |
|
|
_name, |
65 |
|
|
getClass().getName(), |
66 |
|
|
REVISION); |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
/** |
70 |
ajm |
1.11 |
* return the String representation of what the monitor does |
71 |
tdb |
1.1 |
*/ |
72 |
|
|
public String getDescription(){ |
73 |
|
|
return DESC; |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
//---PRIVATE METHODS--- |
77 |
|
|
|
78 |
ajm |
1.11 |
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 |
ajm |
1.9 |
} |
90 |
|
|
} |
91 |
|
|
} |
92 |
tdb |
1.16 |
return Alert.thresholdNORMAL; |
93 |
ajm |
1.2 |
} |
94 |
|
|
|
95 |
tdb |
1.1 |
//---ACCESSOR/MUTATOR METHODS--- |
96 |
|
|
|
97 |
|
|
//---ATTRIBUTES--- |
98 |
|
|
|
99 |
|
|
/** |
100 |
|
|
* This is the friendly identifier of the |
101 |
|
|
* component this class is running in. |
102 |
|
|
* eg, a Filter may be called "filter1", |
103 |
|
|
* If this class does not have an owning |
104 |
|
|
* component, a name from the configuration |
105 |
|
|
* can be placed here. This name could also |
106 |
|
|
* be changed to null for utility classes. |
107 |
|
|
*/ |
108 |
ajm |
1.11 |
private String _name = "CPU"; |
109 |
ajm |
1.2 |
|
110 |
|
|
/** |
111 |
ajm |
1.11 |
* A reference to the configuration proxy in use |
112 |
ajm |
1.2 |
*/ |
113 |
ajm |
1.11 |
private ConfigurationProxy _cp = ConfigurationProxy.getInstance(); |
114 |
ajm |
1.9 |
|
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 |
tdb |
1.1 |
|
120 |
|
|
//---STATIC ATTRIBUTES--- |
121 |
|
|
|
122 |
|
|
} |