Files
ase_perl/UnitCsvReceiverSmart.pl

224 lines
8.7 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 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 ( $path =~ m/ID\d\d\d\d\/*$/i ) {
my @strings = $path =~ /.*\/(ID.{1,4})\/*$/;
$unit = $strings[0];
}
if ( $filenamecsv =~ m/^G201_ID\d\d\d\d_DT\d\d\d\d_\d*$/i ) {
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/^GFLOW_ID\d\d\d\d_DT\d\d\d\d_\d*$/i ) {
my @strings = $filenamecsv =~
/(.{1,5})_(.{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/^measurements_\d\d_\d\d_\d\d\d\d__\d\d_\d\d$/i ) {
$unittype = "NESA";
}
elsif ( $filenamecsv =~ m/^\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d$/i )
{ # nesa file con serial number centralina
$unittype = "NESA";
}
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];
}
elsif ( $filenamecsv =~ m/^(\d*_|)G301_ID\d\d\d\d_DT\d\d\d\d$/i ) {
my @strings = $filenamecsv =~ /(.{1,4})_(.{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, $unittype );
}
my $readingFile;
my $recvOKstr = "OK UPLOAD";
my @ext = ( ".csv", ".txt" );
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, $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/\.[^.]*/ );
$path = "/home/" . $truser . "/";
$trfile = "/home/" . $truser . $trfile;
if (
( grep( /^$suffix$/i, @ext ) )
and ( $filename =~
m/^(\d\d_\d\d\d\d_|)(DT\d\d\d\d|LOC\d\d\d\d|GD\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
or $filename =~ m/^(\d*_|)G301_ID\d\d\d\d_DT\d\d\d\d$/i
or $filename =~ m/^GFLOW_ID\d\d\d\d_DT\d\d\d\d_\d*$/i
or $filename =~ m/^measurements_\d\d_\d\d_\d\d\d\d__\d\d_\d\d$/i
or $filename =~ m/^\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d$/i )
)
{
my ( $unit, $unittype ) = getUnitName($trfile);
print getTimeStamp("log") . " >> Unit $unit - Filename $trfile\n";
if ( !-d "$path/log" ) {
make_path "$path/log",
{ mode => 0755, owner => $truser, group => $gid }
or warn getTimeStamp("log")
. " >> Failed to create path: $path/log";
}
my $outpath = $path . $unit;
if ( !-d "$outpath/SQL" ) {
make_path "$outpath/SQL",
{ mode => 0755, owner => $truser, group => $gid }
or warn getTimeStamp("log")
. " >> Failed to create path: $outpath/SQL";
}
my $timestamp = getTimeStamp();
my $dest = $outpath . "/" . $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;
if (
(
$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
or $filename =~ m/^(\d*_|)G301_ID\d\d\d\d_DT\d\d\d\d$/i
or $filename =~ m/^GFLOW_ID\d\d\d\d_DT\d\d\d\d_\d*$/i
or $filename =~
m/^measurements_\d\d_\d\d_\d\d\d\d__\d\d_\d\d$/i
or $filename =~
m/^\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d$/i
)
and ( $unit ne 'ID9999' )
and ( $truser ne 'corra' )
)
{
print getTimeStamp("log")
. " >> Sender user $truser: load data into DB.\n";
unless ( fork() ) {
setgid($gid);
setuid($uid);
$ENV{"HOME"} = 'home/' . $truser;
if ( $unittype eq 'GFLOW' ) {
exec( $scriptpath
. "gflowScript.py \"$dest\" >> /home/$truser/log/loadgflow.log 2>&1"
);
}
elsif ( $unittype eq 'NESA' ) {
exec( $scriptpath
. "nesaScript.py \"$dest\" >> /home/$truser/log/loadnesa.log 2>&1"
);
}
else {
exec( $scriptpath
. "LoadCSVData.pl -f \"$dest\" -s \"$outpath/SQL/$filename"
. "_"
. "$timestamp.sql\" -d $truser >> /home/$truser/log/loadcsvdata.log 2>&1"
);
}
exit(0);
}
if ( ( $truser ne 'asega' ) and ( $truser ne 'corra' ) ) {
print getTimeStamp("log")
. " >> Sender user $truser: duplicate as asega user -> load data into DB.\n";
my $realtruser = $truser;
$truser = "asega";
$outpath =~ s/$realtruser/$truser/;
my ( $login, $pass, $uid, $gid ) = getpwnam($truser)
or warn getTimeStamp("log")
. " >> $truser not in passwd file.\n";
unless ( fork() ) {
setgid($gid);
setuid($uid);
$ENV{"HOME"} = 'home/' . $truser;
exec( $scriptpath
. "LoadCSVData.pl -f \"$dest\" -s \"$outpath/SQL/$filename"
. "_"
. "$timestamp.sql\" -d $truser >> /home/$truser/log/loadcsvdata.log 2>&1"
);
exit(0);
}
}
}
}
}
}
}
exit(0);