#!/usr/bin/perl -w use strict; use File::Find (); use Net::OpenSSH; use File::Copy; my @testbox = qw( mail1 mail2 ); my $dir = "/spool/stats"; my $ckdir = "if [ ! -d $dir ]; then sudo mkdir -p $dir && sudo chmod -R 777 $dir; else exit; fi"; my $getbouncelogs = "find /var/archive '(' -name 'bounce'* ')' -mtime -7 -mtime +0 |xargs -I {} cp -a {} $dir"; my $getdeliverylogs = "find /var/archive '(' -name 'delivery'* ')' -mtime -7 -mtime +0 |xargs -I {} cp -a {} $dir"; for my $host(@testbox){ my $ssh = Net::OpenSSH->new("$host"); $ssh->system("$ckdir"); #Remove the old logs print "removing old logs on $host\n"; $ssh->system("rm -f $dir/*") or die "$!"; &getlogs; &gunzip; } sub getlogs { #Get the most recent 7 days my $pid = fork; if (!defined $pid) { die "Cannot fork: $!"; } elsif ($pid == 0) { # client process print "Client copy delivery logs starting...\n"; $ssh->system("$getdeliverylogs"); print "Client copy delivery logs sterminating\n"; exit 0; } else { # parent process print "Parent process copy bouncelogs, waiting for child...\n"; $ssh->system("$getbouncelogs"); waitpid $pid, 0; } print "Parent copy bouncelogs process started after child has finished\n"; } } sub gunzip { #gunzip the logs my $pid = fork; if (!defined $pid) { die "Cannot fork: $!"; } elsif ($pid == 0) { # client process print "Client gunzip delivery logs starting...\n"; $ssh->system("gunzip $dir/delivery*.gz"); print "Client gunzip delivery logs terminating\n"; exit 0; } else { # parent process print "Parent process gunzip bounce logs, waiting for child...\n"; $ssh->system("gunzip $dir/bounce*.gz"); waitpid $pid, 0; } print "Parent gunzip bouncelogs process started after child has finished\n"; } }