[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.34

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

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