1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.org.iscream.cms.server.client.monitors; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import java.util.HashMap; |
6 |
import uk.org.iscream.cms.server.client.*; |
7 |
import uk.org.iscream.cms.server.core.*; |
8 |
import uk.org.iscream.cms.server.util.*; |
9 |
import uk.org.iscream.cms.server.componentmanager.*; |
10 |
|
11 |
/** |
12 |
* This Monitor watches the CPU load for all machines |
13 |
* |
14 |
* @author $Author: tdb1 $ |
15 |
* @version $Id: CPU__Monitor.java,v 1.23 2001/03/23 02:32:48 tdb1 Exp $ |
16 |
*/ |
17 |
public class CPU__Monitor extends MonitorSkeleton { |
18 |
|
19 |
//---FINAL ATTRIBUTES--- |
20 |
|
21 |
/** |
22 |
* The current CVS revision of this class |
23 |
*/ |
24 |
public final String REVISION = "$Revision: 1.23 $"; |
25 |
|
26 |
/** |
27 |
* A description of this monitor |
28 |
*/ |
29 |
public final String DESC = "Monitors CPU."; |
30 |
|
31 |
//---STATIC METHODS--- |
32 |
|
33 |
//---CONSTRUCTORS--- |
34 |
|
35 |
//---PUBLIC METHODS--- |
36 |
|
37 |
/** |
38 |
* Analyse a packet of data, and generate an alert if |
39 |
* necessary. |
40 |
* |
41 |
* @param packet the XMLPacket to analyse |
42 |
*/ |
43 |
public void analysePacket(XMLPacket packet) { |
44 |
String source = packet.getParam("packet.attributes.machine_name"); |
45 |
if (!_hosts.containsKey(source)) { |
46 |
HashMap attributeRegisters = new HashMap(); |
47 |
initAttributeRegsiters(source, attributeRegisters); |
48 |
_hosts.put(source, attributeRegisters); |
49 |
} |
50 |
|
51 |
HashMap attributeRegisters = (HashMap) _hosts.get(source); |
52 |
for(int attributeNum = 0; attributeNum < _attributes.length; attributeNum++) { |
53 |
Register reg = (Register) attributeRegisters.get(_attributes[attributeNum]); |
54 |
// find out the threshold level we're at |
55 |
String attribute = _attributes[attributeNum]; |
56 |
String attributeName = _attributeNames[attributeNum]; |
57 |
String currentValue = packet.getParam(attribute); |
58 |
int newThreshold = checkAttributeThreshold(currentValue, reg); |
59 |
processAlert(newThreshold, attributeName, reg, source, currentValue); |
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.org.iscream.cms.server.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 monitor does |
81 |
*/ |
82 |
public String getDescription(){ |
83 |
return DESC; |
84 |
} |
85 |
|
86 |
//---PRIVATE METHODS--- |
87 |
|
88 |
/** |
89 |
* Checks a piece of current data, and returns the |
90 |
* threshold it breaches, if any. |
91 |
* |
92 |
* @param attributeString a String representing the current data value |
93 |
* @param reg the Register for the host |
94 |
* @return the threshold level breached, if any |
95 |
*/ |
96 |
private int checkAttributeThreshold(String attributeString, Register reg) { |
97 |
for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) { |
98 |
if (reg.getThreshold(thresholdLevel) != -1.0) { |
99 |
if(attributeString != null) { |
100 |
try { |
101 |
double attribute = Double.parseDouble(attributeString); |
102 |
if (reg.getThreshold(thresholdLevel) < attribute) return thresholdLevel; |
103 |
} catch (NumberFormatException e) { |
104 |
// we got some duff data in the packet, but we shouldn't have |
105 |
_logger.write(toString(), Logger.DEBUG, "possible errenous packet data, should be double value - " + attributeString); |
106 |
} |
107 |
} |
108 |
} |
109 |
} |
110 |
return Alert.thresholdNORMAL; |
111 |
} |
112 |
|
113 |
/** |
114 |
* Initialises a HashMap of Registers with the current list |
115 |
* of attributes. This is only used if we are looking at more |
116 |
* than one distinct attribute. |
117 |
* |
118 |
* @param source the host we are looking at |
119 |
* @param attributeRegisters a HashMap to put the new Registers in |
120 |
*/ |
121 |
private void initAttributeRegsiters(String source, HashMap attributeRegisters) { |
122 |
for(int attributeNum = 0; attributeNum < _attributes.length; attributeNum++) { |
123 |
String attributeName = _attributes[attributeNum].substring(_attributes[attributeNum].lastIndexOf(".") + 1); |
124 |
attributeRegisters.put(_attributes[attributeNum], new Register(source, _name, attributeName)); |
125 |
} |
126 |
} |
127 |
|
128 |
//---ACCESSOR/MUTATOR METHODS--- |
129 |
|
130 |
/** |
131 |
* Returns a reference to a specific Queue for this |
132 |
* monitor. This Queue returns only the data packets |
133 |
* (based on type) that we want too look at. |
134 |
* |
135 |
* @return a reference to a Queue |
136 |
*/ |
137 |
protected Queue getQueue() { |
138 |
return MonitorManager.getInstance().getDataQueue(); |
139 |
} |
140 |
|
141 |
//---ATTRIBUTES--- |
142 |
|
143 |
/** |
144 |
* This is the friendly identifier of the |
145 |
* component this class is running in. |
146 |
* eg, a Filter may be called "filter1", |
147 |
* If this class does not have an owning |
148 |
* component, a name from the configuration |
149 |
* can be placed here. This name could also |
150 |
* be changed to null for utility classes. |
151 |
*/ |
152 |
private String _name = "CPU"; |
153 |
|
154 |
/** |
155 |
* A HashMap of Registers (or groups of Registers), one |
156 |
* for each host we're monitoring. |
157 |
*/ |
158 |
private HashMap _hosts = new HashMap(); |
159 |
|
160 |
/** |
161 |
* An array of attributes which we will be checking. |
162 |
*/ |
163 |
private String[] _attributes = { "packet.cpu.user", "packet.cpu.kernel", "packet.cpu.iowait", "packet.cpu.swap" }; |
164 |
|
165 |
/** |
166 |
* An array of "nice names" for the attributes in _attributes. |
167 |
*/ |
168 |
private String[] _attributeNames = {"User CPU", "Kernel CPU", "I/O Wait CPU", "Swap CPU"}; |
169 |
|
170 |
//---STATIC ATTRIBUTES--- |
171 |
|
172 |
} |