144 |
|
|
145 |
|
// list the files and delete as appropriate |
146 |
|
File alertsDir = new File(rootPath, alertSubDir); |
147 |
< |
// get all the hostnames directories |
148 |
< |
File[] contents = alertsDir.listFiles(); |
149 |
< |
for(int i=0; i < contents.length; i++) { |
150 |
< |
// get a single directory from the array.. |
151 |
< |
File hostdir = contents[i]; |
152 |
< |
// ..and check it's a directory |
153 |
< |
if(hostdir.isDirectory()) { |
154 |
< |
// if this is set, we clean files older than it |
155 |
< |
long deleteFiles = -1; |
156 |
< |
// get all the contents of that directory |
157 |
< |
File[] hostdirContents = hostdir.listFiles(); |
158 |
< |
for(int j=0; j < hostdirContents.length; j++) { |
159 |
< |
File alertFile = hostdirContents[j]; |
160 |
< |
// get the filename.. |
161 |
< |
String filename = alertFile.getName(); |
162 |
< |
// ..and see if it ends with OK or FINAL |
163 |
< |
if(filename.endsWith(Alert.alertLevels[0]) || |
164 |
< |
filename.endsWith(Alert.alertLevels[Alert.alertLevels.length-1])) { |
165 |
< |
// it does end with either OK or FINAL |
166 |
< |
// ... so we can check it for deletion |
167 |
< |
long lastModified = alertFile.lastModified(); |
168 |
< |
long age = System.currentTimeMillis() - lastModified; |
169 |
< |
if(age > ((long) deleteOlderThan*1000)) { |
170 |
< |
// if we're on a final heartbeat, we probably want to |
171 |
< |
// clean up any stale alerts left behind |
172 |
< |
// by setting this flag, we'll clean them up on leaving this loop |
173 |
< |
if(filename.endsWith(".HB."+Alert.alertLevels[Alert.alertLevels.length-1])) { |
174 |
< |
// we do this so that delete files is set to the |
175 |
< |
// latest date of a HB.FINAL. There should only be |
176 |
< |
// one of them though :) |
177 |
< |
if(lastModified > deleteFiles) { |
178 |
< |
deleteFiles = lastModified; |
147 |
> |
// check it's a directory |
148 |
> |
if(alertsDir.isDirectory()) { |
149 |
> |
// get all the hostnames directories |
150 |
> |
File[] contents = alertsDir.listFiles(); |
151 |
> |
for(int i=0; i < contents.length; i++) { |
152 |
> |
// get a single directory from the array.. |
153 |
> |
File hostdir = contents[i]; |
154 |
> |
// ..and check it's a directory |
155 |
> |
if(hostdir.isDirectory()) { |
156 |
> |
// if this is set, we clean files older than it |
157 |
> |
long deleteFiles = -1; |
158 |
> |
// get all the contents of that directory |
159 |
> |
File[] hostdirContents = hostdir.listFiles(); |
160 |
> |
for(int j=0; j < hostdirContents.length; j++) { |
161 |
> |
File alertFile = hostdirContents[j]; |
162 |
> |
// get the filename.. |
163 |
> |
String filename = alertFile.getName(); |
164 |
> |
// ..and see if it ends with OK or FINAL |
165 |
> |
if(filename.endsWith(Alert.alertLevels[0]) || |
166 |
> |
filename.endsWith(Alert.alertLevels[Alert.alertLevels.length-1])) { |
167 |
> |
// it does end with either OK or FINAL |
168 |
> |
// ... so we can check it for deletion |
169 |
> |
long lastModified = alertFile.lastModified(); |
170 |
> |
long age = System.currentTimeMillis() - lastModified; |
171 |
> |
if(age > ((long) deleteOlderThan*1000)) { |
172 |
> |
// if we're on a final heartbeat, we probably want to |
173 |
> |
// clean up any stale alerts left behind |
174 |
> |
// by setting this flag, we'll clean them up on leaving this loop |
175 |
> |
if(filename.endsWith(".HB."+Alert.alertLevels[Alert.alertLevels.length-1])) { |
176 |
> |
// we do this so that delete files is set to the |
177 |
> |
// latest date of a HB.FINAL. There should only be |
178 |
> |
// one of them though :) |
179 |
> |
if(lastModified > deleteFiles) { |
180 |
> |
deleteFiles = lastModified; |
181 |
> |
} |
182 |
|
} |
183 |
+ |
// it's also older than our age to delete older than |
184 |
+ |
if(!alertFile.delete()) { |
185 |
+ |
_logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'old' alert file: "+alertFile.getPath()); |
186 |
+ |
} |
187 |
|
} |
181 |
– |
// it's also older than our age to delete older than |
182 |
– |
if(!alertFile.delete()) { |
183 |
– |
_logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'old' alert file: "+alertFile.getPath()); |
184 |
– |
} |
188 |
|
} |
189 |
|
} |
190 |
< |
} |
191 |
< |
// cleanup stale alerts |
192 |
< |
if(deleteFiles >= 0) { |
193 |
< |
File[] remainingHostdirContents = hostdir.listFiles(); |
194 |
< |
for(int j=0; j < remainingHostdirContents.length; j++) { |
195 |
< |
File alertFile = remainingHostdirContents[j]; |
196 |
< |
if(alertFile.lastModified() < deleteFiles) { |
197 |
< |
// alert file is older than the most recent |
198 |
< |
// FINAL Heartbeat alert. |
199 |
< |
if(alertFile.delete()) { |
200 |
< |
_logger.write(this.toString(), Logger.DEBUG, "Deleted stale alert file: "+alertFile.getPath()); |
190 |
> |
// cleanup stale alerts |
191 |
> |
if(deleteFiles >= 0) { |
192 |
> |
File[] remainingHostdirContents = hostdir.listFiles(); |
193 |
> |
for(int j=0; j < remainingHostdirContents.length; j++) { |
194 |
> |
File alertFile = remainingHostdirContents[j]; |
195 |
> |
if(alertFile.lastModified() < deleteFiles) { |
196 |
> |
// alert file is older than the most recent |
197 |
> |
// FINAL Heartbeat alert. |
198 |
> |
if(alertFile.delete()) { |
199 |
> |
_logger.write(this.toString(), Logger.DEBUG, "Deleted stale alert file: "+alertFile.getPath()); |
200 |
> |
} |
201 |
> |
else { |
202 |
> |
_logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'stale' alert file: "+alertFile.getPath()); |
203 |
> |
} |
204 |
|
} |
199 |
– |
else { |
200 |
– |
_logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'stale' alert file: "+alertFile.getPath()); |
201 |
– |
} |
205 |
|
} |
206 |
|
} |
207 |
< |
} |
208 |
< |
// ---- RECAP ---- |
209 |
< |
// at this point, we have cleaned up any OK or FINAL alerts |
210 |
< |
// that have passed our age limit. We have then cleaned up |
211 |
< |
// any alerts older the most recent Heartbeat FINAL alert, |
212 |
< |
// as these are probably stale. Any files left are valid and |
213 |
< |
// active alerts. We are now in a position to remove the host |
214 |
< |
// directory if it's empty. |
215 |
< |
// --------------- |
216 |
< |
// do a quick check to see if the directory is now empty |
217 |
< |
File[] newHostdirContents = hostdir.listFiles(); |
218 |
< |
if(newHostdirContents.length == 0) { |
219 |
< |
// it does seem to be, try and delete it |
220 |
< |
// this will fail anyway if files still remain |
221 |
< |
if(!hostdir.delete()) { |
222 |
< |
_logger.write(this.toString(), Logger.WARNING, "Failed to delete the following empty host directory: "+hostdir.getPath()); |
207 |
> |
// ---- RECAP ---- |
208 |
> |
// at this point, we have cleaned up any OK or FINAL alerts |
209 |
> |
// that have passed our age limit. We have then cleaned up |
210 |
> |
// any alerts older the most recent Heartbeat FINAL alert, |
211 |
> |
// as these are probably stale. Any files left are valid and |
212 |
> |
// active alerts. We are now in a position to remove the host |
213 |
> |
// directory if it's empty. |
214 |
> |
// --------------- |
215 |
> |
// do a quick check to see if the directory is now empty |
216 |
> |
File[] newHostdirContents = hostdir.listFiles(); |
217 |
> |
if(newHostdirContents.length == 0) { |
218 |
> |
// it does seem to be, try and delete it |
219 |
> |
// this will fail anyway if files still remain |
220 |
> |
if(!hostdir.delete()) { |
221 |
> |
_logger.write(this.toString(), Logger.WARNING, "Failed to delete the following empty host directory: "+hostdir.getPath()); |
222 |
> |
} |
223 |
|
} |
224 |
|
} |
225 |
|
} |
226 |
+ |
} |
227 |
+ |
else { |
228 |
+ |
_logger.write(toString(), Logger.WARNING, "IO error reading alerts directory, maybe it doesn't exist? : " +rootPath+sep+alertSubDir); |
229 |
|
} |
230 |
|
} |
231 |
|
} |