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.client; |
22 |
> |
package uk.org.iscream.cms.server.client; |
23 |
|
|
24 |
|
//---IMPORTS--- |
25 |
< |
import uk.org.iscream.componentmanager.*; |
26 |
< |
import uk.org.iscream.core.*; |
27 |
< |
import uk.org.iscream.util.*; |
25 |
> |
import uk.org.iscream.cms.server.componentmanager.*; |
26 |
> |
import uk.org.iscream.cms.server.core.*; |
27 |
> |
import uk.org.iscream.cms.server.util.*; |
28 |
|
import java.io.*; |
29 |
|
|
30 |
|
/** |
31 |
|
* Provides a feed to the webpage system. |
32 |
|
* |
13 |
– |
* !! There may also be need to have a Thread to grab any |
14 |
– |
* !! required config (groups, "nice names, etc) and dump |
15 |
– |
* !! that to a file. |
16 |
– |
* |
33 |
|
* @author $Author$ |
34 |
|
* @version $Id$ |
35 |
|
*/ |
63 |
|
* Return a reference to the single class. |
64 |
|
* Construct it if it does not already exist, otherwise just return the reference. |
65 |
|
*/ |
66 |
< |
public static WebFeeder getInstance() { |
66 |
> |
public synchronized static WebFeeder getInstance() { |
67 |
|
if (_instance == null){ |
68 |
|
_instance = new WebFeeder(); |
69 |
|
} |
164 |
|
|
165 |
|
// list the files and delete as appropriate |
166 |
|
File alertsDir = new File(rootPath, alertSubDir); |
167 |
< |
// get all the hostnames directories |
168 |
< |
File[] contents = alertsDir.listFiles(); |
169 |
< |
for(int i=0; i < contents.length; i++) { |
170 |
< |
// get a single directory from the array.. |
171 |
< |
File hostdir = contents[i]; |
172 |
< |
// ..and check it's a directory |
173 |
< |
if(hostdir.isDirectory()) { |
174 |
< |
// get all the contents of that directory |
175 |
< |
File[] hostdirContents = hostdir.listFiles(); |
176 |
< |
for(int j=0; j < hostdirContents.length; j++) { |
177 |
< |
File alertFile = hostdirContents[j]; |
178 |
< |
// get the filename.. |
179 |
< |
String filename = alertFile.getName(); |
180 |
< |
// ..and see if it ends with OK or FINAL |
181 |
< |
if(filename.endsWith(Alert.alertLevels[0]) || |
182 |
< |
filename.endsWith(Alert.alertLevels[Alert.alertLevels.length-1])) { |
183 |
< |
// it does end with either OK or FINAL |
184 |
< |
// ... so we can check it for deletion |
185 |
< |
long lastModified = alertFile.lastModified(); |
186 |
< |
long age = System.currentTimeMillis() - lastModified; |
187 |
< |
if(age > ((long) deleteOlderThan*1000)) { |
188 |
< |
// it's also older than our age to delete older than |
189 |
< |
if(alertFile.delete()) { |
190 |
< |
// do a quick check to see if the directory is now empty |
191 |
< |
File[] newHostdirContents = hostdir.listFiles(); |
192 |
< |
if(newHostdirContents.length == 0) { |
193 |
< |
// it does seem to be, try and delete it |
194 |
< |
// this will fail anyway if files still remain |
195 |
< |
if(!hostdir.delete()) { |
196 |
< |
_logger.write(this.toString(), Logger.WARNING, "Failed to delete the following empty host directory: "+hostdir.getPath()); |
167 |
> |
// check it's a directory |
168 |
> |
if(alertsDir.isDirectory()) { |
169 |
> |
// get all the hostnames directories |
170 |
> |
File[] contents = alertsDir.listFiles(); |
171 |
> |
for(int i=0; i < contents.length; i++) { |
172 |
> |
// get a single directory from the array.. |
173 |
> |
File hostdir = contents[i]; |
174 |
> |
// ..and check it's a directory |
175 |
> |
if(hostdir.isDirectory()) { |
176 |
> |
// if this is set, we clean files older than it |
177 |
> |
long deleteFiles = -1; |
178 |
> |
// get all the contents of that directory |
179 |
> |
File[] hostdirContents = hostdir.listFiles(); |
180 |
> |
for(int j=0; j < hostdirContents.length; j++) { |
181 |
> |
File alertFile = hostdirContents[j]; |
182 |
> |
// get the filename.. |
183 |
> |
String filename = alertFile.getName(); |
184 |
> |
// ..and see if it ends with OK or FINAL |
185 |
> |
if(filename.endsWith(Alert.alertLevels[0]) || |
186 |
> |
filename.endsWith(Alert.alertLevels[Alert.alertLevels.length-1])) { |
187 |
> |
// it does end with either OK or FINAL |
188 |
> |
// ... so we can check it for deletion |
189 |
> |
long lastModified = alertFile.lastModified(); |
190 |
> |
long age = System.currentTimeMillis() - lastModified; |
191 |
> |
if(age > ((long) deleteOlderThan*1000)) { |
192 |
> |
// if we're on a final heartbeat, we probably want to |
193 |
> |
// clean up any stale alerts left behind |
194 |
> |
// by setting this flag, we'll clean them up on leaving this loop |
195 |
> |
if(filename.endsWith(".HB."+Alert.alertLevels[Alert.alertLevels.length-1])) { |
196 |
> |
// we do this so that delete files is set to the |
197 |
> |
// latest date of a HB.FINAL. There should only be |
198 |
> |
// one of them though :) |
199 |
> |
if(lastModified > deleteFiles) { |
200 |
> |
deleteFiles = lastModified; |
201 |
|
} |
202 |
|
} |
203 |
+ |
// it's also older than our age to delete older than |
204 |
+ |
if(!alertFile.delete()) { |
205 |
+ |
_logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'old' alert file: "+alertFile.getPath()); |
206 |
+ |
} |
207 |
|
} |
208 |
< |
else { |
209 |
< |
_logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'old' alert file: "+alertFile.getPath()); |
208 |
> |
} |
209 |
> |
} |
210 |
> |
// cleanup stale alerts |
211 |
> |
if(deleteFiles >= 0) { |
212 |
> |
File[] remainingHostdirContents = hostdir.listFiles(); |
213 |
> |
for(int j=0; j < remainingHostdirContents.length; j++) { |
214 |
> |
File alertFile = remainingHostdirContents[j]; |
215 |
> |
if(alertFile.lastModified() < deleteFiles) { |
216 |
> |
// alert file is older than the most recent |
217 |
> |
// FINAL Heartbeat alert. |
218 |
> |
if(alertFile.delete()) { |
219 |
> |
_logger.write(this.toString(), Logger.DEBUG, "Deleted stale alert file: "+alertFile.getPath()); |
220 |
> |
} |
221 |
> |
else { |
222 |
> |
_logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'stale' alert file: "+alertFile.getPath()); |
223 |
> |
} |
224 |
|
} |
225 |
|
} |
226 |
|
} |
227 |
+ |
// ---- RECAP ---- |
228 |
+ |
// at this point, we have cleaned up any OK or FINAL alerts |
229 |
+ |
// that have passed our age limit. We have then cleaned up |
230 |
+ |
// any alerts older the most recent Heartbeat FINAL alert, |
231 |
+ |
// as these are probably stale. Any files left are valid and |
232 |
+ |
// active alerts. We are now in a position to remove the host |
233 |
+ |
// directory if it's empty. |
234 |
+ |
// --------------- |
235 |
+ |
// do a quick check to see if the directory is now empty |
236 |
+ |
File[] newHostdirContents = hostdir.listFiles(); |
237 |
+ |
if(newHostdirContents.length == 0) { |
238 |
+ |
// it does seem to be, try and delete it |
239 |
+ |
// this will fail anyway if files still remain |
240 |
+ |
if(!hostdir.delete()) { |
241 |
+ |
_logger.write(this.toString(), Logger.WARNING, "Failed to delete the following empty host directory: "+hostdir.getPath()); |
242 |
+ |
} |
243 |
+ |
} |
244 |
|
} |
245 |
|
} |
246 |
|
} |
247 |
+ |
else { |
248 |
+ |
_logger.write(toString(), Logger.WARNING, "IO error reading alerts directory, maybe it doesn't exist? : " +rootPath+sep+alertSubDir); |
249 |
+ |
} |
250 |
|
} |
251 |
|
} |
252 |
|
|
315 |
|
File outFile; |
316 |
|
// check if we're at a special "end case" (OK or FINAL) |
317 |
|
if(alert.getLevel()==0 || alert.getLevel()==Alert.alertLevels.length-1) { |
318 |
+ |
if(alert.getAttributeName().equals("Heartbeat") && alert.getLevel()==Alert.alertLevels.length-1) { |
319 |
+ |
// new file is something like alert.nnnnnnnn.HB.FINAL |
320 |
+ |
outFile = new File(rootPath, destFile+".HB."+Alert.alertLevels[alert.getLevel()]); |
321 |
+ |
} else { |
322 |
+ |
// new file is something like alert.nnnnnnnn.OK |
323 |
+ |
outFile = new File(rootPath, destFile+"."+Alert.alertLevels[alert.getLevel()]); |
324 |
+ |
} |
325 |
|
File oldFile = new File(rootPath, destFile); |
261 |
– |
outFile = new File(rootPath, destFile+"."+Alert.alertLevels[alert.getLevel()]); |
326 |
|
if(!oldFile.renameTo(outFile)) { |
327 |
|
_logger.write(this.toString(), Logger.WARNING, "Failed to rename old file, "+oldFile.getPath()+" to new file, "+outFile.getPath()); |
328 |
|
} |
337 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
338 |
|
* method to provide clean logging (every class should have this). |
339 |
|
* |
340 |
< |
* This uses the uk.org.iscream.util.FormatName class |
340 |
> |
* This uses the uk.org.iscream.cms.server.util.FormatName class |
341 |
|
* to format the toString() |
342 |
|
* |
343 |
|
* @return the name of this class and its CVS revision |