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 |
* |
14 |
* @author $Author: ajm4 $ |
15 |
* @version $Id: MonitorSkeleton.java,v 1.7 2001/03/22 17:56:58 ajm4 Exp $ |
16 |
*/ |
17 |
public abstract class MonitorSkeleton extends Thread implements PluginMonitor { |
18 |
|
19 |
//---FINAL ATTRIBUTES--- |
20 |
|
21 |
//---STATIC METHODS--- |
22 |
|
23 |
//---CONSTRUCTORS--- |
24 |
|
25 |
public MonitorSkeleton() { |
26 |
this.start(); |
27 |
} |
28 |
|
29 |
//---PUBLIC METHODS--- |
30 |
|
31 |
public void run() { |
32 |
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 |
} |
39 |
} |
40 |
|
41 |
protected abstract void analysePacket(XMLPacket packet); |
42 |
|
43 |
protected void processAlert(int newThreshold, String attributeName, Register reg, String source, String currentValue) { |
44 |
// decide what threshold level we're on, if we've changed, record that |
45 |
if (newThreshold != reg.getLastThresholdLevel()) { |
46 |
reg.setLastThresholdLevel(newThreshold); |
47 |
} |
48 |
// as long as this isn't a normal level |
49 |
if(reg.getLastThresholdLevel() != Alert.thresholdNORMAL) { |
50 |
// if the time since the last alert is more than the time for |
51 |
// its timeout, fire an alert, escalate the alert |
52 |
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 |
} |
61 |
// if we don't have a timeout configured...we got STRAIGHT to the next level |
62 |
} else { |
63 |
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 |
} |
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 |
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 |
} |
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 |
protected void fireAlert(Register reg, int lastAlert, String source, String currentValue, String attributeName) { |
99 |
int alertLevel = reg.getLastAlertLevel(); |
100 |
int thresholdLevel = reg.getLastThresholdLevel(); |
101 |
String thresholdValue = String.valueOf(reg.getThreshold(thresholdLevel)); |
102 |
String timeout = String.valueOf(reg.getAlertTimeout(reg.getLastAlertLevel()) / 1000); |
103 |
// ensures we display a nice thing if its -1.0 |
104 |
if (thresholdValue.equals("-1.0")) { |
105 |
thresholdValue = "-"; |
106 |
} |
107 |
if (alertLevel == Alert.alertOK) { |
108 |
timeout = "0"; |
109 |
} |
110 |
Alert alert = new Alert(alertLevel, lastAlert, thresholdLevel, source, thresholdValue, currentValue, attributeName, timeout, reg.getInitialAlertTime()); |
111 |
_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 |
protected abstract Queue getQueue(); |
118 |
|
119 |
protected int getQueueId() { |
120 |
if (_qID == -1) { |
121 |
_qID = getQueue().getQueue(); |
122 |
} |
123 |
return _qID; |
124 |
} |
125 |
|
126 |
//---ATTRIBUTES--- |
127 |
|
128 |
protected Logger _logger = ReferenceManager.getInstance().getLogger(); |
129 |
|
130 |
protected Queue _alerterQueue = ClientMain._alerterQueue; |
131 |
|
132 |
protected int _qID = -1; |
133 |
|
134 |
protected boolean _running = true; |
135 |
|
136 |
//---STATIC ATTRIBUTES--- |
137 |
|
138 |
} |