Files
ase_perl/aruba/UnitCsvReceiverSmart.pl

171 lines
5.9 KiB
Perl

#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Long;
use File::SmartTail;
use File::Basename qw( fileparse );
use File::Path qw( make_path );
use File::Copy qw( move );
use Net::FTP;
use POSIX;
$SIG{CHLD} = 'IGNORE';
$|++; # Autoflush
sub getTimeStamp
{ # parm [ts] => timestamp for filename; log => timestamp for log
my $format = "%04d%02d%02d%02d%02d%02d";
my ($p1) = @_;
if ( defined $p1 and $p1 eq "log" ) {
$format = "%04d%02d%02d %02d:%02d:%02d";
}
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
localtime(time);
my $timestamp =
sprintf( $format, $year + 1900, $mon + 1, $mday, $hour, $min, $sec );
return $timestamp;
}
sub getUnitName { # parm => file received ($trfile)
my ($filename) = @_;
my ( $fileDate, $fileTime );
my ( $unittype, $unit );
my ( $filenamecsv, $path, $suffix ) = fileparse( $filename, qr/\.[^.]*/ );
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";
( $fileDate, $fileTime ) = split( /\s/, <FILE> );
( $unittype, $unit ) = split( /\s/, uc <FILE> );
$unit =~ s/;+$//;
close FILE;
}
return $unit;
}
my $readingFile;
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';
GetOptions( "file=s" => \$readingFile )
or die("Error in command line arguments\n");
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";
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/^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";
my $cfgpath = $path . $unit . "/config";
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";
}
if ( !-d "$cfgpath" ) {
make_path "$cfgpath",
{ mode => 0755, owner => $truser, group => $truser }
or warn getTimeStamp("log")
. " >> Failed to create path: $cfgpath";
}
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 => 10,
Debug => 0,
Passive => 0
)
or warn getTimeStamp("log")
. " >> Cannot connect to $hostname: $@";
if ( defined $ftp ) {
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);