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.util.HashSet; |
27 |
import java.util.Set; |
28 |
import java.util.Iterator; |
29 |
import uk.org.iscream.cms.server.client.*; |
30 |
import uk.org.iscream.cms.server.core.*; |
31 |
import uk.org.iscream.cms.util.*; |
32 |
import uk.org.iscream.cms.server.componentmanager.*; |
33 |
|
34 |
/** |
35 |
* This Monitor watches the network IO for all machines |
36 |
* |
37 |
* @author $Author$ |
38 |
* @version $Id$ |
39 |
*/ |
40 |
public class NetIO__Monitor extends MonitorSkeleton { |
41 |
|
42 |
//---FINAL ATTRIBUTES--- |
43 |
|
44 |
/** |
45 |
* The current CVS revision of this class |
46 |
*/ |
47 |
public final String REVISION = "$Revision: 1.1 $"; |
48 |
|
49 |
/** |
50 |
* A description of this monitor |
51 |
*/ |
52 |
public final String DESC = "Monitors network IO on all hosts."; |
53 |
|
54 |
//---STATIC METHODS--- |
55 |
|
56 |
//---CONSTRUCTORS--- |
57 |
|
58 |
//---PUBLIC METHODS--- |
59 |
|
60 |
/** |
61 |
* Analyse a packet of data, and generate an alert if |
62 |
* necessary. |
63 |
* |
64 |
* @param packet the XMLPacket to analyse |
65 |
*/ |
66 |
public void analysePacket(XMLPacket packet) { |
67 |
String source = packet.getParam("packet.attributes.machine_name"); |
68 |
if (!_hostsR.containsKey(source)) { |
69 |
_hostsR.put(source, new HashMap()); |
70 |
} |
71 |
if (!_hostsW.containsKey(source)) { |
72 |
_hostsW.put(source, new HashMap()); |
73 |
} |
74 |
|
75 |
HashMap netRegistersR = (HashMap) _hostsR.get(source); |
76 |
HashMap netRegistersW = (HashMap) _hostsW.get(source); |
77 |
|
78 |
// key prefix |
79 |
String keyPrefix = "packet.net.p"; |
80 |
|
81 |
// a temporary holder for all the network interfaces we find |
82 |
HashSet netifs = new HashSet(); |
83 |
|
84 |
// unfortunatly we need to check the whole packet to find |
85 |
// the interfaces, and then get the data attributes |
86 |
Set packetSet = packet.getSet(); |
87 |
Iterator i = packetSet.iterator(); |
88 |
while (i.hasNext()) { |
89 |
String dataKey = (String) i.next(); |
90 |
if(dataKey.startsWith(keyPrefix)) { |
91 |
// pos is after "packet.net.p" |
92 |
int pos = keyPrefix.length(); |
93 |
String ifNumber = dataKey.substring(pos, dataKey.indexOf('.', pos)); |
94 |
String thisIf = keyPrefix.concat(ifNumber); |
95 |
|
96 |
if(!netifs.contains(thisIf)) { |
97 |
// add the interface to our list |
98 |
netifs.add(thisIf); |
99 |
|
100 |
String name = packet.getParam(thisIf + ".attributes.name"); |
101 |
|
102 |
String rxbytes = packet.getParam(thisIf + ".attributes.rx"); |
103 |
String txbytes = packet.getParam(thisIf + ".attributes.tx"); |
104 |
|
105 |
// check if we've seen this interface before on a previous |
106 |
// run if not, we need to create a register for it |
107 |
if(!netRegistersR.containsKey(name)) { |
108 |
netRegistersR.put(name, new Register(source, _name + ".rxbytes", name)); |
109 |
} |
110 |
if(!netRegistersW.containsKey(name)) { |
111 |
netRegistersW.put(name, new Register(source, _name + ".txbytes", name)); |
112 |
} |
113 |
|
114 |
// get the register for this interface |
115 |
Register regR = (Register) netRegistersR.get(name); |
116 |
Register regW = (Register) netRegistersW.get(name); |
117 |
|
118 |
// get the packet data |
119 |
int netRbytes, netWbytes; |
120 |
try { |
121 |
if(rxbytes==null || txbytes==null) { |
122 |
throw new NumberFormatException("Network data invalid"); |
123 |
} |
124 |
netRbytes = Integer.parseInt(rxbytes); |
125 |
netWbytes = Integer.parseInt(txbytes); |
126 |
} catch (NumberFormatException e) { |
127 |
_logger.write(this.toString(), Logger.WARNING, "Received packet from "+source+" with bad network information: "+e); |
128 |
// don't try to continue and process, try next interface |
129 |
break; |
130 |
} |
131 |
|
132 |
int newThresholdR = checkAttributeThreshold(netRbytes, regR); |
133 |
int newThresholdW = checkAttributeThreshold(netWbytes, regW); |
134 |
|
135 |
// say which disk had the problem |
136 |
String attributeNameR = "Network rx bytes on " + name; |
137 |
String attributeNameW = "Network tx bytes on " + name; |
138 |
|
139 |
processAlert(newThresholdR, attributeNameR, regR, source, String.valueOf(netRbytes)); |
140 |
processAlert(newThresholdW, attributeNameW, regW, source, String.valueOf(netWbytes)); |
141 |
} |
142 |
} |
143 |
} |
144 |
} |
145 |
|
146 |
/** |
147 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
148 |
* method to provide clean logging (every class should have this). |
149 |
* |
150 |
* This uses the uk.org.iscream.cms.util.NameFormat class |
151 |
* to format the toString() |
152 |
* |
153 |
* @return the name of this class and its CVS revision |
154 |
*/ |
155 |
public String toString() { |
156 |
return FormatName.getName( |
157 |
_name, |
158 |
getClass().getName(), |
159 |
REVISION); |
160 |
} |
161 |
|
162 |
/** |
163 |
* return the String representation of what the monitor does |
164 |
*/ |
165 |
public String getDescription(){ |
166 |
return DESC; |
167 |
} |
168 |
|
169 |
//---PRIVATE METHODS--- |
170 |
|
171 |
/** |
172 |
* Checks a piece of current data, and returns the |
173 |
* threshold it breaches, if any. |
174 |
* |
175 |
* @param value the current value |
176 |
* @param reg the Register for the host |
177 |
* @return the threshold level breached, if any |
178 |
*/ |
179 |
private int checkAttributeThreshold(double value, Register reg) { |
180 |
for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) { |
181 |
if (reg.getThreshold(thresholdLevel) != -1.0) { |
182 |
// normal check - has the value gone *over* the threshold |
183 |
if(((double) reg.getThreshold(thresholdLevel)) < value) { |
184 |
return thresholdLevel; |
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 |
//---ATTRIBUTES--- |
205 |
|
206 |
/** |
207 |
* This is the friendly identifier of the |
208 |
* component this class is running in. |
209 |
* eg, a Filter may be called "filter1", |
210 |
* If this class does not have an owning |
211 |
* component, a name from the configuration |
212 |
* can be placed here. This name could also |
213 |
* be changed to null for utility classes. |
214 |
*/ |
215 |
private String _name = "NetIO"; |
216 |
|
217 |
/** |
218 |
* A reference to the configuration proxy in use |
219 |
*/ |
220 |
private ConfigurationProxy _cp = ConfigurationProxy.getInstance(); |
221 |
|
222 |
/** |
223 |
* A HashMap of Registers (or groups of Registers), one |
224 |
* for each host we're monitoring (reads). |
225 |
*/ |
226 |
private HashMap _hostsR = new HashMap(); |
227 |
|
228 |
/** |
229 |
* A HashMap of Registers (or groups of Registers), one |
230 |
* for each host we're monitoring (writes). |
231 |
*/ |
232 |
private HashMap _hostsW = new HashMap(); |
233 |
|
234 |
//---STATIC ATTRIBUTES--- |
235 |
|
236 |
} |