Perl script to rotate logs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script to rotate logs
# 8  
Old 03-20-2008
as suggested I added/modified the code as follows

Code:
@LOGNAMES=map { glob($_) } 'Proxy*','Claim*','Eligibility*';
# @LOGNAMES=('fin*.log*','Proxy*','Claim*','Eligibility*');
%ARCHIVE=('*.$MONTH.*'=>1);

chdir $LOGPATH;  # Change to the log directory
printf "$LOGPATH\n";
foreach $filename (@LOGNAMES) {
  my $oldest = "$filename.$MAXCYCLE";
printf $filename\n;
print "DEBUG: oldest is '$oldest'\n";
  archive($oldest) if -e $oldest and $ARCHIVE{$filename};
print "DEBUG: '$oldest' exists\n" if -e $oldest;
print "DEBUG: ARCHIVE{'$filename'} is true\n" if $ARCHIVE{$filename};
print "DEBUG: would archive oldest\n" if -e $oldest and $ARCHIVE{$filename};
  for (my $s=$MAXCYCLE; $s >= 0; $s-- ) {
print "DEBUG: s is $s\n";
        $oldname = $s ? "$filename.$s" : $filename;
        $newname = join(".",$filename,$s+1);
print "DEBUG: oldname now '$oldname', newname is '$newname'\n";
print "DEBUG: would rename '$oldname' to '$newname'\n" if -e $oldname;
        rename $oldname,$newname if -e $oldname;

Whe I run it, it only prints out the $LOGPATH then hangs with no output. So i am asuming that one of these two lines is failing....
foreach $filename (@LOGNAMES) {
my $oldest = "$filename.$MAXCYCLE";

Perls doesn't seem very easy to debug, no stdout or stderr?
# 9  
Old 03-20-2008
The code posted doesn't compile on my machine. Try changing

Code:
printf $filename\n;

Code:
printf "$filename\n";

And adding 2 closing curly brackets - "}"
# 10  
Old 03-20-2008
Not sure what this is supposed to do:

%ARCHIVE=('*.$MONTH.*'=>1);

archive() is not a perl function, not sure what that does unless you have loaded a module that has that function imported into your perl program.

This line is wrong:

printf $filename\n;

\n will not be interpolated properly when unquoted:

printf "$filename\n";

printf in perl is for writing to files or adding formatting, you should just use print instead of printf when printing to stdout. Add the warnings pragma to your script:

use warnings;

and it will alert you to problems and potential problems.
# 11  
Old 03-20-2008
My bad, I guess I should post the entire thang.... This is what I am testing

Code:
#!/usr/local/bin/perl

$LOGPATH='/home/logs';          # Location of Log files
$ARCHIVE_DIR='/home/log_archive/moar_logs';     # Location of AR archived logs
$MONTH=`date +%m`;                      # Current Month
$DAY=`date +%d`;                        # Current Day of month
$MAXCYCLE='2';                          # Number of days to keep log files
$GZIP='/usr/contrib/bin/gzip';          # Location of gzip for compression

@LOGNAMES=map { glob($_) } 'fin**'twiz*.log*','nestl*','riesen*';
# @LOGNAMES=('fin*.log*','twiz*.log*','nestl*','riesen*');
%ARCHIVE=('*.$MONTH.*'=>1);

chdir $LOGPATH;  # Change to the log directory
print "$LOGPATH\n";
foreach $filename (@LOGNAMES) {
  my $oldest = "$filename.$MAXCYCLE";
print "$filename\n";
print "DEBUG: oldest is '$oldest'\n";
  archive($oldest) if -e $oldest and $ARCHIVE{$filename};
print "DEBUG: '$oldest' exists\n" if -e $oldest;
print "DEBUG: ARCHIVE{'$filename'} is true\n" if $ARCHIVE{$filename};
print "DEBUG: would archive oldest\n" if -e $oldest and $ARCHIVE{$filename};
  for (my $s=$MAXCYCLE; $s >= 0; $s-- ) {
print "DEBUG: s is $s\n";
        $oldname = $s ? "$filename.$s" : $filename;
        $newname = join(".",$filename,$s+1);
print "DEBUG: oldname now '$oldname', newname is '$newname'\n";
print "DEBUG: would rename '$oldname' to '$newname'\n" if -e $oldname;
        rename $oldname,$newname if -e $oldname;
    }
}
kill 'HUP',`cat $PIDFILE`;

 sub archive {
        my $f = shift;
        my $base = $f;
        $base =~ s/\.\d+$//;
        my $fn = strftime("$base.%Y-%m-%d_%H:%M.gz.idea",localtime);
        system "$GZIP -9 -c $f | $GZIP -kfile $MAP > $fn";
        system "$TAR rvf $base.tar --remove-files $fn";
 }

This prints out the $LOGPATH and hangs......
I am pretty sure I have this wrong, but hey it's a learning experience. Making for long frustrating days like I like.
# 12  
Old 03-20-2008
This still doesn't compile. Your quotes don't match up.
Code:
@LOGNAMES=map { glob($_) } 'fin**'twiz*.log*','nestl*','riesen*';

Are you sure you have files in the current directory that match the wild carding? Try adding file "abctest" to the current directory, then replace the above with
Code:
@LOGNAMES=map { glob($_) } 'fin*','*twiz*.log*','nestl*','riesen*', '*abc*';

As KevinADC mentioned, it would probably help to have
Code:
#!/usr/local/bin/perl
use warnings;

at the top of your script
# 13  
Old 03-20-2008
use warnings is worrking well, now to debugg...

./logrotate.pl
Name "main::TAR" used only once: possible typo at ./logrotate.pl line 43.
Name "main::PIDFILE" used only once: possible typo at ./logrotate.pl line 35.
Name "main::MAXDAYS" used only once: possible typo at ./logrotate.pl line 50.
/home/p029052/logs
Use of uninitialized value in print at ./logrotate.pl line 17.
HASH(0x40010f78)
DEBUG: oldest is 'HASH(0x40010f78).2'
DEBUG: s is 2
DEBUG: oldname now 'HASH(0x40010f78).2', newname is 'HASH(0x40010f78).3'
DEBUG: s is 1
DEBUG: oldname now 'HASH(0x40010f78).1', newname is 'HASH(0x40010f78).2'
DEBUG: s is 0
DEBUG: oldname now 'HASH(0x40010f78)', newname is 'HASH(0x40010f78).1'
Use of uninitialized value in concatenation (.) or string at ./logrotate.pl line 35.

Thanks for the help everyone, I am getting somewhere now. I will post what I get after debugging.

Last edited by theninja; 03-20-2008 at 05:34 PM..
# 14  
Old 03-20-2008
Ok I am almost there, only two issues left I think...
1. $PIDFILE format not correct for Perl? Currently commented out because it bombed.
2. Not picking up the logfile names....see output (print $map does not pick up the lognames) I did move the script into the log directory where the logs reside.

Code:
#!/usr/local/bin/perl
use warnings;

# $PIDFILE=`ps -aefx | grep logrot | grep -v grep |awk '{print $2}'`;
$LOGPATH='/home/logs';          # Location of Log files
$ARCHIVE_DIR='/home/log_archive/logs';     # Location of archived logs
$MONTH=`date +%m`;                      # Current Month
$DAY=`date +%d`;                        # Current Day of month
$MAXCYCLE='2';                          # Number of days to keep log files
$GZIP='/usr/contrib/bin/gzip';          # Location of gzip for compression
$TAR='/usr/bin/tar';                    # Location of tar command

@LOGNAMES=map {glob($_) } {'fin*.log*','*twiz*.log*','nestl*','riesen*'};
%ARCHIVE=('*.$MONTH.*'=>1);
# print "$PIDFILE\n";
chdir $LOGPATH;  # Change to the log directory
printf "$LOGPATH\n";
print "$map";
foreach $filename (@LOGNAMES) {
  my $oldest = "$filename.$MAXCYCLE";
printf "$filename\n";
print "DEBUG: oldest is '$oldest'\n";
  archive($oldest) if -e $oldest and $ARCHIVE{$filename};
print "DEBUG: '$oldest' exists\n" if -e $oldest;
print "DEBUG: ARCHIVE{'$filename'} is true\n" if $ARCHIVE{$filename};
print "DEBUG: would archive oldest\n" if -e $oldest and $ARCHIVE{$filename};
  for (my $s=$MAXCYCLE; $s >= 0; $s-- ) {
print "DEBUG: s is $s\n";
        $oldname = $s ? "$filename.$s" : $filename;
        $newname = join(".",$filename,$s+1);
print "DEBUG: oldname now '$oldname', newname is '$newname'\n";
print "DEBUG: would rename '$oldname' to '$newname'\n" if -e $oldname;
        rename $oldname,$newname if -e $oldname;
    }
}
#kill 'HUP',`cat $PIDFILE`;

 sub archive {
        my $f = shift;
        my $base = $f;
        $base =~ s/\.\d+$//;
        my $fn = strftime("$base.%Y-%m-%d_%H:%M.gz.idea",localtime);
        system "$GZIP -9 -c $f | $GZIP $map > $fn";
        system "$TAR rvf $base.tar --remove-files $fn";
 }

printf "$LOGPATH\n";
printf "$ARCHIVE_DIR\n";
printf $MONTH;
printf $DAY;
printf "$MAXCYCLE\n";
printf "$GZIP\n";
printf "$TAR\n"


here is the output of above code

/home/logs
Use of uninitialized value in concatenation (.) or string at ./logrotate.pl line 19. (line 19 = print "$map"Smilie

HASH(0x40010f78)
DEBUG: oldest is 'HASH(0x40010f78).2'
DEBUG: s is 2
DEBUG: oldname now 'HASH(0x40010f78).2', newname is 'HASH(0x40010f78).3'
DEBUG: s is 1
DEBUG: oldname now 'HASH(0x40010f78).1', newname is 'HASH(0x40010f78).2'
DEBUG: s is 0
DEBUG: oldname now 'HASH(0x40010f78)', newname is 'HASH(0x40010f78).1'
/home//logs
/home/log_archive/ogs
03
20
2
/usr/contrib/bin/gzip
/usr/bin/tar


Thanks everyone almost there.

Last edited by theninja; 03-20-2008 at 06:32 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If I ran perl script again,old logs should move with today date and new logs should generate.

Appreciate help for the below issue. Im using below code.....I dont want to attach the logs when I ran the perl twice...I just want to take backup with today date and generate new logs...What I need to do for the below scirpt.............. 1)if logs exist it should move the logs with extention... (1 Reply)
Discussion started by: Sanjeev G
1 Replies

2. UNIX for Dummies Questions & Answers

Logs do not rotate

My problem: Both access and error logs do not rotate any more and get really large. They are located here: /srv/www/+vHost name here+/logs/ Configuration seems to be here: /etc/logrotate.conf => looks OK, including "size 10M" to avoid large files (/etc/logrotate.d => is empty) manually... (4 Replies)
Discussion started by: floko
4 Replies

3. UNIX for Dummies Questions & Answers

Need script to rotate logs

I have few solaris-10 non global zones, where one application is writing some logs to /var/ovd/ConfigLogs. It keeps increasing all the time, as it is needed by application team as of now. I want a small script, which I can configure in cronjob, which should : - Run every Saturday 10 PM - Copy... (5 Replies)
Discussion started by: solaris_1977
5 Replies

4. Shell Programming and Scripting

Perl script to extract last 24 hrs logs from cronlog

Hi Friends, Can anybody help me to create a perl script to generate log file for last 24 hrs from cron log file ?? Thank You (2 Replies)
Discussion started by: syamji.vm
2 Replies

5. Shell Programming and Scripting

Perl script to parse multiple windows event logs.

Hi all, I am developing a log parsing agent in perl to send windows Event logs to Zenoss Monitoring tool. Using Win32::EventLog i can able to get the Event messages but only one Eventype eg Application or System could able to parse at a time. Can you please help to how to open mutiple eventlogs... (3 Replies)
Discussion started by: kar_333
3 Replies

6. Shell Programming and Scripting

Script to rotate file log

Hi Experts, I have script on crontab and give output quite large. I would like to know how to create rotate log when the size of log maximum 50MB if the test.log is 50MB then create test.0 Thanks Edy (2 Replies)
Discussion started by: edydsuranta
2 Replies

7. Shell Programming and Scripting

Help with a rotate log script

Hi all, Am trying to write my own log rotate script. Curremtly, what I have is as below: #!/bin/ksh file_to_rotate=${1} x=${2} while ] do let curr=${x} let prev=${x}-1 if ] ; then #echo "cp -p ${file_to_rotate} ${file_to_rotate}.${curr}" cp -p... (7 Replies)
Discussion started by: newbie_01
7 Replies

8. UNIX for Dummies Questions & Answers

Rotate logs every 1 hour

Hello All, I am learning unix and basically I want to rotate one of my application logs every 1 hour. I need to rotate that file every one hour. I looked in the forums and googled.. but couldn;t get proper information. Requesting you all to kindly guide me. Our application is running on... (4 Replies)
Discussion started by: arunpvp
4 Replies

9. Shell Programming and Scripting

Script for Log Rotate

Hello, I only know the basic for shell programing. I need help for this, I thinks this is a basic for anyone who know a litle of shell scripting. I need creat a script for a rotatate logs, when a filesystem is full. I have a filesystem. The rotate consist in zip the current log (copy) and... (1 Reply)
Discussion started by: El Rengo
1 Replies
Login or Register to Ask a Question