Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

POE::FollowTail and xterm

by wishartz (Beadle)
on Apr 16, 2008 at 13:52 UTC ( [id://680799]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks, I wonder if anybody can help me. I would like to use the POE::FollowTail module to follow various system log files, like /var/log/messages and /var/log/secure and then report any new messages to the user. I have the following example from the POE website, but what I would like it to do, is every time a new message appears to bring up a new xterm console window with the new message being displayed. Using the following code, how can I make it display the messages in a new xterm window, for each different log file. Thanks.
#!/usr/bin/perl -w # http://poe.perl.org/?POE_Cookbook/Watching_Logs use POE qw/Wheel::FollowTail/; use strict; $| = 1; my $filename = $ARGV[0]; die "Usage: $0 <filename>\n" unless $filename; die "$0: $filename: No such file or directory\n" unless -e $filename; die "$0: $filename: Permission denied\n" unless -r $filename; POE::Session->create ( inline_states => { _start => sub { $_[HEAP]->{wheel} = POE::Wheel::FollowTail->new( Filename => $_[ARG0], InputEvent => 'got_line', ErrorEvent => 'got_error', SeekBack => 1024, ); $_[HEAP]->{first} = 0; }, got_line => sub { print "$_[ARG0]\n" if $_[HEAP]->{first}++ }, got_error => sub { warn "$_[ARG0]\n" }, }, args => [$filename], ); $poe_kernel->run();

Replies are listed 'Best First'.
Re: POE::FollowTail and xterm
by moklevat (Priest) on Apr 16, 2008 at 15:30 UTC
    If I understand correctly, you would like to use this followtail script to monitor several log files, and you would like to launch a separate xterm for each separate log file. This is fairly straightforward and you could simply shell script the launching of the followtail script
    #!/bin/bash xterm -hold -e /path/to/follow-tail.pl /var/log/httpd/access_log > /d +ev/null & xterm -hold -e /path/to/follow-tail.pl /var/log/httpd/error_log > /de +v/null &
    Is this what you had in mind? I thought briefly that you might want a separate xterm for each message, but that could generate thousands of xterms very quickly, and thus would be silly (to use the technical term).
Re: POE::FollowTail and xterm
by elmex (Friar) on Apr 16, 2008 at 15:25 UTC

    Hmm, I guess the problem is to start an xterm and actually delivering the message there. I would probably start the X term with another script that would connect the my log-analyzing process via a unix socket or some other form of IPC and receive the message from there.

    But I didn't really understand your problem: Do you want a new xterm for every log-message or only one xterm per logfile?

    If you want one xterm per logfile the best would probably just to startup the xterms and run a tail there.

Re: POE::FollowTail and xterm
by glide (Pilgrim) on Apr 16, 2008 at 22:19 UTC
      Thanks for your help. I wasn't absolutely clear on what I wanted to do. The problem is this program will be for some operators at work and they will be informed when there is a new message for them to take note of. The old script that we use at work was written in kornshell and is run by a crontab every two minutes. What it does is, make a copy of the log file and then compare another version of the log file two minutes later and report if there are any new messages and bring them up in a new xterm window. There is a new xterm window for each log file every two minutes. The reason, I am changing the script is it is not ideal, it's a bit of an overhead to compare the files every two mins. With POE::followtail I don't need to run a cron I could just keep it running and informing on new messages. I could also choose to exclude certain messages as well. I didn't really want to run two scripts to redirect the output to an xterm window, is there anyway of redirecting the output to a new xterm console in the perl script? Thanks
        Using the following code, is there any way that I can make it open a new xterm for each log file?
        my %logs_to_watch = ( cron => "/var/log/cron", mail => "/var/log/maillog", ppp => "/var/log/ppp.log", httpd => "/var/log/httpd-access.log", msg => "/var/log/messages", ); # Start a session to watch the logs. POE::Session->create ( inline_states => { _start => \&begin_watchers, # Handle records from each log differently. cron_record => \&cron_got_record, mail_record => \&mail_got_record, ppp_record => \&ppp_got_record, httpd_record => \&httpd_got_record, msg_record => \&msg_got_record, # Handle log resets and errors the same way for each file. log_reset => \&generic_log_reset, log_error => \&generic_log_error, } ); =for cookbook Start log watchers. Scans the hash of %logs_to_watch, creating a new FollowTail wheel to watch each. Each watcher emits an event based on its key in %logs_to_watch. Those events are handled by functions that will parse, filter, and if necessary display information about the records. For example, cron is the key for "/var/log/cron". The cron log watcher will emit a "cron_record" event whenever that file extends. The POE::Session->create() call above associates the "cron_record" event with the cron_got_record() function later on. =cut sub begin_watchers { my $heap = $_[HEAP]; while ( my ( $service, $log_file ) = each %logs_to_watch ) { my $log_watcher = POE::Wheel::FollowTail->new ( Filename => $log_file, InputEvent => $service . "_record", ResetEvent => "log_reset", ErrorEvent => "log_error", ); $heap->{services}->{ $log_watcher->ID } = $service; $heap->{watchers}->{ $log_watcher->ID } = $log_watcher; } }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-25 19:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found