73 lines
3.0 KiB
Perl
73 lines
3.0 KiB
Perl
package My::parser::file;
|
|
use strict;
|
|
use warnings;
|
|
use File::Tail 0.91;
|
|
|
|
|
|
sub new {
|
|
my $class = shift;
|
|
my $config = shift;
|
|
my $rest = shift;
|
|
return \$class if($rest);
|
|
my $self = {};
|
|
bless ($self, $class);
|
|
$self->{'config'} = $config;
|
|
$self->{'config'}->{'fetcher'}->{'file'} = $self;
|
|
return $self;
|
|
}
|
|
|
|
sub init {
|
|
my $self = shift;
|
|
my $parser = shift;
|
|
if($parser) {
|
|
my $hash = $self->{'config'}->get_parser_info($parser);
|
|
if($hash->{'type'} eq 'file') {
|
|
my %opts = %{ &set_defaults };
|
|
my $filename = $hash->{'source'};
|
|
my $maxinterval = $self->{'config'}->get_as_single_val('config','maxinterval');
|
|
my $interval = $self->{'config'}->get_as_single_val('config','interval');
|
|
my $adjustafter = $self->{'config'}->get_as_single_val('config','adjustafter');
|
|
my $resetafter = $self->{'config'}->get_as_single_val('config','resetafter');
|
|
my $maxbuf = $self->{'config'}->get_as_single_val('config','maxbuf');
|
|
my $nowait = $self->{'config'}->get_as_single_val('config','nowait');
|
|
my $ignore_nonexistant = $self->{'config'}->get_as_single_val('config','ignore_nonexistant');
|
|
my $tail = $self->{'config'}->get_as_single_val('config','tail');
|
|
my $reset_tail = $self->{'config'}->get_as_single_val('config','reset_tail');
|
|
$opts{'name'} = $filename;
|
|
$opts{'maxinterval'} = $maxinterval if(defined($maxinterval));
|
|
$opts{'interval'} = $interval if(defined($interval));
|
|
$opts{'adjustafter'} = $adjustafter if(defined($adjustafter));
|
|
$opts{'resetafter'} = $resetafter if(defined($resetafter));
|
|
$opts{'maxbuf'} = $maxbuf if(defined($maxbuf));
|
|
$opts{'nowait'} = $nowait if(defined($nowait));
|
|
$opts{'ignore_nonexistant'} = $ignore_nonexistant if(defined($ignore_nonexistant));
|
|
$opts{'tail'} = $tail if(defined($tail));
|
|
$opts{'reset_tail'} = $reset_tail if(defined($reset_tail));
|
|
$self->{'config'}->{'logger'}->log("Initializing fetcher file from parser $parser with file $filename");
|
|
my $newfetcher = File::Tail->new(%opts);
|
|
$self->{'config'}->set_fetcher('file',$parser,$newfetcher);
|
|
return 1;
|
|
} else {
|
|
return 0;
|
|
}
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
sub set_defaults {
|
|
my $opts;
|
|
$opts->{'maxinterval'} = 60; #max time spent sleeping between checks
|
|
$opts->{'interval'} = 10; #initial time before first check
|
|
$opts->{'adjustafter'} = 10; #number of times $interval passes before adjusting the check interval
|
|
$opts->{'resetafter'} = $opts->{'interval'} * $opts->{'adjustafter'}; #Number of seconds after last change to file before reopening the file
|
|
$opts->{'maxbuf'} = 16384;
|
|
$opts->{'nowait'} = 0; #Does not block on read, but returns an empty string if there is nothing to read.
|
|
$opts->{'ignore_nonexistant'} = 0; #Do not complain if the file doesn't exist when it is first opened or when it is to be reopened.
|
|
$opts->{'tail'} = 0; #When first started, read and return C<n> lines from the file. If C<n> is zero, start at the end of file. If C<n> is negative, return the whole file.
|
|
$opts->{'reset_tail'} = 0; #Same as tail, but for when $resetafter has gone by without any changes to the file
|
|
return $opts;
|
|
}
|
|
|
|
1;
|