#!/usr/bin/perl use POE qw/Wheel::FollowTail/; use strict; use warnings; our $filename; our $output="output"; open(CONFIG_M, "exempt.messages") || die("Could not open file!"); open(CONFIG_S, "exempt.secure") || die("Could not open file!"); my @exempt_messages=<CONFIG_M>; close(CONFIG_M); my @exempt_secure=<CONFIG_S>; close(CONFIG_S); open(OUTPUT,">>$output") || die("Cannot Open File"); my %logs_to_watch = ( secure => "/var/log/secure", 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. secure_record => \&secure_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, } ); 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, PollInterval => 120, InputEvent => $service . "_record", ResetEvent => "log_reset", ErrorEvent => "log_error", ); $heap->{services}->{ $log_watcher->ID } = $service; $heap->{watchers}->{ $log_watcher->ID } = $log_watcher; } } # Handle log resets the same way for each file. Simply recognize that # the log file was reset. sub generic_log_reset { my ( $heap, $wheel_id ) = @_[ HEAP, ARG0 ]; my $service = $heap->{services}->{$wheel_id}; print "--- $service log reset at ", scalar(gmtime), " GMT\n"; } # Handle log errors the same way for each file. Recognize that an # error occurred while watching the file, and shut the watcher down. # If this were a real log watcher, it would periodically try to resume # watching the log file. sub generic_log_error { my ( $heap, $operation, $errno, $error_string, $wheel_id ) = @_[ HEAP, ARG0, ARG1, ARG2, ARG3 ]; my $service = $heap->{services}->{$wheel_id}; print "--- $service log $operation error $errno: $error_string\n"; print "--- Shutting down $service log watcher.\n"; delete $heap->{services}->{$wheel_id}; delete $heap->{watchers}->{$wheel_id}; } # Display some interesting things from the messages log. sub msg_got_record { my $log_record = $_[ARG0]; print "$log_record\n"; print OUTPUT " $log_record\n"; #system ("xterm less output"); foreach my $ignored (@exempt_messages) { return if $log_record eq $ignored; } } sub secure_got_record { my $log_record = $_[ARG0]; print "$log_record\n"; print OUTPUT " $log_record\n"; #system ("xterm | less output"); foreach my $ignored (@exempt_secure) { #Not sure how to ignore messages in exempt_secure # system ("/bin/grep -v $ignored $log_record # return if $log_record eq $ignored; } } POE::Kernel->run(); exit;
In reply to Log watcher which outputs alerts in xterm windows by wishartz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |