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.13
Committed: Sun Mar 4 05:23:41 2001 UTC (23 years, 3 months ago) by ajm
Branch: MAIN
Changes since 1.12: +7 -4 lines
Log Message:
now FINALLY detects reverting to OK levels correctly.

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.13 * @version $Id: CPU__Monitor.java,v 1.12 2001/03/04 03:42:34 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.13 public final String REVISION = "$Revision: 1.12 $";
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 ajm 1.13 long timeout = reg.getLastAlertTimeout(attributeNum);
55 ajm 1.11 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 ajm 1.13 reg.setLastAlertTimeout(attributeNum, reg.getAlertTimeout(reg.getLastAlertLevel(attributeNum), attributeNum));
60 ajm 1.11 fireAlert(reg, packet, attributeNum);
61     }
62     // if we don't have a timeout configured...we got STRAIGHT to the next level
63     } else {
64     reg.escalateAlert(attributeNum);
65     reg.setTimeLastSent(attributeNum, System.currentTimeMillis());
66 ajm 1.13 reg.setLastAlertTimeout(attributeNum, reg.getAlertTimeout(reg.getLastAlertLevel(attributeNum), attributeNum));
67 ajm 1.11 fireAlert(reg, packet, attributeNum);
68     }
69    
70     // we must be on ok, check the timeout value for this
71     } else {
72     // if we were on an OK alert before, then we don't do anything
73     // but if we weren't we only set OK, once the timout of the last
74     // alert has occourd
75     if (reg.getLastAlertLevel(attributeNum) != Alert.alertOK) {
76 ajm 1.13 long timeout = reg.getLastAlertTimeout(attributeNum);
77 ajm 1.11 if ((timeout > 0) && (reg.getTimeLastSent(attributeNum) > 0)) {
78     if ((System.currentTimeMillis() - reg.getTimeLastSent(attributeNum)) > timeout) {
79     reg.setLastAlertLevel(attributeNum, Alert.alertOK);
80     reg.setTimeLastSent(attributeNum, System.currentTimeMillis());
81 ajm 1.13 reg.setLastAlertTimeout(attributeNum, timeout);
82 ajm 1.11 fireAlert(reg, packet, attributeNum);
83     }
84     }
85     }
86     }
87     }
88 ajm 1.2 }
89 tdb 1.1 }
90    
91     /**
92     * Overrides the {@link java.lang.Object#toString() Object.toString()}
93     * method to provide clean logging (every class should have this).
94     *
95     * This uses the uk.ac.ukc.iscream.util.NameFormat class
96     * to format the toString()
97     *
98     * @return the name of this class and its CVS revision
99     */
100     public String toString() {
101     return FormatName.getName(
102     _name,
103     getClass().getName(),
104     REVISION);
105     }
106    
107     /**
108 ajm 1.11 * return the String representation of what the monitor does
109 tdb 1.1 */
110     public String getDescription(){
111     return DESC;
112     }
113    
114     //---PRIVATE METHODS---
115    
116 ajm 1.11 private int checkAttributeThreshold(String attributeString, Register reg) {
117     for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) {
118     if (reg.getThreshold(thresholdLevel) != -1.0) {
119     if(attributeString != null) {
120     try {
121     double attribute = Double.parseDouble(attributeString);
122     if (reg.getThreshold(thresholdLevel) < attribute) return thresholdLevel;
123     } catch (NumberFormatException e) {
124     // we got some duff data in the packet, but we shouldn't have
125     _logger.write(toString(), Logger.DEBUG, "possible errenous packet data, should be double value - " + attributeString);
126     }
127 ajm 1.9 }
128     }
129     }
130     return 0;
131     }
132    
133 ajm 1.11 private void fireAlert(Register reg, XMLPacket packet, int attributeNum) {
134     int alertLevel = reg.getLastAlertLevel(attributeNum);
135     int thresholdLevel = reg.getLastThresholdLevel(attributeNum);
136     String source = packet.getParam("packet.attributes.machine_name");
137     String currentValue = packet.getParam(_attributes[attributeNum]);
138     String attributeName = _attributeNames[attributeNum];
139     String thresholdValue = Double.toString(reg.getThreshold(thresholdLevel));
140     String time = Long.toString(reg.getAlertTimeout(reg.getLastAlertLevel(attributeNum), attributeNum) / 1000);
141     if (thresholdLevel == Alert.thresholdNORMAL) {
142     thresholdValue = "-";
143     }
144     if (alertLevel == Alert.alertOK) {
145     time = "0";
146     }
147     Alert alert = new Alert(alertLevel, thresholdLevel, source, thresholdValue, currentValue, attributeName, time);
148 ajm 1.2 _alerterQueue.add(alert);
149 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");
150 ajm 1.2 }
151    
152 tdb 1.1 //---ACCESSOR/MUTATOR METHODS---
153    
154     //---ATTRIBUTES---
155    
156     /**
157     * This is the friendly identifier of the
158     * component this class is running in.
159     * eg, a Filter may be called "filter1",
160     * If this class does not have an owning
161     * component, a name from the configuration
162     * can be placed here. This name could also
163     * be changed to null for utility classes.
164     */
165 ajm 1.11 private String _name = "CPU";
166 tdb 1.1
167     /**
168     * This holds a reference to the
169     * system logger that is being used.
170     */
171     private Logger _logger = ReferenceManager.getInstance().getLogger();
172 ajm 1.2
173 ajm 1.11 private Queue _alerterQueue = ClientMain._alerterQueue;
174 ajm 1.2
175     /**
176 ajm 1.11 * A reference to the configuration proxy in use
177 ajm 1.2 */
178 ajm 1.11 private ConfigurationProxy _cp = ConfigurationProxy.getInstance();
179 ajm 1.9
180     private HashMap _hosts = new HashMap();
181    
182     private String[] _attributes = { "packet.cpu.user", "packet.cpu.kernel", "packet.cpu.iowait", "packet.cpu.swap" };
183     private String[] _attributeNames = {"User CPU", "Kernel CPU", "I/O Wait CPU", "Swap CPU"};
184 tdb 1.1
185     //---STATIC ATTRIBUTES---
186    
187     }