1 |
tdb |
1.13 |
/* |
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 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
21 |
tdb |
1.12 |
package uk.org.iscream.cms.server.client; |
22 |
tdb |
1.1 |
|
23 |
|
|
//---IMPORTS--- |
24 |
|
|
import java.util.HashMap; |
25 |
tdb |
1.12 |
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 |
tdb |
1.1 |
|
30 |
|
|
/** |
31 |
|
|
* Skeleton class for Monitors |
32 |
ajm |
1.11 |
* 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 |
tdb |
1.1 |
* |
37 |
tdb |
1.13 |
* @author $Author: tdb $ |
38 |
|
|
* @version $Id: MonitorSkeleton.java,v 1.12 2001/05/29 17:02:34 tdb Exp $ |
39 |
tdb |
1.1 |
*/ |
40 |
ajm |
1.7 |
public abstract class MonitorSkeleton extends Thread implements PluginMonitor { |
41 |
tdb |
1.1 |
|
42 |
|
|
//---FINAL ATTRIBUTES--- |
43 |
ajm |
1.11 |
|
44 |
|
|
/** |
45 |
|
|
* The current CVS revision of this class |
46 |
|
|
*/ |
47 |
tdb |
1.13 |
public final String REVISION = "$Revision: 1.12 $"; |
48 |
tdb |
1.1 |
|
49 |
|
|
//---STATIC METHODS--- |
50 |
|
|
|
51 |
|
|
//---CONSTRUCTORS--- |
52 |
|
|
|
53 |
ajm |
1.11 |
/** |
54 |
|
|
* Constructs and start the monitor reading its data |
55 |
|
|
*/ |
56 |
ajm |
1.7 |
public MonitorSkeleton() { |
57 |
ajm |
1.10 |
_logger.write(toString(), Logger.SYSINIT, "started."); |
58 |
ajm |
1.7 |
this.start(); |
59 |
|
|
} |
60 |
|
|
|
61 |
tdb |
1.1 |
//---PUBLIC METHODS--- |
62 |
|
|
|
63 |
ajm |
1.11 |
/** |
64 |
|
|
* Obtains data from the monitors data queue and |
65 |
|
|
* passes the packet to the analysePacket method. |
66 |
|
|
*/ |
67 |
ajm |
1.7 |
public void run() { |
68 |
ajm |
1.8 |
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 |
ajm |
1.7 |
} |
75 |
|
|
} |
76 |
|
|
|
77 |
ajm |
1.11 |
/** |
78 |
|
|
* Extending classes should override this method to |
79 |
|
|
* analyse the given packet for the attribute |
80 |
|
|
* they are responsible for. |
81 |
|
|
*/ |
82 |
ajm |
1.7 |
protected abstract void analysePacket(XMLPacket packet); |
83 |
tdb |
1.1 |
|
84 |
ajm |
1.11 |
/** |
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 |
ajm |
1.7 |
protected void processAlert(int newThreshold, String attributeName, Register reg, String source, String currentValue) { |
97 |
tdb |
1.1 |
// decide what threshold level we're on, if we've changed, record that |
98 |
ajm |
1.3 |
if (newThreshold != reg.getLastThresholdLevel()) { |
99 |
|
|
reg.setLastThresholdLevel(newThreshold); |
100 |
tdb |
1.1 |
} |
101 |
|
|
// as long as this isn't a normal level |
102 |
ajm |
1.3 |
if(reg.getLastThresholdLevel() != Alert.thresholdNORMAL) { |
103 |
tdb |
1.1 |
// if the time since the last alert is more than the time for |
104 |
|
|
// its timeout, fire an alert, escalate the alert |
105 |
ajm |
1.3 |
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 |
tdb |
1.1 |
} |
114 |
|
|
// if we don't have a timeout configured...we got STRAIGHT to the next level |
115 |
|
|
} else { |
116 |
ajm |
1.3 |
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 |
tdb |
1.1 |
} |
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 |
ajm |
1.3 |
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 |
tdb |
1.1 |
} |
138 |
|
|
} |
139 |
|
|
} |
140 |
|
|
} |
141 |
|
|
} |
142 |
|
|
|
143 |
|
|
/** |
144 |
ajm |
1.11 |
* Return the String representation of what the alerter does |
145 |
|
|
* |
146 |
|
|
* @return the description |
147 |
tdb |
1.1 |
*/ |
148 |
|
|
public abstract String getDescription(); |
149 |
|
|
|
150 |
|
|
|
151 |
|
|
//---PRIVATE METHODS--- |
152 |
|
|
|
153 |
ajm |
1.11 |
/** |
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 |
ajm |
1.3 |
int alertLevel = reg.getLastAlertLevel(); |
167 |
|
|
int thresholdLevel = reg.getLastThresholdLevel(); |
168 |
tdb |
1.1 |
String thresholdValue = String.valueOf(reg.getThreshold(thresholdLevel)); |
169 |
ajm |
1.3 |
String timeout = String.valueOf(reg.getAlertTimeout(reg.getLastAlertLevel()) / 1000); |
170 |
ajm |
1.4 |
// ensures we display a nice thing if its -1.0 |
171 |
tdb |
1.6 |
if (thresholdValue.equals("-1.0")) { |
172 |
tdb |
1.1 |
thresholdValue = "-"; |
173 |
|
|
} |
174 |
|
|
if (alertLevel == Alert.alertOK) { |
175 |
|
|
timeout = "0"; |
176 |
|
|
} |
177 |
ajm |
1.3 |
Alert alert = new Alert(alertLevel, lastAlert, thresholdLevel, source, thresholdValue, currentValue, attributeName, timeout, reg.getInitialAlertTime()); |
178 |
tdb |
1.1 |
_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 |
ajm |
1.11 |
/** |
185 |
|
|
* Obtain the queue which contains the data |
186 |
|
|
* the Monitor is reading. |
187 |
|
|
* eg, MonitorManager.getInstance().getHeartbeatQueue() |
188 |
|
|
*/ |
189 |
ajm |
1.7 |
protected abstract Queue getQueue(); |
190 |
|
|
|
191 |
ajm |
1.11 |
/** |
192 |
|
|
* Create a queue ID on the feeding |
193 |
|
|
* data queue |
194 |
|
|
*/ |
195 |
ajm |
1.7 |
protected int getQueueId() { |
196 |
|
|
if (_qID == -1) { |
197 |
|
|
_qID = getQueue().getQueue(); |
198 |
ajm |
1.9 |
_logger.write(toString(), Logger.DEBUG, "Assigned Queue - " + _qID); |
199 |
ajm |
1.7 |
} |
200 |
|
|
return _qID; |
201 |
|
|
} |
202 |
|
|
|
203 |
tdb |
1.1 |
//---ATTRIBUTES--- |
204 |
|
|
|
205 |
ajm |
1.11 |
/** |
206 |
|
|
* This holds a reference to the |
207 |
|
|
* system logger that is being used. |
208 |
|
|
*/ |
209 |
tdb |
1.1 |
protected Logger _logger = ReferenceManager.getInstance().getLogger(); |
210 |
|
|
|
211 |
ajm |
1.11 |
/** |
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 |
ajm |
1.7 |
|
217 |
ajm |
1.11 |
/** |
218 |
|
|
* The ID of the queue the monitor will use. |
219 |
|
|
* Initially -1, but initialised on first use. |
220 |
|
|
*/ |
221 |
ajm |
1.7 |
protected int _qID = -1; |
222 |
ajm |
1.8 |
|
223 |
ajm |
1.11 |
/** |
224 |
|
|
* The state of the alerter thread |
225 |
|
|
*/ |
226 |
ajm |
1.8 |
protected boolean _running = true; |
227 |
tdb |
1.1 |
|
228 |
|
|
//---STATIC ATTRIBUTES--- |
229 |
|
|
|
230 |
|
|
} |