ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/client/MonitorSkeleton.java
Revision: 1.19
Committed: Sun Aug 1 10:40:41 2004 UTC (19 years, 9 months ago) by tdb
Branch: MAIN
CVS Tags: HEAD
Changes since 1.18: +3 -3 lines
Error occurred while calculating annotation data.
Log Message:
Catch a lot of old URL's and update them. Also remove a couple of old files
that aren't used.

File Contents

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