1 |
/* |
2 |
* i-scream central monitoring system |
3 |
* Copyright (C) 2000-2002 i-scream |
4 |
* |
5 |
* This program is free software; you can redistribute it and/or |
6 |
* modify it under the terms of the GNU General Public License |
7 |
* as published by the Free Software Foundation; either version 2 |
8 |
* of the License, or (at your option) any later version. |
9 |
* |
10 |
* This program is distributed in the hope that it will be useful, |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
* GNU General Public License for more details. |
14 |
* |
15 |
* You should have received a copy of the GNU General Public License |
16 |
* along with this program; if not, write to the Free Software |
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
*/ |
19 |
|
20 |
//---PACKAGE DECLARATION--- |
21 |
package uk.org.iscream.cms.server.client.monitors; |
22 |
|
23 |
//---IMPORTS--- |
24 |
import java.util.HashMap; |
25 |
import java.util.ArrayList; |
26 |
import java.util.Set; |
27 |
import java.util.Iterator; |
28 |
import java.text.NumberFormat; |
29 |
import uk.org.iscream.cms.server.client.*; |
30 |
import uk.org.iscream.cms.server.core.*; |
31 |
import uk.org.iscream.cms.server.util.*; |
32 |
import uk.org.iscream.cms.server.componentmanager.*; |
33 |
|
34 |
/** |
35 |
* This Monitor watches the Service checks on hosts |
36 |
* |
37 |
* @author $Author: tdb $ |
38 |
* @version $Id: Services__Monitor.java,v 1.5 2001/05/29 17:02:34 tdb Exp $ |
39 |
*/ |
40 |
public class Services__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.5 $"; |
48 |
|
49 |
/** |
50 |
* A description of this monitor |
51 |
*/ |
52 |
public final String DESC = "Monitors a hosts services."; |
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 (!_hosts.containsKey(source)) { |
69 |
_hosts.put(source, new HashMap()); |
70 |
} |
71 |
|
72 |
HashMap serviceRegisters = (HashMap) _hosts.get(source); |
73 |
|
74 |
// a tempory holder for all the disk attributes we find |
75 |
ArrayList services = new ArrayList(); |
76 |
|
77 |
// the prefix for keys relating to service checks |
78 |
String keyPrefix = "packet.services."; |
79 |
|
80 |
// unfortunatly we need to check the whole packet |
81 |
// to find the disks, and then get the data attributes |
82 |
Set packetSet = packet.getSet(); |
83 |
Iterator i = packetSet.iterator(); |
84 |
while (i.hasNext()) { |
85 |
String dataKey = (String) i.next(); |
86 |
if(dataKey.startsWith(keyPrefix)) { |
87 |
if(!services.contains(dataKey)) { |
88 |
String serviceType = ""; |
89 |
|
90 |
// pos is after "packet.services." |
91 |
int pos = keyPrefix.length(); |
92 |
while (dataKey.charAt(pos) != '.') { |
93 |
serviceType = serviceType + dataKey.charAt(pos); |
94 |
pos++; |
95 |
} |
96 |
|
97 |
// add the service to our list, with the packet data |
98 |
services.add(keyPrefix + serviceType + ".attributes.status"); |
99 |
String status = packet.getParam(keyPrefix + serviceType + ".attributes.status"); |
100 |
|
101 |
services.add(keyPrefix + serviceType + ".attributes.message"); |
102 |
String message = packet.getParam(keyPrefix + serviceType + ".attributes.message"); |
103 |
|
104 |
// *** now process this service *** |
105 |
|
106 |
// check if we've seen this service before on a previous run |
107 |
// if not, we need to create a register for it |
108 |
if(!serviceRegisters.containsKey(serviceType)) { |
109 |
serviceRegisters.put(serviceType, new Register(source, _name)); |
110 |
} |
111 |
|
112 |
// get the register for this service |
113 |
Register reg = (Register) serviceRegisters.get(serviceType); |
114 |
|
115 |
|
116 |
// as we don't really have a threshold for services, |
117 |
// a temporary fix is to use the status value as a threshold. |
118 |
// after all, a 0 status is a NORMAL threshold ;) |
119 |
int newThreshold = 0; |
120 |
try { |
121 |
newThreshold = Integer.parseInt(status); |
122 |
} catch (NumberFormatException e) { |
123 |
_logger.write(this.toString(), Logger.WARNING, "Received heartbeat from "+source+" with bad service information: "+e); |
124 |
// don't try to continue and go try the next service |
125 |
break; |
126 |
} |
127 |
|
128 |
// say which service it was |
129 |
String attributeName = serviceType + " service"; |
130 |
String displayValue = ""; |
131 |
if (newThreshold == 0) { |
132 |
displayValue = "RUNNING"; |
133 |
} else { |
134 |
displayValue = "FAILED"; |
135 |
} |
136 |
|
137 |
processAlert(newThreshold, attributeName, reg, source, displayValue); |
138 |
} |
139 |
} |
140 |
} |
141 |
} |
142 |
|
143 |
/** |
144 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
145 |
* method to provide clean logging (every class should have this). |
146 |
* |
147 |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
148 |
* to format the toString() |
149 |
* |
150 |
* @return the name of this class and its CVS revision |
151 |
*/ |
152 |
public String toString() { |
153 |
return FormatName.getName( |
154 |
_name, |
155 |
getClass().getName(), |
156 |
REVISION); |
157 |
} |
158 |
|
159 |
/** |
160 |
* return the String representation of what the monitor does |
161 |
*/ |
162 |
public String getDescription(){ |
163 |
return DESC; |
164 |
} |
165 |
|
166 |
//---PRIVATE METHODS--- |
167 |
|
168 |
//---ACCESSOR/MUTATOR METHODS--- |
169 |
|
170 |
/** |
171 |
* Returns a reference to a specific Queue for this |
172 |
* monitor. This Queue returns only the data packets |
173 |
* (based on type) that we want too look at. |
174 |
* |
175 |
* @return a reference to a Queue |
176 |
*/ |
177 |
protected Queue getQueue() { |
178 |
return MonitorManager.getInstance().getHeartbeatQueue(); |
179 |
} |
180 |
|
181 |
//---ATTRIBUTES--- |
182 |
|
183 |
/** |
184 |
* This is the friendly identifier of the |
185 |
* component this class is running in. |
186 |
* eg, a Filter may be called "filter1", |
187 |
* If this class does not have an owning |
188 |
* component, a name from the configuration |
189 |
* can be placed here. This name could also |
190 |
* be changed to null for utility classes. |
191 |
*/ |
192 |
private String _name = "Services"; |
193 |
|
194 |
/** |
195 |
* A HashMap of Registers (or groups of Registers), one |
196 |
* for each host we're monitoring. |
197 |
*/ |
198 |
private HashMap _hosts = new HashMap(); |
199 |
|
200 |
//---STATIC ATTRIBUTES--- |
201 |
|
202 |
} |