abilita tutti local user

This commit is contained in:
2017-09-17 21:49:43 +02:00
parent a86359d4b2
commit 6810d4a1d6

View File

@@ -11,20 +11,24 @@ $SIG{CHLD} = 'IGNORE';
$|++; # Autoflush $|++; # Autoflush
sub getTimeStamp { # parm [ts] => timestamp for filename; log => timestamp for log sub getTimeStamp
{ # parm [ts] => timestamp for filename; log => timestamp for log
my $format = "%04d%02d%02d%02d%02d%02d"; my $format = "%04d%02d%02d%02d%02d%02d";
my ($p1) = @_; my ($p1) = @_;
if ( defined $p1 and $p1 eq "log" ) { if ( defined $p1 and $p1 eq "log" ) {
$format = "%04d%02d%02d %02d:%02d:%02d"; $format = "%04d%02d%02d %02d:%02d:%02d";
} }
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
my $timestamp = sprintf ( $format, $year+1900,$mon+1,$mday,$hour,$min,$sec); localtime(time);
my $timestamp =
sprintf( $format, $year + 1900, $mon + 1, $mday, $hour, $min, $sec );
return $timestamp; return $timestamp;
} }
sub getUnitName { # parm => file received ($trfile) sub getUnitName { # parm => file received ($trfile)
my ($filename) = @_; my ($filename) = @_;
open FILE, $filename or warn getTimeStamp("log") . " >> Error: opening input file $filename\n"; open FILE, $filename
or warn getTimeStamp("log") . " >> Error: opening input file $filename\n";
my ( $fileDate, $fileTime ) = split( /\s/, <FILE> ); my ( $fileDate, $fileTime ) = split( /\s/, <FILE> );
my ( $unittype, $unit ) = split( /\s/, uc <FILE> ); my ( $unittype, $unit ) = split( /\s/, uc <FILE> );
close FILE; close FILE;
@@ -32,8 +36,6 @@ sub getUnitName { # parm => file received ($trfile)
} }
my $readingFile; my $readingFile;
my $ftpuser = "asega";
my $ftpuser1 = "corra";
my $recvOKstr = "OK UPLOAD"; my $recvOKstr = "OK UPLOAD";
my $ext = ".csv"; my $ext = ".csv";
my $dbname = "ase_mums"; my $dbname = "ase_mums";
@@ -47,36 +49,63 @@ 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 ( index( $line, $recvOKstr ) != -1 ) {
my (undef, undef, undef, $truser, undef, $trip, undef, $trfile, $trstat) = split(/[\"\[\]]/, $line); my (
my ($login,$pass,$uid,$gid) = getpwnam($truser) or warn getTimeStamp("log") . " >> $truser not in passwd file.\n"; 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*)$/i) ){ if ( ( ( uc $suffix ) eq ( uc $ext ) )
and ( $filename =~ m/^(\d\d_\d\d\d\d_|)(DT\d\d\d\d|LOC\d*)$/i ) )
{
my $unit = getUnitName($trfile); my $unit = getUnitName($trfile);
print getTimeStamp("log") . " >> Unit $unit - Filename $trfile\n"; print getTimeStamp("log") . " >> Unit $unit - Filename $trfile\n";
if ( !-d "$path/log" ) {
make_path "$path/log",
{ mode => 0755, owner => $truser, group => $truser }
or warn getTimeStamp("log")
. " >> Failed to create path: $path/log";
}
my $outpath = $path . $unit; my $outpath = $path . $unit;
if ( !-d "$outpath/SQL" ) { if ( !-d "$outpath/SQL" ) {
make_path "$outpath/SQL" , {mode => 0755, owner=>$truser, group=>$truser} or warn getTimeStamp("log") . " >> Failed to create path: $outpath/SQL"; make_path "$outpath/SQL",
{ mode => 0755, owner => $truser, group => $truser }
or warn getTimeStamp("log")
. " >> Failed to create path: $outpath/SQL";
} }
my $timestamp = getTimeStamp(); my $timestamp = getTimeStamp();
my $dest = $outpath . "/" . $filename . "_" . $timestamp . $suffix; my $dest = $outpath . "/" . $filename . "_" . $timestamp . $suffix;
if ( !move $trfile, $dest ) { if ( !move $trfile, $dest ) {
warn getTimeStamp("log") . " >> Move $trfile -> $dest failed: $!"; warn getTimeStamp("log")
} else { . " >> Move $trfile -> $dest failed: $!";
}
else {
print getTimeStamp("log") . " >> Moved $trfile -> $dest.\n"; print getTimeStamp("log") . " >> Moved $trfile -> $dest.\n";
chmod 0664, $dest; chmod 0664, $dest;
my @fname = ($dest); my @fname = ($dest);
chown $uid, $gid, @fname; chown $uid, $gid, @fname;
if ($filename =~ m/^(\d\d_\d\d\d\d_|)(DT\d\d\d\d|LOC\d.*)$/i and ($unit ne 'ID9999') and (index($line, $ftpuser) != -1)) { if ( $filename =~ m/^(\d\d_\d\d\d\d_|)(DT\d\d\d\d|LOC\d.*)$/i
print getTimeStamp("log") . " >> Sender user $ftpuser: load data into DB.\n"; and ( $unit ne 'ID9999' ) )
{
print getTimeStamp("log")
. " >> Sender user $truser: load data into DB.\n";
unless ( fork() ) { unless ( fork() ) {
setgid($gid); setgid($gid);
setuid($uid); setuid($uid);
$ENV{"HOME"} = 'home/' . $ftpuser; $ENV{"HOME"} = 'home/' . $truser;
exec( $scriptpath . "LoadCSVData.pl -f \"$dest\" -s \"$outpath/SQL/$filename" . "_" . "$timestamp.sql\" -d $dbname >> /home/$truser/log/loadcsvdata.log 2>&1"); exec( $scriptpath
exit(0) . "LoadCSVData.pl -f \"$dest\" -s \"$outpath/SQL/$filename"
. "_"
. "$timestamp.sql\" -d $dbname >> /home/$truser/log/loadcsvdata.log 2>&1"
);
exit(0);
} }
} }
} }