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 Swap for all machines |
34 |
* |
35 |
* @author $Author: tdb $ |
36 |
* @version $Id: Swap__Monitor.java,v 1.9 2002/05/18 18:16:00 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.9 $"; |
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 (!_hosts.containsKey(source)) { |
67 |
_hosts.put(source, new Register(source, _name)); |
68 |
} |
69 |
|
70 |
Register reg = (Register) _hosts.get(source); |
71 |
|
72 |
// find out the threshold level we're at |
73 |
String attributeName = "Swap In Use %"; |
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("Memory 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 |
// percentage of memory in use |
92 |
double swapInUse = (1-(swapFree / swapTotal)) * 100; |
93 |
int newThreshold = checkAttributeThreshold(swapInUse, reg); |
94 |
|
95 |
// format the memoryInUse to a String |
96 |
NumberFormat nf = NumberFormat.getInstance(); |
97 |
nf.setMaximumFractionDigits(2); |
98 |
nf.setMinimumFractionDigits(2); |
99 |
String strSwapInUse = nf.format(swapInUse); |
100 |
|
101 |
processAlert(newThreshold, attributeName, reg, source, strSwapInUse); |
102 |
|
103 |
} |
104 |
|
105 |
/** |
106 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
107 |
* method to provide clean logging (every class should have this). |
108 |
* |
109 |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
110 |
* to format the toString() |
111 |
* |
112 |
* @return the name of this class and its CVS revision |
113 |
*/ |
114 |
public String toString() { |
115 |
return FormatName.getName( |
116 |
_name, |
117 |
getClass().getName(), |
118 |
REVISION); |
119 |
} |
120 |
|
121 |
/** |
122 |
* return the String representation of what the monitor does |
123 |
*/ |
124 |
public String getDescription(){ |
125 |
return DESC; |
126 |
} |
127 |
|
128 |
//---PRIVATE METHODS--- |
129 |
|
130 |
/** |
131 |
* Checks a piece of current data, and returns the |
132 |
* threshold it breaches, if any. |
133 |
* |
134 |
* @param attributeString a String representing the current data value |
135 |
* @param reg the Register for the host |
136 |
* @return the threshold level breached, if any |
137 |
*/ |
138 |
private int checkAttributeThreshold(double swapInUse, Register reg) { |
139 |
for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) { |
140 |
if (reg.getThreshold(thresholdLevel) != -1.0) { |
141 |
if(((double) reg.getThreshold(thresholdLevel)) < swapInUse) { |
142 |
return thresholdLevel; |
143 |
} |
144 |
} |
145 |
} |
146 |
return Alert.thresholdNORMAL; |
147 |
} |
148 |
|
149 |
//---ACCESSOR/MUTATOR METHODS--- |
150 |
|
151 |
/** |
152 |
* Returns a reference to a specific Queue for this |
153 |
* monitor. This Queue returns only the data packets |
154 |
* (based on type) that we want too look at. |
155 |
* |
156 |
* @return a reference to a Queue |
157 |
*/ |
158 |
protected Queue getQueue() { |
159 |
return MonitorManager.getInstance().getDataQueue(); |
160 |
} |
161 |
|
162 |
|
163 |
//---ATTRIBUTES--- |
164 |
|
165 |
/** |
166 |
* This is the friendly identifier of the |
167 |
* component this class is running in. |
168 |
* eg, a Filter may be called "filter1", |
169 |
* If this class does not have an owning |
170 |
* component, a name from the configuration |
171 |
* can be placed here. This name could also |
172 |
* be changed to null for utility classes. |
173 |
*/ |
174 |
private String _name = "Swap"; |
175 |
|
176 |
/** |
177 |
* A HashMap of Registers (or groups of Registers), one |
178 |
* for each host we're monitoring. |
179 |
*/ |
180 |
private HashMap _hosts = new HashMap(); |
181 |
|
182 |
//---STATIC ATTRIBUTES--- |
183 |
|
184 |
} |