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