symgryph has asked for the wisdom of the Perl Monks concerning the following question:

I have a situation where I want my disks to 'keep themselves' from getting full. I have *.log in various directories, and when the disk is 80% full I would like to delete the 'oldest' of these, till its 80% again.

Any Ideas? Logrotate doesn't seem to do this, and I could only find 'logadm' which only runs on solaris.

"Two Wheels good, Four wheels bad."
  • Comment on Rotate log files based upon disk percentage full?

Replies are listed 'Best First'.
Re: Rotate log files based upon disk percentage full?
by Laurent_R (Canon) on Feb 07, 2014 at 00:00 UTC
    I am pretty sure that there must be some modules that do that (or at least part of that task). If not, I think it can be done easily in pure Perl in a few dozens of code lines at most. You need to sort your log files by date into an array and remove the oldest entries until you get below the threshold. I would be glad to offer some basic code if you gave some more details on the directory hierarchy where your files are stored.
      sub rotate_logs { my @files = target_files_by_date_in_dir( ... ); my $cent = size_in_percentage( \@files ); print "# size_in_percentage is $cent\n"; while( $cent > 80 ){ my @ten = splice @files, 0, 10; ## should be path()s already but just in case for my $file ( @ten ){ print "# removing oldest $file\n"; path( $file )->remove; } $cent = size_in_percentage( @files ); } print "# size_in_percentage is $cent\n"; } sub target_files_by_date_in_dir { use Path::Tiny; map { $$_[0] } sort { $$b[1] <=> $$a[1] } map { [ $_, $_->stat->mtime ] } map { path( $_ )->children } @_ } sub size_in_percentage { use Capture::Tiny; my( $files ) = @_; my ($du_stdout, $du_stderr, $df_exit) = capture { system( 'du', ..., @$files ); }; my ($df_stdout, $df_stderr, $df_exit) = capture { system( 'df', ... ); }; ... # math return $size; }
Re: Rotate log files based upon disk percentage full?
by GotToBTru (Prior) on Feb 06, 2014 at 23:06 UTC

    What have you tried? I am curious also why you posted this here. Is perl definitely going to be the solution? Logrotate and logadm are not perl modules, near as I can tell.