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: tdb1 $ |
14 |
* @version $Id: CPU__Monitor.java,v 1.1 2001/02/27 01:03:28 tdb1 Exp $ |
15 |
*/ |
16 |
public class CPU__Monitor implements Monitor { |
17 |
|
18 |
//---FINAL ATTRIBUTES--- |
19 |
|
20 |
/** |
21 |
* The current CVS revision of this class |
22 |
*/ |
23 |
public final String REVISION = "$Revision: 1.1 $"; |
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 |
_levels[x] = -1; |
41 |
} |
42 |
} |
43 |
} |
44 |
//---PUBLIC METHODS--- |
45 |
|
46 |
public void analysePacket(XMLPacket packet) { |
47 |
for(int x=0; x < _levels.length; x++) { |
48 |
if (_levels[x] != -1) { |
49 |
double idle = Double.parseDouble(packet.getParam("packet.cpu.idle")); |
50 |
if (_levels[x] < idle) fireAlert(x, idle, "idle"); |
51 |
double user = Double.parseDouble(packet.getParam("packet.cpu.user")); |
52 |
if (_levels[x] < user) fireAlert(x, user, "user"); |
53 |
double kernel = Double.parseDouble(packet.getParam("packet.cpu.kernel")); |
54 |
if (_levels[x] < kernel) fireAlert(x, kernel, "kernel"); |
55 |
double iowait = Double.parseDouble(packet.getParam("packet.cpu.iowait")); |
56 |
if (_levels[x] < iowait) fireAlert(x, iowait, "iowait"); |
57 |
double swap = Double.parseDouble(packet.getParam("packet.cpu.swap")); |
58 |
if (_levels[x] < swap) fireAlert(x, swap, "swap"); |
59 |
} |
60 |
} |
61 |
} |
62 |
|
63 |
/** |
64 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
65 |
* method to provide clean logging (every class should have this). |
66 |
* |
67 |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
68 |
* to format the toString() |
69 |
* |
70 |
* @return the name of this class and its CVS revision |
71 |
*/ |
72 |
public String toString() { |
73 |
return FormatName.getName( |
74 |
_name, |
75 |
getClass().getName(), |
76 |
REVISION); |
77 |
} |
78 |
|
79 |
/** |
80 |
* return the String representation of what the filter does |
81 |
*/ |
82 |
public String getDescription(){ |
83 |
return DESC; |
84 |
} |
85 |
|
86 |
//---PRIVATE METHODS--- |
87 |
|
88 |
private void fireAlert(int alertLevel, double currentValue, String type) { |
89 |
String value = Double.toString(currentValue); |
90 |
String thresholdValue = Double.toString(_levels[alertLevel]); |
91 |
String attributeName = "CPU " + type; |
92 |
Alert alert = new Alert(alertLevel, thresholdValue, value, attributeName); |
93 |
_alerterQueue.add(alert); |
94 |
} |
95 |
|
96 |
//---ACCESSOR/MUTATOR METHODS--- |
97 |
|
98 |
//---ATTRIBUTES--- |
99 |
|
100 |
/** |
101 |
* This is the friendly identifier of the |
102 |
* component this class is running in. |
103 |
* eg, a Filter may be called "filter1", |
104 |
* If this class does not have an owning |
105 |
* component, a name from the configuration |
106 |
* can be placed here. This name could also |
107 |
* be changed to null for utility classes. |
108 |
*/ |
109 |
private String _name = ClientMain.NAME; |
110 |
|
111 |
/** |
112 |
* This holds a reference to the |
113 |
* system logger that is being used. |
114 |
*/ |
115 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
116 |
|
117 |
private double[] _levels; |
118 |
|
119 |
private Queue _alerterQueue; |
120 |
|
121 |
/** |
122 |
* A reference to the reference manager in use |
123 |
*/ |
124 |
private ReferenceManager _refman = ReferenceManager.getInstance(); |
125 |
|
126 |
//---STATIC ATTRIBUTES--- |
127 |
|
128 |
} |