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.filter; |
22 |
|
23 |
//---IMPORTS--- |
24 |
import java.io.*; |
25 |
import java.util.*; |
26 |
|
27 |
import uk.org.iscream.cms.server.util.*; |
28 |
import uk.org.iscream.cms.server.componentmanager.*; |
29 |
import uk.org.iscream.cms.server.core.*; |
30 |
|
31 |
/** |
32 |
* This class setups up and manages Plugins for performing service |
33 |
* checks. When asked to run some service checks it runs the checks |
34 |
* on the service check pipeline for that host. If the pipleline does |
35 |
* not exist it creates a new one. |
36 |
* |
37 |
* This is a singleton class. |
38 |
* |
39 |
* ### |
40 |
* This does not fully manage the pipelines, it should check |
41 |
* if a host has not heartbeated for a while and thus remove |
42 |
* the pipeline. At the moment it does not. |
43 |
* A suggestions for doing this is to use the getCreated() |
44 |
* method on the PluginServiceCheckPipeline. This tells you when |
45 |
* according to the system time the pipeline was created. You could |
46 |
* then have a thread that periodically checks to see if a host |
47 |
* has not run its pipeline in say two heartbeats, and thus |
48 |
* this indicates that it is no longer running. The pipeline |
49 |
* could then be removed from the hashmap and thus be eligable |
50 |
* for gc. |
51 |
* ### |
52 |
* |
53 |
* @author $Author: tdb $ |
54 |
* @version $Id: PluginServiceCheckManager.java,v 1.6 2001/05/29 17:02:35 tdb Exp $ |
55 |
*/ |
56 |
class PluginServiceCheckManager { |
57 |
|
58 |
//---FINAL ATTRIBUTES--- |
59 |
|
60 |
/** |
61 |
* The current CVS revision of this class |
62 |
*/ |
63 |
public final String REVISION = "$Revision: 1.6 $"; |
64 |
|
65 |
/** |
66 |
* file name suffix for plugin service check classes: |
67 |
*/ |
68 |
private final String _suffix = "__ServiceCheck"; |
69 |
|
70 |
//---STATIC METHODS--- |
71 |
|
72 |
/** |
73 |
* Return a reference to the singleton class. |
74 |
* Construct it if it does not already exist, otherwise just return the reference. |
75 |
*/ |
76 |
public static PluginServiceCheckManager getInstance() { |
77 |
if (_instance == null){ |
78 |
_instance = new PluginServiceCheckManager(); |
79 |
} |
80 |
return _instance; |
81 |
} |
82 |
|
83 |
//---CONSTRUCTORS--- |
84 |
|
85 |
/** |
86 |
* Private Constructor - this part creates the filter pipeline |
87 |
* This is a singleton class, btw. |
88 |
*/ |
89 |
private PluginServiceCheckManager(){ |
90 |
_logger.write(toString(), Logger.SYSINIT, "created"); |
91 |
} |
92 |
|
93 |
//---PUBLIC METHODS--- |
94 |
|
95 |
/** |
96 |
* run all registered service checks on a given host, |
97 |
* returning XML data with information about the check. |
98 |
* |
99 |
* @param hostname the host to run service checks for |
100 |
* @return the xml output from the service checks |
101 |
*/ |
102 |
public String runServiceChecks(String hostname){ |
103 |
if (!_hostPipelines.containsKey(hostname)) { |
104 |
_hostPipelines.put(hostname, new PluginServiceCheckPipeline(hostname, this)); |
105 |
} |
106 |
return "<services>" + ((PluginServiceCheckPipeline) _hostPipelines.get(hostname)).runPipeline() + "</services>"; |
107 |
} |
108 |
|
109 |
/** |
110 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
111 |
* method to provide clean logging (every class should have this). |
112 |
* |
113 |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
114 |
* to format the toString() |
115 |
* |
116 |
* @return the name of this class and its CVS revision |
117 |
*/ |
118 |
public String toString() { |
119 |
return FormatName.getName( |
120 |
_name, |
121 |
getClass().getName(), |
122 |
REVISION); |
123 |
} |
124 |
|
125 |
//---PRIVATE METHODS--- |
126 |
|
127 |
/** |
128 |
* Returns a reference to the specified service check. |
129 |
* This method takes the name of the class that contains |
130 |
* a service check implementation. It then checks to see |
131 |
* if it has loaded that class before. If it has it returns |
132 |
* a reference to it, if not, it loads it for the first time, |
133 |
* remembers it, then returns that. |
134 |
* |
135 |
* @param className the PluginServiceCheck to be loaded |
136 |
* @return the requested PluginServiceCheck |
137 |
*/ |
138 |
public synchronized PluginServiceCheck getServiceCheck(String className) { |
139 |
_logger.write(toString(), Logger.DEBUG, "got request for service check - " + className); |
140 |
// see if we've already got the service check loaded |
141 |
if(_loadedServiceChecks.containsKey(className)) { |
142 |
_logger.write(toString(), Logger.DEBUG, className + " already loaded - returning reference to it."); |
143 |
return (PluginServiceCheck) _loadedServiceChecks.get(className); |
144 |
} |
145 |
// otherwise we need to load it and return |
146 |
_logger.write(toString(), Logger.DEBUG, "attempting to load service check - " + className); |
147 |
|
148 |
// Create an instance of the specified PluginServiceCheck |
149 |
// then added it to the list of already loaded service checks |
150 |
PluginServiceCheck serviceCheck = null; |
151 |
try { |
152 |
serviceCheck = (PluginServiceCheck)ClassLoader.getSystemClassLoader().loadClass(className).newInstance(); |
153 |
_loadedServiceChecks.put(className, serviceCheck); |
154 |
_logger.write(toString(), Logger.DEBUG, "loaded service check - "+className+" ("+serviceCheck.getDescription()+")"); |
155 |
} |
156 |
catch (InstantiationException e){ |
157 |
_logger.write(toString(), Logger.ERROR, "failed to instantiate - "+className); |
158 |
_logger.write(toString(), Logger.ERROR, e.getMessage()); |
159 |
} |
160 |
catch (Exception e){ |
161 |
_logger.write(toString(), Logger.ERROR, "failed to add - "+className); |
162 |
_logger.write(toString(), Logger.ERROR, e.toString()); |
163 |
} |
164 |
|
165 |
return serviceCheck; |
166 |
} |
167 |
|
168 |
//---ACCESSOR/MUTATOR METHODS--- |
169 |
|
170 |
//---ATTRIBUTES--- |
171 |
|
172 |
/** |
173 |
* A hash map containing host pipelines |
174 |
*/ |
175 |
private HashMap _hostPipelines = new HashMap(); |
176 |
|
177 |
/** |
178 |
* A hash map containing host pipelines |
179 |
*/ |
180 |
private HashMap _loadedServiceChecks = new HashMap(); |
181 |
|
182 |
/** |
183 |
* This is the friendly identifier of the |
184 |
* component this class is running in. |
185 |
* eg, a Filter may be called "filter1", |
186 |
* If this class does not have an owning |
187 |
* component, a name from the configuration |
188 |
* can be placed here. This name could also |
189 |
* be changed to null for utility classes. |
190 |
*/ |
191 |
private String _name = FilterMain.NAME; |
192 |
|
193 |
/** |
194 |
* This holds a reference to the |
195 |
* system logger that is being used. |
196 |
*/ |
197 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
198 |
|
199 |
/** |
200 |
* A reference to the reference manager in use |
201 |
*/ |
202 |
private ReferenceManager _refman = ReferenceManager.getInstance(); |
203 |
|
204 |
//---STATIC ATTRIBUTES--- |
205 |
|
206 |
/** |
207 |
* A reference to the single instance of this class |
208 |
*/ |
209 |
private static PluginServiceCheckManager _instance; |
210 |
} |