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/CPU__Monitor.java
Revision: 1.12
Committed: Sun Mar 4 03:42:34 2001 UTC (23 years, 3 months ago) by ajm
Branch: MAIN
Changes since 1.11: +2 -6 lines
Log Message:
now only releases an alert level once the timeout has expired properly.

File Contents

# User Rev Content
1 tdb 1.1 //---PACKAGE DECLARATION---
2     package uk.ac.ukc.iscream.client.monitors;
3    
4     //---IMPORTS---
5 ajm 1.9 import java.util.HashMap;
6 tdb 1.1 import uk.ac.ukc.iscream.client.*;
7     import uk.ac.ukc.iscream.core.*;
8     import uk.ac.ukc.iscream.util.*;
9     import uk.ac.ukc.iscream.componentmanager.*;
10    
11     /**
12     * This Monitor watches the CPU load for all machines
13     *
14 ajm 1.10 * @author $Author: ajm4 $
15 ajm 1.12 * @version $Id: CPU__Monitor.java,v 1.11 2001/03/04 02:41:16 ajm4 Exp $
16 tdb 1.1 */
17 ajm 1.6 public class CPU__Monitor implements PluginMonitor {
18 tdb 1.1
19     //---FINAL ATTRIBUTES---
20    
21     /**
22     * The current CVS revision of this class
23     */
24 ajm 1.12 public final String REVISION = "$Revision: 1.11 $";
25 tdb 1.1
26     public final String DESC = "Monitors CPU.";
27    
28     //---STATIC METHODS---
29    
30     //---CONSTRUCTORS---
31    
32     //---PUBLIC METHODS---
33    
34     public void analysePacket(XMLPacket packet) {
35 ajm 1.7 if (packet.getParam("packet.attributes.type").equals("data")) {
36 tdb 1.8 String source = packet.getParam("packet.attributes.machine_name");
37 ajm 1.9 if (!_hosts.containsKey(source)) {
38 ajm 1.11 _hosts.put(source, new Register(source, _name, _attributes.length));
39 ajm 1.2 }
40 ajm 1.9
41     Register reg = (Register) _hosts.get(source);
42 ajm 1.11 for(int attributeNum = 0; attributeNum < _attributes.length; attributeNum++) {
43     // find out the threshold level we're at
44     int result = checkAttributeThreshold(packet.getParam(_attributes[attributeNum]), reg);
45    
46     // decide what threshold level we're on, if we've changed, record that
47     if (result != reg.getLastThresholdLevel(attributeNum)) {
48     reg.setLastThresholdLevel(attributeNum, result);
49     }
50     // as long as this isn't a normal level
51     if(reg.getLastThresholdLevel(attributeNum) != Alert.thresholdNORMAL) {
52     // if the time since the last alert is more than the time for
53     // its timeout, fire an alert, escalate the alert
54     long timeout = reg.getAlertTimeout(reg.getLastAlertLevel(attributeNum), attributeNum);
55     if ((timeout > 0) && (reg.getTimeLastSent(attributeNum) > 0)) {
56     if((System.currentTimeMillis() - reg.getTimeLastSent(attributeNum)) > timeout) {
57     reg.escalateAlert(attributeNum);
58     reg.setTimeLastSent(attributeNum, System.currentTimeMillis());
59     fireAlert(reg, packet, attributeNum);
60     }
61     // if we don't have a timeout configured...we got STRAIGHT to the next level
62     } else {
63     reg.escalateAlert(attributeNum);
64     reg.setTimeLastSent(attributeNum, System.currentTimeMillis());
65     fireAlert(reg, packet, attributeNum);
66     }
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     if (reg.getLastAlertLevel(attributeNum) != Alert.alertOK) {
74     long timeout = reg.getAlertTimeout(reg.getLastAlertLevel(attributeNum), attributeNum);
75     if ((timeout > 0) && (reg.getTimeLastSent(attributeNum) > 0)) {
76     if ((System.currentTimeMillis() - reg.getTimeLastSent(attributeNum)) > timeout) {
77     reg.setLastAlertLevel(attributeNum, Alert.alertOK);
78     reg.setTimeLastSent(attributeNum, System.currentTimeMillis());
79     fireAlert(reg, packet, attributeNum);
80     }
81     }
82     }
83     }
84     }
85 ajm 1.2 }
86 tdb 1.1 }
87    
88     /**
89     * Overrides the {@link java.lang.Object#toString() Object.toString()}
90     * method to provide clean logging (every class should have this).
91     *
92     * This uses the uk.ac.ukc.iscream.util.NameFormat class
93     * to format the toString()
94     *
95     * @return the name of this class and its CVS revision
96     */
97     public String toString() {
98     return FormatName.getName(
99     _name,
100     getClass().getName(),
101     REVISION);
102     }
103    
104     /**
105 ajm 1.11 * return the String representation of what the monitor does
106 tdb 1.1 */
107     public String getDescription(){
108     return DESC;
109     }
110    
111     //---PRIVATE METHODS---
112    
113 ajm 1.11 private int checkAttributeThreshold(String attributeString, Register reg) {
114     for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) {
115     if (reg.getThreshold(thresholdLevel) != -1.0) {
116     if(attributeString != null) {
117     try {
118     double attribute = Double.parseDouble(attributeString);
119     if (reg.getThreshold(thresholdLevel) < attribute) return thresholdLevel;
120     } catch (NumberFormatException e) {
121     // we got some duff data in the packet, but we shouldn't have
122     _logger.write(toString(), Logger.DEBUG, "possible errenous packet data, should be double value - " + attributeString);
123     }
124 ajm 1.9 }
125     }
126     }
127     return 0;
128     }
129    
130 ajm 1.11 private void fireAlert(Register reg, XMLPacket packet, int attributeNum) {
131     int alertLevel = reg.getLastAlertLevel(attributeNum);
132     int thresholdLevel = reg.getLastThresholdLevel(attributeNum);
133     String source = packet.getParam("packet.attributes.machine_name");
134     String currentValue = packet.getParam(_attributes[attributeNum]);
135     String attributeName = _attributeNames[attributeNum];
136     String thresholdValue = Double.toString(reg.getThreshold(thresholdLevel));
137     String time = Long.toString(reg.getAlertTimeout(reg.getLastAlertLevel(attributeNum), attributeNum) / 1000);
138     if (thresholdLevel == Alert.thresholdNORMAL) {
139     thresholdValue = "-";
140     }
141     if (alertLevel == Alert.alertOK) {
142     time = "0";
143     }
144     Alert alert = new Alert(alertLevel, thresholdLevel, source, thresholdValue, currentValue, attributeName, time);
145 ajm 1.2 _alerterQueue.add(alert);
146 ajm 1.11 _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:" + time + "secs");
147 ajm 1.2 }
148    
149 tdb 1.1 //---ACCESSOR/MUTATOR METHODS---
150    
151     //---ATTRIBUTES---
152    
153     /**
154     * This is the friendly identifier of the
155     * component this class is running in.
156     * eg, a Filter may be called "filter1",
157     * If this class does not have an owning
158     * component, a name from the configuration
159     * can be placed here. This name could also
160     * be changed to null for utility classes.
161     */
162 ajm 1.11 private String _name = "CPU";
163 tdb 1.1
164     /**
165     * This holds a reference to the
166     * system logger that is being used.
167     */
168     private Logger _logger = ReferenceManager.getInstance().getLogger();
169 ajm 1.2
170 ajm 1.11 private Queue _alerterQueue = ClientMain._alerterQueue;
171 ajm 1.2
172     /**
173 ajm 1.11 * A reference to the configuration proxy in use
174 ajm 1.2 */
175 ajm 1.11 private ConfigurationProxy _cp = ConfigurationProxy.getInstance();
176 ajm 1.9
177     private HashMap _hosts = new HashMap();
178    
179     private String[] _attributes = { "packet.cpu.user", "packet.cpu.kernel", "packet.cpu.iowait", "packet.cpu.swap" };
180     private String[] _attributeNames = {"User CPU", "Kernel CPU", "I/O Wait CPU", "Swap CPU"};
181 tdb 1.1
182     //---STATIC ATTRIBUTES---
183    
184     }