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 |
* @author $Author$ |
15 |
* @version $Id$ |
16 |
*/ |
17 |
public class UserCount__Monitor extends MonitorSkeleton { |
18 |
|
19 |
//---FINAL ATTRIBUTES--- |
20 |
|
21 |
/** |
22 |
* The current CVS revision of this class |
23 |
*/ |
24 |
public final String REVISION = "$Revision$"; |
25 |
|
26 |
public final String DESC = "Monitors number of logged on users."; |
27 |
|
28 |
//---STATIC METHODS--- |
29 |
|
30 |
//---CONSTRUCTORS--- |
31 |
|
32 |
//---PUBLIC METHODS--- |
33 |
|
34 |
public void analysePacket(XMLPacket packet) { |
35 |
// what host are we looking at |
36 |
String source = packet.getParam("packet.attributes.machine_name"); |
37 |
|
38 |
// if we don't have an entry in our HashMap, make one |
39 |
if (!_hosts.containsKey(source)) { |
40 |
_hosts.put(source, new Register(source, _name)); |
41 |
} |
42 |
|
43 |
// get the Register for this host |
44 |
Register reg = (Register) _hosts.get(source); |
45 |
|
46 |
// get some required bits of data |
47 |
String currentCount = packet.getParam("packet.users.count"); |
48 |
String niceName = "User Count"; |
49 |
int newThreshold = checkAttributeThreshold(currentCount, reg); |
50 |
|
51 |
// process an alert |
52 |
processAlert(newThreshold, niceName, reg, source, currentCount); |
53 |
} |
54 |
|
55 |
/** |
56 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
57 |
* method to provide clean logging (every class should have this). |
58 |
* |
59 |
* This uses the uk.org.iscream.util.NameFormat class |
60 |
* to format the toString() |
61 |
* |
62 |
* @return the name of this class and its CVS revision |
63 |
*/ |
64 |
public String toString() { |
65 |
return FormatName.getName( |
66 |
_name, |
67 |
getClass().getName(), |
68 |
REVISION); |
69 |
} |
70 |
|
71 |
/** |
72 |
* return the String representation of what the monitor does |
73 |
*/ |
74 |
public String getDescription(){ |
75 |
return DESC; |
76 |
} |
77 |
|
78 |
//---PRIVATE METHODS--- |
79 |
|
80 |
private int checkAttributeThreshold(String attributeString, Register reg) { |
81 |
for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) { |
82 |
if (reg.getThreshold(thresholdLevel) != -1.0) { |
83 |
if(attributeString != null) { |
84 |
try { |
85 |
double attribute = Double.parseDouble(attributeString); |
86 |
if (reg.getThreshold(thresholdLevel) < attribute) return thresholdLevel; |
87 |
} catch (NumberFormatException e) { |
88 |
// we got some duff data in the packet, but we shouldn't have |
89 |
_logger.write(toString(), Logger.DEBUG, "possible errenous packet data, should be double value - " + attributeString); |
90 |
} |
91 |
} |
92 |
} |
93 |
} |
94 |
return Alert.thresholdNORMAL; |
95 |
} |
96 |
|
97 |
//---ACCESSOR/MUTATOR METHODS--- |
98 |
|
99 |
protected Queue getQueue() { |
100 |
return MonitorManager.getInstance().getDataQueue(); |
101 |
} |
102 |
|
103 |
//---ATTRIBUTES--- |
104 |
|
105 |
/** |
106 |
* This is the friendly identifier of the |
107 |
* component this class is running in. |
108 |
* eg, a Filter may be called "filter1", |
109 |
* If this class does not have an owning |
110 |
* component, a name from the configuration |
111 |
* can be placed here. This name could also |
112 |
* be changed to null for utility classes. |
113 |
*/ |
114 |
private String _name = "UserCount"; |
115 |
|
116 |
private HashMap _hosts = new HashMap(); |
117 |
|
118 |
//---STATIC ATTRIBUTES--- |
119 |
|
120 |
} |