Mystery with Files with loop

R

Rob L

I have a program that I have written to read a few files for
information and then takes some of that information and puts the data
into arrays based on certain things. What I did was to run this
program out of CRON on a Unix system every few mins. I figured out
today that I needed this job to almost continuously so I added a while
loop to the code and was testing it to see how it would work. Well
what I found out is that after the arrays are created I write the data
to a "temp" file and then copy/move the data file to the real name the
data is appending to the "real" file instead of replacing it.

Psuodeo code...
while ( !-e $stopfile) {
read data file
read other data file
get info from data
check and put data into correct array
close other data file
close data file
write array data to temp file
copy or move temp file to real file
end while

The copy or move temp file is appending the data to the real file. If
I just run it once it works great it is on the second go around (and
so on) that it is appending the data. I have been kicking this thing
around for a while. Its not the prettiest code but its what I have.
The reason for the copy/move is because there is other processes that
read the "real" file and I do not want those other processes to miss
any data if they read the file while its building.

Below is my real code...
use POSIX qw(setsid);
use POSIX; #For date format function.
use File::Copy;
use File::Find;
use Shell qw(ps netstat grep);

$IMPACT = $ENV{"IMPACT"}
$OWBASE = $ENV{"OWBASE"}; #Path to /cnet
$DATA = $ENV{"DATA"};

$BSMworkDir = "$OWBASE" . "/Ofwr.sys";
$workDir = "$OWBASE";
# $workoutdir = "/opt/" . "$DATA" . "/misc/intranet_data/";
$workoutdir = "$OWBASE" . "intranet_data/";

$u = "~";

$stopfile = "$OWBASE" .
"Ofwr.sys/get_conn_status_daemon.stop";
$runningfile = "$OWBASE" .
"Ofwr.sys/get_conn_status_daemon.running";
$daemonLogFile = "$OWBASE" .
"Ofwr.sys/get_conn_status_daemon.LOG";


while ( !-e $stopfile ) {
&doIt();
print "sleeping...\n";
sleep 60;
}
if ( -e $runningfile ) {
unlink $runningfile;
}

exit;

sub doIt {
open(RUNFILE, "> $runningfile");
print RUNFILE "$$\n";
close RUNFILE;

getpendings(); # get a listing of pending transactions for each
AIM
# from the hash fileHash

open(BSMTAB, "< $BSMworkDir" . "/bsmtab") or
die "\nCan\'t open BSMTAB: $!\n";

while ($line = <BSMTAB>) { #Read and output each line of
bsmtab.

# get first character

($firchar,$aimname) = split(/:/, $line);

if ( $firchar eq "R" ) {
$firthree = substr($aimname, 0, 3);
if ( $firthree eq "sfm" ) { next }
if ( ! -e "$workDir" . "$aimname" ) { next }
$grepdm = "\"dm \"";
$grepmsgsrv = "\"msgsrv \"";
$dmoutput = ps("-fu $aimname -x | grep $grepdm");
if ( $dmoutput ne "" ) {
$aimstatus = "Up"
} else {
$aimstatus = "Down"
}
$msgoutput = ps("-fu $aimname -x | grep $grepmsgsrv");

$aimprefix = substr($aimname, 0, 1); #Get the 'a' or 'd'
from aimnam
e
@flist = ("dgsfmaim.cfg", "aaconfig.cfg", "agswpaim.cfg",
"daconfig.
cfg");
foreach $file (@flist) {

$pathToOpen = "$workDir" . "$aimname" . "/desktop/" .
"$file";
if ( -e $pathToOpen ) {
$fileToOpen = $pathToOpen;
}

}

open(CONFIG, "< $fileToOpen") or die "\nCan\'t open
CONFIG: $!\n";

$port = "";
$ref = "";
while ($cline = <CONFIG>) {
@pieces = split(/=/,$cline);
$cline = lc($cline); ;# make sure the line is
lowercase
$firfour = substr($cline, 0, 4);
if ( $firfour eq 'port') {
$port = $pieces[1];
$port =~ s/^\s+//; #Get rid of leading spaces.
$port =~ s/\n//; #Get rid of newline chars.
}
if ( $firfour eq "refe" ) {
$ref = $pieces[1];
$ref =~ s/^\s+//; #Get rid of leading spaces.
$ref =~ s/\n//; #Get rid of newline chars.
}
if ( $firfour eq "regi" ) {
$aimreg = lc($pieces[1]);
$aimreg =~ s/^\s+//; #Get rid of leading
spaces.
$aimreg =~ s/\n//; #Get rid of newline chars.
$aimreg =~ s/\r//; #Get rid of carriage return
chars.
}
}

close CONFIG;

if ( $ref =~ /SFM/ ) { next }

$xlogpath = "$workDir" . "$aimname" . "/desktop/xlog*";
$xlogfile = "$workDir" . "$aimname" . "/desktop/xlog";
if ( $aimprefix eq "a" ) {
$xsearch = "Data received";
$aimtype = "Acq.";
$lastsnd = "N/A";
$lastrec = "Unknown";
} elsif ( $aimprefix eq "d" ) {
$xsearch = "Sending this data";
$aimtype = "Del.";
$lastsnd = "Unknown";
$lastrec = "N/A";
} else {
$xsearch = "";
$aimtype = "";
}

if ( $xsearch ne "" && -e $xlogfile ) {
$xcmd = "grep -h \"$xsearch\" $xlogpath \| sort |";
open(XGREP, $xcmd);
@xgrep = <XGREP>;
close(XGREP);
$xlast = $xgrep[$#xgrep];
if ( $xlast ne "" ) {
($xdate,$xhr,$xmin,$xsec) = split(/:/,$xlast);
($xyr,$xmth,$xday) = split(/\//,$xdate);
$xdate = "$xmth\/$xday";
($xsec,$xmill) = split(/\./,$xsec);
if ( $aimprefix eq "a" ) {
$lastrec = "$xdate $xhr:$xmin:$xsec";
} elsif ( $aimprefix eq "d" ) {
$lastsnd = "$xdate $xhr:$xmin:$xsec";
} else {
}
}
}

if ( $aimprefix eq "d" && $lastsnd eq "Unknown" ) {
$lastidPath = "$workDir" . "$aimname" . "/desktop/";
$foundfile = "";
find(\&Wanted, $lastidPath);
if ( $foundfile ne "" ) {
$lastidfile = $lastidPath . $foundfile;
if ( -e $lastidfile ) {

($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mt
ime,$ctime,$blksize,$blocks) = stat($lastidfile);

($sec,$min,$hour,$mday,$mon,$year,$weekday,$yearday,$isd
st) = localtime($mtime);

$mon = ($mon+1); # Month starts from 0, e.g.
Jan is 0
$year = ($year + 1900);
$year = substr($year,2,2);
$time =
sprintf("%02d:%02d:%02d",$hour,$min,$sec);
$fmmddyy = "$mon/$mday $time";
$lastsnd = $fmmddyy;
}
}
}

%cstatus = (
'ESTABLISHED','Up',
'LISTEN','Opening'
);

if ($port ne "") {
$type = "Port";
# $port = "\" $port\"";
$connline = netstat("-an | grep $port");
@conpieces = split(/\s+/,$connline);
$connstatus = $conpieces[5];
$connstatus = $cstatus{$connstatus};
if ( $connstatus eq "" ) {
$connstatus = "Sleeping";
}
} else {
$type = "File";
$connstatus = "Up";
}

$aimpending = $fileHash{$aimname}[1];
if ( $aimpending eq "" ) {
$aimpending = 0;
}

if ( $aimpending > 0 && $msgoutput ne "" ) {
$connstatus = "Refused";
$xsearch = "wrong dest";
if ( $xsearch ne "" && -e $xlogfile ) {
$xcmd = "grep -h \"$xsearch\" $xlogpath\| sort |";
open(XGREP, $xcmd);
@xgrep = <XGREP>;
close(XGREP);
$xlast = $xgrep[$#xgrep];
if ( $xlast ne "" ) {
$connstatus = "BadFlavor";
}
}
}
if ( $aimstatus =~ "Down" ) {
$connstatus = "Down";
}

$outline = "$aimname$u$aimstatus$u$ref$u$type$u$connstatus$u$lastrec$u$lasts
nd$u$aimpending$u$aimreg$u$aimtype$u";

$outData{"$aimname"} = $outline;
if ( $aimreg eq "central" ) {
push(@central, $aimname);
} elsif ( $aimreg eq "east" ) {
push(@east, $aimname);
} elsif ( $aimreg eq "west" ) {
push(@west, $aimname);
} else {
push(@unknown, $aimname);
}

if ( $aimpending > 0 || ($connstatus ne "Sleeping" &&
$connstatus ne
"Opening" && $connstatus ne "Up") ) {
if ( $aimreg eq "central" ) {
push(@prob_central, $aimname);
} elsif ( $aimreg eq "east" ) {
push(@prob_east, $aimname);
} elsif ( $aimreg eq "west" ) {
push(@prob_west, $aimname);
} else {
push(@prob_unk, $aimname);
}
}

} ;# end of if ( $firchar eq "R" )

} ;# end of while ($line = <BSMTAB>)

close BSMTAB;

$source = "$workoutdir" . "conn_status_data.beforecopy";
$dest = "$workoutdir" . "conn_status_data.txt";

open(CONNFILE, "> $source") or
die "\nCan\'t open the output file: $!\n";

foreach $aim (@east) {
print CONNFILE "$outData{$aim}\n";
}
foreach $aim (@central) {
print CONNFILE "$outData{$aim}\n";
}
foreach $aim (@west) {
print CONNFILE "$outData{$aim}\n";
}
foreach $aim (@unknown) {
print CONNFILE "$outData{$aim}\n";
}

close CONNFILE;
# @args = ("mv", "$source", "$dest");
# system(@args) == 0 or die "system @args failed: $?";
# $mvCmd="mv $source $dest";
# print "mvCmd == $mvCmd\n";
# unlink $source;
# move("$source","$dest");
$output = `mv $source $dest`;
print "output == $output\n";

$psource = "$workoutdir" . "problem_conn_status_data.beforecopy";
$pdest = "$workoutdir" . "problem_conn_status_data.txt";

open(PROBFILE, "> $psource") or
die "\nCan\'t open the output file: $!\n";

foreach $aim (@prob_east) {
print PROBFILE "$outData{$aim}\n";
}
foreach $aim (@prob_central) {
print PROBFILE "$outData{$aim}\n";
}
foreach $aim (@prob_west) {
print PROBFILE "$outData{$aim}\n";
}
foreach $aim (@prob_unk) {
print PROBFILE "$outData{$aim}\n";
}

close PROBFILE;
# @args = ("mv", "$psource", "$pdest");
# system(@args) == 0 or die "system @args failed: $?";
# $mvCmd="mv $psource $pdest";
# print "mvCmd == $mvCmd\n";
# system("$mvCmd");
# unlink $psource;
# move("$psource","$pdest");
$output = `mv $psource $pdest`;
print "output == $output\n";

}

sub Wanted {
/archive/ and $File::Find::prune = 1;
next if ( $_ !~ /lastid/);
$foundfile = $_;
}

Thanks in advance for any help.
 
A

Anno Siegel

Rob L said:
I have a program that I have written to read a few files for
information and then takes some of that information and puts the data
into arrays based on certain things. What I did was to run this ^^^^^^
program out of CRON on a Unix system every few mins. I figured out
today that I needed this job to almost continuously so I added a while
loop to the code and was testing it to see how it would work. Well
what I found out is that after the arrays are created I write the data
to a "temp" file and then copy/move the data file to the real name the
data is appending to the "real" file instead of replacing it.

The "mv" command that you have been using doesn't do that. Nor
does the Perl "rename" function you should have been using.

[snip lots of ugliness]

I suspect that you forgot to clear those arrays you are using, or
rather that you failed to declare them lexically in an appropriate
scope. I'm not going to dig through 360 lines of code to verify.

If you want your code checked, make a short excerpt that shows the
behavior you don't understand. Dumping the whole program to the
newsgroup is the wrong kind of laziness.

Anno
 
J

Joe Smith

Rob said:
data is appending to the "real" file instead of replacing it.

You mean that the old data and the new data are replacing it,
since the old data is never removed from your arrays.
foreach $aim (@prob_east) {
print PROBFILE "$outData{$aim}\n";
}
foreach $aim (@prob_central) {
print PROBFILE "$outData{$aim}\n";
}
foreach $aim (@prob_west) {
print PROBFILE "$outData{$aim}\n";
}
foreach $aim (@prob_unk) {
print PROBFILE "$outData{$aim}\n";
}

foreach $aim (@prob_east, @prob_central, @prob_west, @prob_unk) {
print PROBFILE "$outData{$aim}\n";
}
@prob_east = @prob_central = @prob_west = @prob_unk = ();

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top