-#!@@PERL@@
+#!/usr/bin/perl
#
# This program performs nightly maintanence to the spong database.
#
# History:
# (1) Created (Ed Hill, 06/17/1998)
#
-# $Id: spong-cleanup.pl,v 1.6 2000/10/16 15:34:32 sljohnson Exp $
+# $Id: spong-cleanup.pl,v 1.7 2002/12/17 21:31:21 sljohnson Exp $
use POSIX;
use Socket;
&remove_old_acks( $host );
&remove_old_services( $host );
&remove_old_history( $host ); # Yes, I suppose all history is old...
+ &remove_old_events( $host ); # Old event status data
}
closedir( DIR );
sub remove_old_history {
my( $host ) = @_;
my $file = "$main::SPONGDB/$host/history/current";
- my $archive = "$main::SPONG_ARCHIVE/$host";
+ my $archive = "$main::SPONG_ARCHIVE/$host/history";
return if ! -f $file;
+ if ( -f "$main::SPONG_ARCHIVE/$host" ) {
+ warn "Refusing to overwrite $host archive file, please the run convert-history Spong utility";
+ return;
+ }
+
+ if ( ! -d "$main::SPONG_ARCHIVE/$host" ) {
+ system "mkdir $main::SPONG_ARCHIVE/$host" || die "Could not mkdir $main::SPONG_ARCHIVE/$host : $!";
+ }
+
+
open( HISTORY, $file ) || die "Can't open $file: $!";
open( ARCHIVE, ">> $archive" ) || die "Can't open $archive: $!";
open( NEW, "> $file.new" ) || die "Can't create $file.new: $!";
system( "cp $file.new $file && rm $file.new" );
}
-
+
+# This function scans through all of the event status of the given host,
+# and moved old event status files older then N days to the host's archive
+
+sub remove_old_events {
+ my ( $host ) = @_;
+ my ( $eventdir ) = "$main::SPONGDB/$host/history/status";
+ my ( $archdir ) = "$main::SPONG_ARCHIVE/$host/status";
+
+
+ return if ! -d $eventdir;
+
+ if ( -f "$main::SPONG_ARCHIVE/$host" ) {
+ warn "Refusing to overwrite $host archive file, please the run convert-history Spong utility";
+ return;
+ }
+
+ if ( ! -d $archdir ) {
+ system "mkdir -p $archdir" || die "Could not mkdir $archdir : $!";
+ }
+
+ opendir( STDIR,$eventdir ) || die "Could not opendir $eventdir: $!";
+
+ while( my $file = readdir(STDIR) ) {
+ next if $file =~ /^\./ || $file =~ /^\.\./ ; # Skip . and .. dirs
+
+ my( $stamp ) = ( $file =~ /^(\d+)-/ ); # Get the event timestamp
+
+ if ( $time < (time() - ($main::OLD_HISTORY * 24*60*60)) ) {
+ system "mv $eventdir/$file $archdir/$file" ||
+ die "Could not mv $event/$file to $archdir/$file: $!";
+ }
+ }
+
+ closedir(STDIR);
+
+}
# This function just loads in all the configuration information from the
# spong.conf. The spong.hosts and spong.group files are not needed - all the