Skip to content

Instantly share code, notes, and snippets.

@xtetsuji
Last active June 21, 2016 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xtetsuji/8acad9636029da2f04fe68a957dd36c8 to your computer and use it in GitHub Desktop.
Save xtetsuji/8acad9636029da2f04fe68a957dd36c8 to your computer and use it in GitHub Desktop.
Syslog manual rotatation. It fills alphabet as temprary year information.
#!/usr/bin/perl
use strict;
use warnings;
use Term::ANSIColor qw(colored);
my %mon_digit_of = (
qw(Jan 1 Feb 2 Mar 3 Apr 4 May 5 Jun 6 Jul 7 Aug 8 Sep 9 Oct 10 Nov 11 Dec 12)
);
my $cur_mon = '';
my $cur_year_alphabet = "a";
my $cur_fh;
my $file = shift
or die "usage: $0 FILE";
open my $fh, '<', $file or die;
while (<$fh>) {
my ($mon) = /^(\w\w\w)/;
if ( $cur_mon ne $mon ) {
info("change month: $mon");
my $is_skew = is_skew($cur_mon, $mon);
if ( is_skew($cur_mon, $mon) ) {
$cur_year_alphabet++;
info("change year: $cur_year_alphabet");
}
$cur_fh = get_cur_fh($mon_digit_of{$mon}, $cur_year_alphabet);
$cur_mon = $mon;
}
info("line tick: $.") if $. % 1_000_000 == 0;
print {$cur_fh} $_;
}
close $fh;
sub is_skew {
my ($mon1, $mon2) = map { $mon_digit_of{$_} || 0 } @_;
#print "> mon1,mon2 = $mon1,$mon2\n";
if ( $mon1 > $mon2 ) {
return 1; # skew
} else {
return 0;
}
}
sub get_cur_fh {
my ($mon_digit, $year_alphabet) = @_;
open my $fh, '>', "$file.$year_alphabet-$mon_digit";
return $fh;
}
sub info {
my $message = shift;
print colored($message, "green"), "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment