1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.org.iscream.client.monitors; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import java.util.HashMap; |
6 |
import uk.org.iscream.client.*; |
7 |
import uk.org.iscream.core.*; |
8 |
import uk.org.iscream.util.*; |
9 |
import uk.org.iscream.componentmanager.*; |
10 |
|
11 |
/** |
12 |
* This Monitor watches the Process Counts for all machines |
13 |
* |
14 |
* @author $Author: ajm4 $ |
15 |
* @version $Id: Process__Monitor.java,v 1.2 2001/03/22 17:57:06 ajm4 Exp $ |
16 |
*/ |
17 |
public class Process__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.2 $"; |
25 |
|
26 |
/** |
27 |
* A description of this monitor |
28 |
*/ |
29 |
public final String DESC = "Monitors Process Counts."; |
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 |
|
45 |
String source = packet.getParam("packet.attributes.machine_name"); |
46 |
if (!_hosts.containsKey(source)) { |
47 |
HashMap attributeRegisters = new HashMap(); |
48 |
initAttributeRegsiters(source, attributeRegisters); |
49 |
_hosts.put(source, attributeRegisters); |
50 |
} |
51 |
|
52 |
HashMap attributeRegisters = (HashMap) _hosts.get(source); |
53 |
for(int attributeNum = 0; attributeNum < _attributes.length; attributeNum++) { |
54 |
Register reg = (Register) attributeRegisters.get(_attributes[attributeNum]); |
55 |
// find out the threshold level we're at |
56 |
String attribute = _attributes[attributeNum]; |
57 |
String attributeName = _attributeNames[attributeNum]; |
58 |
String currentValue = packet.getParam(attribute); |
59 |
int newThreshold = checkAttributeThreshold(currentValue, reg); |
60 |
processAlert(newThreshold, attributeName, reg, source, currentValue); |
61 |
} |
62 |
} |
63 |
|
64 |
/** |
65 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
66 |
* method to provide clean logging (every class should have this). |
67 |
* |
68 |
* This uses the uk.org.iscream.util.NameFormat class |
69 |
* to format the toString() |
70 |
* |
71 |
* @return the name of this class and its CVS revision |
72 |
*/ |
73 |
public String toString() { |
74 |
return FormatName.getName( |
75 |
_name, |
76 |
getClass().getName(), |
77 |
REVISION); |
78 |
} |
79 |
|
80 |
/** |
81 |
* return the String representation of what the monitor does |
82 |
*/ |
83 |
public String getDescription(){ |
84 |
return DESC; |
85 |
} |
86 |
|
87 |
//---PRIVATE METHODS--- |
88 |
|
89 |
/** |
90 |
* Checks a piece of current data, and returns the |
91 |
* threshold it breaches, if any. |
92 |
* |
93 |
* @param attributeString a String representing the current data value |
94 |
* @param reg the Register for the host |
95 |
* @return the threshold level breached, if any |
96 |
*/ |
97 |
private int checkAttributeThreshold(String attributeString, Register reg) { |
98 |
for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) { |
99 |
if (reg.getThreshold(thresholdLevel) != -1.0) { |
100 |
if(attributeString != null) { |
101 |
try { |
102 |
double attribute = Double.parseDouble(attributeString); |
103 |
if (reg.getThreshold(thresholdLevel) < attribute) return thresholdLevel; |
104 |
} catch (NumberFormatException e) { |
105 |
// we got some duff data in the packet, but we shouldn't have |
106 |
_logger.write(toString(), Logger.DEBUG, "possible errenous packet data, should be double value - " + attributeString); |
107 |
} |
108 |
} |
109 |
} |
110 |
} |
111 |
return Alert.thresholdNORMAL; |
112 |
} |
113 |
|
114 |
/** |
115 |
* Initialises a HashMap of Registers with the current list |
116 |
* of attributes. This is only used if we are looking at more |
117 |
* than one distinct attribute. |
118 |
* |
119 |
* @param source the host we are looking at |
120 |
* @param attributeRegisters a HashMap to put the new Registers in |
121 |
*/ |
122 |
private void initAttributeRegsiters(String source, HashMap attributeRegisters) { |
123 |
for(int attributeNum = 0; attributeNum < _attributes.length; attributeNum++) { |
124 |
String attributeName = _attributes[attributeNum].substring(_attributes[attributeNum].lastIndexOf(".") + 1); |
125 |
attributeRegisters.put(_attributes[attributeNum], new Register(source, _name, attributeName)); |
126 |
} |
127 |
} |
128 |
|
129 |
//---ACCESSOR/MUTATOR METHODS--- |
130 |
|
131 |
/** |
132 |
* Returns a reference to a specific Queue for this |
133 |
* monitor. This Queue returns only the data packets |
134 |
* (based on type) that we want too look at. |
135 |
* |
136 |
* @return a reference to a Queue |
137 |
*/ |
138 |
protected Queue getQueue() { |
139 |
return MonitorManager.getInstance().getDataQueue(); |
140 |
} |
141 |
|
142 |
//---ATTRIBUTES--- |
143 |
|
144 |
/** |
145 |
* This is the friendly identifier of the |
146 |
* component this class is running in. |
147 |
* eg, a Filter may be called "filter1", |
148 |
* If this class does not have an owning |
149 |
* component, a name from the configuration |
150 |
* can be placed here. This name could also |
151 |
* be changed to null for utility classes. |
152 |
*/ |
153 |
private String _name = "Process"; |
154 |
|
155 |
/** |
156 |
* A HashMap of Registers (or groups of Registers), one |
157 |
* for each host we're monitoring. |
158 |
*/ |
159 |
private HashMap _hosts = new HashMap(); |
160 |
|
161 |
/** |
162 |
* An array of attributes which we will be checking. |
163 |
*/ |
164 |
private String[] _attributes = { "packet.processes.total", "packet.processes.cpu", "packet.processes.sleeping", "packet.processes.stopped", "packet.processes.zombie" }; |
165 |
|
166 |
/** |
167 |
* An array of "nice names" for the attributes in _attributes. |
168 |
*/ |
169 |
private String[] _attributeNames = {"Total Processes", "on CPU Processes", "Sleeping Processes", "Stopped Processes", "Zombie Processes"}; |
170 |
|
171 |
//---STATIC ATTRIBUTES--- |
172 |
|
173 |
} |