#!/usr/bin/perl -w
use strict;
sub usage {
print "sas_cleanup log_dir begin_time end_time bak_dir cleanup_log_dir\n";
die "param error.\n";
}
our ($success, $faile, $rtime);
sub logcls {
my $file_name = $_[0];
my $rtime1;
open (F, "$file_name") || die "open $file_name failed! $!";
$rtime1 = time();
$success = 0;
$faile = 0;
while(my $line = ) {
chop($line);
if($line =~ /storage[ \t]+\'([^\']+)\'[ \t]+successed\.$/) {
unlink($1);
#`rm -f $1`;
$success += 1;
}
elsif($line =~ /storage[ \t]+\'([^\']+)\'[ \t]+failed\.$/) {
$faile += 1;
}
}
$rtime = time()-$rtime1;
close(F);
}
my $log_dir = shift @ARGV;
my $begin_time = shift @ARGV;
my $end_time = shift @ARGV;
my $bak_dir = shift @ARGV;
my $cleanup_log_dir = shift @ARGV;
$log_dir =~ /[A-Za-z0-9\/-_.]+/ && chdir($log_dir) || &usage();
$begin_time =~ /\d{10}/ || &usage();
$end_time =~ /\d{10}/ || &usage();
$bak_dir =~ /[A-Za-z0-9\/-_.]+/ && chdir($bak_dir) || &usage();
$cleanup_log_dir =~ /[A-Za-z0-9\/-_.]+/ && chdir($cleanup_log_dir) || &usage();
my @filename = `find $log_dir -name "sas-*.log"`;
@filename = sort @filename;
#print @filename;
if(!-d $bak_dir or !-w $bak_dir) {
print "$bak_dir error!";
exit;
}
if(!-d $cleanup_log_dir or !-w $cleanup_log_dir) {
print "$cleanup_log_dir error!";
exit;
}
while (my $filedate = shift @filename) {
my $filename = $filedate;
chomp($filename);
#print $filename."\n";
if($filedate =~ /sas-(\d{8})-(\d{2})\.log$/) {
$filedate = $1 . $2;
my $temp=$1;
if(length($filedate) == 10 and $filedate >= $begin_time and $filedate < $end_time) {
#print $filedate.$filename."\n";
&logcls($filename);
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
my $nowtime = ($year + 1900) . ($mon + 1) . $mday . "-" . $hour . ":" . $min . ":" . $sec;
my $logfile = $cleanup_log_dir . '/sas_cleanup_' . $temp . '.log';
open (FL, ">> $logfile") || die "open $logfile failed! $!";
print FL "[$nowtime][$filedate] storage_successed=$success storage_failed=$faile rtime=$rtime\n";
close(FL);
`mv $filename $bak_dir` ;
}
}
}
阅读(963) | 评论(1) | 转发(0) |