Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Log rotaion for the files which are existed in the absolute paths from the config files

by perladdict (Chaplain)
on Mar 27, 2014 at 20:41 UTC ( #1080003=perlquestion: print w/ replies, xml ) Need Help??
perladdict has asked for the wisdom of the Perl Monks concerning the following question:

Hi Dear Monks
I have a task to develop the script in perl to rotate the log based on the threshold size and the number if iterations which has been given in the config file.
Below is the content of the config file
########################################################### #This file has the details required for archival. #This is to be used for Log Roration script to be #excuted on each instance. #The columns are separated by a space. # #meshid absolutepathoflog/file thresholdsize iterationcount ########################################################### test0613 /opt/xxx/domains/test0613/dev/domain/test0613-dev/ipe/logs/*. +log 5MB 3 test0513 /opt/xxx/domains/test0513/dev/domain/test0513-dev/* 3MB 2 test0413 /opt/xxx/domains/test0413/dev/domain/test0413-dev/bw/logs/* 2 +MB 2 test0813 /opt/xxx/domains/test0813/dev/platform/log/*.log 5KB 4
In the above config the first field is the userid,second field is absolute path,3rd field is threshold size and the last field is number of iteration need to be done before archival of log files. I need to develop the script from bottom up approach,means first i need to check is there any file for ex a.log exist for three iteration i.e a1.log,a2.log and a3.log, if exist i need to archive the all the a1,a2,a3.logs,if not need to check that in each path is there any files exist with more than the threshold size as mentioned in the above config file and need to rename the a.log to a1.log and if a1.log reaches the threshold size then need to rename it to a1.log to a2.log and and a2.log to a3.log as mentioned in the last field in the above config file
Below is the sample code i am trying to parse the config file, but i am not getting the how to strt with this task.Please guide me how to start with this implemetaion.
$file = "logconfig2.txt"; open(F,$file) or die "can't open the file $!\n"; while($line=<F>) { chomp $line; if($line !~ /^#/) { ($id,$path,$size,$iter) = split(" ",$line); @ids = $id; @paths = $path; @sizes = $size; @iters = $iter; @files = `ls $path`; #print "$path\n"; foreach(@files) { $abspath = $path . $_; print "$abspath\n"; $flsize = `ls -lrt "$abspath" | awk -F" " '{print $5}'`; print "$flsize\n"; } } }
From the above code i can able to print the absolute path for all the files for all the paths which exist in config file, but in $flsize i am not able to get the size of all the files, it is displaying as :No such files or directory. Monks it will be a greate help if you can guide me accomplish this task.

Comment on Log rotaion for the files which are existed in the absolute paths from the config files
Select or Download Code
Re: Log rotaion for the files which are existed in the absolute paths from the config files
by Anonymous Monk on Mar 27, 2014 at 21:11 UTC

    First, why reinvent the wheel? logrotate

    On to your code:

    Assignments such as @ids = $id; are probably not doing what you expect, I suspect you want push instead: push @ids, $id;

    For storing the values of the file, you might also want to look into a data structures such as "arrays of arrays" or "arrays of hashes" as described in perldsc.

    For a simple way to list the contents of a directory, use glob: @files = glob("$path/*"); (there are several other ways but this one might be enough for your purposes). Also, if you do that, you won't need to concatenate the pathname and filename to get the absolute pathname, as you are currently doing.

    To get the size of a file, it is enough to write -s $filename, i.e. in your case $flsize = -s $abspath; (see -X)

    Lastly, the style of Perl you are writing in is a little outdated, you should begin your scrips with use warnings; use strict; to force yourself to stick to some less-error prone standards such as pre-declaring your variables.

    Just one good resource of many is Modern Perl, available for free online.

      Minor correction: Since your $path already contains wildcard characters, it's enough to do @files = glob($path); (assuming the wildcards used in the config file are compatible with glob)

Re: Log rotaion for the files which are existed in the absolute paths from the config files
by kcott (Abbot) on Mar 27, 2014 at 21:15 UTC

    G'day perladdict,

    It would have helped if you'd shown (at least a sample of) what you're actually getting for $abspath. I'll have to guess.

    When $path is

    /opt/xxx/domains/test0813/dev/platform/log/*.log

    You get a value of $abspath that looks like

    /opt/xxx/domains/test0813/dev/platform/log/*.logwhatever.log

    But what you really want is

    /opt/xxx/domains/test0813/dev/platform/log/whatever.log

    You'll need to chop of the wildcard part (i.e. *.log, in this instance) before concatenating the filename.

    -- Ken

      Hi Kcott,
      For testing perpose i have created a config file as like below and created directory structure as like in below config and created few sample log files and text files in appropriate paths.I am getting the absolute path as like below
      mesh0123 /tmp/rajp/test123/logs/ 185KB 3 mesh0456 /tmp/rajp/test456/ 255KB 2 mesh0789 /tmp/rajp/test789-/logs/ 240KB 2 mesh0001 /tmp/rajp/link/platform/log/ 185KB 4 /tmp/rajp/test123/logs/a.log /tmp/rajp/test456/logs/b.log /tmp/rajp/test789/logs/c.log /tmp/rajp/xxxxx/logs/d.log /tmp/rajp/yyyyy/logs/z.log /tmp/rajp/zzzzz/ab.txt /tmp/rajp/test/ac.log /tmp/rajp/test456/ad.txt /tmp/rajp/test789/logs/S.txt /tmp/rajp/test789/logs/T.txt /tmp/rajp//xxxx/log/a.log /tmp/rajp/xxxxx/platform/log/b.log /tmp/rajp/xxxxx/platform/log/c.log /tmp/rajp/yyyyyy/platform/log/d.log

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1080003]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2014-12-27 10:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls