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

version 1.9, 2005/11/18 19:17:12 version 1.28, 2007/02/02 22:37:24
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.27 2007/02/02 21:36:00 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;  use strict;
 use warnings;  use warnings;
   
 use Net::TFTP;  use YAML qw/ LoadFile /;
   use Net::TFTP;
 my $config_file = shift || 'update_trango.conf';  use lib '.';
 my $max_tries = 3;  use Net::Telnet::Trango;
   use RedRiver::Wireless;
   
   my $config_file = shift || 'update_trango.yaml';
 my $l = Mylogger->new( { log_prefix => 'UT' } );  my $max_tries = 3;
   
   my $l = Mylogger->new( { log_prefix => 'UT' } );
 $l->sp("Reading config file '$config_file'");  
 my $conf = read_conf($config_file);  $l->sp("Reading config file '$config_file'");
   my $conf = LoadFile($config_file);
 $l->sp("  Hardware Type: $conf->{'type'}");  
 $l->sp("  File Name:     $conf->{'file_name'}");  my $hosts;
 $l->sp("  File Size:     $conf->{'file_size'}");  if (@ARGV) {
 $l->sp("  File Checksum: $conf->{'file_cksum'}");    @{ $hosts } = map { { name => $_, group => 'Trango-Client' } } @ARGV
 $l->sp("  FW Version:    $conf->{'ver'}");  } else {
 $l->sp("  FW Checksum:   $conf->{'cksum'}");    $l->sp("Reading hosts");
 $l->sp("");    $hosts = RedRiver::Wireless::Read_Hosts();
   }
   
   #@{ $hosts } = grep { $_->{name} eq '10.100.7.2' } @{ $hosts };
   
   my $global_tries = $max_tries * 2;
   while ($global_tries > 0) {
 foreach my $fox (@{ $conf->{'ips'} }) {    $global_tries--;
   $l->sp("Updating: $fox");    my $processed = 0;
   
   ## Connect and login.  
   my $t = new Trango::Telnet ({  foreach my $host (@{ $hosts }) {
     Host    => $fox,    next if $host->{group} !~ /^Trango/;
     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;  
   }    # XXX I am not sure this is the best way to check if we should look for
   $t->close;    # XXX associated stations.
   $l->sp("");    if ($host->{group} !~ /Client$/) {
 }      $l->p("Logging in");
       $t->login($password);
 sub upload      unless ($t->logged_in) {
 {        $l->p('Failed!');
   my $t    = shift;        $t->close;
   my $file = shift;        next;
       }
   $l->p("Getting current version");  
   my $ver = $t->ver;      $l->sp("Getting sudb");
       my $sudb = $t->sudb_view;
   if (      if ($sudb) {
     $ver->{'Firmware Version'}  eq $conf->{'ver'} &&        foreach my $su (@{ $sudb }) {
     $ver->{'Firmware Checksum'} eq $conf->{'cksum'}          $l->p("Getting su info $su->{suid}");
   ) {          my $su_info = $t->su_info( $su->{suid} );
     $l->sp("Already updated!");          if ($su_info->{ip}) {
     return 1;            if (grep { $_->{name} eq $su_info->{'ip'} } @{ $hosts }) {
   }              $l->p("Already have $su_info->{ip}");
               next;
   my $try = 0;            }
   while (1) {            $l->sp("Adding host $su_info->{ip}");
     if ($try >= $max_tries) {            my $new_host = {
       $l->sp("Couldn't update in $max_tries tries!");              Telnet_Password => $host->{Telnet_Password},
       return undef;              group   => $host->{group} . '-Client',
     }              name    => $su_info->{ip},
     $try++;              remarks => $su_info->{remarks},
             };
     #sysinfo($self->{'_host'});            push @{ $hosts }, $new_host;
               } else {
     $l->p("Enabling TFTPd");            $l->sp("Couldn't get su info for $su->{suid}");
     $t->enable_tftpd || die "Couldn't enable tftpd";            if ($su_info->{ERR}) {
               $l->sp("ERR: $su_info->{ERR}");
     $l->p("Uploading file ($file)");            }
     # use tftp to push the file up          }
     my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');        }
       }
     $tftp->put($file, $file)    }
       or die "Error uploading: " . $tftp->error;  
     foreach my $firmware_type ('Firmware', 'FPGA') {
     # waitfor some sort of output  
     # make sure it says 'Success.' otherwise error      if (! exists $conf->{$firmware_type}) {
     #print "LAST: " . $self->lastline;        $l->s("No configs for '$firmware_type'");
     #my @lines = $self->getlines;        $t->close;
     #print Dump \@lines;        next;
           }
     $l->p("Checking upload ($conf->{'file_cksum'})");  
     my $results = $t->tftpd;      my $host_type = $t->host_type;
     # check the 'File Length' against ???      if ($firmware_type eq 'FPGA') {
     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {        $host_type =~ s/\s.*$//;
       $l->sp("File checksum does not match config file!");      }
       next;  
     }      if (! exists $conf->{$firmware_type}->{$host_type}) {
     $l->p("File checksum ($results->{'File Checksum'}) " .        $l->sp("No '$firmware_type' config for type $host_type");
           "matches ($conf->{'file_cksum'})");        $t->close;
         next;
     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {      }
       $l->sp("File length does not match config file!");  
       next;      if ($firmware_type eq 'Firmware' &&
     }        $t->firmware_version eq
     $l->p("File length ($results->{'File Length'}) " .        $conf->{$firmware_type}->{$host_type}->{ver}
           "matches ($conf->{'file_size'})");      ) {
         $l->sp("Firmware already up to date");
     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {        next;
       $l->sp("File name does not match config file!");      }
       next;  
     }      if (! $t->logged_in) {
     $l->p("File name ($results->{'File Name'}) " .        $l->p("Logging in");
           "matches ($conf->{'file_name'})");        $t->login($password);
         unless ($t->logged_in) {
     $l->p("Updating flash (new checksum '$conf->{'cksum'}')");          $l->p('Failed!');
     unless ($results = $t->updateflash(          $t->close;
       $ver->{'Firmware Checksum'}, $conf->{'cksum'}          next;
     ) ) {        }
       $l->sp("Couldn't update flash: $!");      }
       next;  
     }      foreach my $k (keys %{ $conf->{general} }) {
         $conf->{$firmware_type}->{$host_type}->{$k} ||= $conf->{general}->{$k};
     unless (      }
       defined $results->{'Checksum'} &&      $conf->{$firmware_type}->{$host_type}->{firmware_type} ||= $firmware_type;
       $results->{'Checksum'} eq $conf->{'cksum'}      $conf->{$firmware_type}->{$host_type}->{type} = $host_type;
     ) {  
       $l->sp("Saved checksum does not match config file!");      $l->sp("$host_type $firmware_type");
       next;      ## Send commands
     }      my $rc = upload($t, $conf->{$firmware_type}->{$host_type});
     $l->p("Uploaded checksum ($results->{'Checksum'}) " .      if ($rc) {
           "matches ($conf->{'cksum'})");        $l->sp("Successfull!");
             $host->{retry}--;
     $l->sp("Successfully updated!");        $needs_reboot = 1;
     return 1;      } elsif (defined $rc) {
   }        $l->sp("Already up to date");
 }        $host->{retry}--;
       } else {
 sub read_conf        $l->sp("Failed");
 {        $t->bye;
   my $file = shift;        next;
   my %conf;      }
   my $in_ip_list = 0;  
   open my $fh, $file or die "Couldn't open file $file: $!";    }
   while (<$fh>) {  
     chomp;    if ($needs_reboot) {
     next if /^#/;      $l->sp("Rebooting $host->{name}");
     next if /^$/;      $t->reboot;
     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;  
   foreach (    my $fw_type = $conf->{firmware_type};
     'password',  
     'file_name', 'file_size', 'file_cksum',    my $ver = $t->ver;
     'ver', 'cksum', 'ips',  
   ) {    if (! (
     die "No $_ specified in config file!"      $ver->{$fw_type . ' Version'}  &&
       if (not exists $conf{$_});      $ver->{$fw_type . ' Checksum'}
   }    )) {
       $l->sp("Error getting current version numbers");
   #print Dump \%conf;      return;
   #exit;    }
   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'},      # use tftp to push the file up
       Errmode => 'return',      my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
   ) ) {  
     $! = "Couldn't connect to $self->{'Host'}.  Connection timed out.";      unless ($tftp->put($file, $file)) {
     return undef, undef;        $l->sp("Error uploading: " . $tftp->error);
   }        next;
   #$self->dump_log('dump.log');      }
   
   ## Login to remote host.      $l->p("Checking upload ($conf->{'file_cksum'})");
   unless ($self->waitfor(      my $results = $t->tftpd;
     -match => '/password: ?$/i',      # check the 'File Length' against ???
     -errmode => "return",      if (! (
   ) ) {        $results->{'File Checksum'} &&
     $! = "problem connecting to host ($self->{'Host'}): " . $self->lastline;        $results->{'File Length'}   &&
     return undef;        $results->{'File Name'}
   }      )) {
         $l->sp("Unable to get results of upload");
   $self->login_banner($self->lastline);        next;
       }
   $PRIVATE{'connected'} = 1;      if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
         $l->sp(
   return ($self->host_type, $self->firmware_version);          "File checksum (" . $results->{'File Checksum'} .
 }          ") does not match config file (" . $conf->{'file_cksum'} . ")!"
         );
 sub login        next;
 {      }
   my $self = shift;      $l->p("File checksum matches . . . ");
   
   my $password = shift;      if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
         $l->sp(
   $self->print($password);          "File length (" . $results->{'File Length'} .
   unless ($self->waitfor(          ") does not match config file (" . $conf->{'file_size'} . " bytes)!"
     -match => $self->prompt,        );
     -errmode => "return",        next;
   ) ) {      }
     $! = "login ($self->{'Host'}) failed: " . $self->lastline;      $l->p("File length matches . . . ");
     return undef;  
   }      if ( uc($results->{'File Name'}) ne uc($file) ) {
         $l->sp(
   $PRIVATE{'logged_in'} = 1;          "File name (" . $results->{'File Name'} .
           ") does not match config file (" . $file . ")!"
   return 1;        );
 }        next;
       }
 sub login_banner      $l->p("File name matches . . . ");
 {  
   my $self = shift;      my $image_type = 'mainimage';
       if ($fw_type eq 'FPGA') {
   my $banner = shift || $PRIVATE{'login_banner'};        $image_type = 'fpgaimage';
       }
   my ($type, $ver) = $banner =~      $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
     /Welcome to Trango Broadband Wireless (\w+)-(.+)$/i;      unless ($results = $t->updateflash(
         args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} .
   $self->host_type($type);                ' '          . $conf->{'cksum'},
   $self->firmware_version($ver);        Timeout => 90,
       ) ) {
   return $banner;        $l->sp("Couldn't update flash: $!");
 }        next;
       }
 sub host_type  
 {      unless (
   my $self = shift;        defined $results->{'Checksum'} &&
         $results->{'Checksum'} eq $conf->{'cksum'}
   my $type = shift || $PRIVATE{'host_type'};      ) {
   $PRIVATE{'host_type'} = $type;        $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!");
         next;
   return $type;      }
 }  
       $l->p("$fw_type saved checksum matches . . . ");
 sub firmware_version  
 {      return 1;
   my $self = shift;    }
   }
   my $ver = shift || $PRIVATE{'firmware_version'};  
   $PRIVATE{'firmware_version'} = $ver;  package Mylogger;
     
   return $ver  use Fcntl ':flock'; # import LOCK_* constants
 }  #use YAML;
   use constant LOG_PRINT => 128;
 sub Host  use constant LOG_SAVE  =>  64;
 {  
   my $self = shift;  DESTROY {
       my $self = shift;
   my $host = shift || $PRIVATE{'Host'};    if ($self->{'MYLOG'}) {
   $PRIVATE{'HOST'} = $host;      $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
       close $self->{'MYLOG'};
   return $host;    }
 }  }
   
   sub new {
 sub reboot    my $package = shift;
 {    my $self = shift || {};
   my $self = shift;  
     $self->{'base_path'}  ||= '.';
   $self->print("reboot\n");    $self->{'log_path'}   ||= $self->{'base_path'};
   $self->getline;    $self->{'log_prefix'} ||= 'LOG';
     $self->{'log_file'}   ||= GetLogName(
   return 1;      $self->{'log_prefix'},
 }      $self->{'log_path'}
     );
 sub exit    bless $self, $package;
 {  }
   my $self = shift;  
   sub s
   $self->print("exit\n");  {
   $self->getline;    my $self = shift;
     my $m = shift;
   return 1;    return $self->mylog($m, LOG_SAVE);
 }  }
   
 sub ver  sub p
 {  {
   my $self = shift;    my $self = shift;
     my $m = shift;
   return $self->cmd('ver');    return $self->mylog($m, LOG_PRINT);
 }  }
   
 sub tftpd  sub sp
 {  {
   my $self = shift;    my $self = shift;
     my $m = shift;
   return $self->cmd('tftpd', 'Success.');    return $self->mylog($m, LOG_SAVE | LOG_PRINT);
 }  }
   
 sub sysinfo  sub mylog
 {  {
   my $self = shift;    my $self = shift;
   
   return $self->cmd('sysinfo', 'Success.');    my $thing = shift;
 }    chomp $thing;
   
 sub enable_tftpd    my $which = shift;
 {  
   my $self = shift;    my $MYLOG;
     if ($which & LOG_PRINT) {
   my $vals = $self->cmd('tftpd on', 'Success.');      print $thing, "\n";
     }
   if ($vals->{'Tftpd'} eq 'listen') {  
     return $vals;    if ($which & LOG_SAVE) {
   } else {      if ($self->{'MYLOG'}) {
     return undef;        $MYLOG = $self->{'MYLOG'};
   }      } else {
 }        unless ($MYLOG) {
           open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})
 sub updateflash            or die "Couldn't open logfile!\n";
 {          my $ofh = select $MYLOG;
   my $self = shift;          $|=1;
           select $ofh;
   my $old = shift;          $self->{'MYLOG'} = $MYLOG;
   my $new = shift;  
           $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");
   return undef unless $new;        }
       }
   return $self->cmd("updateflash mainimage $old $new", 'Success.', 90);      flock($MYLOG, LOCK_EX);
 }      print $MYLOG (scalar gmtime), "\t", $thing, "\n"
         or die "Couldn't print to MYLOG: $!";
 sub cmd      flock($MYLOG, LOCK_UN);
 {    }
   my $self = shift;  }
   
   my $string = shift;  sub GetLogName
   my $expect_last = shift;  {
   my $timeout = shift || $PRIVATE{'Timeout'};    my $prefix  = shift || die "Invalid prefix passed for log";
   
   unless (defined $string) {    my $logdate = GetLogDate();
     $! = "No command passed";    my $logver  = 0;
     return undef;    my $logname;
   }  
     do {
   unless ($PRIVATE{'connected'}) {      $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';
     $! = "Not connected";      $logver++;
     return undef;    } until (not -e $logname);
   }  
     return $logname;
   unless ($PRIVATE{'logged_in'}) {  }
     $! = "Not logged in";  
     return undef;  sub GetLogDate
   }  {
     my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();
   my @lines = $self->SUPER::cmd(String => $string, Timeout => $timeout);  
     $mon++;
   my $vals = _decode_lines(@lines);    $year += 1900;
   
   unless ($expect_last) {    if ($min  < 10) { $min  = "0$min"  }
     return $vals;    if ($sec  < 10) { $sec  = "0$sec"  }
   }    if ($hour < 10) { $hour = "0$hour" }
     if ($mday < 10) { $mday = "0$mday" }
   my $last = $self->lastline;    if ($mon  < 10) { $mon  = "0$mon"  }
   
   if ($last =~ /$expect_last$/) {    my $time = $year . $mon . $mday;
     return $vals;  
   } else {    return $time;
     warn "Error with command ($string): $last";  }
     return undef;  
   }  
 }  
   
 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.9  
changed lines
  Added in v.1.28

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