#!/usr/bin/perl $|=1; %hosts=( "host1" => "192.168.168.32", "host2" => "192.168.168.33", "host3" => "192.168.168.34" ); $file = "/var/log/bluebolt/bluebolt.log"; $tail = "/usr/bin/tail"; $ssh = "/usr/bin/ssh"; $grep = "/bin/grep --color=always"; #die "Usage: $0 \n" unless @ARGV; if ($ARGV[0] eq "--help"){ print "Usage $0 [pattern]\n"; print "$0 will show all log files for App servers\n"; print "$0 [pattern] will grep the log files for the pattern\n"; exit; } $SIG{CHLD} = 'IGNORE'; $SIG{INT} = sub {done("Received SIGINT")}; sub done { print "@_\n"; exit; } foreach $key (keys %hosts) { if(fork){ #parent process does nothing } else { #child process does all the work #add any grep commands here if ($ARGV[0]) { if ($ARGV[1]) { $command = "$ssh $hosts{$key} $tail -f $file | $grep $ARGV[0] $ARGV[1]"; }else{ $command = "$ssh $hosts{$key} $tail -f $file | $grep $ARGV[0]"; } } else { $command = "$ssh $hosts{$key} $tail -f $file"; } open (README, "$command |") or die "Couldn't fork: $!\n"; select README; $|=1; while(){ print STDOUT "$hosts{$key}: "; print STDOUT $_; } } } while(1){sleep(5);}#parent process needs to wait for Ctrl-C exit;