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

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

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