#!/usr/bin/perl -sw ## ## zyxelwatcher -- tail a zyxel log file and reset the modem and kill -hup ## the zyxel application when it resets. ## ## Copyright (c) 1998, Vipul Ved Prakash. All rights reserved. ## This code is free software; you can redistribute it and/or modify ## it under the same terms as Perl itself. ## ## $Id$ use POE qw(Wheel::FollowTail Driver::SysRW Filter::Line Wheel::ReadWrite); use IO::File; my @modems = qw/incoming0 incoming1/; for ( @modems ) { my $i = "zyxelwatcher.$_"; my $name = "/tmp/zyxel.$_"; new POE::Session ( '_start' => sub { my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; $heap->{'id'} = $i; if (defined(my $handle = new IO::File("<$name"))) { $heap->{'wheel'} = new POE::Wheel::FollowTail ( 'Handle' => $handle, # follow this handle 'Driver' => new POE::Driver::SysRW(), # use sysread to read 'Filter' => new POE::Filter::Line(), # file contains lines 'InputState' => 'got a line', # input handler 'ErrorState' => 'error reading', # error handler 'PollInterval' => 2, ); } else { print "Reader $heap->{'id'} can't open $name for reading: $!\n"; } }, '_stop' => sub { my $heap = $_[HEAP]; delete $heap->{'wheel'}; print "Reader $heap->{'id'} has stopped.\n"; }, 'error reading' => sub { my ($heap, $operation, $errnum, $errstr) = @_[HEAP, ARG0, ARG1, ARG2]; print( "Reader ",$heap->{'id'}, " encountered $operation error $errnum: $errstr.\n" ); delete $heap->{'wheel'}; }, 'got a line' => sub { my $text= $_[ARG0]; if ( $text =~ /do it/i ) { system "echo  > /dev/console"; } print $text, "\n"; }, ); } $poe_kernel->run(); exit;