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/monitors/Load__Monitor.java
Revision: 1.3
Committed: Fri Mar 23 02:32:49 2001 UTC (23 years, 2 months ago) by tdb
Branch: MAIN
CVS Tags: PROJECT_COMPLETION
Changes since 1.2: +51 -11 lines
Log Message:
Fully javadoc'd all the monitors. Also made a few little changes here and there,
removing code that had been duplicated by copying other monitors, and tidying
up any silly little things (such has hardcoded integer values).

File Contents

# User Rev Content
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 Load for all machines
13     *
14 tdb 1.3 * @author $Author: ajm4 $
15     * @version $Id: Load__Monitor.java,v 1.2 2001/03/22 17:57:06 ajm4 Exp $
16 tdb 1.1 */
17     public class Load__Monitor extends MonitorSkeleton {
18    
19     //---FINAL ATTRIBUTES---
20    
21     /**
22     * The current CVS revision of this class
23     */
24 tdb 1.3 public final String REVISION = "$Revision: 1.2 $";
25 tdb 1.1
26 tdb 1.3 /**
27     * A description of this monitor
28     */
29 tdb 1.1 public final String DESC = "Monitors Load.";
30    
31     //---STATIC METHODS---
32    
33     //---CONSTRUCTORS---
34    
35     //---PUBLIC METHODS---
36 tdb 1.3
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    
45 ajm 1.2 String source = packet.getParam("packet.attributes.machine_name");
46     if (!_hosts.containsKey(source)) {
47     HashMap attributeRegisters = new HashMap();
48     initAttributeRegsiters(source, attributeRegisters);
49     _hosts.put(source, attributeRegisters);
50     }
51    
52     HashMap attributeRegisters = (HashMap) _hosts.get(source);
53     for(int attributeNum = 0; attributeNum < _attributes.length; attributeNum++) {
54     Register reg = (Register) attributeRegisters.get(_attributes[attributeNum]);
55     // find out the threshold level we're at
56     String attribute = _attributes[attributeNum];
57     String attributeName = _attributeNames[attributeNum];
58     String currentValue = packet.getParam(attribute);
59 tdb 1.3 // find out our new threshold
60 ajm 1.2 int newThreshold = checkAttributeThreshold(currentValue, reg);
61 tdb 1.3 // process this data to generate an alert
62 ajm 1.2 processAlert(newThreshold, attributeName, reg, source, currentValue);
63 tdb 1.1 }
64 ajm 1.2
65 tdb 1.1 }
66    
67     /**
68     * Overrides the {@link java.lang.Object#toString() Object.toString()}
69     * method to provide clean logging (every class should have this).
70     *
71     * This uses the uk.org.iscream.util.NameFormat class
72     * to format the toString()
73     *
74     * @return the name of this class and its CVS revision
75     */
76     public String toString() {
77     return FormatName.getName(
78     _name,
79     getClass().getName(),
80     REVISION);
81     }
82    
83     /**
84     * return the String representation of what the monitor does
85     */
86     public String getDescription(){
87     return DESC;
88     }
89    
90     //---PRIVATE METHODS---
91 tdb 1.3
92     /**
93     * Checks a piece of current data, and returns the
94     * threshold it breaches, if any.
95     *
96     * @param attributeString a String representing the current data value
97     * @param reg the Register for the host
98     * @return the threshold level breached, if any
99     */
100 tdb 1.1 private int checkAttributeThreshold(String attributeString, Register reg) {
101     for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) {
102     if (reg.getThreshold(thresholdLevel) != -1.0) {
103     if(attributeString != null) {
104     try {
105     double attribute = Double.parseDouble(attributeString);
106     if (reg.getThreshold(thresholdLevel) < attribute) return thresholdLevel;
107     } catch (NumberFormatException e) {
108     // we got some duff data in the packet, but we shouldn't have
109     _logger.write(toString(), Logger.DEBUG, "possible errenous packet data, should be double value - " + attributeString);
110     }
111     }
112     }
113     }
114     return Alert.thresholdNORMAL;
115     }
116 tdb 1.3
117     /**
118     * Initialises a HashMap of Registers with the current list
119     * of attributes. This is only used if we are looking at more
120     * than one distinct attribute.
121     *
122     * @param source the host we are looking at
123     * @param attributeRegisters a HashMap to put the new Registers in
124     */
125 tdb 1.1 private void initAttributeRegsiters(String source, HashMap attributeRegisters) {
126     for(int attributeNum = 0; attributeNum < _attributes.length; attributeNum++) {
127     String attributeName = _attributes[attributeNum].substring(_attributes[attributeNum].lastIndexOf(".") + 1);
128     attributeRegisters.put(_attributes[attributeNum], new Register(source, _name, attributeName));
129     }
130     }
131    
132     //---ACCESSOR/MUTATOR METHODS---
133 tdb 1.3
134     /**
135     * Returns a reference to a specific Queue for this
136     * monitor. This Queue returns only the data packets
137     * (based on type) that we want too look at.
138     *
139     * @return a reference to a Queue
140     */
141 ajm 1.2 protected Queue getQueue() {
142     return MonitorManager.getInstance().getDataQueue();
143     }
144    
145 tdb 1.1
146     //---ATTRIBUTES---
147    
148     /**
149     * This is the friendly identifier of the
150     * component this class is running in.
151     * eg, a Filter may be called "filter1",
152     * If this class does not have an owning
153     * component, a name from the configuration
154     * can be placed here. This name could also
155     * be changed to null for utility classes.
156     */
157     private String _name = "Load";
158    
159     /**
160 tdb 1.3 * A HashMap of Registers (or groups of Registers), one
161     * for each host we're monitoring.
162 tdb 1.1 */
163     private HashMap _hosts = new HashMap();
164 tdb 1.3
165     /**
166     * An array of attributes which we will be checking.
167     */
168 tdb 1.1 private String[] _attributes = { "packet.load.load1", "packet.load.load5", "packet.load.load15" };
169 tdb 1.3
170     /**
171     * An array of "nice names" for the attributes in _attributes.
172     */
173 tdb 1.1 private String[] _attributeNames = {"1 min load avg.", "5 min load avg.", "15 min load avg."};
174    
175     //---STATIC ATTRIBUTES---
176    
177     }