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