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.11
Committed: Fri Mar 23 01:32:38 2001 UTC (23 years, 1 month ago) by ajm
Branch: MAIN
CVS Tags: PROJECT_COMPLETION
Changes since 1.10: +75 -4 lines
Log Message:
Lots of commenting to explain their use.

File Contents

# Content
1 //---PACKAGE DECLARATION---
2 package uk.org.iscream.client;
3
4 //---IMPORTS---
5 import java.util.HashMap;
6 import uk.org.iscream.client.*;
7 import uk.org.iscream.core.*;
8 import uk.org.iscream.util.*;
9 import uk.org.iscream.componentmanager.*;
10
11 /**
12 * Skeleton class for Monitors
13 * 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 *
18 * @author $Author: ajm4 $
19 * @version $Id: MonitorSkeleton.java,v 1.10 2001/03/22 23:35:11 ajm4 Exp $
20 */
21 public abstract class MonitorSkeleton extends Thread implements PluginMonitor {
22
23 //---FINAL ATTRIBUTES---
24
25 /**
26 * The current CVS revision of this class
27 */
28 public final String REVISION = "$Revision: 1.21 $";
29
30 //---STATIC METHODS---
31
32 //---CONSTRUCTORS---
33
34 /**
35 * Constructs and start the monitor reading its data
36 */
37 public MonitorSkeleton() {
38 _logger.write(toString(), Logger.SYSINIT, "started.");
39 this.start();
40 }
41
42 //---PUBLIC METHODS---
43
44 /**
45 * Obtains data from the monitors data queue and
46 * passes the packet to the analysePacket method.
47 */
48 public void run() {
49 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 }
56 }
57
58 /**
59 * Extending classes should override this method to
60 * analyse the given packet for the attribute
61 * they are responsible for.
62 */
63 protected abstract void analysePacket(XMLPacket packet);
64
65 /**
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 protected void processAlert(int newThreshold, String attributeName, Register reg, String source, String currentValue) {
78 // decide what threshold level we're on, if we've changed, record that
79 if (newThreshold != reg.getLastThresholdLevel()) {
80 reg.setLastThresholdLevel(newThreshold);
81 }
82 // as long as this isn't a normal level
83 if(reg.getLastThresholdLevel() != Alert.thresholdNORMAL) {
84 // if the time since the last alert is more than the time for
85 // its timeout, fire an alert, escalate the alert
86 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 }
95 // if we don't have a timeout configured...we got STRAIGHT to the next level
96 } else {
97 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 }
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 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 }
119 }
120 }
121 }
122 }
123
124 /**
125 * Return the String representation of what the alerter does
126 *
127 * @return the description
128 */
129 public abstract String getDescription();
130
131
132 //---PRIVATE METHODS---
133
134 /**
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 int alertLevel = reg.getLastAlertLevel();
148 int thresholdLevel = reg.getLastThresholdLevel();
149 String thresholdValue = String.valueOf(reg.getThreshold(thresholdLevel));
150 String timeout = String.valueOf(reg.getAlertTimeout(reg.getLastAlertLevel()) / 1000);
151 // ensures we display a nice thing if its -1.0
152 if (thresholdValue.equals("-1.0")) {
153 thresholdValue = "-";
154 }
155 if (alertLevel == Alert.alertOK) {
156 timeout = "0";
157 }
158 Alert alert = new Alert(alertLevel, lastAlert, thresholdLevel, source, thresholdValue, currentValue, attributeName, timeout, reg.getInitialAlertTime());
159 _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 /**
166 * Obtain the queue which contains the data
167 * the Monitor is reading.
168 * eg, MonitorManager.getInstance().getHeartbeatQueue()
169 */
170 protected abstract Queue getQueue();
171
172 /**
173 * Create a queue ID on the feeding
174 * data queue
175 */
176 protected int getQueueId() {
177 if (_qID == -1) {
178 _qID = getQueue().getQueue();
179 _logger.write(toString(), Logger.DEBUG, "Assigned Queue - " + _qID);
180 }
181 return _qID;
182 }
183
184 //---ATTRIBUTES---
185
186 /**
187 * This holds a reference to the
188 * system logger that is being used.
189 */
190 protected Logger _logger = ReferenceManager.getInstance().getLogger();
191
192 /**
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
198 /**
199 * The ID of the queue the monitor will use.
200 * Initially -1, but initialised on first use.
201 */
202 protected int _qID = -1;
203
204 /**
205 * The state of the alerter thread
206 */
207 protected boolean _running = true;
208
209 //---STATIC ATTRIBUTES---
210
211 }