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.14
Committed: Tue May 21 16:47:16 2002 UTC (22 years ago) by tdb
Branch: MAIN
Changes since 1.13: +3 -2 lines
Log Message:
Added URL to GPL headers.

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     import uk.org.iscream.cms.server.util.*;
29     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.14 * @version $Id: MonitorSkeleton.java,v 1.13 2002/05/18 18:16:00 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.14 public final String REVISION = "$Revision: 1.13 $";
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     analysePacket((XMLPacket) getQueue().get(getQueueId()));
72     } catch (InvalidQueueException e) {
73     _logger.write(this.toString(), Logger.ERROR, "Unable to get queue.");
74     }
75 ajm 1.7 }
76     }
77    
78 ajm 1.11 /**
79     * Extending classes should override this method to
80     * analyse the given packet for the attribute
81     * they are responsible for.
82     */
83 ajm 1.7 protected abstract void analysePacket(XMLPacket packet);
84 tdb 1.1
85 ajm 1.11 /**
86     * Once a Monitor has determined which threshold the given data packet
87     * is at, it should then call this method. This method handles ALL
88     * the alerting logic to determine escalation of alerts.
89     * If it decides an alert needs to be send, it will send one using fireAlert.
90     *
91     * @param newThreshold the threshold that has been determined by the monitor
92     * @param attributeName the textual name of the attribute the monitor is responsible for
93     * @param reg the register that holds the current alert state for the machine/attribute
94     * @param source the source of the alert eg, hostname
95     * @param currentValue the data value for the attribute
96     */
97 ajm 1.7 protected void processAlert(int newThreshold, String attributeName, Register reg, String source, String currentValue) {
98 tdb 1.1 // decide what threshold level we're on, if we've changed, record that
99 ajm 1.3 if (newThreshold != reg.getLastThresholdLevel()) {
100     reg.setLastThresholdLevel(newThreshold);
101 tdb 1.1 }
102     // as long as this isn't a normal level
103 ajm 1.3 if(reg.getLastThresholdLevel() != Alert.thresholdNORMAL) {
104 tdb 1.1 // if the time since the last alert is more than the time for
105     // its timeout, fire an alert, escalate the alert
106 ajm 1.3 long timeout = reg.getLastAlertTimeout();
107     if ((timeout > 0) && (reg.getTimeLastSent() > 0)) {
108     if((System.currentTimeMillis() - reg.getTimeLastSent()) > timeout) {
109     int lastAlert = reg.getLastAlertLevel();
110     reg.escalateAlert();
111     reg.setTimeLastSent( System.currentTimeMillis());
112     reg.setLastAlertTimeout(reg.getAlertTimeout(reg.getLastAlertLevel()));
113     fireAlert(reg, lastAlert, source, currentValue, attributeName);
114 tdb 1.1 }
115     // if we don't have a timeout configured...we got STRAIGHT to the next level
116     } else {
117 ajm 1.3 int lastAlert = reg.getLastAlertLevel();
118     reg.escalateAlert();
119     reg.setTimeLastSent( System.currentTimeMillis());
120     reg.setLastAlertTimeout(reg.getAlertTimeout(reg.getLastAlertLevel()));
121     fireAlert(reg, lastAlert, source, currentValue, attributeName);
122 tdb 1.1 }
123    
124     // we must be on ok, check the timeout value for this
125     } else {
126     // if we were on an OK alert before, then we don't do anything
127     // but if we weren't we only set OK, once the timout of the last
128     // alert has occourd
129 ajm 1.3 if (reg.getLastAlertLevel() != Alert.alertOK) {
130     long timeout = reg.getLastAlertTimeout();
131     if ((timeout > 0) && (reg.getTimeLastSent() > 0)) {
132     if ((System.currentTimeMillis() - reg.getTimeLastSent()) > timeout) {
133     int lastAlert = reg.getLastAlertLevel();
134     reg.setLastAlertLevel(Alert.alertOK);
135     reg.setTimeLastSent(System.currentTimeMillis());
136     reg.setLastAlertTimeout(timeout);
137     fireAlert(reg, lastAlert, source, currentValue, attributeName);
138 tdb 1.1 }
139     }
140     }
141     }
142     }
143    
144     /**
145 ajm 1.11 * Return the String representation of what the alerter does
146     *
147     * @return the description
148 tdb 1.1 */
149     public abstract String getDescription();
150    
151    
152     //---PRIVATE METHODS---
153    
154 ajm 1.11 /**
155     * Fires an alert. This creates a new Alert object
156     * and populates it with the given alert information.
157     * It then adds the alert to the Alerter queue.
158     *
159     * This method should only be called by the processAlert method.
160     *
161     * @param reg the register holding the state values for the alert
162     * @param source the source of the alert eg, hostname
163     * @param currentValue the data value for the attribute
164     * @param attributeName the textual name of the attribute the alert is for
165     */
166     private void fireAlert(Register reg, int lastAlert, String source, String currentValue, String attributeName) {
167 ajm 1.3 int alertLevel = reg.getLastAlertLevel();
168     int thresholdLevel = reg.getLastThresholdLevel();
169 tdb 1.1 String thresholdValue = String.valueOf(reg.getThreshold(thresholdLevel));
170 ajm 1.3 String timeout = String.valueOf(reg.getAlertTimeout(reg.getLastAlertLevel()) / 1000);
171 ajm 1.4 // ensures we display a nice thing if its -1.0
172 tdb 1.6 if (thresholdValue.equals("-1.0")) {
173 tdb 1.1 thresholdValue = "-";
174     }
175     if (alertLevel == Alert.alertOK) {
176     timeout = "0";
177     }
178 ajm 1.3 Alert alert = new Alert(alertLevel, lastAlert, thresholdLevel, source, thresholdValue, currentValue, attributeName, timeout, reg.getInitialAlertTime());
179 tdb 1.1 _alerterQueue.add(alert);
180     _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");
181     }
182    
183     //---ACCESSOR/MUTATOR METHODS---
184    
185 ajm 1.11 /**
186     * Obtain the queue which contains the data
187     * the Monitor is reading.
188     * eg, MonitorManager.getInstance().getHeartbeatQueue()
189     */
190 ajm 1.7 protected abstract Queue getQueue();
191    
192 ajm 1.11 /**
193     * Create a queue ID on the feeding
194     * data queue
195     */
196 ajm 1.7 protected int getQueueId() {
197     if (_qID == -1) {
198     _qID = getQueue().getQueue();
199 ajm 1.9 _logger.write(toString(), Logger.DEBUG, "Assigned Queue - " + _qID);
200 ajm 1.7 }
201     return _qID;
202     }
203    
204 tdb 1.1 //---ATTRIBUTES---
205    
206 ajm 1.11 /**
207     * This holds a reference to the
208     * system logger that is being used.
209     */
210 tdb 1.1 protected Logger _logger = ReferenceManager.getInstance().getLogger();
211    
212 ajm 1.11 /**
213     * A reference to the Alerter queue, into which
214     * all new alerts will be placed.
215     */
216     protected Queue _alerterQueue = AlerterManager.getInstance().getQueue();
217 ajm 1.7
218 ajm 1.11 /**
219     * The ID of the queue the monitor will use.
220     * Initially -1, but initialised on first use.
221     */
222 ajm 1.7 protected int _qID = -1;
223 ajm 1.8
224 ajm 1.11 /**
225     * The state of the alerter thread
226     */
227 ajm 1.8 protected boolean _running = true;
228 tdb 1.1
229     //---STATIC ATTRIBUTES---
230    
231     }