1 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
|
|
package uk.org.iscream.client.monitors; |
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 |
|
|
* This Monitor watches the logged on user count for all machines |
13 |
|
|
* |
14 |
tdb |
1.2 |
* @author $Author: tdb1 $ |
15 |
|
|
* @version $Id: UserCount__Monitor.java,v 1.1 2001/03/23 01:17:09 tdb1 Exp $ |
16 |
tdb |
1.1 |
*/ |
17 |
|
|
public class UserCount__Monitor extends MonitorSkeleton { |
18 |
|
|
|
19 |
|
|
//---FINAL ATTRIBUTES--- |
20 |
|
|
|
21 |
|
|
/** |
22 |
|
|
* The current CVS revision of this class |
23 |
|
|
*/ |
24 |
tdb |
1.2 |
public final String REVISION = "$Revision: 1.1 $"; |
25 |
tdb |
1.1 |
|
26 |
tdb |
1.2 |
/** |
27 |
|
|
* A description of this monitor |
28 |
|
|
*/ |
29 |
tdb |
1.1 |
public final String DESC = "Monitors number of logged on users."; |
30 |
|
|
|
31 |
|
|
//---STATIC METHODS--- |
32 |
|
|
|
33 |
|
|
//---CONSTRUCTORS--- |
34 |
|
|
|
35 |
|
|
//---PUBLIC METHODS--- |
36 |
tdb |
1.2 |
|
37 |
|
|
/** |
38 |
|
|
* Analyse a packet of data, and generate an alert if |
39 |
|
|
* necessary. |
40 |
|
|
* |
41 |
|
|
* @param packet the XMLPacket to analyse |
42 |
|
|
*/ |
43 |
tdb |
1.1 |
public void analysePacket(XMLPacket packet) { |
44 |
|
|
// what host are we looking at |
45 |
|
|
String source = packet.getParam("packet.attributes.machine_name"); |
46 |
|
|
|
47 |
|
|
// if we don't have an entry in our HashMap, make one |
48 |
|
|
if (!_hosts.containsKey(source)) { |
49 |
|
|
_hosts.put(source, new Register(source, _name)); |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
// get the Register for this host |
53 |
|
|
Register reg = (Register) _hosts.get(source); |
54 |
|
|
|
55 |
|
|
// get some required bits of data |
56 |
|
|
String currentCount = packet.getParam("packet.users.count"); |
57 |
|
|
String niceName = "User Count"; |
58 |
|
|
int newThreshold = checkAttributeThreshold(currentCount, reg); |
59 |
|
|
|
60 |
|
|
// process an alert |
61 |
|
|
processAlert(newThreshold, niceName, reg, source, currentCount); |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
/** |
65 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
66 |
|
|
* method to provide clean logging (every class should have this). |
67 |
|
|
* |
68 |
|
|
* This uses the uk.org.iscream.util.NameFormat class |
69 |
|
|
* to format the toString() |
70 |
|
|
* |
71 |
|
|
* @return the name of this class and its CVS revision |
72 |
|
|
*/ |
73 |
|
|
public String toString() { |
74 |
|
|
return FormatName.getName( |
75 |
|
|
_name, |
76 |
|
|
getClass().getName(), |
77 |
|
|
REVISION); |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
/** |
81 |
|
|
* return the String representation of what the monitor does |
82 |
|
|
*/ |
83 |
|
|
public String getDescription(){ |
84 |
|
|
return DESC; |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
//---PRIVATE METHODS--- |
88 |
tdb |
1.2 |
|
89 |
|
|
/** |
90 |
|
|
* Checks a piece of current data, and returns the |
91 |
|
|
* threshold it breaches, if any. |
92 |
|
|
* |
93 |
|
|
* @param attributeString a String representing the current data value |
94 |
|
|
* @param reg the Register for the host |
95 |
|
|
* @return the threshold level breached, if any |
96 |
|
|
*/ |
97 |
tdb |
1.1 |
private int checkAttributeThreshold(String attributeString, Register reg) { |
98 |
|
|
for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) { |
99 |
|
|
if (reg.getThreshold(thresholdLevel) != -1.0) { |
100 |
|
|
if(attributeString != null) { |
101 |
|
|
try { |
102 |
|
|
double attribute = Double.parseDouble(attributeString); |
103 |
|
|
if (reg.getThreshold(thresholdLevel) < attribute) return thresholdLevel; |
104 |
|
|
} catch (NumberFormatException e) { |
105 |
|
|
// we got some duff data in the packet, but we shouldn't have |
106 |
|
|
_logger.write(toString(), Logger.DEBUG, "possible errenous packet data, should be double value - " + attributeString); |
107 |
|
|
} |
108 |
|
|
} |
109 |
|
|
} |
110 |
|
|
} |
111 |
|
|
return Alert.thresholdNORMAL; |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
115 |
tdb |
1.2 |
|
116 |
|
|
/** |
117 |
|
|
* Returns a reference to a specific Queue for this |
118 |
|
|
* monitor. This Queue returns only the data packets |
119 |
|
|
* (based on type) that we want too look at. |
120 |
|
|
* |
121 |
|
|
* @return a reference to a Queue |
122 |
|
|
*/ |
123 |
tdb |
1.1 |
protected Queue getQueue() { |
124 |
|
|
return MonitorManager.getInstance().getDataQueue(); |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
//---ATTRIBUTES--- |
128 |
|
|
|
129 |
|
|
/** |
130 |
|
|
* This is the friendly identifier of the |
131 |
|
|
* component this class is running in. |
132 |
|
|
* eg, a Filter may be called "filter1", |
133 |
|
|
* If this class does not have an owning |
134 |
|
|
* component, a name from the configuration |
135 |
|
|
* can be placed here. This name could also |
136 |
|
|
* be changed to null for utility classes. |
137 |
|
|
*/ |
138 |
|
|
private String _name = "UserCount"; |
139 |
tdb |
1.2 |
|
140 |
|
|
/** |
141 |
|
|
* A HashMap of Registers (or groups of Registers), one |
142 |
|
|
* for each host we're monitoring. |
143 |
|
|
*/ |
144 |
tdb |
1.1 |
private HashMap _hosts = new HashMap(); |
145 |
|
|
|
146 |
|
|
//---STATIC ATTRIBUTES--- |
147 |
|
|
|
148 |
|
|
} |