allineato all'ultima versio di aseDB

This commit is contained in:
2020-11-12 21:52:45 +01:00
parent 2088a289fa
commit 20fd892060

View File

@@ -28,18 +28,26 @@ sub getTimeStamp
sub getUnitName { # parm => file received ($trfile) sub getUnitName { # parm => file received ($trfile)
my ($filename) = @_; my ($filename) = @_;
my ( $fileDate, $fileTime ); my ( $fileDate, $fileTime );
my ( $unittype, $unit ); my ( $unittype, $unit );
my ( $filenamecsv, $path, $suffix ) = fileparse( $filename, qr/\.[^.]*/ ); my ( $filenamecsv, $path, $suffix ) = fileparse( $filename, qr/\.[^.]*/ );
if ( $filenamecsv =~ m/^(\d\d_\d\d\d\d_|)(\d\d\d\d\d\d\d\d\d\d\d\d\d\d_)(G201_ID\d\d\d\d_DT\d\d\d\d)$/i ) { # GD201 if ( $filenamecsv =~ m/^G201_ID\d\d\d\d_DT\d\d\d\d_\d*$/i ) { # G201
my @strings = $filenamecsv =~ /(.{1,4})(.{1,2})(.{1,2})(.{1,2})(.{1,2})(.{1,2})_(.{1,4})_(.{1,6})_(.{1,6}).*/; my @strings = $filenamecsv =~
$fileDate = $strings[0] . "/" . $strings[1] . "/" . $strings[2];; /(.{1,4})_(.{1,6})_(.{1,6})_(.{1,4})(.{1,2})(.{1,2})(.{1,2})(.{1,2})(.{1,2}).*/;
$fileTime = $strings[3] . ":" . $strings[4] . ":" . $strings[5]; $unittype = $strings[0];
$unittype = $strings[6]; $unit = $strings[1];
$unit = $strings[7]; $fileDate = $strings[3] . "/" . $strings[4] . "/" . $strings[5];
} else { $fileTime = $strings[6] . ":" . $strings[7] . ":" . $strings[8];
}
elsif ( $filenamecsv =~ m/^D2W_ID\d\d\d\d_DT\d\d\d\d$/i ) {
my @strings = $filenamecsv =~ /(.{1,3})_(.{1,6})_(.{1,6}).*/;
$unittype = $strings[0];
$unit = $strings[1];
}
else {
open FILE, $filename open FILE, $filename
or warn getTimeStamp("log") . " >> Error: opening input file $filename\n"; or warn getTimeStamp("log")
. " >> Error: opening input file $filename\n";
( $fileDate, $fileTime ) = split( /\s/, <FILE> ); ( $fileDate, $fileTime ) = split( /\s/, <FILE> );
( $unittype, $unit ) = split( /\s/, uc <FILE> ); ( $unittype, $unit ) = split( /\s/, uc <FILE> );
$unit =~ s/;+$//; $unit =~ s/;+$//;
@@ -49,72 +57,105 @@ sub getUnitName { # parm => file received ($trfile)
} }
my $readingFile; my $readingFile;
my $ftpuser = "aseftp"; my $ftpuser = "aseftp";
my $ftpuser1 = "asega"; my $ftpuser1 = "asega";
my $recvOKstr = "OK UPLOAD"; my $recvOKstr = "OK UPLOAD";
my $ext = ".csv"; my $ext = ".csv";
my $hostname = "160.78.21.55"; my $hostname = "160.78.21.55";
my $username = 'asega'; my $username = 'asega';
my $password = 'mums'; my $password = 'mums';
GetOptions( "file=s" => \$readingFile ) GetOptions( "file=s" => \$readingFile )
or die("Error in command line arguments\n"); or die("Error in command line arguments\n");
my($scriptname, $scriptpath) = fileparse($0); my ( $scriptname, $scriptpath ) = fileparse($0);
my $tail = new File::SmartTail; my $tail = new File::SmartTail;
$tail->WatchFile( -file => $readingFile, -type => "UNIX", -timeout => '10' ); $tail->WatchFile( -file => $readingFile, -type => "UNIX", -timeout => '10' );
while ( my $line = $tail->GetLine() ) { while ( my $line = $tail->GetLine() ) {
if (((index($line, $ftpuser) != -1) or (index($line, $ftpuser1) != -1)) and (index($line, $recvOKstr) != -1)) { if (
my (undef, undef, undef, $truser, undef, $trip, undef, $trfile, $trstat) = split(/[\"\[\]]/, $line); (
my ($login,$pass,$uid,$gid) = getpwnam($truser) or warn getTimeStamp("log") . " >> $truser not in passwd file.\n"; ( index( $line, $ftpuser ) != -1 )
or ( index( $line, $ftpuser1 ) != -1 )
)
and ( index( $line, $recvOKstr ) != -1 )
)
{
my (
undef, undef, undef, $truser, undef,
$trip, undef, $trfile, $trstat
) = split( /[\"\[\]]/, $line );
my ( $login, $pass, $uid, $gid ) = getpwnam($truser)
or warn getTimeStamp("log") . " >> $truser not in passwd file.\n";
my($filename, $path, $suffix) = fileparse($trfile, qr/\.[^.]*/); my ( $filename, $path, $suffix ) = fileparse( $trfile, qr/\.[^.]*/ );
if ( ((uc $suffix) eq (uc $ext)) if (
and ( ( uc $suffix ) eq ( uc $ext ) )
( $filename =~ m/^(\d\d_\d\d\d\d_|)(DT\d\d\d\d|LOC\d*|GD\d*)$/i and ( $filename =~ m/^(\d\d_\d\d\d\d_|)(DT\d\d\d\d|LOC\d*|GD\d*)$/i
or $filename =~ m/^(\d\d_\d\d\d\d_|)(\d\d\d\d\d\d\d\d\d\d\d\d\d\d_)(G201_ID\d\d\d\d_DT\d\d\d\d)$/i) ) or $filename =~ m/^G201_ID\d\d\d\d_DT\d\d\d\d_\d*$/i
{ or $filename =~ m/^D2W_ID\d\d\d\d_DT\d\d\d\d$/i )
my $unit = getUnitName($trfile); )
print getTimeStamp("log") . " >> Unit $unit - Filename $trfile\n"; {
my $outpath = $path . $unit . "/received"; my $unit = getUnitName($trfile);
my $trnpath = $path . $unit . "/transmitted"; print getTimeStamp("log") . " >> Unit $unit - Filename $trfile\n";
if ( !-d "$outpath" ) { my $outpath = $path . $unit . "/received";
make_path "$outpath" , {mode => 0755, owner=>$truser, group=>$truser} or warn getTimeStamp("log") . " >> Failed to create path: $outpath"; my $trnpath = $path . $unit . "/transmitted";
} if ( !-d "$outpath" ) {
if ( !-d "$trnpath" ) { make_path "$outpath",
make_path "$trnpath" , {mode => 0755, owner=>$truser, group=>$truser} or warn getTimeStamp("log") . " >> Failed to create path: $trnpath"; { mode => 0755, owner => $truser, group => $truser }
} or warn getTimeStamp("log")
my $timestamp = getTimeStamp(); . " >> Failed to create path: $outpath";
my $dest = $outpath . "/" . $filename . "_". $timestamp . $suffix; }
my $send = $trnpath . "/" . $filename . "_". $timestamp . $suffix; if ( !-d "$trnpath" ) {
if ( !move $trfile, $dest) { make_path "$trnpath",
warn getTimeStamp("log") . " >> Move $trfile -> $dest failed: $!"; { mode => 0755, owner => $truser, group => $truser }
} else { or warn getTimeStamp("log")
print getTimeStamp("log") . " >> Moved $trfile -> $dest.\n"; . " >> Failed to create path: $trnpath";
chmod 0664 , $dest; }
my @fname = ($dest); my $timestamp = getTimeStamp();
chown $uid, $gid, @fname; my $dest = $outpath . "/" . $filename . "_" . $timestamp . $suffix;
} my $send = $trnpath . "/" . $filename . "_" . $timestamp . $suffix;
my $ftp = Net::FTP->new($hostname, Timeout => 20, Debug => 0, Passive => 0) if ( !move $trfile, $dest ) {
or warn getTimeStamp("log") . " >> Cannot connect to $hostname: $@"; warn getTimeStamp("log")
if ($ftp->login($username,$password)) { . " >> Move $trfile -> $dest failed: $!";
if ($ftp->put($dest, $filename . $suffix)) { }
if ( !move $dest, $send) { else {
warn getTimeStamp("log") . " >> Move $dest -> $send failed: $!"; print getTimeStamp("log") . " >> Moved $trfile -> $dest.\n";
} else { chmod 0664, $dest;
print getTimeStamp("log") . " >> Moved $dest -> $send.\n"; my @fname = ($dest);
} chown $uid, $gid, @fname;
} else { }
warn getTimeStamp("log") . " >> Put $dest failed ", $ftp->message; my $ftp = Net::FTP->new(
} $hostname,
} else { Timeout => 20,
warn getTimeStamp("log") . " >> Cannot login ", $ftp->message; Debug => 0,
} Passive => 0
} )
} or warn getTimeStamp("log")
. " >> Cannot connect to $hostname: $@";
if ( $ftp->login( $username, $password ) ) {
if ( $ftp->put( $dest, $filename . $suffix ) ) {
if ( !move $dest, $send ) {
warn getTimeStamp("log")
. " >> Move $dest -> $send failed: $!";
}
else {
print getTimeStamp("log")
. " >> Moved $dest -> $send.\n";
}
}
else {
warn getTimeStamp("log") . " >> Put $dest failed ",
$ftp->message;
}
}
else {
warn getTimeStamp("log") . " >> Cannot login ", $ftp->message;
}
}
}
} }
exit(0); exit(0);