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.17
Committed: Thu Jan 15 13:41:47 2004 UTC (20 years, 4 months ago) by tdb
Branch: MAIN
Changes since 1.16: +34 -4 lines
Log Message:
Assuming I can still program in Java, these changes allow monitoring to
be disabled at a per-host level or a per-host-per-monitor level.

File Contents

# User Rev Content
1 tdb 1.13 /*
2     * i-scream central monitoring system
3 tdb 1.14 * http://www.i-scream.org.uk
4 tdb 1.13 * 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 tdb 1.1 //---PACKAGE DECLARATION---
22 tdb 1.12 package uk.org.iscream.cms.server.client;
23 tdb 1.1
24     //---IMPORTS---
25     import java.util.HashMap;
26 tdb 1.12 import uk.org.iscream.cms.server.client.*;
27     import uk.org.iscream.cms.server.core.*;
28 tdb 1.15 import uk.org.iscream.cms.util.*;
29 tdb 1.12 import uk.org.iscream.cms.server.componentmanager.*;
30 tdb 1.1
31     /**
32     * Skeleton class for Monitors
33 ajm 1.11 * 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 tdb 1.1 *
38 tdb 1.13 * @author $Author: tdb $
39 tdb 1.17 * @version $Id: MonitorSkeleton.java,v 1.16 2003/02/24 20:18:48 tdb Exp $
40 tdb 1.1 */
41 ajm 1.7 public abstract class MonitorSkeleton extends Thread implements PluginMonitor {
42 tdb 1.1
43     //---FINAL ATTRIBUTES---
44 ajm 1.11
45     /**
46     * The current CVS revision of this class
47     */
48 tdb 1.17 public final String REVISION = "$Revision: 1.16 $";
49 tdb 1.1
50     //---STATIC METHODS---
51    
52     //---CONSTRUCTORS---
53    
54 ajm 1.11 /**
55     * Constructs and start the monitor reading its data
56     */
57 ajm 1.7 public MonitorSkeleton() {
58 ajm 1.10 _logger.write(toString(), Logger.SYSINIT, "started.");
59 ajm 1.7 this.start();
60     }
61    
62 tdb 1.1 //---PUBLIC METHODS---
63    
64 ajm 1.11 /**
65     * Obtains data from the monitors data queue and
66     * passes the packet to the analysePacket method.
67     */
68 ajm 1.7 public void run() {
69 ajm 1.8 while(_running) {
70     try {
71 tdb 1.17 // 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(source, "Monitor.enable");
76     if(enabled) {
77     analysePacket(packet);
78     }
79 ajm 1.8 } catch (InvalidQueueException e) {
80     _logger.write(this.toString(), Logger.ERROR, "Unable to get queue.");
81     }
82 ajm 1.7 }
83     }
84    
85 ajm 1.11 /**
86     * Extending classes should override this method to
87     * analyse the given packet for the attribute
88     * they are responsible for.
89     */
90 ajm 1.7 protected abstract void analysePacket(XMLPacket packet);
91 tdb 1.1
92 ajm 1.11 /**
93     * Once a Monitor has determined which threshold the given data packet
94     * is at, it should then call this method. This method handles ALL
95     * the alerting logic to determine escalation of alerts.
96     * If it decides an alert needs to be send, it will send one using fireAlert.
97     *
98     * @param newThreshold the threshold that has been determined by the monitor
99     * @param attributeName the textual name of the attribute the monitor is responsible for
100     * @param reg the register that holds the current alert state for the machine/attribute
101     * @param source the source of the alert eg, hostname
102     * @param currentValue the data value for the attribute
103     */
104 ajm 1.7 protected void processAlert(int newThreshold, String attributeName, Register reg, String source, String currentValue) {
105 tdb 1.1 // decide what threshold level we're on, if we've changed, record that
106 ajm 1.3 if (newThreshold != reg.getLastThresholdLevel()) {
107     reg.setLastThresholdLevel(newThreshold);
108 tdb 1.1 }
109     // as long as this isn't a normal level
110 ajm 1.3 if(reg.getLastThresholdLevel() != Alert.thresholdNORMAL) {
111 tdb 1.1 // if the time since the last alert is more than the time for
112     // its timeout, fire an alert, escalate the alert
113 ajm 1.3 long timeout = reg.getLastAlertTimeout();
114     if ((timeout > 0) && (reg.getTimeLastSent() > 0)) {
115     if((System.currentTimeMillis() - reg.getTimeLastSent()) > timeout) {
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 tdb 1.1 }
122     // if we don't have a timeout configured...we got STRAIGHT to the next level
123     } else {
124 ajm 1.3 int lastAlert = reg.getLastAlertLevel();
125     reg.escalateAlert();
126     reg.setTimeLastSent( System.currentTimeMillis());
127     reg.setLastAlertTimeout(reg.getAlertTimeout(reg.getLastAlertLevel()));
128     fireAlert(reg, lastAlert, source, currentValue, attributeName);
129 tdb 1.1 }
130    
131     // we must be on ok, check the timeout value for this
132     } else {
133     // if we were on an OK alert before, then we don't do anything
134     // but if we weren't we only set OK, once the timout of the last
135     // alert has occourd
136 ajm 1.3 if (reg.getLastAlertLevel() != Alert.alertOK) {
137     long timeout = reg.getLastAlertTimeout();
138     if ((timeout > 0) && (reg.getTimeLastSent() > 0)) {
139     if ((System.currentTimeMillis() - reg.getTimeLastSent()) > timeout) {
140     int lastAlert = reg.getLastAlertLevel();
141     reg.setLastAlertLevel(Alert.alertOK);
142     reg.setTimeLastSent(System.currentTimeMillis());
143     reg.setLastAlertTimeout(timeout);
144     fireAlert(reg, lastAlert, source, currentValue, attributeName);
145 tdb 1.1 }
146     }
147     }
148     }
149     }
150    
151     /**
152 ajm 1.11 * Return the String representation of what the alerter does
153     *
154     * @return the description
155 tdb 1.1 */
156     public abstract String getDescription();
157    
158 tdb 1.17 /**
159     * Checks a boolean configuration variable.
160     *
161     * @param source the configuration to look up
162     * @param name the key to look up
163     */
164     protected boolean checkBooleanConfig(String source, String name) {
165     boolean enabled = true;
166     try {
167     int e = Integer.parseInt(_cp.getProperty(source, name));
168     enabled = (e == 1);
169     } catch (PropertyNotFoundException e) {
170     enabled = false;
171     } catch (NumberFormatException e) {
172     enabled = false;
173     }
174     return enabled;
175     }
176 tdb 1.1
177     //---PRIVATE METHODS---
178    
179 ajm 1.11 /**
180     * Fires an alert. This creates a new Alert object
181     * and populates it with the given alert information.
182     * It then adds the alert to the Alerter queue.
183     *
184     * This method should only be called by the processAlert method.
185     *
186     * @param reg the register holding the state values for the alert
187     * @param source the source of the alert eg, hostname
188     * @param currentValue the data value for the attribute
189     * @param attributeName the textual name of the attribute the alert is for
190     */
191     private void fireAlert(Register reg, int lastAlert, String source, String currentValue, String attributeName) {
192 ajm 1.3 int alertLevel = reg.getLastAlertLevel();
193     int thresholdLevel = reg.getLastThresholdLevel();
194 tdb 1.1 String thresholdValue = String.valueOf(reg.getThreshold(thresholdLevel));
195 ajm 1.3 String timeout = String.valueOf(reg.getAlertTimeout(reg.getLastAlertLevel()) / 1000);
196 ajm 1.4 // ensures we display a nice thing if its -1.0
197 tdb 1.6 if (thresholdValue.equals("-1.0")) {
198 tdb 1.1 thresholdValue = "-";
199     }
200     if (alertLevel == Alert.alertOK) {
201     timeout = "0";
202     }
203 ajm 1.3 Alert alert = new Alert(alertLevel, lastAlert, thresholdLevel, source, thresholdValue, currentValue, attributeName, timeout, reg.getInitialAlertTime());
204 tdb 1.1 _alerterQueue.add(alert);
205     _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");
206     }
207    
208     //---ACCESSOR/MUTATOR METHODS---
209    
210 ajm 1.11 /**
211     * Obtain the queue which contains the data
212     * the Monitor is reading.
213 tdb 1.16 * eg, MonitorManager.getInstance().getDataQueue()
214 ajm 1.11 */
215 ajm 1.7 protected abstract Queue getQueue();
216    
217 ajm 1.11 /**
218     * Create a queue ID on the feeding
219     * data queue
220     */
221 ajm 1.7 protected int getQueueId() {
222     if (_qID == -1) {
223     _qID = getQueue().getQueue();
224 ajm 1.9 _logger.write(toString(), Logger.DEBUG, "Assigned Queue - " + _qID);
225 ajm 1.7 }
226     return _qID;
227     }
228    
229 tdb 1.1 //---ATTRIBUTES---
230    
231 ajm 1.11 /**
232     * This holds a reference to the
233     * system logger that is being used.
234     */
235 tdb 1.1 protected Logger _logger = ReferenceManager.getInstance().getLogger();
236    
237 ajm 1.11 /**
238     * A reference to the Alerter queue, into which
239     * all new alerts will be placed.
240     */
241     protected Queue _alerterQueue = AlerterManager.getInstance().getQueue();
242 tdb 1.17
243     /**
244     * A reference to the configuration proxy in use
245     */
246     private ConfigurationProxy _cp = ConfigurationProxy.getInstance();
247    
248 ajm 1.11 /**
249     * The ID of the queue the monitor will use.
250     * Initially -1, but initialised on first use.
251     */
252 ajm 1.7 protected int _qID = -1;
253 ajm 1.8
254 ajm 1.11 /**
255     * The state of the alerter thread
256     */
257 ajm 1.8 protected boolean _running = true;
258 tdb 1.1
259     //---STATIC ATTRIBUTES---
260    
261     }