[BACK]Return to update_trango.pl CVS log [TXT][DIR] Up to [local] / trango / Net-Telnet-Trango / scripts

Diff for /trango/Net-Telnet-Trango/scripts/update_trango.pl between version 1.10 and 1.32

version 1.10, 2005/12/21 01:17:06 version 1.32, 2007/02/07 19:25:05
Line 1 
Line 1 
 #!/usr/bin/perl  #!/usr/bin/perl
 # $RedRiver: update_trango.pl,v 1.9 2005/11/18 19:17:12 andrew Exp $  # $RedRiver: update_trango.pl,v 1.31 2007/02/06 23:15:53 andrew Exp $
 ########################################################################  ########################################################################
 # update_trango.pl *** Updates trango foxes with a new firmware  # update_trango.pl *** Updates trango hosts with a new firmware
 #  #
 # 2005.11.15 #*#*# andrew fresh <andrew@mad-techies.org>  # 2005.11.15 #*#*# andrew fresh <andrew@mad-techies.org>
 ########################################################################  ########################################################################
 use strict;  # Copyright (C) 2005, 2006, 2007 by Andrew Fresh
 use warnings;  #
   # This program is free software; you can redistribute it and/or modify
 use Net::TFTP;  # it under the same terms as Perl itself.
   ########################################################################
 my $config_file = shift || 'update_trango.conf';  use strict;
 my $max_tries = 3;  use warnings;
   
   use YAML qw/ LoadFile /;
   use Net::TFTP;
 my $l = Mylogger->new( { log_prefix => 'UT' } );  use Net::Telnet::Trango;
   
   my $config_file = shift || 'update_trango.yaml';
 $l->sp("Reading config file '$config_file'");  my $max_tries = 3;
 my $conf = read_conf($config_file);  
   my $l = Mylogger->new( { log_prefix => 'UT' } );
 $l->sp("  Hardware Type: $conf->{'type'}");  
 $l->sp("  File Name:     $conf->{'file_name'}");  $l->sp("Reading config file '$config_file'");
 $l->sp("  File Size:     $conf->{'file_size'}");  my $conf = LoadFile($config_file);
 $l->sp("  File Checksum: $conf->{'file_cksum'}");  
 $l->sp("  FW Version:    $conf->{'ver'}");  my $hosts;
 $l->sp("  FW Checksum:   $conf->{'cksum'}");  if (@ARGV) {
 $l->sp("");      @{ $hosts } = map { { name => $_, group => 'Trango-Client' } } @ARGV
   } else {
       $hosts = parse_hosts($conf->{hosts});
   }
   
   #@{ $hosts } = grep { $_->{name} eq '10.100.7.2' } @{ $hosts };
   
 foreach my $fox (@{ $conf->{'ips'} }) {  my $global_tries = $max_tries * 2;
   $l->sp("Updating: $fox");  while ($global_tries > 0) {
       $global_tries--;
   ## Connect and login.      my $processed = 0;
   my $t = new Trango::Telnet ({  
     Host    => $fox,      foreach my $host (@{ $hosts }) {
     Timeout => 5,  
   });          if (! exists $host->{retry}) {
               $host->{tries} = 0;
   $l->p("Connecting to $fox");              $host->{retry} = 1;
   my ($type, $version) = $t->connect();          }
   
   unless (defined $type && defined $version) {          if ($host->{tries} >= $max_tries) {
     $l->sp("Error connecting!");              $host->{retry} = 0;
     next;          }
   }  
           if ($host->{retry} <= 0) {
   if ($type ne $conf->{'type'}) {              next;
     $l->sp("Wrong type of unit ('$type' should be '$conf->{'type'}')");          }
     $t->close;  
     next;          $host->{tries}++;
   }          $processed++;
   
   if ($version eq $conf->{'ver'}) {          $l->sp("");
     $l->sp("Already up to date with firmware version '$version'");          $l->sp("Checking: $host->{name} (try $host->{tries})");
     $t->close;          my $needs_reboot = 0;
     next;  
   }          ## Connect and login.
           my $t = new Net::Telnet::Trango (
   $l->p("Logging in");              Timeout => 5,
   $t->login($conf->{'password'}) || die "Couldn't login: $!";              Errmode => 'return',
           ) or die "Couldn't make new connection: $!";
   $l->p("Sending commands");          $l->p("Connecting to $host->{name}");
   ## Send commands          unless ( $t->open($host->{name}) ) {
   if ( upload($t, $conf->{'file_name'}) ) {              $l->sp("Error connecting: $!");
     $l->p("Rebooting");              next;
     $t->reboot;          }
   } else {  
     $l->p("Exiting");          my $password = $host->{Telnet_Password} || $conf->{general}->{password};
     $t->exit;  
   }          $l->p("Logging in");
   $t->close;          $t->login($password);
   $l->sp("");          unless ($t->logged_in) {
 }              $l->p('Failed!');
               $t->close;
 sub upload              next;
 {          }
   my $t    = shift;  
   my $file = shift;          $l->sp("Getting sudb");
           my $sudb = $t->sudb_view;
   $l->p("Getting current version");          if ($sudb) {
   my $ver = $t->ver;              foreach my $su (@{ $sudb }) {
                   $l->p("Getting su info $su->{suid}");
   if (                  my $su_info = $t->su_info( $su->{suid} );
     $ver->{'Firmware Version'}  eq $conf->{'ver'} &&                  if ($su_info->{ip}) {
     $ver->{'Firmware Checksum'} eq $conf->{'cksum'}                      if (grep { $_->{name} eq $su_info->{'ip'} } @{ $hosts }) {
   ) {                          $l->p("Already have $su_info->{ip}");
     $l->sp("Already updated!");                          next;
     return 1;                      }
   }                      $l->sp("Adding host $su_info->{ip}");
                       my $new_host = {
   my $try = 0;                          password => $host->{password},
   while (1) {                          name     => $su_info->{ip},
     if ($try >= $max_tries) {                          remarks  => $su_info->{remarks},
       $l->sp("Couldn't update in $max_tries tries!");                      };
       return undef;                      push @{ $hosts }, $new_host;
     }                  } else {
     $try++;                      $l->sp("Couldn't get su info for $su->{suid}");
                       $l->sp("ERR: " . $t->last_error);
     #sysinfo($self->{'_host'});                  }
                   }
     $l->p("Enabling TFTPd");          }
     $t->enable_tftpd || die "Couldn't enable tftpd";  
           foreach my $firmware_type ('Firmware', 'FPGA') {
     $l->p("Uploading file ($file)");  
     # use tftp to push the file up              if (! exists $conf->{$firmware_type}) {
     my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');                  $l->s("No configs for '$firmware_type'");
                   next;
     $tftp->put($file, $file)              }
       or die "Error uploading: " . $tftp->error;  
               my $host_type = $t->host_type;
     # waitfor some sort of output              if ($firmware_type eq 'FPGA') {
     # make sure it says 'Success.' otherwise error                  $host_type =~ s/\s.*$//;
     #print "LAST: " . $self->lastline;              }
     #my @lines = $self->getlines;  
     #print Dump \@lines;              if (! exists $conf->{$firmware_type}->{$host_type}) {
                       $l->sp("No '$firmware_type' config for type $host_type");
     $l->p("Checking upload ($conf->{'file_cksum'})");                  next;
     my $results = $t->tftpd;              }
     # check the 'File Length' against ???  
     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {              if ($firmware_type eq 'Firmware' &&
       $l->sp(                  $t->firmware_version eq
         "File checksum '" . $results->{'File Checksum'} .                  $conf->{$firmware_type}->{$host_type}->{ver}
         "does not match config file '" . $conf->{'file_cksum'} . "'!"              ) {
       );                  $l->sp("Firmware already up to date");
       next;                  next;
     }              }
     $l->p("File checksum matches . . . ");  
               if (! $t->logged_in) {
     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {                  $l->p("Logging in");
       $l->sp(                  $t->login($password);
         "File length '" . $results->{'File Length'} .                  unless ($t->logged_in) {
         "does not match config file '" . $conf->{'file_size'} . " bytes'!"                      $l->p('Failed!');
       );                      $t->close;
       next;                      last;
     }                  }
     $l->p("File length matches . . . ");              }
   
     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {              foreach my $k (keys %{ $conf->{general} }) {
       $l->sp(                  $conf->{$firmware_type}->{$host_type}->{$k}
         "File name '" . $results->{'File Name'} .                    ||= $conf->{general}->{$k};
         "' does not match config file '" . $conf->{'file_name'} . "'!"              }
       );              $conf->{$firmware_type}->{$host_type}->{firmware_type}
       next;                ||= $firmware_type;
     }              $conf->{$firmware_type}->{$host_type}->{type} = $host_type;
     $l->p("File name  matches . . . ");  
               $l->sp("$host_type $firmware_type");
     $l->p("Updating flash (new checksum '$conf->{'cksum'}')");              ## Send commands
     unless ($results = $t->updateflash(              my $rc = upload($t, $conf->{$firmware_type}->{$host_type});
       $ver->{'Firmware Checksum'}, $conf->{'cksum'}              if ($rc) {
     ) ) {                  $l->sp("Successfull!");
       $l->sp("Couldn't update flash: $!");                  $host->{retry}--;
       next;                  $needs_reboot = 1;
     }              } elsif (defined $rc) {
                   $l->sp("Already up to date");
     unless (                  $host->{retry}--;
       defined $results->{'Checksum'} &&              } else {
       $results->{'Checksum'} eq $conf->{'cksum'}                  $l->sp("Failed");
     ) {                  $t->bye;
       $l->sp("Saved checksum does not match config file!");                  next;
       next;              }
     }  
     $l->p("Uploaded checksum ($results->{'Checksum'}) " .          }
           "matches ($conf->{'cksum'})");  
               if ($needs_reboot) {
     $l->sp("Successfully updated!");              $l->sp("Rebooting $host->{name}");
     return 1;              $t->reboot;
   }          } else {
 }              $l->sp("Bye $host->{name}");
               $t->bye();
 sub read_conf          }
 {      }
   my $file = shift;  
   my %conf;      if (! $processed) {
   my $in_ip_list = 0;          $l->sp("");
   open my $fh, $file or die "Couldn't open file $file: $!";          $l->sp("Finished.  No more hosts.");
   while (<$fh>) {          last;
     chomp;      }
     next if /^#/;  }
     next if /^$/;  
     if ($in_ip_list) {  sub upload
       s/\s+//g; # Whitespace is a no no  {
       my $t    = shift;
       if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {      my $conf = shift;
         push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);  
       } else {      my $file = $conf->{firmware_path} . '/' . $conf->{file_name};
         push @{ $conf{'ips'} }, $_;  
       }      my $fw_type = $conf->{firmware_type};
     } else {  
       my ($key, $val) = split /\s+/, $_, 2;      my $ver = $t->ver;
   
       if (lc($key) eq 'ips') {      if (! (
         $in_ip_list = 1;              $ver->{$fw_type . ' Version'}  &&
         next;              $ver->{$fw_type . ' Checksum'}
       }          )) {
           $l->sp("Error getting current version numbers");
       $key =~ s/^\s+//;          return;
       $key =~ s/\s+$//;      }
       $val =~ s/^\s+//;  
       $val =~ s/\s+$//;      if (
           $ver->{$fw_type . ' Version'}  eq $conf->{'ver'} &&
       $conf{ lc($key) } = $val;          $ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'}
     }      ) {
   }          return 0;
   close $fh;      }
   
   #print Dump \%conf;      $l->sp("Updating $fw_type");
   foreach (      $l->sp("Config information:");
     'password',      $l->sp("  Hardware Type: $conf->{'type'}");
     'file_name', 'file_size', 'file_cksum',      $l->sp("  File Name:     $conf->{'file_name'}");
     'ver', 'cksum', 'ips',      $l->sp("  File Size:     $conf->{'file_size'}");
   ) {      $l->sp("  File Checksum: $conf->{'file_cksum'}");
     die "No $_ specified in config file!"      $l->sp("  Conf Version:  $conf->{'ver'}");
       if (not exists $conf{$_});      $l->sp("  Cur  Version:  $ver->{$fw_type . ' Version'}");
   }      $l->sp("  Conf Checksum: $conf->{'cksum'}");
       $l->sp("  Cur  Checksum: $ver->{$fw_type . ' Checksum'}");
   #print Dump \%conf;  
   #exit;      my $try = 0;
   return \%conf;      while (1) {
 }          if ($try >= $max_tries) {
               $l->sp("Couldn't update in $max_tries tries!");
 package Trango::Telnet;              return;
 use base 'Net::Telnet';          }
           $try++;
 my %PRIVATE = (  
   connected => 0,          $l->p("Enabling TFTPd");
   logged_in => 0,          unless ($t->enable_tftpd) {
 );              $l->sp("Couldn't enable tftpd");
               next;
 sub new {          }
   my $class = shift;  
   my $args = shift || {};          $l->p("Uploading file ($conf->{file_name})");
           # use tftp to push the file up
   $args->{'Timeout'} ||= 5;          my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
   $args->{'Prompt'}  ||= '/#> *$/';  
           unless ($tftp->put($file, $file)) {
   foreach my $key (keys %{ $args }) {              $l->sp("Error uploading: " . $tftp->error);
     $PRIVATE{$key} = $args->{$key};              next;
   }          }
   
   my $self = $class->SUPER::new(%{ $args });          $l->p("Checking upload ($conf->{'file_cksum'})");
   bless $self;          my $results = $t->tftpd;
           # check the 'File Length' against ???
   #bless $self, $package;          if (! (
   return $self;                  $results->{'File Checksum'} &&
 }                  $results->{'File Length'}   &&
                   $results->{'File Name'}
 sub connect              )) {
 {              $l->sp("Unable to get results of upload");
   my $self = shift;              next;
           }
   unless ( $self->open(          if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
       Host => $PRIVATE{'Host'},              $l->sp(
       Errmode => 'return',                  "File checksum (" . $results->{'File Checksum'} .
   ) ) {                  ") does not match config file (" . $conf->{'file_cksum'} . ")!"
     $! = "Couldn't connect to $self->{'Host'}.  Connection timed out.";              );
     return undef, undef;              next;
   }          }
   #$self->dump_log('dump.log');          $l->p("File checksum matches . . . ");
   
   ## Login to remote host.          if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
   unless ($self->waitfor(              $l->sp(
     -match => '/password: ?$/i',                  "File length (" . $results->{'File Length'} .
     -errmode => "return",                  ") does not match config file (" . $conf->{'file_size'} . " bytes)!"
   ) ) {              );
     $! = "problem connecting to host ($self->{'Host'}): " . $self->lastline;              next;
     return undef;          }
   }          $l->p("File length matches . . . ");
   
   $self->login_banner($self->lastline);          if ( uc($results->{'File Name'}) ne uc($file) ) {
               $l->sp(
   $PRIVATE{'connected'} = 1;                  "File name (" . $results->{'File Name'} .
                   ") does not match config file (" . $file . ")!"
   return ($self->host_type, $self->firmware_version);              );
 }              next;
           }
 sub login          $l->p("File name matches . . . ");
 {  
   my $self = shift;          my $image_type = 'mainimage';
           if ($fw_type eq 'FPGA') {
   my $password = shift;              $image_type = 'fpgaimage';
           }
   $self->print($password);          $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
   unless ($self->waitfor(          unless ($results = $t->updateflash(
     -match => $self->prompt,                  args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} .
     -errmode => "return",                  ' '          . $conf->{'cksum'},
   ) ) {                  Timeout => 90,
     $! = "login ($self->{'Host'}) failed: " . $self->lastline;              ) ) {
     return undef;              $l->sp("Couldn't update flash: $!");
   }              next;
           }
   $PRIVATE{'logged_in'} = 1;  
           unless (
   return 1;              defined $results->{'Checksum'} &&
 }              $results->{'Checksum'} eq $conf->{'cksum'}
           ) {
 sub login_banner              $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!");
 {              next;
   my $self = shift;          }
   
   my $banner = shift || $PRIVATE{'login_banner'};          $l->p("$fw_type saved checksum matches . . . ");
   
   my ($type, $ver) = $banner =~          return 1;
     /Welcome to Trango Broadband Wireless (.+)[\s-](\w+)$/i;      }
   }
   $self->host_type($type);  
   $self->firmware_version($ver);  sub parse_hosts
   {
   return $banner;      my $src = shift;
 }  
       my @hosts;
 sub host_type      foreach my $h (@{ $src }) {
 {          if ($h->{name} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {
   my $self = shift;              for ($2..$3) {
                   my %cur_host;
   my $type = shift || $PRIVATE{'host_type'};                  foreach my $k (keys %{ $h }) {
   $PRIVATE{'host_type'} = $type;                      $cur_host{$k} = $h->{$k};
                   }
   return $type;                  $cur_host{name} = $1 . $_;
 }                  if (! grep { $cur_host{name} eq $h->{name} } @hosts) {
                       push @hosts, \%cur_host;
 sub firmware_version                  }
 {              }
   my $self = shift;          } else {
               push @hosts, $h;
   my $ver = shift || $PRIVATE{'firmware_version'};          }
   $PRIVATE{'firmware_version'} = $ver;      }
     
   return $ver      return \@hosts;
 }  }
   
 sub Host  package Mylogger;
 {  
   my $self = shift;  use Fcntl ':flock'; # import LOCK_* constants
     #use YAML;
   my $host = shift || $PRIVATE{'Host'};  use constant LOG_PRINT => 128;
   $PRIVATE{'HOST'} = $host;  use constant LOG_SAVE  =>  64;
   
   return $host;  DESTROY {
 }      my $self = shift;
       if ($self->{'MYLOG'}) {
           $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
 sub reboot          close $self->{'MYLOG'};
 {      }
   my $self = shift;  }
   
   $self->print("reboot\n");  sub new {
   $self->getline;      my $package = shift;
       my $self = shift || {};
   return 1;  
 }      $self->{'base_path'}  ||= '.';
       $self->{'log_path'}   ||= $self->{'base_path'};
 sub exit      $self->{'log_prefix'} ||= 'LOG';
 {      $self->{'log_file'}   ||= GetLogName(
   my $self = shift;          $self->{'log_prefix'},
           $self->{'log_path'}
   $self->print("exit\n");      );
   $self->getline;      bless $self, $package;
   }
   return 1;  
 }  sub s
   {
 sub ver      my $self = shift;
 {      my $m = shift;
   my $self = shift;      return $self->mylog($m, LOG_SAVE);
   }
   return $self->cmd('ver');  
 }  sub p
   {
 sub tftpd      my $self = shift;
 {      my $m = shift;
   my $self = shift;      return $self->mylog($m, LOG_PRINT);
   }
   return $self->cmd('tftpd', 'Success.');  
 }  sub sp
   {
 sub sysinfo      my $self = shift;
 {      my $m = shift;
   my $self = shift;      return $self->mylog($m, LOG_SAVE | LOG_PRINT);
   }
   return $self->cmd('sysinfo', 'Success.');  
 }  sub mylog
   {
 sub enable_tftpd      my $self = shift;
 {  
   my $self = shift;      my $thing = shift;
       chomp $thing;
   my $vals = $self->cmd('tftpd on', 'Success.');  
       my $which = shift;
   if ($vals->{'Tftpd'} eq 'listen') {  
     return $vals;      my $MYLOG;
   } else {      if ($which & LOG_PRINT) {
     return undef;          print $thing, "\n";
   }      }
 }  
       if ($which & LOG_SAVE) {
 sub updateflash          if ($self->{'MYLOG'}) {
 {              $MYLOG = $self->{'MYLOG'};
   my $self = shift;          } else {
               unless ($MYLOG) {
   my $old = shift;                  open ($MYLOG, '>>', $self->{'log_path'} . '/' .
   my $new = shift;                      $self->{'log_file'}) or die "Couldn't open logfile!\n";
                   my $ofh = select $MYLOG;
   return undef unless $new;                  $|=1;
                   select $ofh;
   return $self->cmd("updateflash mainimage $old $new", 'Success.', 90);                  $self->{'MYLOG'} = $MYLOG;
 }  
                   $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");
 sub cmd              }
 {          }
   my $self = shift;          flock($MYLOG, LOCK_EX);
           print $MYLOG (scalar gmtime), "\t", $thing, "\n"
   my $string = shift;              or die "Couldn't print to MYLOG: $!";
   my $expect_last = shift;          flock($MYLOG, LOCK_UN);
   my $timeout = shift || $PRIVATE{'Timeout'};      }
   }
   unless (defined $string) {  
     $! = "No command passed";  sub GetLogName
     return undef;  {
   }      my $prefix  = shift || die "Invalid prefix passed for log";
   
   unless ($PRIVATE{'connected'}) {      my $logdate = GetLogDate();
     $! = "Not connected";      my $logver  = 0;
     return undef;      my $logname;
   }  
       do {
   unless ($PRIVATE{'logged_in'}) {          $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';
     $! = "Not logged in";          $logver++;
     return undef;      } until (not -e $logname);
   }  
       return $logname;
   my @lines = $self->SUPER::cmd(String => $string, Timeout => $timeout);  }
   
   my $vals = _decode_lines(@lines);  sub GetLogDate
   {
   unless ($expect_last) {      my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();
     return $vals;  
   }      $mon++;
       $year += 1900;
   my $last = $self->lastline;  
       if ($min  < 10) { $min  = "0$min"  }
   if ($last =~ /$expect_last$/) {      if ($sec  < 10) { $sec  = "0$sec"  }
     return $vals;      if ($hour < 10) { $hour = "0$hour" }
   } else {      if ($mday < 10) { $mday = "0$mday" }
     warn "Error with command ($string): $last";      if ($mon  < 10) { $mon  = "0$mon"  }
     return undef;  
   }      my $time = $year . $mon . $mday;
 }  
       return $time;
 sub _decode_lines  }
 {  
   my @lines = @_;  
   
   my %conf;  
   
   my $key = '';  
   my $val = '';  
   my $in_key = 0;  
   my $in_val = 0;  
   
   foreach my $line (@lines) {  
     my @chars = split //, $line;  
   
     my $last_key = '';  
     foreach my $c (@chars) {  
   
       if ($c eq '[' || $c eq "\r" || $c eq "\n") {  
         if ($c eq '[') {  
           $in_key = 1;  
           $in_val = 0;  
         } else {  
           $in_key = 0;  
           $in_val = 0;  
         }  
   
         if ($key) {  
           $key =~ s/^\s+//;  
           $key =~ s/\s+$//;  
   
           $val =~ s/^\s+//;  
           $val =~ s/\s+$//;  
   
           if ($key eq 'Checksum' && $last_key) {  
             # Special case for these bastids.  
             my $new = $last_key;  
             $new =~ s/\s+\S+$//;  
             $key = $new . " " . $key;  
           }  
   
           $last_key = $key;  
           $conf{$key} = $val;  
           $key = '';  
           $val = '';  
         }  
   
       } elsif ($c eq ']') {  
         $in_val = 1;  
         $in_key = 0;  
         $c = shift @chars;  
   
       } elsif ($in_key) {  
         $key .= $c;  
   
       } elsif ($in_val) {  
         $val .= $c;  
       }  
     }  
   }  
   #print Dump \%conf;  
   
   if (%conf) {  
     return \%conf;  
   } else {  
     return \@lines;  
   }  
 }  
   
   
   
 package Mylogger;  
   
 use Fcntl ':flock'; # import LOCK_* constants  
 #use YAML;  
 use constant LOG_PRINT => 128;  
 use constant LOG_SAVE  =>  64;  
   
 DESTROY {  
   my $self = shift;  
   if ($self->{'MYLOG'}) {  
     $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");  
     close $self->{'MYLOG'};  
   }  
 }  
   
 sub new {  
   my $package = shift;  
   my $self = shift || {};  
   
   $self->{'base_path'}  ||= '.';  
   $self->{'log_path'}   ||= $self->{'base_path'};  
   $self->{'log_prefix'} ||= 'LOG';  
   $self->{'log_file'}   ||= GetLogName(  
     $self->{'log_prefix'},  
     $self->{'log_path'}  
   );  
   bless $self, $package;  
 }  
   
 sub s  
 {  
   my $self = shift;  
   my $m = shift;  
   return $self->mylog($m, LOG_SAVE);  
 }  
   
 sub p  
 {  
   my $self = shift;  
   my $m = shift;  
   return $self->mylog($m, LOG_PRINT);  
 }  
   
 sub sp  
 {  
   my $self = shift;  
   my $m = shift;  
   return $self->mylog($m, LOG_SAVE | LOG_PRINT);  
 }  
   
 sub mylog  
 {  
   my $self = shift;  
   
   my $thing = shift;  
   chomp $thing;  
   
   my $which = shift;  
   
   my $MYLOG;  
   if ($which & LOG_PRINT) {  
     print $thing, "\n";  
   }  
   
   if ($which & LOG_SAVE) {  
     if ($self->{'MYLOG'}) {  
       $MYLOG = $self->{'MYLOG'};  
     } else {  
       unless ($MYLOG) {  
                 open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})  
           or die "Couldn't open logfile!\n";  
         my $ofh = select $MYLOG;  
         $|=1;  
         select $ofh;  
         $self->{'MYLOG'} = $MYLOG;  
   
         $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");  
       }  
     }  
     flock($MYLOG, LOCK_EX);  
     print $MYLOG (scalar gmtime), "\t", $thing, "\n"  
       or die "Couldn't print to MYLOG: $!";  
     flock($MYLOG, LOCK_UN);  
   }  
 }  
   
 sub GetLogName  
 {  
   my $prefix  = shift || die "Invalid prefix passed for log";  
   
   my $logdate = GetLogDate();  
   my $logver  = 0;  
   my $logname;  
   
   do {  
     $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';  
     $logver++;  
   } until (not -e $logname);  
   
   return $logname;  
 }  
   
 sub GetLogDate  
 {  
   my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();  
   
   $mon++;  
   $year += 1900;  
   
   if ($min  < 10) { $min  = "0$min"  }  
   if ($sec  < 10) { $sec  = "0$sec"  }  
   if ($hour < 10) { $hour = "0$hour" }  
   if ($mday < 10) { $mday = "0$mday" }  
   if ($mon  < 10) { $mon  = "0$mon"  }  
   
   my $time = $year . $mon . $mday;  
   
   return $time;  
 }  

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.32

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>