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.8
Committed: Thu Mar 22 18:28:17 2001 UTC (23 years, 2 months ago) by ajm
Branch: MAIN
Changes since 1.7: +10 -6 lines
Log Message:
now correctly process data from the inbound queue

File Contents

# User Rev Content
1 tdb 1.1 //---PACKAGE DECLARATION---
2 tdb 1.5 package uk.org.iscream.client;
3 tdb 1.1
4     //---IMPORTS---
5     import java.util.HashMap;
6 tdb 1.5 import uk.org.iscream.client.*;
7     import uk.org.iscream.core.*;
8     import uk.org.iscream.util.*;
9     import uk.org.iscream.componentmanager.*;
10 tdb 1.1
11     /**
12     * Skeleton class for Monitors
13     *
14 ajm 1.8 * @author $Author: ajm4 $
15     * @version $Id: MonitorSkeleton.java,v 1.7 2001/03/22 17:56:58 ajm4 Exp $
16 tdb 1.1 */
17 ajm 1.7 public abstract class MonitorSkeleton extends Thread implements PluginMonitor {
18 tdb 1.1
19     //---FINAL ATTRIBUTES---
20    
21     //---STATIC METHODS---
22    
23     //---CONSTRUCTORS---
24    
25 ajm 1.7 public MonitorSkeleton() {
26     this.start();
27     }
28    
29 tdb 1.1 //---PUBLIC METHODS---
30    
31 ajm 1.7 public void run() {
32 ajm 1.8 while(_running) {
33     try {
34     analysePacket((XMLPacket) getQueue().get(getQueueId()));
35     } catch (InvalidQueueException e) {
36     _logger.write(this.toString(), Logger.ERROR, "Unable to get queue.");
37     }
38 ajm 1.7 }
39     }
40    
41     protected abstract void analysePacket(XMLPacket packet);
42 tdb 1.1
43 ajm 1.7 protected void processAlert(int newThreshold, String attributeName, Register reg, String source, String currentValue) {
44 tdb 1.1 // decide what threshold level we're on, if we've changed, record that
45 ajm 1.3 if (newThreshold != reg.getLastThresholdLevel()) {
46     reg.setLastThresholdLevel(newThreshold);
47 tdb 1.1 }
48     // as long as this isn't a normal level
49 ajm 1.3 if(reg.getLastThresholdLevel() != Alert.thresholdNORMAL) {
50 tdb 1.1 // if the time since the last alert is more than the time for
51     // its timeout, fire an alert, escalate the alert
52 ajm 1.3 long timeout = reg.getLastAlertTimeout();
53     if ((timeout > 0) && (reg.getTimeLastSent() > 0)) {
54     if((System.currentTimeMillis() - reg.getTimeLastSent()) > timeout) {
55     int lastAlert = reg.getLastAlertLevel();
56     reg.escalateAlert();
57     reg.setTimeLastSent( System.currentTimeMillis());
58     reg.setLastAlertTimeout(reg.getAlertTimeout(reg.getLastAlertLevel()));
59     fireAlert(reg, lastAlert, source, currentValue, attributeName);
60 tdb 1.1 }
61     // if we don't have a timeout configured...we got STRAIGHT to the next level
62     } else {
63 ajm 1.3 int lastAlert = reg.getLastAlertLevel();
64     reg.escalateAlert();
65     reg.setTimeLastSent( System.currentTimeMillis());
66     reg.setLastAlertTimeout(reg.getAlertTimeout(reg.getLastAlertLevel()));
67     fireAlert(reg, lastAlert, source, currentValue, attributeName);
68 tdb 1.1 }
69    
70     // we must be on ok, check the timeout value for this
71     } else {
72     // if we were on an OK alert before, then we don't do anything
73     // but if we weren't we only set OK, once the timout of the last
74     // alert has occourd
75 ajm 1.3 if (reg.getLastAlertLevel() != Alert.alertOK) {
76     long timeout = reg.getLastAlertTimeout();
77     if ((timeout > 0) && (reg.getTimeLastSent() > 0)) {
78     if ((System.currentTimeMillis() - reg.getTimeLastSent()) > timeout) {
79     int lastAlert = reg.getLastAlertLevel();
80     reg.setLastAlertLevel(Alert.alertOK);
81     reg.setTimeLastSent(System.currentTimeMillis());
82     reg.setLastAlertTimeout(timeout);
83     fireAlert(reg, lastAlert, source, currentValue, attributeName);
84 tdb 1.1 }
85     }
86     }
87     }
88     }
89    
90     /**
91     * return the String representation of what the monitor does
92     */
93     public abstract String getDescription();
94    
95    
96     //---PRIVATE METHODS---
97    
98 ajm 1.3 protected void fireAlert(Register reg, int lastAlert, String source, String currentValue, String attributeName) {
99     int alertLevel = reg.getLastAlertLevel();
100     int thresholdLevel = reg.getLastThresholdLevel();
101 tdb 1.1 String thresholdValue = String.valueOf(reg.getThreshold(thresholdLevel));
102 ajm 1.3 String timeout = String.valueOf(reg.getAlertTimeout(reg.getLastAlertLevel()) / 1000);
103 ajm 1.4 // ensures we display a nice thing if its -1.0
104 tdb 1.6 if (thresholdValue.equals("-1.0")) {
105 tdb 1.1 thresholdValue = "-";
106     }
107     if (alertLevel == Alert.alertOK) {
108     timeout = "0";
109     }
110 ajm 1.3 Alert alert = new Alert(alertLevel, lastAlert, thresholdLevel, source, thresholdValue, currentValue, attributeName, timeout, reg.getInitialAlertTime());
111 tdb 1.1 _alerterQueue.add(alert);
112     _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");
113     }
114    
115     //---ACCESSOR/MUTATOR METHODS---
116    
117 ajm 1.7 protected abstract Queue getQueue();
118    
119     protected int getQueueId() {
120     if (_qID == -1) {
121     _qID = getQueue().getQueue();
122     }
123     return _qID;
124     }
125    
126 tdb 1.1 //---ATTRIBUTES---
127    
128     protected Logger _logger = ReferenceManager.getInstance().getLogger();
129    
130     protected Queue _alerterQueue = ClientMain._alerterQueue;
131 ajm 1.7
132     protected int _qID = -1;
133 ajm 1.8
134     protected boolean _running = true;
135 tdb 1.1
136     //---STATIC ATTRIBUTES---
137    
138     }