U
Unknown
Hello All,
I am working on this script which will parse several Backup Exec logs
and place key details into a database for viewing as statistics
on a web based front end. I posted all the code, but I am only having
trouble with the while loop (inside the foreach). It is not behaving the
way I expect it to in that it prints several copies of each record
instea of just one record for one text file.
thanks
Rocky
#!/usr/bin/perl
use warnings;
use strict;
use DBI;
use DBD:g;
my $filedir = '/var/backuplogs';
my $servername;
my $jobname;
my $jobstarted;
my $medialabel;
my $logfile;
my $jobended;
my $status;
my @inuse;
my @skipped;
my @corrupt;
my @skippednames;
my $skipped;
my $corrupt;
my $inuse;
opendir(DIR, $filedir) or die "could not open logfile directory: $!";
my @filestoparse = grep { /\.txt$/ } readdir(DIR);
closedir(DIR);
foreach my $filename (@filestoparse) {
my $complete = "$filedir/" . "$filename";
open(FH, "$complete") or die "Cannot find file $complete: $!";
while (<FH>) {
if ($_ =~ /^Job\sserver:\s+(.*)$/) { $servername = $1;}
if ($_ =~ /^Job\sname:\s+(.*)$/) { $jobname = $1;}
if ($_ =~ /^Job\sstarted:\s+(.*)$/) { $jobstarted = $1;}
if ($_ =~ /^Media.*Label:\s+(.*)$/) { $medialabel = $1;}
if ($_ =~ /^Job\sended:\s+(.*)$/) { $jobended = $1;}
if ($_ =~ /^Job\scompletion\sstatus:\s+(.*)$/) { $status = $1;}
if ($_ =~ /^(\d+)\s+files\swere\sin.*$/) {push(@inuse, $1);}
if ($_ =~ /^(\d+)\s+items/) {push(@skipped, $1);}
if ($_ =~ /^(\d+)\s+corrupt/) {push(@corrupt, $1);}
if ($_ =~ /^.*item(.*\s-\sskipped.*)$/) {push(@skippednames, $1);}
$corrupt = &addnums(@corrupt);
$skipped = &addnums(@skipped);
$inuse = &addnums(@inuse);
$logfile = $filename;
}
close(FH);
print "$jobname,$jobstarted,$logfile,$servername\n";
my $dbh = DBI->connect( "dbig:dbname=backup", "[snip]", "[snip]") or die
"Cannot connect to PGSQL DB\n"; my $sth = $dbh->prepare("INSERT INTO
backuplogs(jobname,jobstarted,logfile,servername,status,inuse,jobended,skipped,corrupt,medialabel,testlogfile)
VALUES
('$jobname','$jobstarted','$logfile','$servername','$status','$inuse','$jobended','$skipped','$corrupt','$medialabel','<a
href=http://[snip]/oldlogs/$logfile>$logfile</a>')")
or die "Error preparing row: $DBI::errstr\n"; $sth->execute() or die "
$DBI::errstr\n"; $dbh->disconnect;
$servername = "";
$jobname = "";
$jobstarted = "";
$medialabel = "";
$jobended = "";
$status = "";
}
sub addnums {
my @number = @_;
my $total;
my $digit;
foreach $digit (@number) {
$total += $digit;
}
if (defined($total)) {
$total = $total;
}
else
{$total = 0;}
return $total;
}
I am working on this script which will parse several Backup Exec logs
and place key details into a database for viewing as statistics
on a web based front end. I posted all the code, but I am only having
trouble with the while loop (inside the foreach). It is not behaving the
way I expect it to in that it prints several copies of each record
instea of just one record for one text file.
thanks
Rocky
#!/usr/bin/perl
use warnings;
use strict;
use DBI;
use DBD:g;
my $filedir = '/var/backuplogs';
my $servername;
my $jobname;
my $jobstarted;
my $medialabel;
my $logfile;
my $jobended;
my $status;
my @inuse;
my @skipped;
my @corrupt;
my @skippednames;
my $skipped;
my $corrupt;
my $inuse;
opendir(DIR, $filedir) or die "could not open logfile directory: $!";
my @filestoparse = grep { /\.txt$/ } readdir(DIR);
closedir(DIR);
foreach my $filename (@filestoparse) {
my $complete = "$filedir/" . "$filename";
open(FH, "$complete") or die "Cannot find file $complete: $!";
while (<FH>) {
if ($_ =~ /^Job\sserver:\s+(.*)$/) { $servername = $1;}
if ($_ =~ /^Job\sname:\s+(.*)$/) { $jobname = $1;}
if ($_ =~ /^Job\sstarted:\s+(.*)$/) { $jobstarted = $1;}
if ($_ =~ /^Media.*Label:\s+(.*)$/) { $medialabel = $1;}
if ($_ =~ /^Job\sended:\s+(.*)$/) { $jobended = $1;}
if ($_ =~ /^Job\scompletion\sstatus:\s+(.*)$/) { $status = $1;}
if ($_ =~ /^(\d+)\s+files\swere\sin.*$/) {push(@inuse, $1);}
if ($_ =~ /^(\d+)\s+items/) {push(@skipped, $1);}
if ($_ =~ /^(\d+)\s+corrupt/) {push(@corrupt, $1);}
if ($_ =~ /^.*item(.*\s-\sskipped.*)$/) {push(@skippednames, $1);}
$corrupt = &addnums(@corrupt);
$skipped = &addnums(@skipped);
$inuse = &addnums(@inuse);
$logfile = $filename;
}
close(FH);
print "$jobname,$jobstarted,$logfile,$servername\n";
my $dbh = DBI->connect( "dbig:dbname=backup", "[snip]", "[snip]") or die
"Cannot connect to PGSQL DB\n"; my $sth = $dbh->prepare("INSERT INTO
backuplogs(jobname,jobstarted,logfile,servername,status,inuse,jobended,skipped,corrupt,medialabel,testlogfile)
VALUES
('$jobname','$jobstarted','$logfile','$servername','$status','$inuse','$jobended','$skipped','$corrupt','$medialabel','<a
href=http://[snip]/oldlogs/$logfile>$logfile</a>')")
or die "Error preparing row: $DBI::errstr\n"; $sth->execute() or die "
$DBI::errstr\n"; $dbh->disconnect;
$servername = "";
$jobname = "";
$jobstarted = "";
$medialabel = "";
$jobended = "";
$status = "";
}
sub addnums {
my @number = @_;
my $total;
my $digit;
foreach $digit (@number) {
$total += $digit;
}
if (defined($total)) {
$total = $total;
}
else
{$total = 0;}
return $total;
}