ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/client/WebFeeder.java
(Generate patch)

Comparing projects/cms/source/server/uk/org/iscream/cms/server/client/WebFeeder.java (file contents):
Revision 1.12 by tdb, Thu Mar 15 22:20:41 2001 UTC vs.
Revision 1.22 by tdb, Sat May 18 18:16:00 2002 UTC

# Line 1 | Line 1
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.client;
21 > package uk.org.iscream.cms.server.client;
22  
23   //---IMPORTS---
24 < import uk.org.iscream.componentmanager.*;
25 < import uk.org.iscream.core.*;
26 < import uk.org.iscream.util.*;
24 > import uk.org.iscream.cms.server.componentmanager.*;
25 > import uk.org.iscream.cms.server.core.*;
26 > import uk.org.iscream.cms.server.util.*;
27   import java.io.*;
28  
29   /**
30   * Provides a feed to the webpage system.
31   *
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 *
32   * @author  $Author$
33   * @version $Id$
34   */
# Line 36 | Line 51 | public class WebFeeder extends Thread {
51       */
52      public final int DEFAULT_AGE = 3600;
53      
54 +    /**
55 +     * The default path seperator, here for convienience
56 +     */
57 +    private final String sep = File.separator;
58 +    
59   //---STATIC METHODS---
60  
61      /**
62       * Return a reference to the single class.
63       * Construct it if it does not already exist, otherwise just return the reference.
64       */
65 <    public static WebFeeder getInstance() {
65 >    public synchronized static WebFeeder getInstance() {
66          if (_instance == null){
67              _instance = new WebFeeder();
68          }
# Line 71 | Line 91 | public class WebFeeder extends Thread {
91              alertSubDir = cp.getProperty("WebFeeder", "WebFeeder.alertSubDir");
92              File alertsDir = new File(rootPath, alertSubDir);
93              if(deleteContents(alertsDir)) {
94 <                _logger.write(this.toString(), Logger.DEBUG, "Deleted all files and directories from: "+rootPath+"/"+alertSubDir);
94 >                _logger.write(this.toString(), Logger.DEBUG, "Deleted all files and directories from: "+rootPath+sep+alertSubDir);
95              } else {
96 <                _logger.write(this.toString(), Logger.WARNING, "Failed to delete all files and directories from: "+rootPath+"/"+alertSubDir);
96 >                _logger.write(this.toString(), Logger.WARNING, "Failed to delete all files and directories from: "+rootPath+sep+alertSubDir);
97              }
98              // cleanup complete
99          } catch (PropertyNotFoundException e) {
# Line 143 | Line 163 | public class WebFeeder extends Thread {
163              
164              // list the files and delete as appropriate
165              File alertsDir = new File(rootPath, alertSubDir);
166 <            File[] contents = alertsDir.listFiles();
167 <            for(int i=0; i < contents.length; i++) {
168 <                File hostdir = contents[i];
169 <                if(hostdir.isDirectory()) {
170 <                    File[] hostdirContents = hostdir.listFiles();
171 <                    for(int j=0; j < hostdirContents.length; j++) {
172 <                        File alertFile = hostdirContents[j];
173 <                        String filename = alertFile.getName();
174 <                        if(filename.endsWith(Alert.alertLevels[0]) ||
175 <                           filename.endsWith(Alert.alertLevels[Alert.alertLevels.length-1])) {
176 <                            // it ends with either OK or FINAL
177 <                            // ... so we can check it for deletion
178 <                            long lastModified = alertFile.lastModified();
179 <                            long age = System.currentTimeMillis() - lastModified;
180 <                            if(age > ((long) deleteOlderThan*1000)) {
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());
166 >            // check it's a directory
167 >            if(alertsDir.isDirectory()) {
168 >                // get all the hostnames directories
169 >                File[] contents = alertsDir.listFiles();
170 >                for(int i=0; i < contents.length; i++) {
171 >                    // get a single directory from the array..
172 >                    File hostdir = contents[i];
173 >                    // ..and check it's a directory
174 >                    if(hostdir.isDirectory()) {
175 >                        // if this is set, we clean files older than it
176 >                        long deleteFiles = -1;
177 >                        // get all the contents of that directory
178 >                        File[] hostdirContents = hostdir.listFiles();
179 >                        for(int j=0; j < hostdirContents.length; j++) {
180 >                            File alertFile = hostdirContents[j];
181 >                            // get the filename..
182 >                            String filename = alertFile.getName();
183 >                            // ..and see if it ends with OK or FINAL
184 >                            if(filename.endsWith(Alert.alertLevels[0]) ||
185 >                               filename.endsWith(Alert.alertLevels[Alert.alertLevels.length-1])) {
186 >                                // it does end with either OK or FINAL
187 >                                // ... so we can check it for deletion
188 >                                long lastModified = alertFile.lastModified();
189 >                                long age = System.currentTimeMillis() - lastModified;
190 >                                if(age > ((long) deleteOlderThan*1000)) {
191 >                                    // if we're on a final heartbeat, we probably want to
192 >                                    // clean up any stale alerts left behind
193 >                                    // by setting this flag, we'll clean them up on leaving this loop
194 >                                    if(filename.endsWith(".HB."+Alert.alertLevels[Alert.alertLevels.length-1])) {
195 >                                        // we do this so that delete files is set to the
196 >                                        // latest date of a HB.FINAL. There should only be
197 >                                        // one of them though :)
198 >                                        if(lastModified > deleteFiles) {
199 >                                            deleteFiles = lastModified;
200 >                                        }
201 >                                    }
202 >                                    // it's also older than our age to delete older than
203 >                                    if(!alertFile.delete()) {
204 >                                        _logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'old' alert file: "+alertFile.getPath());
205 >                                    }
206                                  }
207                              }
208                          }
209 +                        // cleanup stale alerts
210 +                        if(deleteFiles >= 0) {
211 +                            File[] remainingHostdirContents = hostdir.listFiles();
212 +                            for(int j=0; j < remainingHostdirContents.length; j++) {
213 +                                File alertFile = remainingHostdirContents[j];
214 +                                if(alertFile.lastModified() < deleteFiles) {
215 +                                    // alert file is older than the most recent
216 +                                    // FINAL Heartbeat alert.
217 +                                    if(alertFile.delete()) {
218 +                                        _logger.write(this.toString(), Logger.DEBUG, "Deleted stale alert file: "+alertFile.getPath());
219 +                                    }
220 +                                    else {
221 +                                        _logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'stale' alert file: "+alertFile.getPath());
222 +                                    }
223 +                                }
224 +                            }
225 +                        }
226 +                        // ---- RECAP ----
227 +                        // at this point, we have cleaned up any OK or FINAL alerts
228 +                        // that have passed our age limit. We have then cleaned up
229 +                        // any alerts older the most recent Heartbeat FINAL alert,
230 +                        // as these are probably stale. Any files left are valid and
231 +                        // active alerts. We are now in a position to remove the host
232 +                        // directory if it's empty.
233 +                        // ---------------
234 +                        // do a quick check to see if the directory is now empty
235 +                        File[] newHostdirContents = hostdir.listFiles();
236 +                        if(newHostdirContents.length == 0) {
237 +                            // it does seem to be, try and delete it
238 +                            // this will fail anyway if files still remain
239 +                            if(!hostdir.delete()) {
240 +                                _logger.write(this.toString(), Logger.WARNING, "Failed to delete the following empty host directory: "+hostdir.getPath());
241 +                            }
242 +                        }
243                      }
244                  }
245              }
246 +            else {
247 +                _logger.write(toString(), Logger.WARNING, "IO error reading alerts directory, maybe it doesn't exist? : " +rootPath+sep+alertSubDir);
248 +            }
249          }
250      }
251      
# Line 200 | Line 279 | public class WebFeeder extends Thread {
279          String data = packet.printAll();
280          String hostname = packet.getParam("packet.attributes.machine_name");
281          // set paths
282 <        File outDir = new File(rootPath, latestSubDir+"/"+hostname);
283 <        File outFile = new File(rootPath, latestSubDir+"/"+hostname+"/"+latestFileName);
282 >        File outDir = new File(rootPath, latestSubDir+sep+hostname);
283 >        File outFile = new File(rootPath, latestSubDir+sep+hostname+sep+latestFileName);
284          // write the data out
285          writeData(outDir, outFile, data);
286      }
# Line 230 | Line 309 | public class WebFeeder extends Thread {
309          String data = alert.printAll();
310          String hostname = alert.getSource();
311          // set paths
312 <        File outDir = new File(rootPath, alertSubDir+"/"+hostname);
313 <        String destFile = alertSubDir+"/"+hostname+"/"+alertFileName+"."+String.valueOf(alert.getInitialAlertTime());
312 >        File outDir = new File(rootPath, alertSubDir+sep+hostname);
313 >        String destFile = alertSubDir+sep+hostname+sep+alertFileName+"."+String.valueOf(alert.getInitialAlertTime());
314          File outFile;
315          // check if we're at a special "end case" (OK or FINAL)
316          if(alert.getLevel()==0 || alert.getLevel()==Alert.alertLevels.length-1) {
317 +            if(alert.getAttributeName().equals("Heartbeat") && alert.getLevel()==Alert.alertLevels.length-1) {
318 +                // new file is something like alert.nnnnnnnn.HB.FINAL
319 +                outFile = new File(rootPath, destFile+".HB."+Alert.alertLevels[alert.getLevel()]);
320 +            } else {
321 +                // new file is something like alert.nnnnnnnn.OK
322 +                outFile = new File(rootPath, destFile+"."+Alert.alertLevels[alert.getLevel()]);
323 +            }
324              File oldFile = new File(rootPath, destFile);
239            outFile = new File(rootPath, destFile+"."+Alert.alertLevels[alert.getLevel()]);
325              if(!oldFile.renameTo(outFile)) {
326                  _logger.write(this.toString(), Logger.WARNING, "Failed to rename old file, "+oldFile.getPath()+" to new file, "+outFile.getPath());
327              }
328          } else {
329 <            outFile = new File(destFile);
329 >            outFile = new File(rootPath, destFile);
330          }
331          // write the data out
332          writeData(outDir, outFile, data);
# Line 251 | Line 336 | public class WebFeeder extends Thread {
336       * Overrides the {@link java.lang.Object#toString() Object.toString()}
337       * method to provide clean logging (every class should have this).
338       *
339 <     * This uses the uk.org.iscream.util.FormatName class
339 >     * This uses the uk.org.iscream.cms.server.util.FormatName class
340       * to format the toString()
341       *
342       * @return the name of this class and its CVS revision

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines