| version 1.9, 2005/11/18 19:17:12 |
version 1.24, 2007/02/02 16:29:58 |
|
|
| #!/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.23 2007/02/01 18:44:30 andrew Exp $ |
| ########################################################################
|
######################################################################## |
| # update_trango.pl *** Updates trango foxes with a new firmware
|
# update_trango.pl *** Updates trango foxes 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; |
|
|
|
|
|
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 = read_conf($config_file);
|
my $conf = LoadFile($config_file); |
|
|
|
| $l->sp(" Hardware Type: $conf->{'type'}");
|
my @foxes = ( |
| $l->sp(" File Name: $conf->{'file_name'}");
|
'10.100.1.2', |
| $l->sp(" File Size: $conf->{'file_size'}");
|
'10.100.2.2', |
| $l->sp(" File Checksum: $conf->{'file_cksum'}");
|
'10.100.3.2', |
| $l->sp(" FW Version: $conf->{'ver'}");
|
'10.100.3.3', |
| $l->sp(" FW Checksum: $conf->{'cksum'}");
|
'10.100.3.4', |
| $l->sp("");
|
'10.100.3.8', |
|
|
'10.100.4.2', |
|
|
'10.100.60.5', |
|
|
'10.100.80.5', |
|
|
'10.100.150.5', |
|
|
'10.100.5.2', |
|
|
'10.100.6.2', |
| foreach my $fox (@{ $conf->{'ips'} }) {
|
'10.100.7.2', |
| $l->sp("Updating: $fox");
|
'10.100.8.2', |
|
|
'10.100.9.2', |
| ## Connect and login.
|
'10.100.21.2', |
| my $t = new Trango::Telnet ({
|
'10.100.22.2', |
| Host => $fox,
|
'10.100.23.2', |
| Timeout => 5,
|
'10.100.32.2', |
| });
|
'10.100.33.2', |
|
|
'10.100.41.2', |
| $l->p("Connecting to $fox");
|
'10.100.51.2', |
| my ($type, $version) = $t->connect();
|
'10.100.52.2', |
|
|
'10.100.53.2', |
| unless (defined $type && defined $version) {
|
'10.100.54.2', |
| $l->sp("Error connecting!");
|
'10.100.61.2', |
| next;
|
'10.100.62.2', |
| }
|
); |
|
|
|
| if ($type ne $conf->{'type'}) {
|
foreach my $fox (@foxes) { |
| $l->sp("Wrong type of unit ('$type' should be '$conf->{'type'}')");
|
$l->sp("Checking: $fox"); |
| $t->close;
|
my $needs_reboot = 0; |
| next;
|
|
| }
|
## Connect and login. |
|
|
my $t = new Net::Telnet::Trango ( |
| if ($version eq $conf->{'ver'}) {
|
Timeout => 5, |
| $l->sp("Already up to date with firmware version '$version'");
|
Errmode => 'return', |
| $t->close;
|
) or die "Couldn't make new connection: $!"; |
| next;
|
$l->p("Connecting to $fox"); |
| }
|
unless ( $t->open($fox) ) { |
|
|
$l->sp("Error connecting: $!"); |
| $l->p("Logging in");
|
next; |
| $t->login($conf->{'password'}) || die "Couldn't login: $!";
|
} |
|
|
|
| $l->p("Sending commands");
|
foreach my $firmware_type ('Firmware', 'FPGA') { |
| ## Send commands
|
|
| if ( upload($t, $conf->{'file_name'}) ) {
|
if (! exists $conf->{$firmware_type}) { |
| $l->p("Rebooting");
|
$l->s("No configs for '$firmware_type'"); |
| $t->reboot;
|
$t->close(); |
| } else {
|
next; |
| $l->p("Exiting");
|
} |
| $t->exit;
|
|
| }
|
my $host_type = $t->host_type; |
| $t->close;
|
if ($firmware_type eq 'FPGA') { |
| $l->sp("");
|
$host_type =~ s/\s.*$//; |
| }
|
} |
|
|
|
| sub upload
|
if (! exists $conf->{$firmware_type}->{$host_type}) { |
| {
|
$l->sp("No '$firmware_type' config for type $host_type"); |
| my $t = shift;
|
$t->close(); |
| my $file = shift;
|
next; |
|
|
} |
| $l->p("Getting current version");
|
|
| my $ver = $t->ver;
|
if (! $t->logged_in) { |
|
|
$l->p("Logging in"); |
| if (
|
$t->login($conf->{general}->{'password'}); |
| $ver->{'Firmware Version'} eq $conf->{'ver'} &&
|
unless ($t->logged_in) { |
| $ver->{'Firmware Checksum'} eq $conf->{'cksum'}
|
$l->p('Failed!'); |
| ) {
|
$t->bye(); |
| $l->sp("Already updated!");
|
next; |
| return 1;
|
} |
| }
|
} |
|
|
|
| my $try = 0;
|
foreach my $k (keys %{ $conf->{general} }) { |
| while (1) {
|
$conf->{$firmware_type}->{$host_type}->{$k} ||= $conf->{general}->{$k}; |
| if ($try >= $max_tries) {
|
} |
| $l->sp("Couldn't update in $max_tries tries!");
|
$conf->{$firmware_type}->{$host_type}->{firmware_type} ||= $firmware_type; |
| return undef;
|
$conf->{$firmware_type}->{$host_type}->{type} = $host_type; |
| }
|
|
| $try++;
|
$l->sp("$host_type $firmware_type"); |
|
|
$l->p("Sending commands"); |
| #sysinfo($self->{'_host'});
|
## Send commands |
|
|
my $rc = upload($t, $conf->{$firmware_type}->{$host_type}); |
| $l->p("Enabling TFTPd");
|
if ($rc) { |
| $t->enable_tftpd || die "Couldn't enable tftpd";
|
$l->sp("Successfull!"); |
|
|
$needs_reboot = 1; |
| $l->p("Uploading file ($file)");
|
} elsif (defined $rc) { |
| # use tftp to push the file up
|
$l->sp("Already up to date"); |
| my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
|
} else { |
|
|
$l->sp("Failed"); |
| $tftp->put($file, $file)
|
$t->bye; |
| or die "Error uploading: " . $tftp->error;
|
next; |
|
|
} |
| # waitfor some sort of output
|
|
| # make sure it says 'Success.' otherwise error
|
} |
| #print "LAST: " . $self->lastline;
|
|
| #my @lines = $self->getlines;
|
if ($needs_reboot) { |
| #print Dump \@lines;
|
$l->sp("Rebooting $fox"); |
|
|
$t->reboot; |
| $l->p("Checking upload ($conf->{'file_cksum'})");
|
} else { |
| my $results = $t->tftpd;
|
$l->sp("Bye $fox"); |
| # check the 'File Length' against ???
|
$t->bye(); |
| if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
|
} |
| $l->sp("File checksum does not match config file!");
|
$l->sp(""); |
| next;
|
} |
| }
|
|
| $l->p("File checksum ($results->{'File Checksum'}) " .
|
sub upload |
| "matches ($conf->{'file_cksum'})");
|
{ |
|
|
my $t = shift; |
| if ($results->{'File Length'} !~ /^$conf->{'file_size'} bytes/) {
|
my $conf = shift; |
| $l->sp("File length does not match config file!");
|
|
| next;
|
my $file = $conf->{firmware_path} . '/' . $conf->{file_name}; |
| }
|
|
| $l->p("File length ($results->{'File Length'}) " .
|
my $fw_type = $conf->{firmware_type}; |
| "matches ($conf->{'file_size'})");
|
|
|
|
my $ver = $t->ver; |
| if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {
|
|
| $l->sp("File name does not match config file!");
|
if ( |
| next;
|
$ver->{$fw_type . ' Version'} eq $conf->{'ver'} && |
| }
|
$ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'} |
| $l->p("File name ($results->{'File Name'}) " .
|
) { |
| "matches ($conf->{'file_name'})");
|
return 0; |
|
|
} |
| $l->p("Updating flash (new checksum '$conf->{'cksum'}')");
|
|
| unless ($results = $t->updateflash(
|
$l->sp("Config information:"); |
| $ver->{'Firmware Checksum'}, $conf->{'cksum'}
|
$l->sp(" Hardware Type: $conf->{'type'}"); |
| ) ) {
|
$l->sp(" File Name: $conf->{'file_name'}"); |
| $l->sp("Couldn't update flash: $!");
|
$l->sp(" File Size: $conf->{'file_size'}"); |
| next;
|
$l->sp(" File Checksum: $conf->{'file_cksum'}"); |
| }
|
$l->sp(" Conf Version: $conf->{'ver'}"); |
|
|
$l->sp(" Cur Version: $ver->{$fw_type . ' Version'}"); |
| unless (
|
$l->sp(" Conf Checksum: $conf->{'cksum'}"); |
| defined $results->{'Checksum'} &&
|
$l->sp(" Cur Checksum: $ver->{$fw_type . ' Checksum'}"); |
| $results->{'Checksum'} eq $conf->{'cksum'}
|
|
| ) {
|
$l->sp("Updating"); |
| $l->sp("Saved checksum does not match config file!");
|
my $try = 0; |
| next;
|
while (1) { |
| }
|
if ($try >= $max_tries) { |
| $l->p("Uploaded checksum ($results->{'Checksum'}) " .
|
$l->sp("Couldn't update in $max_tries tries!"); |
| "matches ($conf->{'cksum'})");
|
return; |
|
|
} |
| $l->sp("Successfully updated!");
|
$try++; |
| return 1;
|
|
| }
|
#sysinfo($self->{'_host'}); |
| }
|
|
|
|
$l->p("Enabling TFTPd"); |
| sub read_conf
|
unless ($t->enable_tftpd) { |
| {
|
$l->sp("Couldn't enable tftpd"); |
| my $file = shift;
|
next; |
| my %conf;
|
} |
| my $in_ip_list = 0;
|
|
| open my $fh, $file or die "Couldn't open file $file: $!";
|
$l->p("Uploading file ($file)"); |
| while (<$fh>) {
|
# use tftp to push the file up |
| chomp;
|
my $tftp = Net::TFTP->new($t->Host, Mode => 'octet'); |
| next if /^#/;
|
|
| next if /^$/;
|
unless ($tftp->put($file, $file)) { |
| if ($in_ip_list) {
|
$l->sp("Error uploading: " . $tftp->error); |
| if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {
|
next; |
| push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);
|
} |
| } else {
|
|
| push @{ $conf{'ips'} }, $_;
|
$l->p("Checking upload ($conf->{'file_cksum'})"); |
| }
|
my $results = $t->tftpd; |
| } else {
|
# check the 'File Length' against ??? |
| my ($key, $val) = split /\s+/, $_, 2;
|
if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) { |
|
|
$l->sp( |
| if (lc($key) eq 'ips') {
|
"File checksum '" . $results->{'File Checksum'} . |
| $in_ip_list = 1;
|
" does not match config file '" . $conf->{'file_cksum'} . "'!" |
| next;
|
); |
| }
|
next; |
|
|
} |
| $conf{ lc($key) } = $val;
|
$l->p("File checksum matches . . . "); |
| }
|
|
| }
|
if ($results->{'File Length'} !~ /^$conf->{'file_size'} bytes/) { |
| close $fh;
|
$l->sp( |
|
|
"File length '" . $results->{'File Length'} . |
| #print Dump \%conf;
|
"does not match config file '" . $conf->{'file_size'} . " bytes'!" |
| foreach (
|
); |
| 'password',
|
next; |
| 'file_name', 'file_size', 'file_cksum',
|
} |
| 'ver', 'cksum', 'ips',
|
$l->p("File length matches . . . "); |
| ) {
|
|
| die "No $_ specified in config file!"
|
if ( uc($results->{'File Name'}) ne uc($file) ) { |
| if (not exists $conf{$_});
|
$l->sp( |
| }
|
"File name '" . $results->{'File Name'} . |
|
|
"' does not match config file '" . $file . "'!" |
| #print Dump \%conf;
|
); |
| #exit;
|
next; |
| return \%conf;
|
} |
| }
|
$l->p("File name matches . . . "); |
|
|
|
| package Trango::Telnet;
|
my $image_type = 'mainimage'; |
| use base 'Net::Telnet';
|
if ($fw_type eq 'FPGA') { |
|
|
$image_type = 'fpgaimage'; |
| my %PRIVATE = (
|
} |
| connected => 0,
|
$l->p("Updating $image_type (new checksum '$conf->{'cksum'}')"); |
| logged_in => 0,
|
unless ($results = $t->updateflash( |
| );
|
args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} . |
|
|
' ' . $conf->{'cksum'}, |
| sub new {
|
Timeout => 90, |
| my $class = shift;
|
) ) { |
| my $args = shift || {};
|
$l->sp("Couldn't update flash: $!"); |
|
|
next; |
| $args->{'Timeout'} ||= 5;
|
} |
| $args->{'Prompt'} ||= '/#> *$/';
|
|
|
|
unless ( |
| foreach my $key (keys %{ $args }) {
|
defined $results->{'Checksum'} && |
| $PRIVATE{$key} = $args->{$key};
|
$results->{'Checksum'} eq $conf->{'cksum'} |
| }
|
) { |
|
|
$l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " . $conf->{'cksum'} . "!"); |
| my $self = $class->SUPER::new(%{ $args });
|
next; |
| bless $self;
|
} |
|
|
$l->p("Uploaded checksum ($results->{'Checksum'}) " . |
| #bless $self, $package;
|
"matches ($conf->{'cksum'})"); |
| return $self;
|
|
| }
|
$l->sp("Successfully updated!"); |
|
|
return 1; |
| sub connect
|
} |
| {
|
} |
| my $self = shift;
|
|
|
|
package Mylogger; |
| unless ( $self->open(
|
|
| Host => $PRIVATE{'Host'},
|
use Fcntl ':flock'; # import LOCK_* constants |
| Errmode => 'return',
|
#use YAML; |
| ) ) {
|
use constant LOG_PRINT => 128; |
| $! = "Couldn't connect to $self->{'Host'}. Connection timed out.";
|
use constant LOG_SAVE => 64; |
| return undef, undef;
|
|
| }
|
DESTROY { |
| #$self->dump_log('dump.log');
|
my $self = shift; |
|
|
if ($self->{'MYLOG'}) { |
| ## Login to remote host.
|
$self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})"); |
| unless ($self->waitfor(
|
close $self->{'MYLOG'}; |
| -match => '/password: ?$/i',
|
} |
| -errmode => "return",
|
} |
| ) ) {
|
|
| $! = "problem connecting to host ($self->{'Host'}): " . $self->lastline;
|
sub new { |
| return undef;
|
my $package = shift; |
| }
|
my $self = shift || {}; |
|
|
|
| $self->login_banner($self->lastline);
|
$self->{'base_path'} ||= '.'; |
|
|
$self->{'log_path'} ||= $self->{'base_path'}; |
| $PRIVATE{'connected'} = 1;
|
$self->{'log_prefix'} ||= 'LOG'; |
|
|
$self->{'log_file'} ||= GetLogName( |
| return ($self->host_type, $self->firmware_version);
|
$self->{'log_prefix'}, |
| }
|
$self->{'log_path'} |
|
|
); |
| sub login
|
bless $self, $package; |
| {
|
} |
| my $self = shift;
|
|
|
|
sub s |
| my $password = shift;
|
{ |
|
|
my $self = shift; |
| $self->print($password);
|
my $m = shift; |
| unless ($self->waitfor(
|
return $self->mylog($m, LOG_SAVE); |
| -match => $self->prompt,
|
} |
| -errmode => "return",
|
|
| ) ) {
|
sub p |
| $! = "login ($self->{'Host'}) failed: " . $self->lastline;
|
{ |
| return undef;
|
my $self = shift; |
| }
|
my $m = shift; |
|
|
return $self->mylog($m, LOG_PRINT); |
| $PRIVATE{'logged_in'} = 1;
|
} |
|
|
|
| return 1;
|
sub sp |
| }
|
{ |
|
|
my $self = shift; |
| sub login_banner
|
my $m = shift; |
| {
|
return $self->mylog($m, LOG_SAVE | LOG_PRINT); |
| my $self = shift;
|
} |
|
|
|
| my $banner = shift || $PRIVATE{'login_banner'};
|
sub mylog |
|
|
{ |
| my ($type, $ver) = $banner =~
|
my $self = shift; |
| /Welcome to Trango Broadband Wireless (\w+)-(.+)$/i;
|
|
|
|
my $thing = shift; |
| $self->host_type($type);
|
chomp $thing; |
| $self->firmware_version($ver);
|
|
|
|
my $which = shift; |
| return $banner;
|
|
| }
|
my $MYLOG; |
|
|
if ($which & LOG_PRINT) { |
| sub host_type
|
print $thing, "\n"; |
| {
|
} |
| my $self = shift;
|
|
|
|
if ($which & LOG_SAVE) { |
| my $type = shift || $PRIVATE{'host_type'};
|
if ($self->{'MYLOG'}) { |
| $PRIVATE{'host_type'} = $type;
|
$MYLOG = $self->{'MYLOG'}; |
|
|
} else { |
| return $type;
|
unless ($MYLOG) { |
| }
|
open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'}) |
|
|
or die "Couldn't open logfile!\n"; |
| sub firmware_version
|
my $ofh = select $MYLOG; |
| {
|
$|=1; |
| my $self = shift;
|
select $ofh; |
|
|
$self->{'MYLOG'} = $MYLOG; |
| my $ver = shift || $PRIVATE{'firmware_version'};
|
|
| $PRIVATE{'firmware_version'} = $ver;
|
$self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})"); |
|
|
} |
| return $ver
|
} |
| }
|
flock($MYLOG, LOCK_EX); |
|
|
print $MYLOG (scalar gmtime), "\t", $thing, "\n" |
| sub Host
|
or die "Couldn't print to MYLOG: $!"; |
| {
|
flock($MYLOG, LOCK_UN); |
| my $self = shift;
|
} |
|
|
} |
| my $host = shift || $PRIVATE{'Host'};
|
|
| $PRIVATE{'HOST'} = $host;
|
sub GetLogName |
|
|
{ |
| return $host;
|
my $prefix = shift || die "Invalid prefix passed for log"; |
| }
|
|
|
|
my $logdate = GetLogDate(); |
|
|
my $logver = 0; |
| sub reboot
|
my $logname; |
| {
|
|
| my $self = shift;
|
do { |
|
|
$logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log'; |
| $self->print("reboot\n");
|
$logver++; |
| $self->getline;
|
} until (not -e $logname); |
|
|
|
| return 1;
|
return $logname; |
| }
|
} |
|
|
|
| sub exit
|
sub GetLogDate |
| {
|
{ |
| my $self = shift;
|
my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime(); |
|
|
|
| $self->print("exit\n");
|
$mon++; |
| $self->getline;
|
$year += 1900; |
|
|
|
| return 1;
|
if ($min < 10) { $min = "0$min" } |
| }
|
if ($sec < 10) { $sec = "0$sec" } |
|
|
if ($hour < 10) { $hour = "0$hour" } |
| sub ver
|
if ($mday < 10) { $mday = "0$mday" } |
| {
|
if ($mon < 10) { $mon = "0$mon" } |
| my $self = shift;
|
|
|
|
my $time = $year . $mon . $mday; |
| return $self->cmd('ver');
|
|
| }
|
return $time; |
|
|
} |
| sub tftpd
|
|
| {
|
|
| my $self = shift;
|
|
|
|
|
| return $self->cmd('tftpd', 'Success.');
|
|
| }
|
|
|
|
|
| sub sysinfo
|
|
| {
|
|
| my $self = shift;
|
|
|
|
|
| return $self->cmd('sysinfo', 'Success.');
|
|
| }
|
|
|
|
|
| sub enable_tftpd
|
|
| {
|
|
| my $self = shift;
|
|
|
|
|
| my $vals = $self->cmd('tftpd on', 'Success.');
|
|
|
|
|
| if ($vals->{'Tftpd'} eq 'listen') {
|
|
| return $vals;
|
|
| } else {
|
|
| return undef;
|
|
| }
|
|
| }
|
|
|
|
|
| sub updateflash
|
|
| {
|
|
| my $self = shift;
|
|
|
|
|
| my $old = shift;
|
|
| my $new = shift;
|
|
|
|
|
| return undef unless $new;
|
|
|
|
|
| return $self->cmd("updateflash mainimage $old $new", 'Success.', 90);
|
|
| }
|
|
|
|
|
| sub cmd
|
|
| {
|
|
| my $self = shift;
|
|
|
|
|
| my $string = shift;
|
|
| my $expect_last = shift;
|
|
| my $timeout = shift || $PRIVATE{'Timeout'};
|
|
|
|
|
| unless (defined $string) {
|
|
| $! = "No command passed";
|
|
| return undef;
|
|
| }
|
|
|
|
|
| unless ($PRIVATE{'connected'}) {
|
|
| $! = "Not connected";
|
|
| return undef;
|
|
| }
|
|
|
|
|
| unless ($PRIVATE{'logged_in'}) {
|
|
| $! = "Not logged in";
|
|
| return undef;
|
|
| }
|
|
|
|
|
| my @lines = $self->SUPER::cmd(String => $string, Timeout => $timeout);
|
|
|
|
|
| my $vals = _decode_lines(@lines);
|
|
|
|
|
| unless ($expect_last) {
|
|
| return $vals;
|
|
| }
|
|
|
|
|
| my $last = $self->lastline;
|
|
|
|
|
| if ($last =~ /$expect_last$/) {
|
|
| return $vals;
|
|
| } else {
|
|
| 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;
|
|
| }
|
|