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