1 |
/* |
2 |
* i-scream central monitoring system |
3 |
* http://www.i-scream.org |
4 |
* Copyright (C) 2000-2002 i-scream |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or |
7 |
* modify it under the terms of the GNU General Public License |
8 |
* as published by the Free Software Foundation; either version 2 |
9 |
* of the License, or (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
*/ |
20 |
|
21 |
//---PACKAGE DECLARATION--- |
22 |
package uk.org.iscream.cms.server.client.monitors; |
23 |
|
24 |
//---IMPORTS--- |
25 |
import java.util.HashMap; |
26 |
import java.util.HashSet; |
27 |
import java.util.Set; |
28 |
import java.util.Iterator; |
29 |
import uk.org.iscream.cms.server.client.*; |
30 |
import uk.org.iscream.cms.server.core.*; |
31 |
import uk.org.iscream.cms.util.*; |
32 |
import uk.org.iscream.cms.server.componentmanager.*; |
33 |
|
34 |
/** |
35 |
* This Monitor watches the mail queues on all machines |
36 |
* |
37 |
* @author $Author$ |
38 |
* @version $Id$ |
39 |
*/ |
40 |
public class MailQ__Monitor extends MonitorSkeleton { |
41 |
|
42 |
//---FINAL ATTRIBUTES--- |
43 |
|
44 |
/** |
45 |
* The current CVS revision of this class |
46 |
*/ |
47 |
public final String REVISION = "$Revision$"; |
48 |
|
49 |
/** |
50 |
* A description of this monitor |
51 |
*/ |
52 |
public final String DESC = "Monitors mail queue on all hosts."; |
53 |
|
54 |
//---STATIC METHODS--- |
55 |
|
56 |
//---CONSTRUCTORS--- |
57 |
|
58 |
//---PUBLIC METHODS--- |
59 |
|
60 |
/** |
61 |
* Analyse a packet of data, and generate an alert if |
62 |
* necessary. |
63 |
* |
64 |
* @param packet the XMLPacket to analyse |
65 |
*/ |
66 |
public void analysePacket(XMLPacket packet) { |
67 |
String source = packet.getParam("packet.attributes.machine_name"); |
68 |
if(!checkBooleanConfig("Host." + source, "Monitor." + _name + ".enable")) { |
69 |
return; |
70 |
} |
71 |
if (!_hosts.containsKey(source)) { |
72 |
_hosts.put(source, new HashMap()); |
73 |
} |
74 |
|
75 |
HashMap mailqRegisters = (HashMap) _hosts.get(source); |
76 |
|
77 |
// key prefix |
78 |
String keyPrefix = "packet.mailq.p"; |
79 |
|
80 |
// a temporary holder for all the mail queues we find |
81 |
HashSet mailqueues = new HashSet(); |
82 |
|
83 |
// unfortunatly we need to check the whole packet to find |
84 |
// the interfaces, and then get the data attributes |
85 |
Set packetSet = packet.getSet(); |
86 |
Iterator i = packetSet.iterator(); |
87 |
while (i.hasNext()) { |
88 |
String dataKey = (String) i.next(); |
89 |
if(dataKey.startsWith(keyPrefix)) { |
90 |
// pos is after "packet.mailq.p" |
91 |
int pos = keyPrefix.length(); |
92 |
String qNumber = dataKey.substring(pos, dataKey.indexOf('.', pos)); |
93 |
String thisQ = keyPrefix.concat(qNumber); |
94 |
|
95 |
if(!mailqueues.contains(thisQ)) { |
96 |
// add the queue to our list |
97 |
mailqueues.add(thisQ); |
98 |
|
99 |
String name = packet.getParam(thisQ + ".attributes.name"); |
100 |
|
101 |
String size = packet.getParam(thisQ + ".attributes.size"); |
102 |
|
103 |
// check if we've seen this queue before on a previous |
104 |
// run if not, we need to create a register for it |
105 |
if(!mailqRegisters.containsKey(name)) { |
106 |
mailqRegisters.put(name, new Register(source, _name + ".size", name)); |
107 |
} |
108 |
|
109 |
// get the register for this queue |
110 |
Register reg = (Register) mailqRegisters.get(name); |
111 |
|
112 |
// get the packet data |
113 |
int queueSize; |
114 |
try { |
115 |
if(size==null) { |
116 |
throw new NumberFormatException("Mail queue data invalid"); |
117 |
} |
118 |
queueSize = Integer.parseInt(size); |
119 |
} catch (NumberFormatException e) { |
120 |
_logger.write(this.toString(), Logger.WARNING, "Received packet from "+source+" with bad mail queue information: "+e); |
121 |
// don't try to continue and process, try next queue |
122 |
break; |
123 |
} |
124 |
|
125 |
int newThreshold = checkAttributeThreshold(queueSize, reg); |
126 |
|
127 |
// say which queue had the problem |
128 |
String attributeName = "Mail Queue size on " + name; |
129 |
|
130 |
processAlert(newThreshold, attributeName, reg, source, String.valueOf(queueSize)); |
131 |
} |
132 |
} |
133 |
} |
134 |
} |
135 |
|
136 |
/** |
137 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
138 |
* method to provide clean logging (every class should have this). |
139 |
* |
140 |
* This uses the uk.org.iscream.cms.util.NameFormat class |
141 |
* to format the toString() |
142 |
* |
143 |
* @return the name of this class and its CVS revision |
144 |
*/ |
145 |
public String toString() { |
146 |
return FormatName.getName( |
147 |
_name, |
148 |
getClass().getName(), |
149 |
REVISION); |
150 |
} |
151 |
|
152 |
/** |
153 |
* return the String representation of what the monitor does |
154 |
*/ |
155 |
public String getDescription(){ |
156 |
return DESC; |
157 |
} |
158 |
|
159 |
//---PRIVATE METHODS--- |
160 |
|
161 |
/** |
162 |
* Checks a piece of current data, and returns the |
163 |
* threshold it breaches, if any. |
164 |
* |
165 |
* @param value the current value |
166 |
* @param reg the Register for the host |
167 |
* @return the threshold level breached, if any |
168 |
*/ |
169 |
private int checkAttributeThreshold(double value, Register reg) { |
170 |
for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) { |
171 |
if (reg.getThreshold(thresholdLevel) != -1.0) { |
172 |
// normal check - has the value gone *over* the threshold |
173 |
if(((double) reg.getThreshold(thresholdLevel)) < value) { |
174 |
return thresholdLevel; |
175 |
} |
176 |
} |
177 |
} |
178 |
return Alert.thresholdNORMAL; |
179 |
} |
180 |
|
181 |
//---ACCESSOR/MUTATOR METHODS--- |
182 |
|
183 |
/** |
184 |
* Returns a reference to a specific Queue for this |
185 |
* monitor. This Queue returns only the data packets |
186 |
* (based on type) that we want too look at. |
187 |
* |
188 |
* @return a reference to a Queue |
189 |
*/ |
190 |
protected Queue getQueue() { |
191 |
return MonitorManager.getInstance().getDataQueue(); |
192 |
} |
193 |
|
194 |
//---ATTRIBUTES--- |
195 |
|
196 |
/** |
197 |
* This is the friendly identifier of the |
198 |
* component this class is running in. |
199 |
* eg, a Filter may be called "filter1", |
200 |
* If this class does not have an owning |
201 |
* component, a name from the configuration |
202 |
* can be placed here. This name could also |
203 |
* be changed to null for utility classes. |
204 |
*/ |
205 |
private String _name = "MailQ"; |
206 |
|
207 |
/** |
208 |
* A reference to the configuration proxy in use |
209 |
*/ |
210 |
private ConfigurationProxy _cp = ConfigurationProxy.getInstance(); |
211 |
|
212 |
/** |
213 |
* A HashMap of Registers (or groups of Registers), one |
214 |
* for each host we're monitoring (reads). |
215 |
*/ |
216 |
private HashMap _hosts = new HashMap(); |
217 |
|
218 |
//---STATIC ATTRIBUTES--- |
219 |
|
220 |
} |