1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.ac.ukc.iscream.client.monitors; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import uk.ac.ukc.iscream.client.*; |
6 |
import uk.ac.ukc.iscream.core.*; |
7 |
import uk.ac.ukc.iscream.util.*; |
8 |
import uk.ac.ukc.iscream.componentmanager.*; |
9 |
|
10 |
/** |
11 |
* This Monitor watches the CPU load for all machines |
12 |
* |
13 |
* @author $Author: ajm4 $ |
14 |
* @version $Id: CPU__Monitor.java,v 1.6 2001/03/02 01:07:04 ajm4 Exp $ |
15 |
*/ |
16 |
public class CPU__Monitor implements PluginMonitor { |
17 |
|
18 |
//---FINAL ATTRIBUTES--- |
19 |
|
20 |
/** |
21 |
* The current CVS revision of this class |
22 |
*/ |
23 |
public final String REVISION = "$Revision: 1.6 $"; |
24 |
|
25 |
public final String DESC = "Monitors CPU."; |
26 |
|
27 |
//---STATIC METHODS--- |
28 |
|
29 |
//---CONSTRUCTORS--- |
30 |
|
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 |
_logger.write(toString(), Logger.ERROR, "Invalid configured value for alert level " + x + " - ignoring level."); |
41 |
_levels[x] = -1; |
42 |
} catch (org.omg.CORBA.MARSHAL e2) { |
43 |
_logger.write(toString(), Logger.WARNING, "Alert level " + x + " unused."); |
44 |
_levels[x] = -1; |
45 |
} |
46 |
|
47 |
} |
48 |
} |
49 |
//---PUBLIC METHODS--- |
50 |
|
51 |
public void analysePacket(XMLPacket packet) { |
52 |
if (packet.getParam("packet.attributes.type").equals("data")) { |
53 |
String source = packet.getParam("packet.attributes.hostname"); |
54 |
for(int x=0; x < _levels.length; x++) { |
55 |
if (_levels[x] != -1) { |
56 |
double idle = Double.parseDouble(packet.getParam("packet.cpu.idle")); |
57 |
if (_levels[x] < idle) fireAlert(x, source, idle, "idle"); |
58 |
double user = Double.parseDouble(packet.getParam("packet.cpu.user")); |
59 |
if (_levels[x] < user) fireAlert(x, source, user, "user"); |
60 |
double kernel = Double.parseDouble(packet.getParam("packet.cpu.kernel")); |
61 |
if (_levels[x] < kernel) fireAlert(x, source, kernel, "kernel"); |
62 |
double iowait = Double.parseDouble(packet.getParam("packet.cpu.iowait")); |
63 |
if (_levels[x] < iowait) fireAlert(x, source, iowait, "iowait"); |
64 |
double swap = Double.parseDouble(packet.getParam("packet.cpu.swap")); |
65 |
if (_levels[x] < swap) fireAlert(x, source, swap, "swap"); |
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 void fireAlert(int alertLevel, String source, double currentValue, String type) { |
97 |
String value = Double.toString(currentValue); |
98 |
String thresholdValue = Double.toString(_levels[alertLevel]); |
99 |
String attributeName = "CPU " + type; |
100 |
Alert alert = new Alert(alertLevel, source, thresholdValue, value, attributeName); |
101 |
_alerterQueue.add(alert); |
102 |
_logger.write(toString(), Logger.DEBUG, "Fired alert for source:" + source + " at alert level:" + alertLevel + " on:" + type + " at:" + currentValue + " exceeding threshold:" + _levels[alertLevel]); |
103 |
} |
104 |
|
105 |
//---ACCESSOR/MUTATOR METHODS--- |
106 |
|
107 |
//---ATTRIBUTES--- |
108 |
|
109 |
/** |
110 |
* This is the friendly identifier of the |
111 |
* component this class is running in. |
112 |
* eg, a Filter may be called "filter1", |
113 |
* If this class does not have an owning |
114 |
* component, a name from the configuration |
115 |
* can be placed here. This name could also |
116 |
* be changed to null for utility classes. |
117 |
*/ |
118 |
private String _name = ClientMain.NAME; |
119 |
|
120 |
/** |
121 |
* This holds a reference to the |
122 |
* system logger that is being used. |
123 |
*/ |
124 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
125 |
|
126 |
private double[] _levels; |
127 |
|
128 |
private Queue _alerterQueue; |
129 |
|
130 |
/** |
131 |
* A reference to the reference manager in use |
132 |
*/ |
133 |
private ReferenceManager _refman = ReferenceManager.getInstance(); |
134 |
|
135 |
//---STATIC ATTRIBUTES--- |
136 |
|
137 |
} |