1 |
ajm |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
|
|
package uk.org.iscream.client.monitors; |
3 |
|
|
|
4 |
|
|
//---IMPORTS--- |
5 |
|
|
import java.util.HashMap; |
6 |
|
|
import java.util.ArrayList; |
7 |
|
|
import java.util.Set; |
8 |
|
|
import java.util.Iterator; |
9 |
|
|
import java.text.NumberFormat; |
10 |
|
|
import uk.org.iscream.client.*; |
11 |
|
|
import uk.org.iscream.core.*; |
12 |
|
|
import uk.org.iscream.util.*; |
13 |
|
|
import uk.org.iscream.componentmanager.*; |
14 |
|
|
|
15 |
|
|
/** |
16 |
|
|
* This Monitor watches the internal server queues. |
17 |
|
|
* YES! this system is SO great it monitors itself! |
18 |
|
|
* |
19 |
tdb |
1.8 |
* @author $Author: ajm4 $ |
20 |
|
|
* @version $Id: Queue__Monitor.java,v 1.7 2001/03/22 22:34:18 ajm4 Exp $ |
21 |
ajm |
1.1 |
*/ |
22 |
|
|
public class Queue__Monitor extends MonitorSkeleton { |
23 |
|
|
|
24 |
|
|
//---FINAL ATTRIBUTES--- |
25 |
|
|
|
26 |
|
|
/** |
27 |
|
|
* The current CVS revision of this class |
28 |
|
|
*/ |
29 |
tdb |
1.8 |
public final String REVISION = "$Revision: 1.7 $"; |
30 |
ajm |
1.1 |
|
31 |
tdb |
1.8 |
/** |
32 |
|
|
* A description of this monitor |
33 |
|
|
*/ |
34 |
ajm |
1.1 |
public final String DESC = "Who watches the watchmen?"; |
35 |
|
|
|
36 |
tdb |
1.6 |
private final String SOURCE = "i-scream-server"; |
37 |
|
|
|
38 |
ajm |
1.1 |
//---STATIC METHODS--- |
39 |
|
|
|
40 |
|
|
//---CONSTRUCTORS--- |
41 |
|
|
|
42 |
|
|
//---PUBLIC METHODS--- |
43 |
tdb |
1.8 |
|
44 |
|
|
/** |
45 |
|
|
* Analyse a packet of data, and generate an alert if |
46 |
|
|
* necessary. |
47 |
|
|
* |
48 |
|
|
* @param packet the XMLPacket to analyse |
49 |
|
|
*/ |
50 |
ajm |
1.1 |
public void analysePacket(XMLPacket packet) { |
51 |
tdb |
1.3 |
if (packet.getParam("packet.attributes.type").equals("queueStat")) { |
52 |
|
|
String source = packet.getParam("packet.attributes.hashCode"); |
53 |
ajm |
1.1 |
if (!_hosts.containsKey(source)) { |
54 |
|
|
_hosts.put(source, new HashMap()); |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
HashMap queueRegisters = (HashMap) _hosts.get(source); |
58 |
|
|
|
59 |
|
|
// get info about the source queue class instance |
60 |
|
|
String queueName = packet.getParam("packet.attributes.name"); |
61 |
|
|
String maxQueueSize = packet.getParam("packet.queue.attributes.maxSize"); |
62 |
|
|
// unfortunatly we need to check the whole packet |
63 |
|
|
// to find the queues, and then get the data attributes |
64 |
|
|
Set packetSet = packet.getSet(); |
65 |
|
|
Iterator i = packetSet.iterator(); |
66 |
|
|
|
67 |
|
|
while (i.hasNext()) { |
68 |
|
|
String dataKey = (String) i.next(); |
69 |
|
|
if(dataKey.startsWith("packet.queue.attributes.queue")) { |
70 |
ajm |
1.2 |
String queueSize = packet.getParam(dataKey); |
71 |
ajm |
1.1 |
|
72 |
|
|
// *** now process this queue *** |
73 |
|
|
|
74 |
|
|
// check if we've seen this queue before on a previous run |
75 |
|
|
// if not, we need to create a register for it |
76 |
|
|
if(!queueRegisters.containsKey(dataKey)) { |
77 |
|
|
queueRegisters.put(dataKey, new Register(source, _name)); |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
// get the register for this disk |
81 |
ajm |
1.2 |
Register reg = (Register) queueRegisters.get(dataKey); |
82 |
ajm |
1.1 |
|
83 |
|
|
// get the packet data |
84 |
ajm |
1.2 |
double qMax, qSize; |
85 |
ajm |
1.1 |
try { |
86 |
|
|
|
87 |
ajm |
1.2 |
if(maxQueueSize==null || queueSize==null) { |
88 |
ajm |
1.1 |
throw new NumberFormatException("Queue data invalid"); |
89 |
|
|
} |
90 |
|
|
qMax = Double.parseDouble(maxQueueSize); |
91 |
|
|
qSize = Double.parseDouble(queueSize); |
92 |
|
|
} catch (NumberFormatException e) { |
93 |
|
|
_logger.write(this.toString(), Logger.WARNING, "Received packet from "+source+" with bad queue information: "+e); |
94 |
|
|
// don't try to continue and process, try next queue |
95 |
|
|
break; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
boolean useValue = false; |
99 |
|
|
try { |
100 |
|
|
String option = _cp.getProperty("Host." + source, "Monitor." + _name + ".thresholdMeasure"); |
101 |
|
|
if (option.equals("VALUE")) { |
102 |
|
|
useValue = true; |
103 |
|
|
} |
104 |
|
|
} catch (PropertyNotFoundException e) { |
105 |
|
|
// we default to percentage |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
// this bit determines if the disk check is a % check |
109 |
|
|
// or a kb check |
110 |
|
|
double checkValue; |
111 |
|
|
String type; |
112 |
|
|
if(useValue) { |
113 |
ajm |
1.7 |
// queue count |
114 |
ajm |
1.1 |
checkValue = qSize; |
115 |
|
|
type = "count"; |
116 |
|
|
} else { |
117 |
ajm |
1.7 |
// % of queue |
118 |
ajm |
1.1 |
checkValue = (qSize / qMax) * 100; |
119 |
|
|
type = "%"; |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
int newThreshold = checkAttributeThreshold(checkValue, reg); |
123 |
|
|
|
124 |
|
|
// format the checkValue to a String |
125 |
|
|
NumberFormat nf = NumberFormat.getInstance(); |
126 |
|
|
nf.setMaximumFractionDigits(2); |
127 |
|
|
nf.setMinimumFractionDigits(2); |
128 |
|
|
String strCheckValue = nf.format(checkValue); |
129 |
|
|
|
130 |
|
|
// say which disk had the problem |
131 |
ajm |
1.7 |
String attributeName = "Size of queue " + type + " in " + queueName + " " + dataKey.substring(dataKey.lastIndexOf('.')); |
132 |
ajm |
1.1 |
|
133 |
tdb |
1.6 |
processAlert(newThreshold, attributeName, reg, SOURCE, strCheckValue); |
134 |
ajm |
1.1 |
|
135 |
|
|
} |
136 |
|
|
} |
137 |
|
|
} |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
/** |
141 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
142 |
|
|
* method to provide clean logging (every class should have this). |
143 |
|
|
* |
144 |
|
|
* This uses the uk.org.iscream.util.NameFormat class |
145 |
|
|
* to format the toString() |
146 |
|
|
* |
147 |
|
|
* @return the name of this class and its CVS revision |
148 |
|
|
*/ |
149 |
|
|
public String toString() { |
150 |
|
|
return FormatName.getName( |
151 |
|
|
_name, |
152 |
|
|
getClass().getName(), |
153 |
|
|
REVISION); |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
/** |
157 |
|
|
* return the String representation of what the monitor does |
158 |
|
|
*/ |
159 |
|
|
public String getDescription(){ |
160 |
|
|
return DESC; |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
//---PRIVATE METHODS--- |
164 |
tdb |
1.8 |
|
165 |
|
|
/** |
166 |
|
|
* Checks a piece of current data, and returns the |
167 |
|
|
* threshold it breaches, if any. |
168 |
|
|
* |
169 |
|
|
* @param qSize the size of a queue |
170 |
|
|
* @param reg the Register for the host |
171 |
|
|
* @return the threshold level breached, if any |
172 |
|
|
*/ |
173 |
ajm |
1.1 |
private int checkAttributeThreshold(double qSize, Register reg) { |
174 |
|
|
for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) { |
175 |
|
|
if (reg.getThreshold(thresholdLevel) != -1.0) { |
176 |
|
|
if(((double) reg.getThreshold(thresholdLevel)) < qSize) { |
177 |
|
|
return thresholdLevel; |
178 |
|
|
} |
179 |
|
|
} |
180 |
|
|
} |
181 |
|
|
return Alert.thresholdNORMAL; |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
185 |
tdb |
1.8 |
|
186 |
|
|
/** |
187 |
|
|
* Returns a reference to a specific Queue for this |
188 |
|
|
* monitor. This Queue returns only the data packets |
189 |
|
|
* (based on type) that we want too look at. |
190 |
|
|
* |
191 |
|
|
* @return a reference to a Queue |
192 |
|
|
*/ |
193 |
ajm |
1.4 |
protected Queue getQueue() { |
194 |
|
|
return MonitorManager.getInstance().getOtherQueue(); |
195 |
|
|
} |
196 |
|
|
|
197 |
ajm |
1.1 |
|
198 |
|
|
//---ATTRIBUTES--- |
199 |
|
|
|
200 |
|
|
/** |
201 |
|
|
* This is the friendly identifier of the |
202 |
|
|
* component this class is running in. |
203 |
|
|
* eg, a Filter may be called "filter1", |
204 |
|
|
* If this class does not have an owning |
205 |
|
|
* component, a name from the configuration |
206 |
|
|
* can be placed here. This name could also |
207 |
|
|
* be changed to null for utility classes. |
208 |
|
|
*/ |
209 |
|
|
private String _name = "Queue"; |
210 |
|
|
|
211 |
|
|
/** |
212 |
|
|
* A reference to the configuration proxy in use |
213 |
|
|
*/ |
214 |
|
|
private ConfigurationProxy _cp = ConfigurationProxy.getInstance(); |
215 |
tdb |
1.8 |
|
216 |
|
|
/** |
217 |
|
|
* A HashMap of Registers (or groups of Registers), one |
218 |
|
|
* for each host we're monitoring. |
219 |
|
|
*/ |
220 |
ajm |
1.1 |
private HashMap _hosts = new HashMap(); |
221 |
|
|
|
222 |
|
|
//---STATIC ATTRIBUTES--- |
223 |
|
|
|
224 |
|
|
} |