Perl Script xmpFileClean.pl to archive, remove, compress files
Perl script to administer files, especially DB2 administration files, like db2diag.log, admin log file, or any other file. It helps to archive, compress, rename or delete one or more files, optionally when size or age surpasses a threshold value.
Usage: xmpFileClean.pl {-d|-l|-f file|'file*'} [ [-a|-A ext]|[-r] [-c] [-s nnn] [-D nnn] ]
- -d=apply on db2diag.log (using path of DB2 DBM configuration)
- -l=apply on mydbadmin.log (using constant value of xmpPerl.pm module library)
- -f=apply on any other files, file search mask allowed
 Actions:Â
- -a   archive, add julian day as extension
- -AÂ Â ext archive, add argument value as extension
- -r   remove
- -c   compress
Options:
- -s nnn depends on file size, nnn=threshold value in bytes
- -D nnn depends on age of file, nnn=threshold value in days
#!/usr/local/bin/perl -w
################################################################################
# xmpFileClean.pl - File Clean-Up (archive, remove, compress)
#-------------------------------------------------------------------------------
# This perl script works with any file specified as argument, of works with the
# db2 diagnose log file (db2diag.log), or works with the DB2 administrators log.
# Options -a invokes the archive function: The original file name will be extended
# by number the juluan day of year. -A adds a specified extension suffix the the
# archive file name. -c compresses the file to file.Z. Using -s nnnn archives the
# file if nnn as threshold for the file size is reached. Option -D nnn works on
# files which are older than nnn days. Options -r removes/deletes a file.
#-------------------------------------------------------------------------------
# Examples:
# xmpFileClean.pl -d -a -c create an compressed archive of db2diag.log
# xmpFileClean.pl -l -a -c create an compressed archive of mydb2admin.log
# xmpFileClean.pl -d -D 90 -r remove db2diag.log's older than 90 days
# xmpFileClean.pl -f anyfile.txt -r -s 10000 delete the anyfile.txt if > 10000 bytes
#-------------------------------------------------------------------------------
# Called by - Command Level or other Scripts
# Calls - xmpPerl.pm- xmp DBA Perl Package
# Getopt - Process arguments
# Env - Access to environment variables
# English - Aliases for Perl Variables
# File - File Status Information
# Time - Time arithmetics
# Shell - Process shell commands
# Basename - splits path and file name
# Optionen -d - apply on db2diag.log
# -l - apply on mydbadmin.log
# -f file - apply on any file
# -a - archive
# -A ext - archive using extension "ext"
# -r - remove
# -s nnn - processing depends on size of file (nnn=bytes)
# -D nnn - processing depends on age of file (nnn = days)
#-------------------------------------------------------------------------------
# Autor - GR /02.02.00 - V1.0 erste Version
# 13.07.00 - G.Ruban - V1.1 diverse Anpassungen, Logging in mydbadmin.log, mehr Optionen
# 28.08.00 - G.Ruban - V1.1.1 Ausgabe der File Permissions
# 22.01.03 - G.Ruban - V2.0 Allow multiple files and file search masks
# 15.05.07 - G.Ruban - V3.0 New argument -d nnn for age of file in days.
################################################################################
################################################################################
# Modules
################################################################################
use strict;
use Getopt::Std;
use vars qw($opt_d $opt_l $opt_f $opt_a $opt_A $opt_r $opt_c $opt_s $opt_D);
use xmpPerl;
use IO::File;
use Env;
use English;
use File::stat;
use Time::localtime;
use Shell qw(compress);
use File::Basename;
################################################################################
# Declare/Init
################################################################################
my $VER = "3.0";
my $MSG;
my $hLOG; # file handle
my $nLOG = 0; # no. of table entries
my %tLOG = (); # table of file names
my $work;
my @work;
my ($ino,$tm);
my $n = 0; # counter
my $yd;
my $UDT = xmpPerl::getTimestamp(); # Timestamp
my $t_mtime = 0; # Target modify seconds V3.0
my $t_date = ""; # Target modify timetstamp V3.0
my $f_date = ""; # File modify timestamp V3.0
################################################################################
# Argumente/Options prüfen
################################################################################
getopts("dlf:racs:A:D:");
if ( ((! defined $opt_d ) and (! defined $opt_f )
and (! defined $opt_l )) # entweder -d|-f|-l
or(( defined $opt_a ) and ( defined $opt_A ) ) # not -a, -A togehter
or(( defined $opt_a ) and ( defined $opt_r ) ) # not -a, -r togehter
or(( defined $opt_A ) and ( defined $opt_r ) ) # not -A, -r togehter
or(( defined $opt_s ) and (! defined $opt_a ) # no -s w/o -A|-a|-r|-c
and (! defined $opt_A )
and (! defined $opt_c )
and (! defined $opt_r ))
or(( defined $opt_D ) and (! defined $opt_a ) # no -D w/o -A|-a|-r|-c
and (! defined $opt_A )
and (! defined $opt_c )
and (! defined $opt_r )) ) {
SaySyntax();
}
################################################################################
# Check DB2 Existenz und Informationsblock ausgeben
################################################################################
if ($opt_d) {
xmpPerl::prtHeaderDB2("$0 - File Clean-Up - Version $VER");}
else {
xmpPerl::prtHeader("$0 - File Clean-Up - Version $VER");
}
################################################################################
# Wenn -A Argument nicht angegeben
################################################################################
if (! defined $opt_A) {
############################################################################
# Julianischen Tag im Jahr ermitteln
############################################################################
$tm = localtime();
$yd = ($tm->yday)+1;
print "Archive Log File Extension ..: $yd (used by option -a|-A)\n";
}
else { $yd = $opt_A}
################################################################################
# -d db2diag.log
################################################################################
if ($opt_d) {
############################################################################
# DIAGPATH aus "DB2 GET DBM CFG" Output beschaffen
############################################################################
$nLOG = 1;
$tLOG{1} = xmpPerl::getDBMCfgValue("DIAGPATH");
$tLOG{1}.= "/db2diag.log";
print "DB2 Diagnosis Log ...........: $tLOG{1}\n";
}
################################################################################
# -l mydbadmin.log
################################################################################
if ($opt_l) {
############################################################################
# Log File Name als Konstante aus xmpPerl.pm Perl Module
############################################################################
$nLOG = 1;
$tLOG{1} = xmpPerl::getDBALog();
print "DBA Log File ................: $tLOG{1}\n";
}
################################################################################
# -f Beliebige File
################################################################################
if ($opt_f) {
############################################################################
# Argument contains asterisk for file search mask?
############################################################################
if ($opt_f =~ /\*/) {
@work = glob($opt_f);
}
else { @work = split("\n",$opt_f);
}
$nLOG = 0;
foreach $work (@work) {
$nLOG++;
$tLOG{$nLOG} = xmpPerl::trim($work);
}
}
################################################################################
# -D age of files
################################################################################
if ($opt_D) {
$t_mtime = localtime(time-(86400*$opt_D));
$t_date = sprintf("%4d-%02d-%02d-%02d.%02d.%02d",
$t_mtime->year+1900, $t_mtime->mon+1, $t_mtime->mday,
$t_mtime->hour, $t_mtime->min, $t_mtime->sec);
print "Process file when age is < ..: $opt_D days, which currently is ...\n";
printf(" %s (%-d)\n",$t_date, $t_mtime);
}
################################################################################
# Process all files
################################################################################
for ($n = 1; $n 30 ) {
print "File Path ...................: ".dirname($tLOG{$n})."\n";
print "File Name ...................: ".basename($tLOG{$n})."\n";
}
else {
print "File Name ...................: $tLOG{$n}\n";}
}
else {
if ( length($tLOG{$n}) > 30 ) {
print "File Path ...................: ".dirname($tLOG{$n})."\n";
print "File Name ...................: ".basename($tLOG{$n})."\n";
}
else {
print " $tLOG{$n}\n";
}
}
############################################################################
# Get file informtion and print
############################################################################
if (-e $tLOG{$n}) {
$ino = stat($tLOG{$n});
print " - File Owner and Group.....: ".getpwuid($ino->uid)." ",
getgrgid($ino->gid)."\n";
print " - File Size in Bytes ......: ".$ino->size."\n";
printf " - File Permissions ........: %04o \n",($ino->mode & 07777);
$tm = localtime($ino->mtime);
$f_date = sprintf("%4d-%02d-%02d-%02d.%02d.%02d",
$tm->year+1900, $tm->mon+1, $tm->mday,
$tm->hour, $tm->min, $tm->sec);
printf(" - Last modify time ........: %s (%-d)\n",$f_date, $ino->mtime);
}
else {
print " File does not exist!\n";
print "End of $0 - RC=0\n";
exit 0;
}
############################################################################
# Action requested
############################################################################
if ($opt_a || $opt_A ) {Archiv();} # Archivierung
if ($opt_r) {DelFile();} # Löschung
if ( (! $opt_a)
and (! $opt_A)
and $opt_c ) {Compress($tLOG{$n});} # Komprimierung
}
print "*** End of ".basename($0)." - RC=0 ***\n";
exit 0;
################################################################################
# S U B R O U T I N E S :
################################################################################
# SaySyntax
################################################################################
sub SaySyntax {
die "Usage: $0 {-d|-l|-f file|'file*'} [ [-a|-A ext]|[-r] [-c] [-s nnn] [-D nnn] ]\n\n".
" Script archives, compresses, renames or deletes one or more files,\n".
" optionally when size or age increased the passed value.\n\n".
" -d=apply on db2diag.log (using path of DB2 DBM configuration)\n".
" -l=apply on mydbadmin.log (using constant value of xmpPerl.pm library)\n".
" -f=apply on any other files, file search mask allowed\n".
" Action: -a archive, add julian day as extension\n".
" -A ext archive, add argument value as extension\n".
" -r remove\n".
" -c compress\n".
" Options: -s nnn depends on file size, nnn=threshold value in bytes\n".
" -D nnn depends on age of file, nnn=threshold value in days\n".
"abends ";
}
################################################################################
# Subroutine "Archiv": Rename "Log" in "Log.DoY" (DoY=Day of Year)
################################################################################
sub Archiv {
############################################################################
# Process depends on file size
############################################################################
if ($opt_s) {
$work = $ino->size;
if ($worksize;
if ($worksize;
if ($workuid)." ",
getgrgid($ino->gid)."\n";
print " - File Size in bytes ....: ".$ino->size."\n";
printf " - File Permissions ......: %04o \n",($ino->mode & 07777);
$tm = localtime($ino->mtime);
printf(" - Last modify time ......: %4d-%02d-%02d-%02d.%02d.%02d\n",
$tm->year+1900, $tm->mon+1, $tm->mday,
$tm->hour, $tm->min, $tm->sec);
}
else {
print "File compression failed .....: $ERRNO
Follow the link to perl module xmpPerl.pm, used in above perl script.
Comments
Comments are closed.