1 |
/* |
2 |
* i-scream central monitoring system |
3 |
* http://www.i-scream.org.uk |
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; |
23 |
|
24 |
//---IMPORTS--- |
25 |
import java.util.HashMap; |
26 |
import uk.org.iscream.cms.server.client.*; |
27 |
import uk.org.iscream.cms.server.core.*; |
28 |
import uk.org.iscream.cms.util.*; |
29 |
import uk.org.iscream.cms.server.componentmanager.*; |
30 |
|
31 |
/** |
32 |
* Skeleton class for Monitors |
33 |
* This skeleton reads packets from a queue designated |
34 |
* by the extending class, it then feeds the data to the analysePacket |
35 |
* method, which the extending class should implement. The class |
36 |
* should then handle the monitoring for that packet. |
37 |
* |
38 |
* @author $Author: tdb $ |
39 |
* @version $Id: MonitorSkeleton.java,v 1.16 2003/02/24 20:18:48 tdb Exp $ |
40 |
*/ |
41 |
public abstract class MonitorSkeleton extends Thread implements PluginMonitor { |
42 |
|
43 |
//---FINAL ATTRIBUTES--- |
44 |
|
45 |
/** |
46 |
* The current CVS revision of this class |
47 |
*/ |
48 |
public final String REVISION = "$Revision: 1.16 $"; |
49 |
|
50 |
//---STATIC METHODS--- |
51 |
|
52 |
//---CONSTRUCTORS--- |
53 |
|
54 |
/** |
55 |
* Constructs and start the monitor reading its data |
56 |
*/ |
57 |
public MonitorSkeleton() { |
58 |
_logger.write(toString(), Logger.SYSINIT, "started."); |
59 |
this.start(); |
60 |
} |
61 |
|
62 |
//---PUBLIC METHODS--- |
63 |
|
64 |
/** |
65 |
* Obtains data from the monitors data queue and |
66 |
* passes the packet to the analysePacket method. |
67 |
*/ |
68 |
public void run() { |
69 |
while(_running) { |
70 |
try { |
71 |
// check if monitoring is enabled for this host |
72 |
// if not - just don't bother analyse it's packets |
73 |
XMLPacket packet = (XMLPacket) getQueue().get(getQueueId()); |
74 |
String source = packet.getParam("packet.attributes.machine_name"); |
75 |
boolean enabled = checkBooleanConfig(source, "Monitor.enable"); |
76 |
if(enabled) { |
77 |
analysePacket(packet); |
78 |
} |
79 |
} catch (InvalidQueueException e) { |
80 |
_logger.write(this.toString(), Logger.ERROR, "Unable to get queue."); |
81 |
} |
82 |
} |
83 |
} |
84 |
|
85 |
/** |
86 |
* Extending classes should override this method to |
87 |
* analyse the given packet for the attribute |
88 |
* they are responsible for. |
89 |
*/ |
90 |
protected abstract void analysePacket(XMLPacket packet); |
91 |
|
92 |
/** |
93 |
* Once a Monitor has determined which threshold the given data packet |
94 |
* is at, it should then call this method. This method handles ALL |
95 |
* the alerting logic to determine escalation of alerts. |
96 |
* If it decides an alert needs to be send, it will send one using fireAlert. |
97 |
* |
98 |
* @param newThreshold the threshold that has been determined by the monitor |
99 |
* @param attributeName the textual name of the attribute the monitor is responsible for |
100 |
* @param reg the register that holds the current alert state for the machine/attribute |
101 |
* @param source the source of the alert eg, hostname |
102 |
* @param currentValue the data value for the attribute |
103 |
*/ |
104 |
protected void processAlert(int newThreshold, String attributeName, Register reg, String source, String currentValue) { |
105 |
// decide what threshold level we're on, if we've changed, record that |
106 |
if (newThreshold != reg.getLastThresholdLevel()) { |
107 |
reg.setLastThresholdLevel(newThreshold); |
108 |
} |
109 |
// as long as this isn't a normal level |
110 |
if(reg.getLastThresholdLevel() != Alert.thresholdNORMAL) { |
111 |
// if the time since the last alert is more than the time for |
112 |
// its timeout, fire an alert, escalate the alert |
113 |
long timeout = reg.getLastAlertTimeout(); |
114 |
if ((timeout > 0) && (reg.getTimeLastSent() > 0)) { |
115 |
if((System.currentTimeMillis() - reg.getTimeLastSent()) > timeout) { |
116 |
int lastAlert = reg.getLastAlertLevel(); |
117 |
reg.escalateAlert(); |
118 |
reg.setTimeLastSent( System.currentTimeMillis()); |
119 |
reg.setLastAlertTimeout(reg.getAlertTimeout(reg.getLastAlertLevel())); |
120 |
fireAlert(reg, lastAlert, source, currentValue, attributeName); |
121 |
} |
122 |
// if we don't have a timeout configured...we got STRAIGHT to the next level |
123 |
} else { |
124 |
int lastAlert = reg.getLastAlertLevel(); |
125 |
reg.escalateAlert(); |
126 |
reg.setTimeLastSent( System.currentTimeMillis()); |
127 |
reg.setLastAlertTimeout(reg.getAlertTimeout(reg.getLastAlertLevel())); |
128 |
fireAlert(reg, lastAlert, source, currentValue, attributeName); |
129 |
} |
130 |
|
131 |
// we must be on ok, check the timeout value for this |
132 |
} else { |
133 |
// if we were on an OK alert before, then we don't do anything |
134 |
// but if we weren't we only set OK, once the timout of the last |
135 |
// alert has occourd |
136 |
if (reg.getLastAlertLevel() != Alert.alertOK) { |
137 |
long timeout = reg.getLastAlertTimeout(); |
138 |
if ((timeout > 0) && (reg.getTimeLastSent() > 0)) { |
139 |
if ((System.currentTimeMillis() - reg.getTimeLastSent()) > timeout) { |
140 |
int lastAlert = reg.getLastAlertLevel(); |
141 |
reg.setLastAlertLevel(Alert.alertOK); |
142 |
reg.setTimeLastSent(System.currentTimeMillis()); |
143 |
reg.setLastAlertTimeout(timeout); |
144 |
fireAlert(reg, lastAlert, source, currentValue, attributeName); |
145 |
} |
146 |
} |
147 |
} |
148 |
} |
149 |
} |
150 |
|
151 |
/** |
152 |
* Return the String representation of what the alerter does |
153 |
* |
154 |
* @return the description |
155 |
*/ |
156 |
public abstract String getDescription(); |
157 |
|
158 |
/** |
159 |
* Checks a boolean configuration variable. |
160 |
* |
161 |
* @param source the configuration to look up |
162 |
* @param name the key to look up |
163 |
*/ |
164 |
protected boolean checkBooleanConfig(String source, String name) { |
165 |
boolean enabled = true; |
166 |
try { |
167 |
int e = Integer.parseInt(_cp.getProperty(source, name)); |
168 |
enabled = (e == 1); |
169 |
} catch (PropertyNotFoundException e) { |
170 |
enabled = false; |
171 |
} catch (NumberFormatException e) { |
172 |
enabled = false; |
173 |
} |
174 |
return enabled; |
175 |
} |
176 |
|
177 |
//---PRIVATE METHODS--- |
178 |
|
179 |
/** |
180 |
* Fires an alert. This creates a new Alert object |
181 |
* and populates it with the given alert information. |
182 |
* It then adds the alert to the Alerter queue. |
183 |
* |
184 |
* This method should only be called by the processAlert method. |
185 |
* |
186 |
* @param reg the register holding the state values for the alert |
187 |
* @param source the source of the alert eg, hostname |
188 |
* @param currentValue the data value for the attribute |
189 |
* @param attributeName the textual name of the attribute the alert is for |
190 |
*/ |
191 |
private void fireAlert(Register reg, int lastAlert, String source, String currentValue, String attributeName) { |
192 |
int alertLevel = reg.getLastAlertLevel(); |
193 |
int thresholdLevel = reg.getLastThresholdLevel(); |
194 |
String thresholdValue = String.valueOf(reg.getThreshold(thresholdLevel)); |
195 |
String timeout = String.valueOf(reg.getAlertTimeout(reg.getLastAlertLevel()) / 1000); |
196 |
// ensures we display a nice thing if its -1.0 |
197 |
if (thresholdValue.equals("-1.0")) { |
198 |
thresholdValue = "-"; |
199 |
} |
200 |
if (alertLevel == Alert.alertOK) { |
201 |
timeout = "0"; |
202 |
} |
203 |
Alert alert = new Alert(alertLevel, lastAlert, thresholdLevel, source, thresholdValue, currentValue, attributeName, timeout, reg.getInitialAlertTime()); |
204 |
_alerterQueue.add(alert); |
205 |
_logger.write(toString(), Logger.DEBUG, "Fired alert for source:" + source + " at alert level:" + Alert.alertLevels[alertLevel] + " on:" + attributeName + " for threshold level:" + Alert.thresholdLevels[thresholdLevel] + " at:" + currentValue + " exceeding threshold of:" +thresholdValue + " next alert sent in:" + timeout + "secs"); |
206 |
} |
207 |
|
208 |
//---ACCESSOR/MUTATOR METHODS--- |
209 |
|
210 |
/** |
211 |
* Obtain the queue which contains the data |
212 |
* the Monitor is reading. |
213 |
* eg, MonitorManager.getInstance().getDataQueue() |
214 |
*/ |
215 |
protected abstract Queue getQueue(); |
216 |
|
217 |
/** |
218 |
* Create a queue ID on the feeding |
219 |
* data queue |
220 |
*/ |
221 |
protected int getQueueId() { |
222 |
if (_qID == -1) { |
223 |
_qID = getQueue().getQueue(); |
224 |
_logger.write(toString(), Logger.DEBUG, "Assigned Queue - " + _qID); |
225 |
} |
226 |
return _qID; |
227 |
} |
228 |
|
229 |
//---ATTRIBUTES--- |
230 |
|
231 |
/** |
232 |
* This holds a reference to the |
233 |
* system logger that is being used. |
234 |
*/ |
235 |
protected Logger _logger = ReferenceManager.getInstance().getLogger(); |
236 |
|
237 |
/** |
238 |
* A reference to the Alerter queue, into which |
239 |
* all new alerts will be placed. |
240 |
*/ |
241 |
protected Queue _alerterQueue = AlerterManager.getInstance().getQueue(); |
242 |
|
243 |
/** |
244 |
* A reference to the configuration proxy in use |
245 |
*/ |
246 |
private ConfigurationProxy _cp = ConfigurationProxy.getInstance(); |
247 |
|
248 |
/** |
249 |
* The ID of the queue the monitor will use. |
250 |
* Initially -1, but initialised on first use. |
251 |
*/ |
252 |
protected int _qID = -1; |
253 |
|
254 |
/** |
255 |
* The state of the alerter thread |
256 |
*/ |
257 |
protected boolean _running = true; |
258 |
|
259 |
//---STATIC ATTRIBUTES--- |
260 |
|
261 |
} |