no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
| — | addons:finit [2012/07/11 15:00] (current) – created - external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== finit.pl ====== | ||
| + | |||
| + | ^ Author | [[ nelis@nlr.nl | Wim Nelis ]] | | ||
| + | ^ Compatibility | Xymon 4.2 | | ||
| + | ^ Requirements | Perl, unix | | ||
| + | ^ Download | None | | ||
| + | ^ Last Update | 2010-05-31 | | ||
| + | |||
| + | ===== Description ===== | ||
| + | Tests 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. | ||
| + | |||
| + | ===== Installation ===== | ||
| + | Save the perl script at a suitable directory on the server containing the RRD files, for instance in ~xymon/bin. Then change the variable $HomeDir to point to the directory containing the collection of RRD files, typically ~xymon/ | ||
| + | |||
| + | ===== Usage ==== | ||
| + | Script finit.pl can be used in the following way: | ||
| + | |||
| + | * cd < | ||
| + | * ./finit.pl > finit.out | ||
| + | * vi finit.out | ||
| + | * ksh finit.out | ||
| + | |||
| + | |||
| + | ===== Source ===== | ||
| + | ==== finit.pl ==== | ||
| + | |||
| + | <hidden onHidden=" | ||
| + | <code perl finit.pl> | ||
| + | # | ||
| + | # | ||
| + | # FINd_Inactive_Tests, | ||
| + | # 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, | ||
| + | # RRD databases are actually deleted. | ||
| + | # | ||
| + | use strict ; | ||
| + | |||
| + | # | ||
| + | # Installation constants. | ||
| + | # | ||
| + | my $HomeDir= '/ | ||
| + | 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 " | ||
| + | # | ||
| + | sub GetTime($) { | ||
| + | my $TimeStamp= shift ; # Unix time stamp | ||
| + | my @Time= (localtime($TimeStamp))[3..5] ; | ||
| + | $Time[1]++ ; $Time[2]+=1900 ; | ||
| + | return sprintf ' | ||
| + | } # 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, | ||
| + | |||
| + | opendir( DH, $FullDir ) or die " | ||
| + | @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)}}, | ||
| + | } # of if | ||
| + | } # of foreach | ||
| + | $TotFileCnt += $FileCnt | ||
| + | $TotInactCnt+= $InactCnt ; | ||
| + | if ( $FileCnt == $InactCnt | ||
| + | $LastModTime= (sort keys %Old)[-1] ; | ||
| + | push @{$Host{$LastModTime}}, | ||
| + | } else { | ||
| + | foreach ( keys %Old ) { | ||
| + | push @{$Test{$_}}, | ||
| + | } # of foreach | ||
| + | } # of else | ||
| + | } # of scanDir | ||
| + | |||
| + | print "# Searching for inactive RRD measurements\n" | ||
| + | ScanDir( $HomeDir ) ; | ||
| + | printf "# | ||
| + | printf "# | ||
| + | |||
| + | if ( keys %Host ) { | ||
| + | print "# | ||
| + | print "# List of formerly tested hosts.\n" | ||
| + | foreach ( sort keys %Host ) { | ||
| + | print " | ||
| + | print "# Not updated since $_\n" ; | ||
| + | foreach $FullName ( @{$Host{$_}} ) { | ||
| + | print "rm -rf $FullName\n" | ||
| + | } # of foreach | ||
| + | } # of foreach | ||
| + | } # of if | ||
| + | |||
| + | if ( keys %Test ) { | ||
| + | print " | ||
| + | print "# List of inactive tests.\n" | ||
| + | foreach ( sort keys %Test ) { | ||
| + | print " | ||
| + | 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, | ||
| + | |||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ===== Known Bugs and Issues ===== | ||
| + | None known. | ||
| + | |||
| + | ===== Changelog ===== | ||
| + | |||
| + | * **2010-05-31** | ||
| + | * Initial release | ||