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