LJ Archive

Listing 1. Perl Script for Push Method

#!/usr/bin/perl
$fifo="/var/log/remote";   # fifo name
$host="remote.host.com";   # host to log to
                         # (additional log holder)
$file="mylogs.log";   # logging file on remote
                            # machine
$login="gromit";  # login name on the
                            # remote machine
$ssh_opts="-q -C -o 'FallBackToRsh=no'";
                                     # ssh options
$ssh_opts="-q +C";   # better for ssh2
$local_log="/var/log/remlog"; # local log for
                                     # failures
sub open_remote {       # open the connection
  open(SSH,"|ssh $ssh_opts -l $login $host
    ".'"'."cat >> $file".'"');
  sleep(15);       # wait so it can be established
  select(SSH);
  $|=1;            # make it unbuffered
}
if ($local_log) {  # if local logging enabled
  open(LOG,">>$local_log"); # then open the
                                       # log file
  select(LOG);
  $|=1;
}
select STDOUT;
$SIG{'PIPE'}='IGNORE';
open_remote();   # try to establish the connection
open(FIFO,"$fifo"); # our input comes from here
if (fork()) {
  while (1) {
     $in=<FIFO>;  # read logs
     while (!(print SSH $in)) {  # try to send
                                 # over network
        print LOG "remote end was dead at " .
          `date` if ($local_log);
        close(SSH);     # if error occured try to
        open_remote();  # open connection again
     }
  }
}
else {
   while (1) {
     `date > $fifo`;  # send a timestamp
                                   # to remote end
   sleep(60*30);     # each 30 minutes
   }
}
LJ Archive