3.138.122.4 |    

Navigation

Google Advertisement

cronolog is a simple filter program that reads log file entries from standard input and writes each entry to the output file specified by a filename template and the current date and time.

cronolog
  1. #!/usr/bin/perl
  2. # ****************************************************************
  3. # file: cronolog
  4. # date: 2007-08-24 10:35:27
  5. # author: Marko Schulz - <info@tuxnet24.de>
  6. #
  7. # description: cronolog is a simple filter program that reads log
  8. # file entries from standard input and writes each entry to the
  9. # output file specified by a filename template and the current
  10. # date and time.
  11. # ****************************************************************
  12. use strict;
  13.  
  14. # define the rotation time (HHMM)...
  15. my $rotation_time_hhmm="00:00";
  16.  
  17. # load package for command line arguments (GetOptions)...
  18. use Getopt::Long;
  19.  
  20. # load package for file handling (dirname)...
  21. use File::Basename;
  22.  
  23. # define variables...
  24. use vars qw( $opt_V $opt_p $opt_s $opt_t $opt_h $file );
  25.  
  26. # set alarm handler and trigger initial alarm 
  27. $SIG{ALRM} = \&sig_alarm;
  28. alarm(1);
  29.  
  30. # define command line arguments...
  31. Getopt::Long::Configure('bundling');
  32. GetOptions
  33.     ( "V"   => \$opt_V, "version"   => \$opt_V,
  34.       "p=s" => \$opt_p, "period=s"  => \$opt_p,
  35.       "t=i" => \$opt_t, "tzone=i"   => \$opt_t,
  36.       "s=s" => \$opt_s, "symlink=s" => \$opt_s,
  37.       "h"   => \$opt_h, "help"      => \$opt_h );
  38.  
  39. # default time shift is 0 if opt_t is not defined...
  40. $opt_t=0 if ( !$opt_t );
  41.  
  42. # ****************************************************************
  43. # check cammandline arguments...
  44.  
  45. # display help or usage...
  46. if ( $opt_h ) {
  47.     print "<STDIN> | cronolog --symlink|-s /path/to/symlink [ --tzone|-t <time shift> --period|";
  48.     print "-p 1day --help|-h --version|-V ] /path/to/logfile-\%Y-\%m-\%d.log\n\n";
  49.     print "\t--symlink|-s /path/to/symlink (required)\n";
  50.     print "\t--tzone|-t int (-12..+12) (default 0)\n";
  51.     print "\t--period|-p rotation period (ignored, we rotate at 00:00)\n";
  52.     print "\t--help|-h display this screen\n";
  53.     print "\t--version|-V display version\n\n";
  54.     exit 0;
  55. }
  56.  
  57. # print out a cronolog version...
  58. if ( $opt_V ) {
  59.     print STDERR "cronolog version 1.7.0\n";
  60.     exit 0;
  61. }
  62.  
  63. # error message if no logfile defined...
  64. if ( !$ARGV[-1] ) {
  65.     print "cronolog: No logfile defined.\n";
  66.     exit 1;
  67. }
  68.  
  69. # print error message if logfile directory is not writable...
  70. if ( ! -w dirname( $ARGV[-1] ) ) {
  71.    print "cronolog: Can't create logfile -".$opt_s."- because the directory is not writable.\n";
  72.    exit 1;
  73. }
  74.  
  75. # remove leading character...
  76. $opt_s =~ s/^=//g;
  77.  
  78. # error message if no symlink defined...
  79. if ( !$opt_s ) {
  80.     print "cronolog: No symlink defined.\n";
  81.     exit 1;
  82. }
  83.  
  84. # print error message if symlink directory is not writable...
  85. if ( ! -w dirname( $opt_s ) ) {
  86.    print "cronolog: Can't create symlink -".$opt_s."- because the directory is not writable.\n";
  87.    exit 1;
  88. }
  89.  
  90. # ****************************************************************
  91. # program action...
  92.  
  93. # let the signal handler run at least once
  94. sleep(1);
  95.  
  96. # loop until STDIN is closed...
  97. while (<STDIN>) {
  98.  
  99. 	# get current filename from template...
  100. 	$file=get_filename($ARGV[-1]);
  101.  
  102. 	# flush buffer...
  103. 	$|=1;
  104.  
  105. 	# write data from STDIN...
  106. 	open (DAT, ">>$file") or die( "Can't open -".$file."-: ".$! );
  107.     print DAT $_;
  108. 	close(DAT);
  109.  
  110. }
  111.  
  112.  
  113. # ****************************************************************
  114. # return logfile name from logfile template...
  115.  
  116. sub get_filename {
  117.  
  118.     my $filename=shift;
  119.  
  120.     # get date as array reference( YYYY, MM, DD, HH, MM, SS )...
  121.     my $ref_date = date( $opt_t );
  122.  
  123.     # replace placeholder ( %Y, %m, %d, %H, %M, %S => strftime )...
  124.     $filename =~ s!\%Y!$ref_date->[0]!g;
  125.     $filename =~ s!\%m!$ref_date->[1]!g;
  126.     $filename =~ s!\%d!$ref_date->[2]!g;
  127.     $filename =~ s!\%H!$ref_date->[3]!g;
  128.     $filename =~ s!\%M!$ref_date->[4]!g;
  129.     $filename =~ s!\%S!$ref_date->[5]!g;
  130.  
  131.     return $filename;
  132.  
  133. }
  134.  
  135. # ****************************************************************
  136. # signal handler, invoked on SIG_ALRM...
  137.  
  138. sub sig_alarm {
  139.  
  140.     # get target file of the symlink...
  141.     my $symlink=$opt_s;
  142.     my $target = readlink( $symlink );
  143.  
  144.     # get date as array reference( YYYY, MM, DD, HH, MM, SS )...
  145.     my $ref_date = date( $opt_t );
  146.  
  147.     # refresh filename to use 
  148.     $file=get_filename($ARGV[-1]);
  149.  
  150.     # create new file (only) if it does not exist...
  151.     if ( ! -f $file || $ref_date->[3].":".$ref_date->[4] eq $rotation_time_hhmm ) {
  152.  
  153.         # check if symlink with name of log file exists that would prevent logfile creation
  154.         if ( -l $file) {
  155.  
  156.             # remove symlink only if symlink or file exists...
  157.             unlink( "$file" ) or die( "Can't remove symlink -".$file."-: ".$! );
  158.         }
  159.  
  160.         # open file in append mode (may have been created from within while loop!)
  161.         open (DAT, ">>$file") or die( "Can't open file -".$file."-: ".$! );
  162.         close(DAT);
  163.     }
  164.  
  165.     # remove symlink if target file and current logfile are not equal or rotation_time is reached...
  166.     if ( $target ne $file || $ref_date->[3].":".$ref_date->[4] eq $rotation_time_hhmm ) {
  167.  
  168.         # remove symlink only if symlink or file exists...
  169.         unlink( "$symlink" ) or die( "Can't remove symlink -".$symlink."-: ".$! ) if ( -l $symlink || -f $symlink );
  170.  
  171.  
  172.         # create symlink to current logfile if no symlink exists...
  173.         symlink( "$file", "$symlink" ) or die( "Can't create symlink -".$symlink."- to -".$file."-: ".$! ) if ( ! -l $symlink );
  174.     }
  175.  
  176.     # trigger new alarm to occur when next minute begins ...
  177.     my $ref_date = date( $opt_t );
  178.     alarm( 60-$ref_date->[5] );
  179.  
  180. }
  181.  
  182. # ****************************************************************
  183. # return date as array ( %Y, %m, %d, %H, %M, %S => strftime )...
  184.  
  185. sub date {
  186.  
  187.     my $tzone=shift;
  188.     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time+($tzone*3600));
  189.     $year = 1900+$year;
  190.     $mon = $mon+1;
  191.  
  192.     $sec  = sprintf( "%02d", $sec  );
  193.     $min  = sprintf( "%02d", $min  );
  194.     $hour = sprintf( "%02d", $hour );
  195.     $mon  = sprintf( "%02d", $mon  );
  196.     $mday = sprintf( "%02d", $mday );
  197.  
  198.     my @date=( $year, $mon, $mday, $hour, $min, $sec );
  199.     return \@date;
  200.  
  201. }
  202.  
  203. # ****************************************************************
  204. # end of this script...
Parsed in 0.004 seconds at 1436.96 KB/s

Search
 
Full text search by name and content of a snippet.

User online
There are 4 users online.

Tags Cloud

Latest snippets
str2seconds
(Bash::Function)
is_integer
(Bash::Function)
file_rotate
(Bash::Function)
confirm
(Bash::Function)
is_workingtime
(Bash::Function)
last day of last month
(Bash::Snippets)
crypt_apr1_md5
(PHP::Function)
crypt_apr1_md5
(Perl::Function)
transparent
(CSS)
rfc2822Toiso8601
(PHP::Function)