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.server.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.13 2002/05/18 18:16:00 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.13 $"; |
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 |
analysePacket((XMLPacket) getQueue().get(getQueueId())); |
72 |
} catch (InvalidQueueException e) { |
73 |
_logger.write(this.toString(), Logger.ERROR, "Unable to get queue."); |
74 |
} |
75 |
} |
76 |
} |
77 |
|
78 |
/** |
79 |
* Extending classes should override this method to |
80 |
* analyse the given packet for the attribute |
81 |
* they are responsible for. |
82 |
*/ |
83 |
protected abstract void analysePacket(XMLPacket packet); |
84 |
|
85 |
/** |
86 |
* Once a Monitor has determined which threshold the given data packet |
87 |
* is at, it should then call this method. This method handles ALL |
88 |
* the alerting logic to determine escalation of alerts. |
89 |
* If it decides an alert needs to be send, it will send one using fireAlert. |
90 |
* |
91 |
* @param newThreshold the threshold that has been determined by the monitor |
92 |
* @param attributeName the textual name of the attribute the monitor is responsible for |
93 |
* @param reg the register that holds the current alert state for the machine/attribute |
94 |
* @param source the source of the alert eg, hostname |
95 |
* @param currentValue the data value for the attribute |
96 |
*/ |
97 |
protected void processAlert(int newThreshold, String attributeName, Register reg, String source, String currentValue) { |
98 |
// decide what threshold level we're on, if we've changed, record that |
99 |
if (newThreshold != reg.getLastThresholdLevel()) { |
100 |
reg.setLastThresholdLevel(newThreshold); |
101 |
} |
102 |
// as long as this isn't a normal level |
103 |
if(reg.getLastThresholdLevel() != Alert.thresholdNORMAL) { |
104 |
// if the time since the last alert is more than the time for |
105 |
// its timeout, fire an alert, escalate the alert |
106 |
long timeout = reg.getLastAlertTimeout(); |
107 |
if ((timeout > 0) && (reg.getTimeLastSent() > 0)) { |
108 |
if((System.currentTimeMillis() - reg.getTimeLastSent()) > timeout) { |
109 |
int lastAlert = reg.getLastAlertLevel(); |
110 |
reg.escalateAlert(); |
111 |
reg.setTimeLastSent( System.currentTimeMillis()); |
112 |
reg.setLastAlertTimeout(reg.getAlertTimeout(reg.getLastAlertLevel())); |
113 |
fireAlert(reg, lastAlert, source, currentValue, attributeName); |
114 |
} |
115 |
// if we don't have a timeout configured...we got STRAIGHT to the next level |
116 |
} else { |
117 |
int lastAlert = reg.getLastAlertLevel(); |
118 |
reg.escalateAlert(); |
119 |
reg.setTimeLastSent( System.currentTimeMillis()); |
120 |
reg.setLastAlertTimeout(reg.getAlertTimeout(reg.getLastAlertLevel())); |
121 |
fireAlert(reg, lastAlert, source, currentValue, attributeName); |
122 |
} |
123 |
|
124 |
// we must be on ok, check the timeout value for this |
125 |
} else { |
126 |
// if we were on an OK alert before, then we don't do anything |
127 |
// but if we weren't we only set OK, once the timout of the last |
128 |
// alert has occourd |
129 |
if (reg.getLastAlertLevel() != Alert.alertOK) { |
130 |
long timeout = reg.getLastAlertTimeout(); |
131 |
if ((timeout > 0) && (reg.getTimeLastSent() > 0)) { |
132 |
if ((System.currentTimeMillis() - reg.getTimeLastSent()) > timeout) { |
133 |
int lastAlert = reg.getLastAlertLevel(); |
134 |
reg.setLastAlertLevel(Alert.alertOK); |
135 |
reg.setTimeLastSent(System.currentTimeMillis()); |
136 |
reg.setLastAlertTimeout(timeout); |
137 |
fireAlert(reg, lastAlert, source, currentValue, attributeName); |
138 |
} |
139 |
} |
140 |
} |
141 |
} |
142 |
} |
143 |
|
144 |
/** |
145 |
* Return the String representation of what the alerter does |
146 |
* |
147 |
* @return the description |
148 |
*/ |
149 |
public abstract String getDescription(); |
150 |
|
151 |
|
152 |
//---PRIVATE METHODS--- |
153 |
|
154 |
/** |
155 |
* Fires an alert. This creates a new Alert object |
156 |
* and populates it with the given alert information. |
157 |
* It then adds the alert to the Alerter queue. |
158 |
* |
159 |
* This method should only be called by the processAlert method. |
160 |
* |
161 |
* @param reg the register holding the state values for the alert |
162 |
* @param source the source of the alert eg, hostname |
163 |
* @param currentValue the data value for the attribute |
164 |
* @param attributeName the textual name of the attribute the alert is for |
165 |
*/ |
166 |
private void fireAlert(Register reg, int lastAlert, String source, String currentValue, String attributeName) { |
167 |
int alertLevel = reg.getLastAlertLevel(); |
168 |
int thresholdLevel = reg.getLastThresholdLevel(); |
169 |
String thresholdValue = String.valueOf(reg.getThreshold(thresholdLevel)); |
170 |
String timeout = String.valueOf(reg.getAlertTimeout(reg.getLastAlertLevel()) / 1000); |
171 |
// ensures we display a nice thing if its -1.0 |
172 |
if (thresholdValue.equals("-1.0")) { |
173 |
thresholdValue = "-"; |
174 |
} |
175 |
if (alertLevel == Alert.alertOK) { |
176 |
timeout = "0"; |
177 |
} |
178 |
Alert alert = new Alert(alertLevel, lastAlert, thresholdLevel, source, thresholdValue, currentValue, attributeName, timeout, reg.getInitialAlertTime()); |
179 |
_alerterQueue.add(alert); |
180 |
_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"); |
181 |
} |
182 |
|
183 |
//---ACCESSOR/MUTATOR METHODS--- |
184 |
|
185 |
/** |
186 |
* Obtain the queue which contains the data |
187 |
* the Monitor is reading. |
188 |
* eg, MonitorManager.getInstance().getHeartbeatQueue() |
189 |
*/ |
190 |
protected abstract Queue getQueue(); |
191 |
|
192 |
/** |
193 |
* Create a queue ID on the feeding |
194 |
* data queue |
195 |
*/ |
196 |
protected int getQueueId() { |
197 |
if (_qID == -1) { |
198 |
_qID = getQueue().getQueue(); |
199 |
_logger.write(toString(), Logger.DEBUG, "Assigned Queue - " + _qID); |
200 |
} |
201 |
return _qID; |
202 |
} |
203 |
|
204 |
//---ATTRIBUTES--- |
205 |
|
206 |
/** |
207 |
* This holds a reference to the |
208 |
* system logger that is being used. |
209 |
*/ |
210 |
protected Logger _logger = ReferenceManager.getInstance().getLogger(); |
211 |
|
212 |
/** |
213 |
* A reference to the Alerter queue, into which |
214 |
* all new alerts will be placed. |
215 |
*/ |
216 |
protected Queue _alerterQueue = AlerterManager.getInstance().getQueue(); |
217 |
|
218 |
/** |
219 |
* The ID of the queue the monitor will use. |
220 |
* Initially -1, but initialised on first use. |
221 |
*/ |
222 |
protected int _qID = -1; |
223 |
|
224 |
/** |
225 |
* The state of the alerter thread |
226 |
*/ |
227 |
protected boolean _running = true; |
228 |
|
229 |
//---STATIC ATTRIBUTES--- |
230 |
|
231 |
} |