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