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.ArrayList; |
27 |
import java.util.Set; |
28 |
import java.util.Iterator; |
29 |
import java.text.NumberFormat; |
30 |
import uk.org.iscream.cms.server.client.*; |
31 |
import uk.org.iscream.cms.server.core.*; |
32 |
import uk.org.iscream.cms.util.*; |
33 |
import uk.org.iscream.cms.server.componentmanager.*; |
34 |
|
35 |
/** |
36 |
* This Monitor watches the Service checks on hosts |
37 |
* |
38 |
* @author $Author: tdb $ |
39 |
* @version $Id: Services__Monitor.java,v 1.9 2003/02/24 20:18:48 tdb Exp $ |
40 |
*/ |
41 |
public class Services__Monitor extends MonitorSkeleton { |
42 |
|
43 |
//---FINAL ATTRIBUTES--- |
44 |
|
45 |
/** |
46 |
* The current CVS revision of this class |
47 |
*/ |
48 |
public final String REVISION = "$Revision: 1.9 $"; |
49 |
|
50 |
/** |
51 |
* A description of this monitor |
52 |
*/ |
53 |
public final String DESC = "Monitors a hosts services."; |
54 |
|
55 |
//---STATIC METHODS--- |
56 |
|
57 |
//---CONSTRUCTORS--- |
58 |
|
59 |
//---PUBLIC METHODS--- |
60 |
|
61 |
/** |
62 |
* Analyse a packet of data, and generate an alert if |
63 |
* necessary. |
64 |
* |
65 |
* @param packet the XMLPacket to analyse |
66 |
*/ |
67 |
public void analysePacket(XMLPacket packet) { |
68 |
String source = packet.getParam("packet.attributes.machine_name"); |
69 |
if(!checkBooleanConfig(source, "Monitor." + _name + ".enable")) { |
70 |
return; |
71 |
} |
72 |
if (!_hosts.containsKey(source)) { |
73 |
_hosts.put(source, new HashMap()); |
74 |
} |
75 |
|
76 |
HashMap serviceRegisters = (HashMap) _hosts.get(source); |
77 |
|
78 |
// a tempory holder for all the disk attributes we find |
79 |
ArrayList services = new ArrayList(); |
80 |
|
81 |
// the prefix for keys relating to service checks |
82 |
String keyPrefix = "packet.services."; |
83 |
|
84 |
// unfortunatly we need to check the whole packet |
85 |
// to find the disks, 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 |
if(!services.contains(dataKey)) { |
92 |
String serviceType = ""; |
93 |
|
94 |
// pos is after "packet.services." |
95 |
int pos = keyPrefix.length(); |
96 |
while (dataKey.charAt(pos) != '.') { |
97 |
serviceType = serviceType + dataKey.charAt(pos); |
98 |
pos++; |
99 |
} |
100 |
|
101 |
// add the service to our list, with the packet data |
102 |
services.add(keyPrefix + serviceType + ".attributes.status"); |
103 |
String status = packet.getParam(keyPrefix + serviceType + ".attributes.status"); |
104 |
|
105 |
services.add(keyPrefix + serviceType + ".attributes.message"); |
106 |
String message = packet.getParam(keyPrefix + serviceType + ".attributes.message"); |
107 |
|
108 |
// *** now process this service *** |
109 |
|
110 |
// check if we've seen this service before on a previous run |
111 |
// if not, we need to create a register for it |
112 |
if(!serviceRegisters.containsKey(serviceType)) { |
113 |
serviceRegisters.put(serviceType, new Register(source, _name)); |
114 |
} |
115 |
|
116 |
// get the register for this service |
117 |
Register reg = (Register) serviceRegisters.get(serviceType); |
118 |
|
119 |
|
120 |
// as we don't really have a threshold for services, |
121 |
// a temporary fix is to use the status value as a threshold. |
122 |
// after all, a 0 status is a NORMAL threshold ;) |
123 |
int newThreshold = 0; |
124 |
try { |
125 |
newThreshold = Integer.parseInt(status); |
126 |
} catch (NumberFormatException e) { |
127 |
_logger.write(this.toString(), Logger.WARNING, "Received heartbeat from "+source+" with bad service information: "+e); |
128 |
// don't try to continue and go try the next service |
129 |
break; |
130 |
} |
131 |
|
132 |
// say which service it was |
133 |
String attributeName = serviceType + " service"; |
134 |
String displayValue = ""; |
135 |
if (newThreshold == 0) { |
136 |
displayValue = "RUNNING"; |
137 |
} else { |
138 |
displayValue = "FAILED"; |
139 |
} |
140 |
|
141 |
processAlert(newThreshold, attributeName, reg, source, displayValue); |
142 |
} |
143 |
} |
144 |
} |
145 |
} |
146 |
|
147 |
/** |
148 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
149 |
* method to provide clean logging (every class should have this). |
150 |
* |
151 |
* This uses the uk.org.iscream.cms.util.NameFormat class |
152 |
* to format the toString() |
153 |
* |
154 |
* @return the name of this class and its CVS revision |
155 |
*/ |
156 |
public String toString() { |
157 |
return FormatName.getName( |
158 |
_name, |
159 |
getClass().getName(), |
160 |
REVISION); |
161 |
} |
162 |
|
163 |
/** |
164 |
* return the String representation of what the monitor does |
165 |
*/ |
166 |
public String getDescription(){ |
167 |
return DESC; |
168 |
} |
169 |
|
170 |
//---PRIVATE METHODS--- |
171 |
|
172 |
//---ACCESSOR/MUTATOR METHODS--- |
173 |
|
174 |
/** |
175 |
* Returns a reference to a specific Queue for this |
176 |
* monitor. This Queue returns only the data packets |
177 |
* (based on type) that we want too look at. |
178 |
* |
179 |
* @return a reference to a Queue |
180 |
*/ |
181 |
protected Queue getQueue() { |
182 |
return MonitorManager.getInstance().getOtherQueue(); |
183 |
} |
184 |
|
185 |
//---ATTRIBUTES--- |
186 |
|
187 |
/** |
188 |
* This is the friendly identifier of the |
189 |
* component this class is running in. |
190 |
* eg, a Filter may be called "filter1", |
191 |
* If this class does not have an owning |
192 |
* component, a name from the configuration |
193 |
* can be placed here. This name could also |
194 |
* be changed to null for utility classes. |
195 |
*/ |
196 |
private String _name = "Services"; |
197 |
|
198 |
/** |
199 |
* A HashMap of Registers (or groups of Registers), one |
200 |
* for each host we're monitoring. |
201 |
*/ |
202 |
private HashMap _hosts = new HashMap(); |
203 |
|
204 |
//---STATIC ATTRIBUTES--- |
205 |
|
206 |
} |