Revision 3 (by ahitrov@rambler.ru, 2010/03/24 15:19:32) |
The CORE
|
#!/usr/bin/perl
##############################################################################
# $HeadURL: http://svn.dev.rambler.ru/Contenido/trunk/services/logtail $
# $Id: logtail 335 2006-08-10 13:06:40Z lonerr $
#
# ������ ���������� ����, �������� ��������� � crontab:
# */5 10-19 * * * logtail /.../error_log ~/tmp/error_log.off
##############################################################################
use strict;
use warnings 'all';
use File::stat;
use FindBin;
if ($#ARGV<0 || $#ARGV>1) {
&usage;
exit 1;
}
my $log_file = $ARGV[0];
my $offset_file = $ARGV[1] || "$log_file.offset";
my $stat = stat($log_file) or die "Can't stat $log_file - $!";
my ($prev_size, $prev_ino);
if (-f $offset_file) {
open OFF, "< $offset_file" or die "Can't read offset_file - $!";
$prev_size = <OFF>;
$prev_ino = <OFF>;
close OFF;
}
$prev_size = 0 unless $prev_ino==$stat->ino && $prev_size<=$stat->size;
exit if $prev_size==$stat->size;
open LOG, "< $log_file" or die "Can't read log - $!";
seek LOG, $prev_size, 0 or die "Can't seek - $!";
my $buf;
my $readed = read LOG, $buf, $stat->size - $prev_size;
close LOG;
die "Can't read log_file - $!" unless $readed;
print $buf;
open OFF, "> $offset_file" or die "Can't write offset_file - $!";
print OFF $stat->size."\n".$stat->ino."\n$log_file\n".localtime()."\n";
close OFF;
sub usage {
print <<EOM;
Usage:
$FindBin::Script logfile [offset.file]
EOM
}