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/Memory__Monitor.java
Revision: 1.18
Committed: Thu Jan 15 14:10:13 2004 UTC (20 years, 4 months ago) by tdb
Branch: MAIN
Changes since 1.17: +3 -3 lines
Log Message:
OK - I can still program Java, I just can't remember how this works :-)

File Contents

# User Rev Content
1 tdb 1.10 /*
2     * i-scream central monitoring system
3 tdb 1.11 * http://www.i-scream.org.uk
4 tdb 1.10 * Copyright (C) 2000-2002 i-scream
5     *
6     * This program is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU General Public License
8     * as published by the Free Software Foundation; either version 2
9     * of the License, or (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19     */
20    
21 tdb 1.1 //---PACKAGE DECLARATION---
22 tdb 1.8 package uk.org.iscream.cms.server.client.monitors;
23 tdb 1.1
24     //---IMPORTS---
25     import java.util.HashMap;
26 tdb 1.2 import java.text.NumberFormat;
27 tdb 1.8 import uk.org.iscream.cms.server.client.*;
28     import uk.org.iscream.cms.server.core.*;
29 tdb 1.13 import uk.org.iscream.cms.util.*;
30 tdb 1.8 import uk.org.iscream.cms.server.componentmanager.*;
31 tdb 1.1
32     /**
33     * This Monitor watches the Memory for all machines
34     *
35 tdb 1.10 * @author $Author: tdb $
36 tdb 1.18 * @version $Id: Memory__Monitor.java,v 1.17 2004/01/15 13:41:48 tdb Exp $
37 tdb 1.1 */
38     public class Memory__Monitor extends MonitorSkeleton {
39    
40     //---FINAL ATTRIBUTES---
41    
42     /**
43     * The current CVS revision of this class
44     */
45 tdb 1.18 public final String REVISION = "$Revision: 1.17 $";
46 tdb 1.1
47 tdb 1.7 /**
48     * A description of this monitor
49     */
50 tdb 1.1 public final String DESC = "Monitors Memory.";
51    
52     //---STATIC METHODS---
53    
54     //---CONSTRUCTORS---
55    
56     //---PUBLIC METHODS---
57 tdb 1.7
58     /**
59     * Analyse a packet of data, and generate an alert if
60     * necessary.
61     *
62     * @param packet the XMLPacket to analyse
63     */
64 tdb 1.1 public void analysePacket(XMLPacket packet) {
65 ajm 1.6 String source = packet.getParam("packet.attributes.machine_name");
66 tdb 1.18 if(!checkBooleanConfig("Host." + source, "Monitor." + _name + ".enable")) {
67 tdb 1.17 return;
68     }
69 ajm 1.6 if (!_hosts.containsKey(source)) {
70     _hosts.put(source, new Register(source, _name));
71     }
72    
73     Register reg = (Register) _hosts.get(source);
74    
75     // get the packet data
76 tdb 1.12 double memoryTotal, memoryFree, memoryCache;
77 ajm 1.6 try {
78     String total = packet.getParam("packet.memory.total");
79     String free = packet.getParam("packet.memory.free");
80     if(total==null || free==null) {
81 tdb 1.12 throw new NumberFormatException("Memory data invalid or not supplied");
82 tdb 1.1 }
83 ajm 1.6 memoryTotal = Double.parseDouble(total);
84     memoryFree = Double.parseDouble(free);
85 tdb 1.12 // get hold of the cache data.. default to 0
86     memoryCache = 0.0;
87     String cache = packet.getParam("packet.memory.cache");
88     if(cache != null) {
89     memoryCache = Double.parseDouble(cache);
90     }
91 ajm 1.6 } catch (NumberFormatException e) {
92     _logger.write(this.toString(), Logger.WARNING, "Received packet from "+source+" with bad memory information: "+e);
93     // don't try to continue and process
94     return;
95     }
96    
97     boolean useValue = false;
98     try {
99     String option = _cp.getProperty("Host." + source, "Monitor." + _name + ".thresholdMeasure");
100     if (option.equals("VALUE")) {
101     useValue = true;
102 tdb 1.12 }
103 ajm 1.6 } catch (PropertyNotFoundException e) {
104     // we default to percentage
105     }
106 tdb 1.12
107     boolean useCache = false;
108     try {
109     int useCacheAsFree = Integer.parseInt(_cp.getProperty("Host." + source, "Monitor." + _name + ".useCacheAsFree"));
110     useCache = (useCacheAsFree == 1);
111     } catch (PropertyNotFoundException e) {
112     // we default to false
113     } catch (NumberFormatException e) {
114     // we default to false
115     }
116    
117 tdb 1.14 // this bit determines if the memory check is a % check
118     // or a byte check
119 tdb 1.9 String type;
120     double curValue;
121     int newThreshold;
122 ajm 1.6 if(useValue) {
123 tdb 1.14 // bytes of memory available
124 tdb 1.12 if(useCache) {
125     // NOTE: we take cache as being "probably free" for this check
126     curValue = memoryFree + memoryCache;
127     } else {
128     curValue = memoryFree;
129     }
130 tdb 1.9 // negate check
131     newThreshold = checkAttributeThreshold(curValue, reg, true);
132 tdb 1.14 type = "bytes";
133 ajm 1.6 } else {
134     // % memory in use
135 tdb 1.12 if(useCache) {
136     // NOTE: we take cache as being "probably free" for this check
137 tdb 1.15 curValue = (1 - (((double)memoryFree + (double)memoryCache) / (double)memoryTotal)) * 100;
138 tdb 1.12 } else {
139 tdb 1.15 curValue = (1 - ((double)memoryFree / (double)memoryTotal)) * 100;
140 tdb 1.12 }
141 tdb 1.9 // normal check
142     newThreshold = checkAttributeThreshold(curValue, reg, false);
143     type = "%";
144 tdb 1.1 }
145 ajm 1.6
146 tdb 1.16 // format the value to a String
147 ajm 1.6 NumberFormat nf = NumberFormat.getInstance();
148     nf.setMaximumFractionDigits(2);
149     nf.setMinimumFractionDigits(2);
150 tdb 1.9 String strCurValue = nf.format(curValue);
151    
152     // set the attributeName nicely
153 tdb 1.16 String attributeName = type + " Memory in use " + type;
154 tdb 1.12 if(useCache) {
155     attributeName += " (ex. cache)";
156     }
157 tdb 1.9
158     processAlert(newThreshold, attributeName, reg, source, strCurValue);
159 tdb 1.1 }
160    
161     /**
162     * Overrides the {@link java.lang.Object#toString() Object.toString()}
163     * method to provide clean logging (every class should have this).
164     *
165 tdb 1.13 * This uses the uk.org.iscream.cms.util.NameFormat class
166 tdb 1.1 * to format the toString()
167     *
168     * @return the name of this class and its CVS revision
169     */
170     public String toString() {
171     return FormatName.getName(
172     _name,
173     getClass().getName(),
174     REVISION);
175     }
176    
177     /**
178     * return the String representation of what the monitor does
179     */
180     public String getDescription(){
181     return DESC;
182     }
183    
184     //---PRIVATE METHODS---
185 tdb 1.7
186     /**
187     * Checks a piece of current data, and returns the
188     * threshold it breaches, if any.
189     *
190 tdb 1.9 * The option to negate the check can be used in
191     * situations where being *below* the threshold
192     * is an 'alertable' situation. In this specific
193     * case, we'd do this with kb disk checks.
194     *
195     * @param value the current value
196 tdb 1.7 * @param reg the Register for the host
197 tdb 1.9 * @param negateCheck whether to negate the check
198 tdb 1.7 * @return the threshold level breached, if any
199     */
200 tdb 1.9 private int checkAttributeThreshold(double value, Register reg, boolean negateCheck) {
201 tdb 1.1 for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) {
202     if (reg.getThreshold(thresholdLevel) != -1.0) {
203 tdb 1.9 if(!negateCheck) {
204     // normal check - has the value gone *over* the threshold
205     if(((double) reg.getThreshold(thresholdLevel)) < value) {
206     return thresholdLevel;
207     }
208     }
209     else {
210     // negated check - has the value gone *under* the threshold
211     if(((double) reg.getThreshold(thresholdLevel)) > value) {
212     return thresholdLevel;
213     }
214 tdb 1.1 }
215     }
216     }
217     return Alert.thresholdNORMAL;
218     }
219    
220     //---ACCESSOR/MUTATOR METHODS---
221 tdb 1.7
222     /**
223     * Returns a reference to a specific Queue for this
224     * monitor. This Queue returns only the data packets
225     * (based on type) that we want too look at.
226     *
227     * @return a reference to a Queue
228     */
229 ajm 1.6 protected Queue getQueue() {
230     return MonitorManager.getInstance().getDataQueue();
231     }
232 tdb 1.1
233     //---ATTRIBUTES---
234    
235     /**
236     * This is the friendly identifier of the
237     * component this class is running in.
238     * eg, a Filter may be called "filter1",
239     * If this class does not have an owning
240     * component, a name from the configuration
241     * can be placed here. This name could also
242     * be changed to null for utility classes.
243     */
244     private String _name = "Memory";
245    
246     /**
247     * A reference to the configuration proxy in use
248     */
249     private ConfigurationProxy _cp = ConfigurationProxy.getInstance();
250 tdb 1.7
251     /**
252     * A HashMap of Registers (or groups of Registers), one
253     * for each host we're monitoring.
254     */
255 tdb 1.1 private HashMap _hosts = new HashMap();
256    
257     //---STATIC ATTRIBUTES---
258    
259     }