#!/usr/bin/perl
#
# FINd_Inactive_Tests, finit:
# Make a list of the RRD databases, which have not been updated for some
# time. The tests (measurements) are organised per host. If all tests of
# one host are not updated for some time, the host rather than the tests
# is reported.
#
# The report is written to standard output, and is in fact a shell script
# which contains the commands to delete the outdated RRD databases. This
# intermediate file allows for inspection and modification, before the
# RRD databases are actually deleted.
#
use strict ;
#
# Installation constants.
#
my $HomeDir= '/home/xymon/data/rrd/' ; # Top of dir tree to search
my $Threshold= 7 * 86400 ; # Minimum age of old measurement
#
# Global variables.
#
my %Host= () ; # List of inactive hosts
my %Test= () ; # List of inactive tests
my $FullName ; # Full path name of a file
my $TotFileCnt= 0 ; # Number of files checked
my $TotInactCnt= 0 ; # Number of inactive tests
#
# Function GetTime returns the date in "yyyymmdd" notation, given a timestamp.
#
sub GetTime($) {
my $TimeStamp= shift ; # Unix time stamp
my @Time= (localtime($TimeStamp))[3..5] ;
$Time[1]++ ; $Time[2]+=1900 ;
return sprintf '%4d%02d%02d', reverse @Time ;
} # of GetTime
#
# Function ScanDir scans the given directory, and all of its subdirectories,
# for RRD files, which are not updated for at least $Threshold seconds. The
# results are stored in %Test and %Host.
#
sub ScanDir($) ; # Prototype definition
sub ScanDir($) {
#
# Scan the supplied directory for (a) subdirectories and (b) RRD database
# files. The last modification date of the database is compared with the
# current time. If old enough, it is reported as an inactive measurement.
#
my $FullDir= shift ; # Absolute pathname of directory to scan
my @Files ; # Files and subdirectories
my %Old ;
my $LastModTime ; # Time of last modification of a file
my ($FileCnt,$InactCnt) ; # Count number of files & inactive tests
opendir( DH, $FullDir ) or die "Can't open $FullDir : $!\n" ;
@Files= grep /^[^\.]/, readdir( DH ) ;
closedir( DH ) ;
#
# Check each subdirectory within this subdirectory too.
#
foreach ( sort @Files ) {
$FullName= $FullDir . $_ . '/' ;
next unless -d $FullName ; # Skip non-directories
ScanDir( $FullName ) ; # Scan this directory too
} # of foreach
#
# Check the files in the current subdirectory.
#
# print "# Scanning $FullDir\n" ;
$FileCnt= $InactCnt= 0 ; # Preset counters
%Old= () ; # Preset list
foreach ( sort @Files ) {
$FullName= $FullDir . $_ ;
next unless -f $FullName ; # Skip non-files
$FileCnt++ ;
next unless m/\.rrd$/ ; # Skip non-RRD files
$LastModTime= (stat($FullName))[9] ;
if ( time - $LastModTime > $Threshold ) {
$InactCnt++ ;
push @{$Old{GetTime($LastModTime)}}, $FullName ;
} # of if
} # of foreach
$TotFileCnt += $FileCnt ;
$TotInactCnt+= $InactCnt ;
if ( $FileCnt == $InactCnt and $InactCnt > 0 ) {
$LastModTime= (sort keys %Old)[-1] ;
push @{$Host{$LastModTime}}, $FullDir ;
} else {
foreach ( keys %Old ) {
push @{$Test{$_}}, @{$Old{$_}} ;
} # of foreach
} # of else
} # of scanDir
print "# Searching for inactive RRD measurements\n" ;
ScanDir( $HomeDir ) ;
printf "# %5d files scanned\n", $TotFileCnt ;
printf "# %5d inactive tests found\n", $TotInactCnt ;
if ( keys %Host ) {
print "#\n" ;
print "# List of formerly tested hosts.\n" ;
foreach ( sort keys %Host ) {
print "\n" ;
print "# Not updated since $_\n" ;
foreach $FullName ( @{$Host{$_}} ) {
print "rm -rf $FullName\n" ;
} # of foreach
} # of foreach
} # of if
if ( keys %Test ) {
print "\n#\n" ;
print "# List of inactive tests.\n" ;
foreach ( sort keys %Test ) {
print "\n" ;
print "# Not updated since $_\n" ;
foreach $FullName ( @{$Test{$_}} ) {
print "rm $FullName\n" ;
} # of foreach
} # of foreach
} # of if
__END__
=head1 NAME
finit.pl - The name of the script is an acronym of FINd_Inactive_Tests. It
generates on standard output a list of RRD database which have not been
updated for some time, indicating that the associated tests are not active
any more.
=head1 DESCRIPTION
Test in Xymon may result in time-series of data, which are stored in RRD
databases. As the configuration changes, tests are changed and hosts come
and go, a number of RRD databases are no longer updated. Xymon contains
a tool to cleanup outdated (historical) data, but it does not remove the
associated RRD databases. Script finit.pl generates a list of RRD databases
which have not been updated for some time. This list is sorted on the date
of the last update.
The result of script finit.pl is a list, but formatted as a shell script
which removes those RRD databases. The list can be reviewed and edited if
necessary before it is executed by a shell. The user of the script thus
can control which inactive RRD database are retained and which are removed.
=head1 AUTHOR
Wim Nelis, nelis@nlr.nl, 2009.08