NAME

App::FileCleanerByDiskUage - Removes files based on disk space usage till it drops below the specified amount.

VERSION

Version 0.4.0

SYNOPSIS

use App::FileCleanerByDiskUage;

# remove files under /var/log/suricata/pcap when disk usage is over 90%
# If over 90% make sure there are atleast 32 files and if there are atleast 32, remove them based
# on age till we drop below 90%. The newest 32 will be ignored regardless of disk usage.
my $removed=App::FileCleanerByDiskUage->clean(path=>'/var/log/suricata/pcap/', du=>90, min_files=>32);
if (defined( $removed->{errors}[0] )){
    die('One or more file could not be removed... '.join('  ', @{ $removed->{errors} }));
}
my $int=0;
while (defined( $removed->{unlined}[$int] )){
    print 'Removed ' . $removed->{unlinked}[$int]{name} . "\n";

    $int++;
}

This works via doing the following.

1: Check if disk usage is above the specified threshold. If not it ends here.

2: Search for files under the specified path.

3: If the number of found files is less than the number of files to keep regardless of disk size it ends here. So if min_files is set to 32 and there are only 3 files, then it would just return.

4: Get the stats for all the found files.

5: If min_files is specified, remove that many of the files from the list, starting with the newest.

6: Removes the oldest file.

7: Check disk usage again and if it is less it ends here.

8: Go back to 6.

Functions

clean

This performs the cleaning actions. As long as the path exists and .path and .du are defined this will not die. But if any of those are undef or do not exist it will die.

The following hash values are taken by it.

Minimum Required Vars: path, du

- path :: The path to look for files under. May be a array of paths. Only the first is used
          for getting the disk usage though, so this should not have paths in it that are on
          a different partition.
    Default :: undef

- du :: Disk usage to remove files down till.
    Default :: undef

- min_files :: Minimum number of files to keep, regardless of disk usage.
    Default :: undef

- ignore :: A regexp to use for ignoring files. So lets say you want to ignore,
            files matching /\.pcap$/, it would be '\.pcap$'.
    Default :: undef

- dry_run :: Do not actually remove anything. Just check to see if the file writable by the
             current user.

-use_pid :: Create a PID to make sure multiple instances can't run at once.
    Default :: undef

-pid_dir :: Create a PID to make sure multiple instances can't run at once.
    Default :: /var/run

- pid_name :: Append this to the the name of the pid file created. If specified with a
        value of 'foo' then the file would be 'file_cleaner_by_du-foo.pid'.
    Default :: undef

The returned value is a hash ref.

- dry_run :: Boolean for fir it was a dry run or not.

- found_files :: Array of hashes of data for all files found. This will only be defined if du is above
                 threshold for removing files. If it is below it, the function will return instead of taking
                 the time required to run a search.

- found_files_count :: A count of files found.

- path :: The value of path that it was called with. This will always be a array, regardless of if a array or
          scalar was passed as internally converts a scalars into a array containing just a single item.

- missing_paths :: Paths that were passed to it, but don't exist.

- unlinked :: Array of hashes of data for files that have been removed.

- unlinked_count :: A count of how many files were unlinked

- unlink_errors :: Array of strings containing error descriptions.

- unlink_failed :: Array of hashes of data for files that could not removed. The corresponding
                   index in .errors will be the error in question. So $results->{unlink_failed}[0]
                   would be $results->{unlink_errors}[0]

- unlink_fialed_count :: A count of how many files unlinking failed for.

The files hash is composed as below.

- name :: Name of the file, including it's path.

# following are provided via the Perl function stat
- dev
- ino
- mode
- nlink
- uid
- gid
- rdev
- size
- atime
- mtime
- ctime
- blksize
- blocks

AUTHOR

Zane C. Bowers-Hadley, <vvelox at vvelox.net>

BUGS

Please report any bugs or feature requests to bug-app-filecleanerbydiskuage at rt.cpan.org, or through the web interface at https://rt.cpan.org/NoAuth/ReportBug.html?Queue=App-FileCleanerByDiskUage. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc App::FileCleanerByDiskUage

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.

This is free software, licensed under:

The GNU Lesser General Public License, Version 3, June 2007