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.9 2001/03/02 03:57:06 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.9 $"; |
25 |
|
26 |
public final String DESC = "Monitors CPU."; |
27 |
|
28 |
//---STATIC METHODS--- |
29 |
|
30 |
//---CONSTRUCTORS--- |
31 |
|
32 |
public CPU__Monitor() { |
33 |
_alerterQueue = ClientMain._alerterQueue; |
34 |
// get the configuration for this plug-in |
35 |
Configuration config = _refman.getCM().getConfiguration(_name); |
36 |
_levels = new double[(Alert.alerts).length]; |
37 |
for (int x = 0; x < Alert.alerts.length; x++) { |
38 |
try { |
39 |
_levels[x] = Double.parseDouble(config.getProperty("Monitor.CPU.level." + x)); |
40 |
} catch (NumberFormatException e) { |
41 |
_logger.write(toString(), Logger.ERROR, "Invalid configured value for alert level " + x + " - ignoring level."); |
42 |
_levels[x] = -1; |
43 |
} catch (org.omg.CORBA.MARSHAL e2) { |
44 |
_logger.write(toString(), Logger.WARNING, "Alert level " + x + " unused."); |
45 |
_levels[x] = -1; |
46 |
} |
47 |
|
48 |
} |
49 |
} |
50 |
|
51 |
//---PUBLIC METHODS--- |
52 |
|
53 |
public void analysePacket(XMLPacket packet) { |
54 |
if (packet.getParam("packet.attributes.type").equals("data")) { |
55 |
String source = packet.getParam("packet.attributes.machine_name"); |
56 |
if (!_hosts.containsKey(source)) { |
57 |
_hosts.put(source, new Register(_attributes.length)); |
58 |
} |
59 |
|
60 |
Register reg = (Register) _hosts.get(source); |
61 |
for(int x = 0; x < _attributes.length; x++) { |
62 |
int result = checkAttribute(packet, _attributes[x]); |
63 |
if (result != reg.getRegister(x)) { |
64 |
reg.setRegister(x, result); |
65 |
fireAlert(result, source, packet.getParam(_attributes[x]), _attributeNames[x]); |
66 |
} |
67 |
} |
68 |
} |
69 |
} |
70 |
|
71 |
/** |
72 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
73 |
* method to provide clean logging (every class should have this). |
74 |
* |
75 |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
76 |
* to format the toString() |
77 |
* |
78 |
* @return the name of this class and its CVS revision |
79 |
*/ |
80 |
public String toString() { |
81 |
return FormatName.getName( |
82 |
_name, |
83 |
getClass().getName(), |
84 |
REVISION); |
85 |
} |
86 |
|
87 |
/** |
88 |
* return the String representation of what the filter does |
89 |
*/ |
90 |
public String getDescription(){ |
91 |
return DESC; |
92 |
} |
93 |
|
94 |
//---PRIVATE METHODS--- |
95 |
|
96 |
private int checkAttribute(XMLPacket packet, String packetAttribute) { |
97 |
for(int x = _levels.length - 1; x >= 0; x--) { |
98 |
if (_levels[x] != -1) { |
99 |
if(packet.getParam(packetAttribute) != null) { |
100 |
double attribute = Double.parseDouble(packet.getParam(packetAttribute)); |
101 |
if (_levels[x] < attribute) return x; |
102 |
} |
103 |
} |
104 |
} |
105 |
return 0; |
106 |
} |
107 |
|
108 |
private void fireAlert(int alertLevel, String source, String currentValue, String attributeName) { |
109 |
String thresholdValue = Double.toString(_levels[alertLevel]); |
110 |
Alert alert = new Alert(alertLevel, source, thresholdValue, currentValue, attributeName); |
111 |
_alerterQueue.add(alert); |
112 |
_logger.write(toString(), Logger.DEBUG, "Fired alert for source:" + source + " at alert level:" + alertLevel + " on:" + attributeName + " at:" + currentValue + " exceeding threshold:" + _levels[alertLevel]); |
113 |
} |
114 |
|
115 |
//---ACCESSOR/MUTATOR METHODS--- |
116 |
|
117 |
//---ATTRIBUTES--- |
118 |
|
119 |
/** |
120 |
* This is the friendly identifier of the |
121 |
* component this class is running in. |
122 |
* eg, a Filter may be called "filter1", |
123 |
* If this class does not have an owning |
124 |
* component, a name from the configuration |
125 |
* can be placed here. This name could also |
126 |
* be changed to null for utility classes. |
127 |
*/ |
128 |
private String _name = ClientMain.NAME; |
129 |
|
130 |
/** |
131 |
* This holds a reference to the |
132 |
* system logger that is being used. |
133 |
*/ |
134 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
135 |
|
136 |
private double[] _levels; |
137 |
|
138 |
private Queue _alerterQueue; |
139 |
|
140 |
/** |
141 |
* A reference to the reference manager in use |
142 |
*/ |
143 |
private ReferenceManager _refman = ReferenceManager.getInstance(); |
144 |
|
145 |
private HashMap _hosts = new HashMap(); |
146 |
|
147 |
private String[] _attributes = { "packet.cpu.user", "packet.cpu.kernel", "packet.cpu.iowait", "packet.cpu.swap" }; |
148 |
private String[] _attributeNames = {"User CPU", "Kernel CPU", "I/O Wait CPU", "Swap CPU"}; |
149 |
|
150 |
//---STATIC ATTRIBUTES--- |
151 |
|
152 |
} |