user786 has asked for the wisdom of the Perl Monks concerning the following question:
Hello, I have written a library to tail the log file in a router and look for a string.
If the string is found it returns the time stamp at which the string was found.
If the string is not found it returns 0 .the script compiles without any errors. However it returns 0 (flase) everytime even if the string is found.
It would be really helpful if someone can help me out with this. I tried various things since morning but im unable to figure it out
use strict; use warnings; sub new { my ( $this, @_ARGS ) = @_; my $class = ref($this) || $this; my $self = { _permitted => \%CLI::FIELDS, %CLI::FIELDS, @_ARGS }; bless $self, $class; # _validateFields is inherited from AKU::Object and checks to ensu +re # that what is passed as arguments to new is in the %FIELDS hash $self->_validateFields(@_ARGS); return $self->_init(); } # function to tail the logs and watch for a string. sub log_watch { my ($self, $log, $string) = @_; my $timestamp = 0; my $cmd = "type-tail $log follow"; # command to tail the file my $prompt = "::$string"; # without a terminal length of 0 some lines get split so set it ex +plicitly $self->execute( 'terminal length 0' ); # execute the command $self->execute( $cmd . $prompt ); $self->{'stdout'} = $self->get_stdout(); if ( $self->get_error() ) { # try to disconnect $self->execute("\cC"); return 0; } # try to disconnect $self->execute("\cC"); foreach my $line ( @{ $self->{'stdout'} } ) { if ( $line =~ m/^(\d+\/\d+\/\d+\s\d+\:\d+\:\d+\.\d+)/msx ) { $timestamp = $1; } DEBUG("Line: $line"); } return $timestamp; }
Im using the above lib in a sample script as below
Function call :sub testMain { my $log_obj = LogUtils->new( ip => 'ip addr ' ); my $retrieval_time = $log_obj->log_watch( 'logfile.txt', # file name 'm-ns - - 200 GET http://www.google.com' # string to be searched ); INFO("Found retrieval of the specified string at $retrieval_time");
below is the output
output : 20140806T003044 INFO Found retrieval of the specified string at 0
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: tailing the file and look for string
by hippo (Archbishop) on Aug 06, 2014 at 08:20 UTC | |
Re: tailing the file and look for string
by NetWallah (Canon) on Aug 06, 2014 at 02:27 UTC | |
Re: tailing the file and look for string
by Anonymous Monk on Aug 06, 2014 at 01:38 UTC | |
Re: tailing the file and look for string
by locked_user sundialsvc4 (Abbot) on Aug 06, 2014 at 02:10 UTC | |
by user786 (Sexton) on Aug 06, 2014 at 02:20 UTC | |
by GotToBTru (Prior) on Aug 06, 2014 at 14:04 UTC | |
|