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