Commit iniziale

Changes to be committed:
	new file:   AlertNotReceived.pl
	new file:   LoadCSVData.pl
	new file:   UnitCsvReceiverSmart.pl
This commit is contained in:
Alessandro Battilani
2017-08-30 22:33:00 +02:00
commit 134fc78aef
3 changed files with 613 additions and 0 deletions

87
UnitCsvReceiverSmart.pl Normal file
View File

@@ -0,0 +1,87 @@
#!/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) = @_;
open FILE, $filename or warn getTimeStamp("log") . " >> Error: opening input file $filename\n";
my ($fileDate, $fileTime) = split(/\s/, <FILE>);
my ($unittype, $unit) = split(/\s/, uc <FILE>);
close FILE;
return $unit;
}
my $readingFile;
my $ftpuser = "asega";
my $ftpuser1 = "corra";
my $recvOKstr = "OK UPLOAD";
my $ext = ".csv";
my $dbname = "ase_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*)$/i) ){
my $unit = getUnitName($trfile);
print getTimeStamp("log") . " >> Unit $unit - Filename $trfile\n";
my $outpath = $path . $unit;
if ( !-d "$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 $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.*)$/i and ($unit ne 'ID9999') and (index($line, $ftpuser) != -1)) {
print getTimeStamp("log") . " >> Sender user $ftpuser: load data into DB.\n";
unless (fork()) {
setgid($gid);
setuid($uid);
$ENV{"HOME"} = 'home/' . $ftpuser;
exec( $scriptpath . "LoadCSVData.pl -f \"$dest\" -s \"$outpath/SQL/$filename" . "_" . "$timestamp.sql\" -d $dbname >> /home/$truser/log/loadcsvdata.log 2>&1");
exit(0)
}
}
}
}
}
}
exit(0);