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/NetIO__Monitor.java
Revision: 1.3
Committed: Thu Jan 15 14:10:13 2004 UTC (20 years, 4 months ago) by tdb
Branch: MAIN
Changes since 1.2: +3 -3 lines
Log Message:
OK - I can still program Java, I just can't remember how this works :-)

File Contents

# User Rev Content
1 tdb 1.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 tdb 1.2 * @author $Author: tdb $
38 tdb 1.3 * @version $Id: NetIO__Monitor.java,v 1.2 2004/01/15 13:41:48 tdb Exp $
39 tdb 1.1 */
40     public class NetIO__Monitor extends MonitorSkeleton {
41    
42     //---FINAL ATTRIBUTES---
43    
44     /**
45     * The current CVS revision of this class
46     */
47 tdb 1.3 public final String REVISION = "$Revision: 1.2 $";
48 tdb 1.1
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 tdb 1.3 if(!checkBooleanConfig("Host." + source, "Monitor." + _name + ".enable")) {
69 tdb 1.2 return;
70     }
71 tdb 1.1 if (!_hostsR.containsKey(source)) {
72     _hostsR.put(source, new HashMap());
73     }
74     if (!_hostsW.containsKey(source)) {
75     _hostsW.put(source, new HashMap());
76     }
77    
78     HashMap netRegistersR = (HashMap) _hostsR.get(source);
79     HashMap netRegistersW = (HashMap) _hostsW.get(source);
80    
81     // key prefix
82     String keyPrefix = "packet.net.p";
83    
84     // a temporary holder for all the network interfaces we find
85     HashSet netifs = new HashSet();
86    
87     // unfortunatly we need to check the whole packet to find
88     // the interfaces, and then get the data attributes
89     Set packetSet = packet.getSet();
90     Iterator i = packetSet.iterator();
91     while (i.hasNext()) {
92     String dataKey = (String) i.next();
93     if(dataKey.startsWith(keyPrefix)) {
94     // pos is after "packet.net.p"
95     int pos = keyPrefix.length();
96     String ifNumber = dataKey.substring(pos, dataKey.indexOf('.', pos));
97     String thisIf = keyPrefix.concat(ifNumber);
98    
99     if(!netifs.contains(thisIf)) {
100     // add the interface to our list
101     netifs.add(thisIf);
102    
103     String name = packet.getParam(thisIf + ".attributes.name");
104    
105     String rxbytes = packet.getParam(thisIf + ".attributes.rx");
106     String txbytes = packet.getParam(thisIf + ".attributes.tx");
107    
108     // check if we've seen this interface before on a previous
109     // run if not, we need to create a register for it
110     if(!netRegistersR.containsKey(name)) {
111     netRegistersR.put(name, new Register(source, _name + ".rxbytes", name));
112     }
113     if(!netRegistersW.containsKey(name)) {
114     netRegistersW.put(name, new Register(source, _name + ".txbytes", name));
115     }
116    
117     // get the register for this interface
118     Register regR = (Register) netRegistersR.get(name);
119     Register regW = (Register) netRegistersW.get(name);
120    
121     // get the packet data
122     int netRbytes, netWbytes;
123     try {
124     if(rxbytes==null || txbytes==null) {
125     throw new NumberFormatException("Network data invalid");
126     }
127     netRbytes = Integer.parseInt(rxbytes);
128     netWbytes = Integer.parseInt(txbytes);
129     } catch (NumberFormatException e) {
130     _logger.write(this.toString(), Logger.WARNING, "Received packet from "+source+" with bad network information: "+e);
131     // don't try to continue and process, try next interface
132     break;
133     }
134    
135     int newThresholdR = checkAttributeThreshold(netRbytes, regR);
136     int newThresholdW = checkAttributeThreshold(netWbytes, regW);
137    
138     // say which disk had the problem
139     String attributeNameR = "Network rx bytes on " + name;
140     String attributeNameW = "Network tx bytes on " + name;
141    
142     processAlert(newThresholdR, attributeNameR, regR, source, String.valueOf(netRbytes));
143     processAlert(newThresholdW, attributeNameW, regW, source, String.valueOf(netWbytes));
144     }
145     }
146     }
147     }
148    
149     /**
150     * Overrides the {@link java.lang.Object#toString() Object.toString()}
151     * method to provide clean logging (every class should have this).
152     *
153     * This uses the uk.org.iscream.cms.util.NameFormat class
154     * to format the toString()
155     *
156     * @return the name of this class and its CVS revision
157     */
158     public String toString() {
159     return FormatName.getName(
160     _name,
161     getClass().getName(),
162     REVISION);
163     }
164    
165     /**
166     * return the String representation of what the monitor does
167     */
168     public String getDescription(){
169     return DESC;
170     }
171    
172     //---PRIVATE METHODS---
173    
174     /**
175     * Checks a piece of current data, and returns the
176     * threshold it breaches, if any.
177     *
178     * @param value the current value
179     * @param reg the Register for the host
180     * @return the threshold level breached, if any
181     */
182     private int checkAttributeThreshold(double value, Register reg) {
183     for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) {
184     if (reg.getThreshold(thresholdLevel) != -1.0) {
185     // normal check - has the value gone *over* the threshold
186     if(((double) reg.getThreshold(thresholdLevel)) < value) {
187     return thresholdLevel;
188     }
189     }
190     }
191     return Alert.thresholdNORMAL;
192     }
193    
194     //---ACCESSOR/MUTATOR METHODS---
195    
196     /**
197     * Returns a reference to a specific Queue for this
198     * monitor. This Queue returns only the data packets
199     * (based on type) that we want too look at.
200     *
201     * @return a reference to a Queue
202     */
203     protected Queue getQueue() {
204     return MonitorManager.getInstance().getDataQueue();
205     }
206    
207     //---ATTRIBUTES---
208    
209     /**
210     * This is the friendly identifier of the
211     * component this class is running in.
212     * eg, a Filter may be called "filter1",
213     * If this class does not have an owning
214     * component, a name from the configuration
215     * can be placed here. This name could also
216     * be changed to null for utility classes.
217     */
218     private String _name = "NetIO";
219    
220     /**
221     * A reference to the configuration proxy in use
222     */
223     private ConfigurationProxy _cp = ConfigurationProxy.getInstance();
224    
225     /**
226     * A HashMap of Registers (or groups of Registers), one
227     * for each host we're monitoring (reads).
228     */
229     private HashMap _hostsR = new HashMap();
230    
231     /**
232     * A HashMap of Registers (or groups of Registers), one
233     * for each host we're monitoring (writes).
234     */
235     private HashMap _hostsW = new HashMap();
236    
237     //---STATIC ATTRIBUTES---
238    
239     }