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