#!/usr/bin/perl -w # # Jian Zhen # 01/05/06 # # there's a big problem with this script, see get_last_position sub $debug = 0; $curl = "/usr/bin/curl"; ## DO NOT MODIFY BELOW HERE ## $usage = "Usage: $0 \n"; @ARGV == 2 || die $usage; use File::stat; use File::Basename; $tmpdir = "/var/tmp/ccurl"; if (! -d $tmpdir) { mkdir($tmpdir, 0755) || die "Cannot mkdir $tmpdir: $!"; } sub get_pos_file { my ($st, $pfile, $file); $file = shift; -r $file || die "No $file: $!"; $file =~ s/[\/\\]/_/g; $pfile = $tmpdir . "/" . $file; return $pfile } sub get_last_position { my ($st, $pfile, $pos, $file, $size); $file = shift; $pfile = get_pos_file($file); $st = stat($file) || die "No $file: $!"; $size = $st->size; if (! -r $pfile) { open(F, ">$pfile") || die "Can't open $pfile: $!"; $pos = 0; print F "0"; close(F); } else { open (F, $pfile) || die "Can't open $pfile: $!"; $pos = ; close(F); } # if $size < $pos, that means the file has been rotated # however, there are two problems here # 1. $size could have increased so fast that the next time # the file is looked at, that it has increased passed # $pos. this means we will miss all the logs before pos # 2. if the file is rotated, that means there's a possibility # that we have lost some logs from the previous file, # like from $pos to the end of the file # what can we do? return $size < $pos ? 0 : $pos; } sub put_last_position { my ($st, $pfile, $pos); $pfile = get_pos_file(shift); $pos = shift; open(F, ">$pfile") || die "Can't open $pfile: $!"; print F $pos; close(F); } ($file, $logapp) = @ARGV; $logapp =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ || die "Invalid LogLogic appliance IP: $logapp"; print get_pos_file($file),"\n" if $debug; $pos = get_last_position($file); $cmd = $curl . " -C " . $pos . " -w '%{size_upload}' -T " . $file . " http://" . $logapp . ":4433/loglogic"; open(CURL, "$cmd |"); $return = ; close(CURL); print $cmd,"\n" if $debug; print $pos,"\n" if $debug; print $return,"\n" if $debug; if ($return =~ /\d+/) { print $pos,"\n" if $debug; $pos += $return; print $pos,"\n" if $debug; put_last_position($file, $pos); }