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
(Generate patch)

Comparing projects/cms/source/server/uk/org/iscream/cms/server/client/MonitorSkeleton.java (file contents):
Revision 1.1 by tdb, Tue Mar 6 22:33:54 2001 UTC vs.
Revision 1.12 by tdb, Tue May 29 17:02:34 2001 UTC

# Line 1 | Line 1
1   //---PACKAGE DECLARATION---
2 < package uk.ac.ukc.iscream.client.monitors;
2 > package uk.org.iscream.cms.server.client;
3  
4   //---IMPORTS---
5   import java.util.HashMap;
6 < import uk.ac.ukc.iscream.client.*;
7 < import uk.ac.ukc.iscream.core.*;
8 < import uk.ac.ukc.iscream.util.*;
9 < import uk.ac.ukc.iscream.componentmanager.*;
6 > 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  
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$
19   * @version $Id$
20   */
21 < public abstract class MonitorSkeleton implements PluginMonitor {
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$";
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 <    public abstract void analysePacket(XMLPacket packet);
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 <    public void processAlert(int newThreshold, int attributeNum, String attributeName, Register reg, String source, String currentValue) {
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(attributeNum)) {
80 <            reg.setLastThresholdLevel(attributeNum, newThreshold);
79 >        if (newThreshold != reg.getLastThresholdLevel()) {
80 >            reg.setLastThresholdLevel(newThreshold);
81          }
82          // as long as this isn't a normal level
83 <        if(reg.getLastThresholdLevel(attributeNum) != Alert.thresholdNORMAL) {
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(attributeNum);
87 <            if ((timeout > 0) && (reg.getTimeLastSent(attributeNum) > 0)) {
88 <                if((System.currentTimeMillis() - reg.getTimeLastSent(attributeNum)) > timeout) {
89 <                    int lastAlert = reg.getLastAlertLevel(attributeNum);
90 <                    reg.escalateAlert(attributeNum);
91 <                    reg.setTimeLastSent(attributeNum, System.currentTimeMillis());
92 <                    reg.setLastAlertTimeout(attributeNum, reg.getAlertTimeout(reg.getLastAlertLevel(attributeNum), attributeNum));
93 <                    fireAlert(reg, lastAlert, attributeNum, source, currentValue, attributeName);
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(attributeNum);
98 <                reg.escalateAlert(attributeNum);
99 <                reg.setTimeLastSent(attributeNum, System.currentTimeMillis());
100 <                reg.setLastAlertTimeout(attributeNum, reg.getAlertTimeout(reg.getLastAlertLevel(attributeNum), attributeNum));
101 <                fireAlert(reg, lastAlert, attributeNum, source, currentValue, attributeName);
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
# Line 58 | Line 106 | public abstract class MonitorSkeleton implements Plugi
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(attributeNum) != Alert.alertOK) {
110 <                long timeout = reg.getLastAlertTimeout(attributeNum);
111 <                if ((timeout > 0) && (reg.getTimeLastSent(attributeNum) > 0)) {
112 <                    if ((System.currentTimeMillis() - reg.getTimeLastSent(attributeNum)) > timeout) {
113 <                        int lastAlert = reg.getLastAlertLevel(attributeNum);
114 <                        reg.setLastAlertLevel(attributeNum, Alert.alertOK);
115 <                        reg.setTimeLastSent(attributeNum, System.currentTimeMillis());
116 <                        reg.setLastAlertTimeout(attributeNum, timeout);
117 <                        fireAlert(reg, lastAlert, attributeNum, source, currentValue, attributeName);
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              }
# Line 74 | Line 122 | public abstract class MonitorSkeleton implements Plugi
122      }
123  
124      /**
125 <     * return the String representation of what the monitor does
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 <    protected void fireAlert(Register reg, int lastAlert, int attributeNum, String source, String currentValue, String attributeName) {
135 <        int alertLevel = reg.getLastAlertLevel(attributeNum);
136 <        int thresholdLevel = reg.getLastThresholdLevel(attributeNum);
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(attributeNum), attributeNum) / 1000);
151 <        if (thresholdLevel == Alert.thresholdNORMAL) {
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(attributeNum));
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 <    protected Queue _alerterQueue = ClientMain._alerterQueue;
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  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines