Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Checking multiple files..

by penantes (Acolyte)
on Mar 28, 2005 at 10:02 UTC ( [id://442766]=perlquestion: print w/replies, xml ) Need Help??

penantes has asked for the wisdom of the Perl Monks concerning the following question:

Hi there,
I want to check on multiple growing files but I really don't know how to do it.
I know "select()" is somehow related with my question but I cannot figure how to do it... :(
I also want to "uniq"!??! the fetched values. The thing will send emails if trigered but I only want to send 1 from ocurrence every 15 minutes.
The bellow code is not regexed on the fetched values.
Many thanks
Nuno A
#!/usr/bin/perl $naptime = 20; $telcofile="logilestest"; open (LOGFILE, $telcofile) or die "can't open $telcofile: $!"; for (;;) { for ($curpos = tell(LOGFILE); <LOGFILE>; $curpos = tell(LOGFI +LE)) { print "Got Line!: $_"; } sleep $naptime; seek(LOGFILE, $curpos, 0); # seek to where we had been exit if (stat(LOGFILE))[3] == 0 } close(LOGFILE) or die "can't close $telcofile: $!"; print "Exiting...\n";

Replies are listed 'Best First'.
Re: Checking multiple files..
by nobull (Friar) on Mar 28, 2005 at 12:35 UTC
    I want to check on multiple growing files but I really don't know how to do it.

    Basically just use the polling approach you've already partially figured out for a single file but loop over an array (or hash) of filehandles.

    However you've not considered the possibility that there may be an incomplete line at the end of the file. To see how to cope with that take a look at how File::Tail does it.

    This is really quite complex. Perhaps you should just use File::Tail.

    Asside: I think you are saying exit where you meant last.

    I know "select()" is somehow related with my question but I cannot figure how to do it... :(

    The builtin select() applies when you want to monitor multiple pipe/sockets/devices. These things can be in a state where they have no data to return now but are not at an EOF contition. A file cannot. However File::Tail defines another select().

    Oh, and please get into using strictures and warnings now rather than waiting to suffer/inflict the pain that not using them will cause sooner or later.

      Hi there, thanks for your answer.
      I do have a complete version of my question that I setuped using the File::Tail examples, but I was trying to avoid it because for that I need to compile an executable with PAR wich makes a huge bin file (I dont have permission to setup the File::Tail module on the destination server)... oh well!..
      Wont the incomplete line be caugth on the next cycle? Need to do a bit to bit count?!
      So if I need to get stuck with the code bellow how can I do and "uniq" ou the fectched regex values on "$_->read;"

      Best Regards,
      NMA

      #!/usr/bin/perl use warnings; use strict; use File::Tail; my $ConfFile="watchtelecom.conf"; my @LogFiles; my @Files; my $nfound; my $timeleft; my @pending; my $timeout; my $ErrorPatern="error"; #testing purpuses. #my $ErrorPatern="Input: Error reading in input stream"; my $test; my $x; open(CONFFILE, "$ConfFile") or die("Could not open $ConfFile."); foreach (<CONFFILE>) { push(@LogFiles,$_); } close(CONFFILE) or die("Could not close $ConfFile."); foreach (@LogFiles) { push(@Files,File::Tail->new(name=>"$_", maxinterval=>1800)); } while (1) { ($nfound,$timeleft,@pending)= File::Tail::select(undef,undef,undef,$timeout,@Files); foreach (@pending) { my $telcoline=$_->read; if ($telcoline =~ /$ErrorPatern/) { #print $_->{"input"}." (".localtime(time).") ".$_->read; if ($_->{"input"} =~ /TMN/) { print "Via Vodafone: $telcoline"; $test=$telcoline; } else { print "Via TMN: $telcoline"; $test=$telcoline; } } } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://442766]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-16 07:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found