1 |
760 |
ahitrov |
#!/usr/bin/env perl |
2 |
8 |
ahitrov@rambler.ru |
|
3 |
|
|
############################################################################## |
4 |
|
|
# $HeadURL: http://svn.dev.rambler.ru/Contenido/branches/utf8/services/logtail $ |
5 |
|
|
# $Id: logtail 1291 2008-11-10 13:25:19Z phoinix $ |
6 |
|
|
# |
7 |
|
|
# Удобно мониторить логи, например поставить в crontab: |
8 |
|
|
# */5 10-19 * * * logtail /.../error_log ~/tmp/error_log.off |
9 |
|
|
############################################################################## |
10 |
|
|
|
11 |
|
|
use strict; |
12 |
|
|
use warnings 'all'; |
13 |
|
|
|
14 |
|
|
use File::stat; |
15 |
|
|
use FindBin; |
16 |
|
|
|
17 |
|
|
|
18 |
|
|
if ($#ARGV<0 || $#ARGV>1) { |
19 |
|
|
&usage; |
20 |
|
|
exit 1; |
21 |
|
|
} |
22 |
|
|
|
23 |
|
|
my $log_file = $ARGV[0]; |
24 |
|
|
my $offset_file = $ARGV[1] || "$log_file.offset"; |
25 |
|
|
|
26 |
|
|
my $stat = stat($log_file) or die "Can't stat $log_file - $!"; |
27 |
|
|
|
28 |
|
|
my ($prev_size, $prev_ino); |
29 |
|
|
if (-f $offset_file) { |
30 |
|
|
open OFF, "< $offset_file" or die "Can't read offset_file - $!"; |
31 |
|
|
$prev_size = <OFF>; |
32 |
|
|
$prev_ino = <OFF>; |
33 |
|
|
close OFF; |
34 |
|
|
} |
35 |
|
|
$prev_size = 0 unless $prev_ino==$stat->ino && $prev_size<=$stat->size; |
36 |
|
|
|
37 |
|
|
exit if $prev_size==$stat->size; |
38 |
|
|
|
39 |
|
|
open LOG, "< $log_file" or die "Can't read log - $!"; |
40 |
|
|
seek LOG, $prev_size, 0 or die "Can't seek - $!"; |
41 |
|
|
my $buf; |
42 |
|
|
my $readed = read LOG, $buf, $stat->size - $prev_size; |
43 |
|
|
close LOG; |
44 |
|
|
die "Can't read log_file - $!" unless $readed; |
45 |
|
|
|
46 |
|
|
print $buf; |
47 |
|
|
|
48 |
|
|
open OFF, "> $offset_file" or die "Can't write offset_file - $!"; |
49 |
|
|
print OFF $stat->size."\n".$stat->ino."\n$log_file\n".localtime()."\n"; |
50 |
|
|
close OFF; |
51 |
|
|
|
52 |
|
|
|
53 |
|
|
sub usage { |
54 |
|
|
print <<EOM; |
55 |
|
|
Usage: |
56 |
|
|
$FindBin::Script logfile [offset.file] |
57 |
|
|
EOM |
58 |
|
|
} |