1 |
/* |
2 |
* i-scream central monitoring system |
3 |
* Copyright (C) 2000-2002 i-scream |
4 |
* |
5 |
* This program is free software; you can redistribute it and/or |
6 |
* modify it under the terms of the GNU General Public License |
7 |
* as published by the Free Software Foundation; either version 2 |
8 |
* of the License, or (at your option) any later version. |
9 |
* |
10 |
* This program is distributed in the hope that it will be useful, |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
* GNU General Public License for more details. |
14 |
* |
15 |
* You should have received a copy of the GNU General Public License |
16 |
* along with this program; if not, write to the Free Software |
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
*/ |
19 |
|
20 |
//---PACKAGE DECLARATION--- |
21 |
package uk.org.iscream.cms.server.client; |
22 |
|
23 |
//---IMPORTS--- |
24 |
import java.util.HashMap; |
25 |
import uk.org.iscream.cms.server.client.*; |
26 |
import uk.org.iscream.cms.server.core.*; |
27 |
import uk.org.iscream.cms.server.util.*; |
28 |
import uk.org.iscream.cms.server.componentmanager.*; |
29 |
|
30 |
/** |
31 |
* Skeleton class for Monitors |
32 |
* This skeleton reads packets from a queue designated |
33 |
* by the extending class, it then feeds the data to the analysePacket |
34 |
* method, which the extending class should implement. The class |
35 |
* should then handle the monitoring for that packet. |
36 |
* |
37 |
* @author $Author: tdb $ |
38 |
* @version $Id: MonitorSkeleton.java,v 1.12 2001/05/29 17:02:34 tdb Exp $ |
39 |
*/ |
40 |
public abstract class MonitorSkeleton extends Thread implements PluginMonitor { |
41 |
|
42 |
//---FINAL ATTRIBUTES--- |
43 |
|
44 |
/** |
45 |
* The current CVS revision of this class |
46 |
*/ |
47 |
public final String REVISION = "$Revision: 1.12 $"; |
48 |
|
49 |
//---STATIC METHODS--- |
50 |
|
51 |
//---CONSTRUCTORS--- |
52 |
|
53 |
/** |
54 |
* Constructs and start the monitor reading its data |
55 |
*/ |
56 |
public MonitorSkeleton() { |
57 |
_logger.write(toString(), Logger.SYSINIT, "started."); |
58 |
this.start(); |
59 |
} |
60 |
|
61 |
//---PUBLIC METHODS--- |
62 |
|
63 |
/** |
64 |
* Obtains data from the monitors data queue and |
65 |
* passes the packet to the analysePacket method. |
66 |
*/ |
67 |
public void run() { |
68 |
while(_running) { |
69 |
try { |
70 |
analysePacket((XMLPacket) getQueue().get(getQueueId())); |
71 |
} catch (InvalidQueueException e) { |
72 |
_logger.write(this.toString(), Logger.ERROR, "Unable to get queue."); |
73 |
} |
74 |
} |
75 |
} |
76 |
|
77 |
/** |
78 |
* Extending classes should override this method to |
79 |
* analyse the given packet for the attribute |
80 |
* they are responsible for. |
81 |
*/ |
82 |
protected abstract void analysePacket(XMLPacket packet); |
83 |
|
84 |
/** |
85 |
* Once a Monitor has determined which threshold the given data packet |
86 |
* is at, it should then call this method. This method handles ALL |
87 |
* the alerting logic to determine escalation of alerts. |
88 |
* If it decides an alert needs to be send, it will send one using fireAlert. |
89 |
* |
90 |
* @param newThreshold the threshold that has been determined by the monitor |
91 |
* @param attributeName the textual name of the attribute the monitor is responsible for |
92 |
* @param reg the register that holds the current alert state for the machine/attribute |
93 |
* @param source the source of the alert eg, hostname |
94 |
* @param currentValue the data value for the attribute |
95 |
*/ |
96 |
protected void processAlert(int newThreshold, String attributeName, Register reg, String source, String currentValue) { |
97 |
// decide what threshold level we're on, if we've changed, record that |
98 |
if (newThreshold != reg.getLastThresholdLevel()) { |
99 |
reg.setLastThresholdLevel(newThreshold); |
100 |
} |
101 |
// as long as this isn't a normal level |
102 |
if(reg.getLastThresholdLevel() != Alert.thresholdNORMAL) { |
103 |
// if the time since the last alert is more than the time for |
104 |
// its timeout, fire an alert, escalate the alert |
105 |
long timeout = reg.getLastAlertTimeout(); |
106 |
if ((timeout > 0) && (reg.getTimeLastSent() > 0)) { |
107 |
if((System.currentTimeMillis() - reg.getTimeLastSent()) > timeout) { |
108 |
int lastAlert = reg.getLastAlertLevel(); |
109 |
reg.escalateAlert(); |
110 |
reg.setTimeLastSent( System.currentTimeMillis()); |
111 |
reg.setLastAlertTimeout(reg.getAlertTimeout(reg.getLastAlertLevel())); |
112 |
fireAlert(reg, lastAlert, source, currentValue, attributeName); |
113 |
} |
114 |
// if we don't have a timeout configured...we got STRAIGHT to the next level |
115 |
} else { |
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 |
|
123 |
// we must be on ok, check the timeout value for this |
124 |
} else { |
125 |
// if we were on an OK alert before, then we don't do anything |
126 |
// but if we weren't we only set OK, once the timout of the last |
127 |
// alert has occourd |
128 |
if (reg.getLastAlertLevel() != Alert.alertOK) { |
129 |
long timeout = reg.getLastAlertTimeout(); |
130 |
if ((timeout > 0) && (reg.getTimeLastSent() > 0)) { |
131 |
if ((System.currentTimeMillis() - reg.getTimeLastSent()) > timeout) { |
132 |
int lastAlert = reg.getLastAlertLevel(); |
133 |
reg.setLastAlertLevel(Alert.alertOK); |
134 |
reg.setTimeLastSent(System.currentTimeMillis()); |
135 |
reg.setLastAlertTimeout(timeout); |
136 |
fireAlert(reg, lastAlert, source, currentValue, attributeName); |
137 |
} |
138 |
} |
139 |
} |
140 |
} |
141 |
} |
142 |
|
143 |
/** |
144 |
* Return the String representation of what the alerter does |
145 |
* |
146 |
* @return the description |
147 |
*/ |
148 |
public abstract String getDescription(); |
149 |
|
150 |
|
151 |
//---PRIVATE METHODS--- |
152 |
|
153 |
/** |
154 |
* Fires an alert. This creates a new Alert object |
155 |
* and populates it with the given alert information. |
156 |
* It then adds the alert to the Alerter queue. |
157 |
* |
158 |
* This method should only be called by the processAlert method. |
159 |
* |
160 |
* @param reg the register holding the state values for the alert |
161 |
* @param source the source of the alert eg, hostname |
162 |
* @param currentValue the data value for the attribute |
163 |
* @param attributeName the textual name of the attribute the alert is for |
164 |
*/ |
165 |
private void fireAlert(Register reg, int lastAlert, String source, String currentValue, String attributeName) { |
166 |
int alertLevel = reg.getLastAlertLevel(); |
167 |
int thresholdLevel = reg.getLastThresholdLevel(); |
168 |
String thresholdValue = String.valueOf(reg.getThreshold(thresholdLevel)); |
169 |
String timeout = String.valueOf(reg.getAlertTimeout(reg.getLastAlertLevel()) / 1000); |
170 |
// ensures we display a nice thing if its -1.0 |
171 |
if (thresholdValue.equals("-1.0")) { |
172 |
thresholdValue = "-"; |
173 |
} |
174 |
if (alertLevel == Alert.alertOK) { |
175 |
timeout = "0"; |
176 |
} |
177 |
Alert alert = new Alert(alertLevel, lastAlert, thresholdLevel, source, thresholdValue, currentValue, attributeName, timeout, reg.getInitialAlertTime()); |
178 |
_alerterQueue.add(alert); |
179 |
_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"); |
180 |
} |
181 |
|
182 |
//---ACCESSOR/MUTATOR METHODS--- |
183 |
|
184 |
/** |
185 |
* Obtain the queue which contains the data |
186 |
* the Monitor is reading. |
187 |
* eg, MonitorManager.getInstance().getHeartbeatQueue() |
188 |
*/ |
189 |
protected abstract Queue getQueue(); |
190 |
|
191 |
/** |
192 |
* Create a queue ID on the feeding |
193 |
* data queue |
194 |
*/ |
195 |
protected int getQueueId() { |
196 |
if (_qID == -1) { |
197 |
_qID = getQueue().getQueue(); |
198 |
_logger.write(toString(), Logger.DEBUG, "Assigned Queue - " + _qID); |
199 |
} |
200 |
return _qID; |
201 |
} |
202 |
|
203 |
//---ATTRIBUTES--- |
204 |
|
205 |
/** |
206 |
* This holds a reference to the |
207 |
* system logger that is being used. |
208 |
*/ |
209 |
protected Logger _logger = ReferenceManager.getInstance().getLogger(); |
210 |
|
211 |
/** |
212 |
* A reference to the Alerter queue, into which |
213 |
* all new alerts will be placed. |
214 |
*/ |
215 |
protected Queue _alerterQueue = AlerterManager.getInstance().getQueue(); |
216 |
|
217 |
/** |
218 |
* The ID of the queue the monitor will use. |
219 |
* Initially -1, but initialised on first use. |
220 |
*/ |
221 |
protected int _qID = -1; |
222 |
|
223 |
/** |
224 |
* The state of the alerter thread |
225 |
*/ |
226 |
protected boolean _running = true; |
227 |
|
228 |
//---STATIC ATTRIBUTES--- |
229 |
|
230 |
} |