[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.9 and 1.34

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

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