Commit iniziale
Changes to be committed: new file: AlertNotReceived.pl new file: LoadCSVData.pl new file: UnitCsvReceiverSmart.pl
This commit is contained in:
212
AlertNotReceived.pl
Normal file
212
AlertNotReceived.pl
Normal file
@@ -0,0 +1,212 @@
|
||||
#!/usr/bin/perl
|
||||
|
||||
use warnings;
|
||||
use strict;
|
||||
use DBI;
|
||||
use Mail::Sender;
|
||||
|
||||
# Variables
|
||||
my $username = 'aseuser';
|
||||
my $password = 'Mums0001!';
|
||||
my $database = 'ase_mums';
|
||||
my $db_lar = 'ase_lar';
|
||||
my $server = 'localhost';
|
||||
my ($email_msg, $email_obj, $tools_info, $ref_date);
|
||||
my $act_tool = 0;
|
||||
#my $email_addr = 'alessandro.battilani@gmail.com';
|
||||
my $email_addr = 'aseparma@gmail.com,alessandro.battilani@gmail.com,corrado@geielettronica.it,luca.chiapponi@unipr.it';
|
||||
|
||||
sub getLoggingTime {
|
||||
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
|
||||
my $nice_timestamp = sprintf ( "%04d%02d%02d %02d:%02d:%02d",
|
||||
$year+1900,$mon+1,$mday,$hour,$min,$sec);
|
||||
return $nice_timestamp;
|
||||
}
|
||||
|
||||
sub makeEmailMsg {
|
||||
$email_msg = <<"END_MSG";
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||
<title>Alert from ASE</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||
</head>
|
||||
<body style="margin: 0; padding: 0;">
|
||||
<table border="0" cellpadding="0" cellspacing="0" width="100%">
|
||||
<tr>
|
||||
<td>
|
||||
<table align="center" border="0" cellpadding="0" cellspacing="0" width="600" style="border-collapse: collapse;">
|
||||
<tr>
|
||||
<td align="center" bgcolor="#fff" style="padding: 10px 0 10px 0;">
|
||||
<img src="http://www.aseltd.eu/img/logo_ASE_small.png" alt="ASE" style="display: block;" />
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td bgcolor="#ffffff" style="padding: 10px 10px 10px 10px;">
|
||||
<table border="0" cellpadding="0" cellspacing="0" width="100%">
|
||||
<tr>
|
||||
<td style="padding: 10px 10px 10px 10px;">
|
||||
Alert from ASE: data received on $ref_date.
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
|
||||
<table border="0" cellpadding="4" cellspacing="0" width="100%">
|
||||
<tr bgcolor="#64b7ff" align="center" style="color:white">
|
||||
<td><b>Company</b></td><td><b>Sito</b></td><td><b>ID Centralina</b></td><td><b>ID Tools</b></td>
|
||||
<td><b>Letture Nodi</b></td><td><b>Numero Nodi</b></td><td><b>Letture Tool</b></td>
|
||||
<td><b>Letture Nodi in Errore</b></td><td><b>Stato Tool</b></td>
|
||||
</tr>
|
||||
$tools_info
|
||||
</table>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
||||
END_MSG
|
||||
}
|
||||
|
||||
sub emailSend {
|
||||
my $sender = new Mail::Sender {
|
||||
auth => 'PLAIN',
|
||||
authid => 'ase.mums.alert@gmail.com',
|
||||
authpwd => 'ASE@mums!',
|
||||
smtp => 'smtp.gmail.com',
|
||||
port => 587,
|
||||
from => 'ASE Alert System',
|
||||
to => $email_addr,
|
||||
subject => $email_obj,
|
||||
msg => $email_msg,
|
||||
ctype => "text/html",
|
||||
encoding => "7bit"
|
||||
};
|
||||
|
||||
my $mail_res = $sender->MailMsg({msg => $sender->{msg},});
|
||||
if (ref($mail_res) ne "Mail::Sender") {
|
||||
print getLoggingTime() . " - $mail_res: $sender->{error_msg} \n";
|
||||
} else {
|
||||
print getLoggingTime() . " - $email_obj\n";
|
||||
print getLoggingTime() . " - Mail sent to: $email_addr.\n";
|
||||
}
|
||||
}
|
||||
|
||||
my %ToolName = ();
|
||||
my %Tools_infoCompany = ();
|
||||
|
||||
print getLoggingTime() . " - pid $$ >> Start execution.\n";
|
||||
|
||||
my $dbh = DBI->connect("DBI:mysql:$db_lar;host=$server", $username, $password)
|
||||
or die getLoggingTime() . " - Could not connect to database: $DBI::errstr";
|
||||
|
||||
my $sth = $dbh->prepare('select idCompany, CompanyNameShort, SiteName, CompanyEmail, UnitNameId, ToolNameID, ToolState from mail_view;')
|
||||
or die getLoggingTime() . " - $DBI::errstr";
|
||||
|
||||
$sth->execute();
|
||||
my $ToolNum = $sth->rows;
|
||||
if ($sth->rows < 0) {
|
||||
print getLoggingTime() . " - No rows from TOOLS & UNITS.\n";
|
||||
} else {
|
||||
while (my $results = $sth->fetchrow_hashref) {
|
||||
$ToolName{$results->{'ToolNameID'}}{'idCompany'} = $results->{'idCompany'};
|
||||
$ToolName{$results->{'ToolNameID'}}{'CompanyNameShort'} = $results->{'CompanyNameShort'};
|
||||
$ToolName{$results->{'ToolNameID'}}{'SiteName'} = $results->{'SiteName'};
|
||||
$ToolName{$results->{'idCompany'}}{'CompanyEmail'} = $results->{'CompanyEmail'};
|
||||
$ToolName{$results->{'ToolNameID'}}{'UnitNameId'} = $results->{'UnitNameId'};
|
||||
$ToolName{$results->{'ToolNameID'}}{'ToolState'} = $results->{'ToolState'};
|
||||
$ToolName{$results->{'ToolNameID'}}{'LettureNodi'} = "--";
|
||||
$ToolName{$results->{'ToolNameID'}}{'LettureCatene'} = "--";
|
||||
$ToolName{$results->{'ToolNameID'}}{'NumeroNodi'} = "--";
|
||||
$ToolName{$results->{'ToolNameID'}}{'ErroriTotaliLetture'} = "--";
|
||||
if ($results->{'ToolState'} eq "Active" ){
|
||||
$act_tool++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$sth->finish;
|
||||
# Disconnect
|
||||
$dbh->disconnect;
|
||||
|
||||
$dbh = DBI->connect("DBI:mysql:$database;host=$server", $username, $password)
|
||||
or die getLoggingTime() . " - Could not connect to database: $DBI::errstr";
|
||||
|
||||
my $stw = $dbh->prepare("select ToolNameID, EventDate, count(EventTime) 'LettureNodi', truncate(Count(EventTime)/max(NodeNum),1) 'LettureCatene', max(NodeNum) 'NumeroNodi' from RAWDATA where ToolNameId not like 'DT009%' and EventDate = subdate(curdate(),1) group by ToolNameID")
|
||||
or die getLoggingTime() . " - $DBI::errstr";
|
||||
$stw->execute();
|
||||
|
||||
if ($stw->rows == 0) {
|
||||
print getLoggingTime() . " - No rows from RAWDATA.\n";
|
||||
$email_obj = "ATTENZIONE: nessuna lettura pervenuta!!!"
|
||||
} else {
|
||||
while (my $results = $stw->fetchrow_hashref) {
|
||||
$ToolName{$results->{'ToolNameID'}}{'LettureNodi'} = $results->{'LettureNodi'};
|
||||
$ToolName{$results->{'ToolNameID'}}{'LettureCatene'} = $results->{'LettureCatene'};
|
||||
$ToolName{$results->{'ToolNameID'}}{'NumeroNodi'} = $results->{'NumeroNodi'};
|
||||
$ref_date = $results->{'EventDate'};
|
||||
}
|
||||
if ($act_tool == $stw->rows) {
|
||||
$email_obj = "Tutte le letture pervenute.";
|
||||
} else {
|
||||
$email_obj = "ATTENZIONE: alcune letture non pervenute!!!";
|
||||
}
|
||||
}
|
||||
|
||||
$stw->finish;
|
||||
|
||||
my $ste = $dbh->prepare("select ToolNameID, count(val0) 'ErroriTotaliLetture' from RAWDATA where EventDate = ? and val0 = 'Err1' group by ToolNameID")
|
||||
or die getLoggingTime() . " - $DBI::errstr";
|
||||
$ste->bind_param(1, $ref_date);
|
||||
$ste->execute();
|
||||
while (my $results = $ste->fetchrow_hashref) {
|
||||
$ToolName{$results->{'ToolNameID'}}{'ErroriTotaliLetture'} = $results->{'ErroriTotaliLetture'};
|
||||
}
|
||||
|
||||
$ste->finish;
|
||||
|
||||
# Disconnect
|
||||
$dbh->disconnect;
|
||||
|
||||
# Ciclo x email totale rilevazioni
|
||||
foreach my $tool (sort grep { /DT|PT/ } keys %ToolName){
|
||||
|
||||
if ( $ToolName{$tool}{'ToolState'} eq 'Monitoring Completed' ) {
|
||||
next; #skip dei tool nello stato del test
|
||||
}
|
||||
|
||||
my $tool_part = "<td>" . $ToolName{$tool}{'CompanyNameShort'} . "</td><td>". $ToolName{$tool}{'SiteName'} . "</td>";
|
||||
$tool_part .= "<td align=\"center\">" . $ToolName{$tool}{'UnitNameId'} . "</td><td align=\"center\">" . $tool . "</td>";
|
||||
$tool_part .= "<td align=\"right\">" . $ToolName{$tool}{'LettureNodi'} . "</td><td align=\"right\">" . $ToolName{$tool}{'NumeroNodi'} . "</td>";
|
||||
$tool_part .= "<td align=\"right\">" . $ToolName{$tool}{'LettureCatene'} . "</td>";
|
||||
$tool_part .= "<td align=\"center\">" . $ToolName{$tool}{'ErroriTotaliLetture'} . "</td><td align=\"center\">" . $ToolName{$tool}{'ToolState'} . "</td></tr>\n";
|
||||
|
||||
if ($ToolName{$tool}{'LettureNodi'} ne "--") {
|
||||
$tools_info .= "<tr>";
|
||||
} elsif ( $ToolName{$tool}{'ToolState'} eq 'Active' ) {
|
||||
$Tools_infoCompany{$ToolName{$tool}{'idCompany'}} .= "<tr bgcolor=\"#fddada\">" . $tool_part;
|
||||
$tools_info .= "<tr bgcolor=\"#fddada\">";
|
||||
} else {
|
||||
$tools_info .= "<tr bgcolor=\"#cfe5e0\">";
|
||||
}
|
||||
$tools_info .= $tool_part;
|
||||
}
|
||||
makeEmailMsg();
|
||||
emailSend();
|
||||
|
||||
foreach my $comp (keys %Tools_infoCompany) {
|
||||
$tools_info = $Tools_infoCompany{$comp};
|
||||
#$email_addr = $ToolName{$comp}{'CompanyEmail'};
|
||||
makeEmailMsg();
|
||||
emailSend();
|
||||
}
|
||||
|
||||
print getLoggingTime() . " - pid $$ >> Execution ended.\n";
|
||||
314
LoadCSVData.pl
Normal file
314
LoadCSVData.pl
Normal file
@@ -0,0 +1,314 @@
|
||||
#!/usr/bin/perl
|
||||
use warnings;
|
||||
use strict;
|
||||
use DBI;
|
||||
use Getopt::Long;
|
||||
use Net::FTP;
|
||||
use List::Util qw( max );
|
||||
use Data::Dumper qw(Dumper);
|
||||
|
||||
$|++; # Autoflush
|
||||
|
||||
my ($filename, $sqlout);
|
||||
my $username = 'ase_lar';
|
||||
my $password = 'laravel';
|
||||
my $db_lar = 'ase_lar';
|
||||
my $server = 'localhost';
|
||||
my $db_name = "ase_mums";
|
||||
my $table = "RAWDATA";
|
||||
my (@CSVData, @sql);
|
||||
my $outcount = 0;
|
||||
my $outfilenum = 0;
|
||||
my $maxsqllines = 10000;
|
||||
my ($tool, $toolbatt, $tooltemp, $tooltype, @channels, @NewData, @din, @nodetype);
|
||||
my $matlab_ip = "160.78.21.71";
|
||||
my $matlab_cmd = "matlab.cmd";
|
||||
my $matlab_timestamp = "";
|
||||
my $ftp_send = 1;
|
||||
my $ftp_addrs;
|
||||
my $ftp_user;
|
||||
my $ftp_passwd;
|
||||
my $ftp_parm;
|
||||
my $ftp_filename;
|
||||
my $ftp_target;
|
||||
my $unit_id;
|
||||
|
||||
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";
|
||||
}
|
||||
if (defined $p1 and $p1 eq "db_ts") {
|
||||
$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 writeOutSql {
|
||||
my ($tn) = @_;
|
||||
my $outfile = $sqlout . "_" . $outfilenum++;
|
||||
open SQLOUT, ">", $outfile or die(getTimeStamp("log") . " - pid $$ >> Error: opening output file " . $outfile . ":$!\n");
|
||||
print getTimeStamp("log") . " - pid $$ >> file $outfile created\n";
|
||||
print SQLOUT "/* lock " . $db_name . ".$table table */\nLOCK TABLES " . $db_name . ".$table WRITE;\n";
|
||||
print SQLOUT "INSERT IGNORE INTO " . $db_name . ".$table\n";
|
||||
print SQLOUT "(`UnitName`,`ToolNameID`,`NodeNum`,`EventDate`,`EventTime`,`BatLevel`,`Temperature`,\n";
|
||||
print SQLOUT "`Val0`,`Val1`,`Val2`,`Val3`,`Val4`,`Val5`,`Val6`,`Val7`,`Val8`,`Val9`,`ValA`,`ValB`,`ValC`,`ValD`,`ValE`,`ValF`)\n";
|
||||
print SQLOUT "VALUES\n";
|
||||
print SQLOUT @sql;
|
||||
@sql = ();
|
||||
print SQLOUT "/* unlock table */\nUNLOCK TABLES;\n";
|
||||
print SQLOUT "/* call stored procedure to copy in ase_lar */\ncall ase_lar.CopyAndFill('$tn');\n";
|
||||
close SQLOUT or die(getTimeStamp("log") . " - pid $$ >> Error in closing file " . $outfile . "\n");
|
||||
my @args = ("mysql", "--login-path=asepath", "-e source " . $outfile);
|
||||
system(@args) == 0 or die (getTimeStamp("log") . " - pid $$ >> system @args failed: $?\n");
|
||||
print getTimeStamp("log") . " - pid $$ >> file $outfile loaded into DB\n";
|
||||
unlink $outfile;
|
||||
return;
|
||||
}
|
||||
|
||||
sub matlabCalc {
|
||||
getMatlabCmd();
|
||||
$matlab_timestamp = getTimeStamp("db_ts");
|
||||
print getTimeStamp("log") . " - pid $$ >> $tool MatLab calc started...\n";
|
||||
my @out_matlab = `ssh1 $matlab_ip 'cmd /c $matlab_cmd ' $tool`;
|
||||
print getTimeStamp("log") . " - pid $$ >> $tool MatLab calc executed: $out_matlab[-1]";
|
||||
#print getTimeStamp("log") . " - @out_matlab";
|
||||
#print getTimeStamp("log") . " - pid $$ >> $out_matlab[-1]";
|
||||
return;
|
||||
}
|
||||
|
||||
sub trxelab {
|
||||
my $dbh = DBI->connect("DBI:mysql:$db_lar;host=$server", $username, $password)
|
||||
or die getTimeStamp("log") . " - Could not connect to database: $DBI::errstr";
|
||||
|
||||
my $sth = $dbh->prepare('select ftp_addrs, ftp_user, ftp_passwd, ftp_parm, ftp_filename, ftp_target from units
|
||||
where id = ' . $unit_id . ';') or die getTimeStamp("log") . " - $DBI::errstr";
|
||||
|
||||
$sth->execute();
|
||||
if ($sth->rows < 0) {
|
||||
die getTimeStamp("log") . " - No unit ftp params selected.\n";
|
||||
} else {
|
||||
while (my $results = $sth->fetchrow_hashref) {
|
||||
$ftp_addrs = $results->{'ftp_addrs'};
|
||||
$ftp_user = $results->{'ftp_user'};
|
||||
$ftp_passwd = $results->{'ftp_passwd'};
|
||||
$ftp_parm = $results->{'ftp_parm'};
|
||||
$ftp_filename = $results->{'ftp_filename'};
|
||||
$ftp_target = $results->{'ftp_target'};
|
||||
}
|
||||
}
|
||||
|
||||
$sth->finish;
|
||||
# Disconnect
|
||||
|
||||
my $fileelab;
|
||||
if ($ftp_filename eq "") {
|
||||
$fileelab = '/var/lib/mysql-files/' . $tool . '_' . getTimeStamp() . '.csv'; #mettere quello che si prende da query
|
||||
} else {
|
||||
$ftp_filename =~ s/(\$\w+)/$1/eeg;
|
||||
$ftp_filename =~ s/\s/_/g;
|
||||
$fileelab = '/var/lib/mysql-files/' . $ftp_filename . '_' . getTimeStamp() . '.csv';
|
||||
}
|
||||
|
||||
my $sthdo = $dbh->do( qq{
|
||||
select * from (
|
||||
(
|
||||
select 'ToolNameID', 'EventDate', 'EventTime', 'NodeNum', 'NodeType', 'NodeDepth',
|
||||
'XShift', 'YShift', 'ZShift' , 'X', 'Y', 'Z', 'HShift', 'HShiftDir', 'HShift_local',
|
||||
'week_shift', 'week_shift_x', 'week_shift_y', 'week_shift_z', 'T_node', 'water_level', 'pressure', 'load_value'
|
||||
)
|
||||
union all
|
||||
(
|
||||
select ToolNameID, EventDate, EventTime, NodeNum, NodeType, NodeDepth,
|
||||
XShift, YShift, ZShift , X, Y, Z, HShift, HShiftDir, HShift_local,
|
||||
week_shift, week_shift_x, week_shift_y, week_shift_z, T_node, water_level, pressure, load_value
|
||||
from ElabDataView
|
||||
where ToolNameID = '$tool' and created_at > '$matlab_timestamp'
|
||||
order by EventDate, EventTime, NodeDepth
|
||||
)
|
||||
|
||||
) resulting_set
|
||||
into outfile '$fileelab'
|
||||
fields terminated by ',' optionally enclosed by '"' lines terminated by '\n'}) or die getTimeStamp("log") . " - $DBI::errstr";
|
||||
|
||||
$dbh->disconnect;
|
||||
$ftp_parm //= "";
|
||||
if ($ftp_parm ne "") {
|
||||
$ftp_parm = "debug => 0, " . $ftp_parm;
|
||||
} else {
|
||||
$ftp_parm = "debug => 0";
|
||||
}
|
||||
|
||||
print getTimeStamp("log") . " - pid $$ >> $tool: ftp parm = $ftp_parm.\n";
|
||||
|
||||
my $ftp = Net::FTP->new($ftp_addrs, debug => 0)
|
||||
or die getTimeStamp("log") . " - pid $$ >> Cannot connect to $ftp_addrs: $@ \n";
|
||||
$ftp->login($ftp_user,$ftp_passwd)
|
||||
or die getTimeStamp("log") . " - pid $$ >> Cannot login " . $ftp->message . "\n";
|
||||
$ftp_target //= "";
|
||||
if ($ftp_target ne "") {
|
||||
$ftp->cwd($ftp_target)
|
||||
or die getTimeStamp("log") . " - pid $$ >> Change remote dir failed " . $ftp->message . "\n";
|
||||
}
|
||||
|
||||
print getTimeStamp("log") . " - pid $$ >> ftp target: " . $ftp->pwd() . ".\n";
|
||||
|
||||
$ftp->put($fileelab)
|
||||
or die getTimeStamp("log") . " - pid $$ >> Put failed " . $ftp->message . "\n";
|
||||
$ftp->quit;
|
||||
print getTimeStamp("log") . " - pid $$ >> $tool: $fileelab ftp put executed.\n";
|
||||
return;
|
||||
}
|
||||
|
||||
sub getNodesType {
|
||||
my $dbh = DBI->connect("DBI:mysql:$db_lar;host=$server", $username, $password)
|
||||
or die getTimeStamp("log") . " - Could not connect to database: $DBI::errstr";
|
||||
|
||||
my $sth = $dbh->prepare('select t.name as name, n.seq as seq, n.num as num, n.channels as channels, y.type as type, n.din as din
|
||||
from nodes as n
|
||||
inner join tools as t on t.id = n.tool_id
|
||||
inner join nodetypes as y on n.nodetype_id = y.id
|
||||
where t.name = "' . $tool . '" order by 3;') or die getTimeStamp("log") . " - $DBI::errstr";
|
||||
|
||||
$sth->execute();
|
||||
if ($sth->rows < 0) {
|
||||
die getTimeStamp("log") . " - No node channels selected.\n";
|
||||
} else {
|
||||
my $row = 0;
|
||||
while (my $results = $sth->fetchrow_hashref) {
|
||||
$channels[$row] = $results->{'channels'};
|
||||
$nodetype[$row] = $results->{'type'};
|
||||
$din[$row++] = $results->{'din'};
|
||||
}
|
||||
}
|
||||
|
||||
$sth->finish;
|
||||
# Disconnect
|
||||
$dbh->disconnect;
|
||||
return;
|
||||
}
|
||||
|
||||
sub getMatlabCmd {
|
||||
my $dbh = DBI->connect("DBI:mysql:$db_lar;host=$server", $username, $password)
|
||||
or die getTimeStamp("log") . " - Could not connect to database: $DBI::errstr";
|
||||
|
||||
my $sth = $dbh->prepare('select m.matcall, t.ftp_send , t.unit_id from matfuncs as m
|
||||
inner join tools as t on t.matfunc = m.id
|
||||
where t.name = "' . $tool . '" ;') or die getTimeStamp("log") . " - $DBI::errstr";
|
||||
|
||||
$sth->execute();
|
||||
if ($sth->rows < 0) {
|
||||
die getTimeStamp("log") . " - No tool's matlab function selected.\n";
|
||||
} else {
|
||||
my $mfunc;
|
||||
while (my $results = $sth->fetchrow_hashref) {
|
||||
$mfunc = uc $results->{'matcall'};
|
||||
$ftp_send = $results->{'ftp_send'};
|
||||
$unit_id = $results->{'unit_id'};
|
||||
}
|
||||
$matlab_cmd = 'matlab_' . $mfunc . '.cmd';
|
||||
}
|
||||
|
||||
$sth->finish;
|
||||
# Disconnect
|
||||
$dbh->disconnect;
|
||||
return;
|
||||
}
|
||||
|
||||
my $starttime = getTimeStamp("log");
|
||||
print "$starttime - pid $$ >> Start execution.\n";
|
||||
|
||||
GetOptions ("filename=s" => \$filename,
|
||||
"sqlout=s" => \$sqlout,
|
||||
"dbname=s" => \$db_name)
|
||||
or die("Error in command line arguments\n");
|
||||
open FILE, $filename or die("Error: opening input file " . $filename . "\n");
|
||||
|
||||
my ($fileDate, $fileTime) = split(/\s/, <FILE>);
|
||||
my ($unittype, $unit) = split(/\s/, uc <FILE>);
|
||||
|
||||
while (my $line = <FILE>) {
|
||||
my($first, $NodeData) = split(/;/, $line, 2);
|
||||
if ( defined $first and ($first ne '')) {
|
||||
if ( ! defined $NodeData or ($NodeData =~ m/^;+/) ) {
|
||||
my @info = (split(/[\/,\.]/, $first));
|
||||
if (defined $info[3] && $info[3] =~ m/^DT\d\d\d\d$/i ) {
|
||||
$tool= uc $info[3];
|
||||
$tooltype = uc $info[2];
|
||||
print getTimeStamp("log") . " - pid $$ >> Found $tooltype tool name: $tool\n";
|
||||
getNodesType();
|
||||
} elsif (defined $info[2] && $info[2] =~ m/^LOC\d*$/i ) {
|
||||
$tool= uc $info[2];
|
||||
$tooltype = 'LOC';
|
||||
print getTimeStamp("log") . " - pid $$ >> Found $tooltype tool name: $tool\n";
|
||||
getNodesType();
|
||||
}
|
||||
} else {
|
||||
$NodeData =~ s/\x0d{0,1}\x0a\Z//s;
|
||||
#$NodeData =~ s/\r\n\z//;
|
||||
|
||||
if ($tooltype eq "MUX" ) {
|
||||
#$NodeData =~ s/Dis\./N\/A/g;
|
||||
@NewData = grep /\S/, split(/\||;/,$NodeData);
|
||||
my $idx = 0;
|
||||
$CSVData[$idx++] = join( ';', splice(@NewData, 0, 2));
|
||||
foreach my $ch (@channels) {
|
||||
$CSVData[$idx++] = join( ';', splice(@NewData, 0, $ch));
|
||||
}
|
||||
#print Dumper \@NewData;
|
||||
print Dumper \@din;
|
||||
} elsif ($tooltype eq "LOC") {
|
||||
if (!defined $din[0]) {
|
||||
print getTimeStamp("log") . " - pid $$ >> Found Din not defined.\n";
|
||||
print getTimeStamp("log") . " - pid $$ >> Execution ended.\n";
|
||||
exit;
|
||||
};
|
||||
my ($Vbat, $Tmod, $Ain1, $Ain2, $Din1, $Din2) = split(/;/,$NodeData);
|
||||
$CSVData[0] = $Vbat . ";" . $Tmod;
|
||||
$CSVData[1] = $Din1;
|
||||
} else {
|
||||
@CSVData = split(/\|/,$NodeData);
|
||||
}
|
||||
|
||||
my $nodenum = 0;
|
||||
foreach (@CSVData) {
|
||||
$sql[($outcount%$maxsqllines)] = "";
|
||||
my(@data) = grep {/\S/} split(/\;/,$_);
|
||||
if ($nodenum eq 0) {
|
||||
$toolbatt = $data[0];
|
||||
$tooltemp = $data[1];
|
||||
} else {
|
||||
my($y, $m, $d, $t) = split(/[\s\/]/,$first);
|
||||
foreach (@data) {
|
||||
if ($sql[($outcount%$maxsqllines)] eq "") {
|
||||
$sql[($outcount%$maxsqllines)] = "('$unit','$tool','$nodenum','$y-$m-$d','$t','$toolbatt','$tooltemp'";
|
||||
}
|
||||
$sql[($outcount%$maxsqllines)] .= ",'" . $_ . "'";
|
||||
}
|
||||
my $InsCompl = ",NULL" x (15 - $#data);
|
||||
$sql[($outcount%$maxsqllines)] .= $InsCompl . ")";
|
||||
if (++$outcount%$maxsqllines eq 0) {
|
||||
$sql[($outcount%$maxsqllines)-1] .= ";\n";
|
||||
writeOutSql($tool);
|
||||
} else {
|
||||
$sql[($outcount%$maxsqllines)-1] .= ",\n";
|
||||
}
|
||||
}
|
||||
$nodenum++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
close FILE;
|
||||
$sql[($outcount%$maxsqllines)-1] =~ s/,$/;/g;
|
||||
writeOutSql($tool);
|
||||
matlabCalc();
|
||||
if ($ftp_send) {
|
||||
trxelab();
|
||||
}
|
||||
|
||||
print getTimeStamp("log") . " - pid $$ >> Execution ended.\n";
|
||||
exit;
|
||||
87
UnitCsvReceiverSmart.pl
Normal file
87
UnitCsvReceiverSmart.pl
Normal 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);
|
||||
Reference in New Issue
Block a user