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.11
Committed: Tue May 21 16:47:16 2002 UTC (22 years ago) by tdb
Branch: MAIN
Changes since 1.10: +3 -2 lines
Log Message:
Added URL to GPL headers.

File Contents

# Content
1 /*
2 * i-scream central monitoring system
3 * http://www.i-scream.org.uk
4 * 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 //---PACKAGE DECLARATION---
22 package uk.org.iscream.cms.server.client.monitors;
23
24 //---IMPORTS---
25 import java.util.HashMap;
26 import java.text.NumberFormat;
27 import uk.org.iscream.cms.server.client.*;
28 import uk.org.iscream.cms.server.core.*;
29 import uk.org.iscream.cms.server.util.*;
30 import uk.org.iscream.cms.server.componentmanager.*;
31
32 /**
33 * This Monitor watches the Memory for all machines
34 *
35 * @author $Author: tdb $
36 * @version $Id: Memory__Monitor.java,v 1.10 2002/05/18 18:16:00 tdb Exp $
37 */
38 public class Memory__Monitor extends MonitorSkeleton {
39
40 //---FINAL ATTRIBUTES---
41
42 /**
43 * The current CVS revision of this class
44 */
45 public final String REVISION = "$Revision: 1.10 $";
46
47 /**
48 * A description of this monitor
49 */
50 public final String DESC = "Monitors Memory.";
51
52 //---STATIC METHODS---
53
54 //---CONSTRUCTORS---
55
56 //---PUBLIC METHODS---
57
58 /**
59 * Analyse a packet of data, and generate an alert if
60 * necessary.
61 *
62 * @param packet the XMLPacket to analyse
63 */
64 public void analysePacket(XMLPacket packet) {
65 String source = packet.getParam("packet.attributes.machine_name");
66 if (!_hosts.containsKey(source)) {
67 _hosts.put(source, new Register(source, _name));
68 }
69
70 Register reg = (Register) _hosts.get(source);
71
72 // get the packet data
73 double memoryTotal, memoryFree;
74 try {
75 String total = packet.getParam("packet.memory.total");
76 String free = packet.getParam("packet.memory.free");
77 if(total==null || free==null) {
78 throw new NumberFormatException("Memory data invalid");
79 }
80 memoryTotal = Double.parseDouble(total);
81 memoryFree = Double.parseDouble(free);
82 } catch (NumberFormatException e) {
83 _logger.write(this.toString(), Logger.WARNING, "Received packet from "+source+" with bad memory information: "+e);
84 // don't try to continue and process
85 return;
86 }
87
88 boolean useValue = false;
89 try {
90 String option = _cp.getProperty("Host." + source, "Monitor." + _name + ".thresholdMeasure");
91 if (option.equals("VALUE")) {
92 useValue = true;
93 }
94 } catch (PropertyNotFoundException e) {
95 // we default to percentage
96 }
97
98 // this bit determines if the memory check is a % check
99 // or a kb check
100 String type;
101 double curValue;
102 int newThreshold;
103 if(useValue) {
104 // kb memory available
105 curValue = memoryFree;
106 // negate check
107 newThreshold = checkAttributeThreshold(curValue, reg, true);
108 type = "kb";
109 } else {
110 // % memory in use
111 curValue = (1 - (memoryFree / memoryTotal)) * 100;
112 // normal check
113 newThreshold = checkAttributeThreshold(curValue, reg, false);
114 type = "%";
115 }
116
117 // format the memoryInUse to a String
118 NumberFormat nf = NumberFormat.getInstance();
119 nf.setMaximumFractionDigits(2);
120 nf.setMinimumFractionDigits(2);
121 String strCurValue = nf.format(curValue);
122
123 // set the attributeName nicely
124 String attributeName = "Memory in use " + type;
125
126 processAlert(newThreshold, attributeName, reg, source, strCurValue);
127 }
128
129 /**
130 * Overrides the {@link java.lang.Object#toString() Object.toString()}
131 * method to provide clean logging (every class should have this).
132 *
133 * This uses the uk.org.iscream.cms.server.util.NameFormat class
134 * to format the toString()
135 *
136 * @return the name of this class and its CVS revision
137 */
138 public String toString() {
139 return FormatName.getName(
140 _name,
141 getClass().getName(),
142 REVISION);
143 }
144
145 /**
146 * return the String representation of what the monitor does
147 */
148 public String getDescription(){
149 return DESC;
150 }
151
152 //---PRIVATE METHODS---
153
154 /**
155 * Checks a piece of current data, and returns the
156 * threshold it breaches, if any.
157 *
158 * The option to negate the check can be used in
159 * situations where being *below* the threshold
160 * is an 'alertable' situation. In this specific
161 * case, we'd do this with kb disk checks.
162 *
163 * @param value the current value
164 * @param reg the Register for the host
165 * @param negateCheck whether to negate the check
166 * @return the threshold level breached, if any
167 */
168 private int checkAttributeThreshold(double value, Register reg, boolean negateCheck) {
169 for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) {
170 if (reg.getThreshold(thresholdLevel) != -1.0) {
171 if(!negateCheck) {
172 // normal check - has the value gone *over* the threshold
173 if(((double) reg.getThreshold(thresholdLevel)) < value) {
174 return thresholdLevel;
175 }
176 }
177 else {
178 // negated check - has the value gone *under* the threshold
179 if(((double) reg.getThreshold(thresholdLevel)) > value) {
180 return thresholdLevel;
181 }
182 }
183 }
184 }
185 return Alert.thresholdNORMAL;
186 }
187
188 //---ACCESSOR/MUTATOR METHODS---
189
190 /**
191 * Returns a reference to a specific Queue for this
192 * monitor. This Queue returns only the data packets
193 * (based on type) that we want too look at.
194 *
195 * @return a reference to a Queue
196 */
197 protected Queue getQueue() {
198 return MonitorManager.getInstance().getDataQueue();
199 }
200
201 //---ATTRIBUTES---
202
203 /**
204 * This is the friendly identifier of the
205 * component this class is running in.
206 * eg, a Filter may be called "filter1",
207 * If this class does not have an owning
208 * component, a name from the configuration
209 * can be placed here. This name could also
210 * be changed to null for utility classes.
211 */
212 private String _name = "Memory";
213
214 /**
215 * A reference to the configuration proxy in use
216 */
217 private ConfigurationProxy _cp = ConfigurationProxy.getInstance();
218
219 /**
220 * A HashMap of Registers (or groups of Registers), one
221 * for each host we're monitoring.
222 */
223 private HashMap _hosts = new HashMap();
224
225 //---STATIC ATTRIBUTES---
226
227 }