From 20fd892060a365124b92a7bcbfe0f8938acd1057 Mon Sep 17 00:00:00 2001 From: Alessandro Battilani Date: Thu, 12 Nov 2020 21:52:45 +0100 Subject: [PATCH] allineato all'ultima versio di aseDB --- aruba/UnitCsvReceiverSmart.pl | 169 +++++++++++++++++++++------------- 1 file changed, 105 insertions(+), 64 deletions(-) diff --git a/aruba/UnitCsvReceiverSmart.pl b/aruba/UnitCsvReceiverSmart.pl index bb9f307..1eb1cb7 100644 --- a/aruba/UnitCsvReceiverSmart.pl +++ b/aruba/UnitCsvReceiverSmart.pl @@ -28,18 +28,26 @@ sub getTimeStamp sub getUnitName { # parm => file received ($trfile) my ($filename) = @_; - my ( $fileDate, $fileTime ); - my ( $unittype, $unit ); + my ( $fileDate, $fileTime ); + my ( $unittype, $unit ); 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 - my @strings = $filenamecsv =~ /(.{1,4})(.{1,2})(.{1,2})(.{1,2})(.{1,2})(.{1,2})_(.{1,4})_(.{1,6})_(.{1,6}).*/; - $fileDate = $strings[0] . "/" . $strings[1] . "/" . $strings[2];; - $fileTime = $strings[3] . ":" . $strings[4] . ":" . $strings[5]; - $unittype = $strings[6]; - $unit = $strings[7]; - } else { + if ( $filenamecsv =~ m/^G201_ID\d\d\d\d_DT\d\d\d\d_\d*$/i ) { # G201 + my @strings = $filenamecsv =~ +/(.{1,4})_(.{1,6})_(.{1,6})_(.{1,4})(.{1,2})(.{1,2})(.{1,2})(.{1,2})(.{1,2}).*/; + $unittype = $strings[0]; + $unit = $strings[1]; + $fileDate = $strings[3] . "/" . $strings[4] . "/" . $strings[5]; + $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 - or warn getTimeStamp("log") . " >> Error: opening input file $filename\n"; + or warn getTimeStamp("log") + . " >> Error: opening input file $filename\n"; ( $fileDate, $fileTime ) = split( /\s/, ); ( $unittype, $unit ) = split( /\s/, uc ); $unit =~ s/;+$//; @@ -49,72 +57,105 @@ sub getUnitName { # parm => file received ($trfile) } my $readingFile; -my $ftpuser = "aseftp"; -my $ftpuser1 = "asega"; +my $ftpuser = "aseftp"; +my $ftpuser1 = "asega"; my $recvOKstr = "OK UPLOAD"; -my $ext = ".csv"; -my $hostname = "160.78.21.55"; -my $username = 'asega'; -my $password = 'mums'; +my $ext = ".csv"; +my $hostname = "160.78.21.55"; +my $username = 'asega'; +my $password = 'mums'; GetOptions( "file=s" => \$readingFile ) or die("Error in command line arguments\n"); -my($scriptname, $scriptpath) = fileparse($0); +my ( $scriptname, $scriptpath ) = fileparse($0); my $tail = new File::SmartTail; $tail->WatchFile( -file => $readingFile, -type => "UNIX", -timeout => '10' ); while ( my $line = $tail->GetLine() ) { - if (((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"; + if ( + ( + ( 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)) - 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) ) - { - my $unit = getUnitName($trfile); - print getTimeStamp("log") . " >> Unit $unit - Filename $trfile\n"; - my $outpath = $path . $unit . "/received"; - my $trnpath = $path . $unit . "/transmitted"; - if ( !-d "$outpath" ) { - make_path "$outpath" , {mode => 0755, owner=>$truser, group=>$truser} or warn getTimeStamp("log") . " >> Failed to create path: $outpath"; - } - if ( !-d "$trnpath" ) { - make_path "$trnpath" , {mode => 0755, owner=>$truser, group=>$truser} or warn getTimeStamp("log") . " >> Failed to create path: $trnpath"; - } - my $timestamp = getTimeStamp(); - my $dest = $outpath . "/" . $filename . "_". $timestamp . $suffix; - my $send = $trnpath . "/" . $filename . "_". $timestamp . $suffix; - if ( !move $trfile, $dest) { - warn getTimeStamp("log") . " >> Move $trfile -> $dest failed: $!"; - } else { - print getTimeStamp("log") . " >> Moved $trfile -> $dest.\n"; - chmod 0664 , $dest; - my @fname = ($dest); - chown $uid, $gid, @fname; - } - my $ftp = Net::FTP->new($hostname, Timeout => 20, 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; - } - } - } + if ( + ( ( uc $suffix ) eq ( uc $ext ) ) + and ( $filename =~ m/^(\d\d_\d\d\d\d_|)(DT\d\d\d\d|LOC\d*|GD\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 $trnpath = $path . $unit . "/transmitted"; + if ( !-d "$outpath" ) { + make_path "$outpath", + { mode => 0755, owner => $truser, group => $truser } + or warn getTimeStamp("log") + . " >> Failed to create path: $outpath"; + } + if ( !-d "$trnpath" ) { + make_path "$trnpath", + { mode => 0755, owner => $truser, group => $truser } + or warn getTimeStamp("log") + . " >> Failed to create path: $trnpath"; + } + my $timestamp = getTimeStamp(); + my $dest = $outpath . "/" . $filename . "_" . $timestamp . $suffix; + my $send = $trnpath . "/" . $filename . "_" . $timestamp . $suffix; + if ( !move $trfile, $dest ) { + warn getTimeStamp("log") + . " >> Move $trfile -> $dest failed: $!"; + } + else { + print getTimeStamp("log") . " >> Moved $trfile -> $dest.\n"; + chmod 0664, $dest; + my @fname = ($dest); + chown $uid, $gid, @fname; + } + my $ftp = Net::FTP->new( + $hostname, + Timeout => 20, + 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);