boh
This commit is contained in:
@@ -8,6 +8,7 @@ use Mail::Sender;
|
|||||||
# Variables
|
# Variables
|
||||||
my $username = 'aseuser';
|
my $username = 'aseuser';
|
||||||
my $password = 'Mums0001!';
|
my $password = 'Mums0001!';
|
||||||
|
|
||||||
#my $database = 'ase_mums';
|
#my $database = 'ase_mums';
|
||||||
my $database = 'ase_lar';
|
my $database = 'ase_lar';
|
||||||
my $db_lar = 'ase_lar';
|
my $db_lar = 'ase_lar';
|
||||||
@@ -15,14 +16,20 @@ my $server = 'localhost';
|
|||||||
my ( $email_msg, $email_obj, $tools_info, $ref_date );
|
my ( $email_msg, $email_obj, $tools_info, $ref_date );
|
||||||
my $act_tool = 0;
|
my $act_tool = 0;
|
||||||
my $gd_tool = 0;
|
my $gd_tool = 0;
|
||||||
|
|
||||||
#my $email_addr = 'alessandro.battilani@gmail.com';
|
#my $email_addr = 'alessandro.battilani@gmail.com';
|
||||||
my $email_addr = 'andrea.segalini@aseltd.eu,andrea.carri@aseltd.eu,roberto.savi@aseltd.eu,edoardo.cavalca@aseltd.eu,alessandro.valletta@aseltd.eu,alessandro.battilani@gmail.com,corrado@geielettronica.it,luca.chiapponi@aseltd.eu';
|
my $email_addr =
|
||||||
|
'andrea.segalini@aseltd.eu,andrea.carri@aseltd.eu,roberto.savi@aseltd.eu,edoardo.cavalca@aseltd.eu,alessandro.valletta@aseltd.eu,alessandro.battilani@gmail.com,corrado@geielettronica.it,luca.chiapponi@aseltd.eu';
|
||||||
my $cc_email_addr = $email_addr;
|
my $cc_email_addr = $email_addr;
|
||||||
|
|
||||||
sub getLoggingTime {
|
sub getLoggingTime {
|
||||||
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
|
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
|
||||||
my $nice_timestamp = sprintf ( "%04d%02d%02d %02d:%02d:%02d",
|
localtime(time);
|
||||||
$year+1900,$mon+1,$mday,$hour,$min,$sec);
|
my $nice_timestamp = sprintf(
|
||||||
|
"%04d%02d%02d %02d:%02d:%02d",
|
||||||
|
$year + 1900,
|
||||||
|
$mon + 1, $mday, $hour, $min, $sec
|
||||||
|
);
|
||||||
return $nice_timestamp;
|
return $nice_timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,7 +105,8 @@ sub emailSend {
|
|||||||
my $mail_res = $sender->MailMsg( { msg => $sender->{msg}, } );
|
my $mail_res = $sender->MailMsg( { msg => $sender->{msg}, } );
|
||||||
if ( ref($mail_res) ne "Mail::Sender" ) {
|
if ( ref($mail_res) ne "Mail::Sender" ) {
|
||||||
print getLoggingTime() . " - $mail_res: $sender->{error_msg} \n";
|
print getLoggingTime() . " - $mail_res: $sender->{error_msg} \n";
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
print getLoggingTime() . " - $email_obj\n";
|
print getLoggingTime() . " - $email_obj\n";
|
||||||
print getLoggingTime() . " - Mail sent to: $email_addr.\n";
|
print getLoggingTime() . " - Mail sent to: $email_addr.\n";
|
||||||
}
|
}
|
||||||
@@ -106,33 +114,55 @@ sub emailSend {
|
|||||||
|
|
||||||
my %ToolName = ();
|
my %ToolName = ();
|
||||||
my %Tools_infoCompany = ();
|
my %Tools_infoCompany = ();
|
||||||
|
my %Tools_infoCompanySite = ();
|
||||||
|
|
||||||
print getLoggingTime() . " - pid $$ >> Start execution.\n";
|
print getLoggingTime() . " - pid $$ >> Start execution.\n";
|
||||||
|
|
||||||
my $dbh = DBI->connect( "DBI:mysql:$db_lar;host=$server", $username, $password )
|
my $dbh = DBI->connect( "DBI:mysql:$db_lar;host=$server", $username, $password )
|
||||||
or die getLoggingTime() . " - Could not connect to database: $DBI::errstr";
|
or die getLoggingTime() . " - Could not connect to database: $DBI::errstr";
|
||||||
|
|
||||||
my $sth = $dbh->prepare('select idCompany, CompanyNameShort, SiteName, CompanyEmail, SendAlarm, UnitNameId, ToolNameID, ToolState from mail_view;')
|
my $sth = $dbh->prepare(
|
||||||
or die getLoggingTime() . " - $DBI::errstr";
|
'select idCompany, CompanyNameShort, idSite, SiteName, CompanyEmail, SiteAlarmEmail, SendAlarm, UnitNameId, ToolNameID, ToolState from mail_view;'
|
||||||
|
) or die getLoggingTime() . " - $DBI::errstr";
|
||||||
|
|
||||||
$sth->execute();
|
$sth->execute();
|
||||||
my $ToolNum = $sth->rows;
|
my $ToolNum = $sth->rows;
|
||||||
if ( $sth->rows < 0 ) {
|
if ( $sth->rows < 0 ) {
|
||||||
print getLoggingTime() . " - No rows from TOOLS & UNITS.\n";
|
print getLoggingTime() . " - No rows from TOOLS & UNITS.\n";
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
while ( my $results = $sth->fetchrow_hashref ) {
|
while ( my $results = $sth->fetchrow_hashref ) {
|
||||||
$ToolName{$results->{UnitNameId} . $results->{'ToolNameID'}}{'idCompany'} = $results->{'idCompany'};
|
$ToolName{ $results->{'UnitNameId'} . $results->{'ToolNameID'} }
|
||||||
$ToolName{$results->{UnitNameId} . $results->{'ToolNameID'}}{'CompanyNameShort'} = $results->{'CompanyNameShort'};
|
{'idCompany'} = $results->{'idCompany'};
|
||||||
$ToolName{$results->{UnitNameId} . $results->{'ToolNameID'}}{'SiteName'} = $results->{'SiteName'};
|
$ToolName{ $results->{'UnitNameId'} . $results->{'ToolNameID'} }
|
||||||
$ToolName{$results->{'idCompany'}}{'CompanyEmail'} = $results->{'CompanyEmail'};
|
{'CompanyNameShort'} = $results->{'CompanyNameShort'};
|
||||||
$ToolName{$results->{'idCompany'}}{'SendAlarm'} = $results->{'SendAlarm'};
|
$ToolName{ $results->{'UnitNameId'} . $results->{'ToolNameID'} }
|
||||||
$ToolName{$results->{UnitNameId} . $results->{'ToolNameID'}}{'UnitNameId'} = $results->{'UnitNameId'};
|
{'idSite'} = $results->{'idSite'};
|
||||||
$ToolName{$results->{UnitNameId} . $results->{'ToolNameID'}}{'ToolNameID'} = $results->{'ToolNameID'};
|
$ToolName{ $results->{'UnitNameId'} . $results->{'ToolNameID'} }
|
||||||
$ToolName{$results->{UnitNameId} . $results->{'ToolNameID'}}{'ToolState'} = $results->{'ToolState'};
|
{'SiteName'} = $results->{'SiteName'};
|
||||||
$ToolName{$results->{UnitNameId} . $results->{'ToolNameID'}}{'LettureNodi'} = "--";
|
$ToolName{ $results->{'idSite'} }{'SiteAlarmEmail'} =
|
||||||
$ToolName{$results->{UnitNameId} . $results->{'ToolNameID'}}{'LettureCatene'} = "--";
|
$results->{'SiteAlarmEmail'};
|
||||||
$ToolName{$results->{UnitNameId} . $results->{'ToolNameID'}}{'NumeroNodi'} = "--";
|
$ToolName{ $results->{'idSite'} }{'SendAlarm'} =
|
||||||
$ToolName{$results->{UnitNameId} . $results->{'ToolNameID'}}{'ErroriTotaliLetture'} = "--";
|
$results->{'SendAlarm'};
|
||||||
|
$ToolName{ $results->{'idCompany'} }{'CompanyEmail'} =
|
||||||
|
$results->{'CompanyEmail'};
|
||||||
|
$ToolName{ $results->{'idCompany'} }{'SendAlarm'} =
|
||||||
|
$results->{'SendAlarm'};
|
||||||
|
$ToolName{ $results->{'UnitNameId'} . $results->{'ToolNameID'} }
|
||||||
|
{'UnitNameId'} = $results->{'UnitNameId'};
|
||||||
|
$ToolName{ $results->{'UnitNameId'} . $results->{'ToolNameID'} }
|
||||||
|
{'ToolNameID'} = $results->{'ToolNameID'};
|
||||||
|
$ToolName{ $results->{'UnitNameId'} . $results->{'ToolNameID'} }
|
||||||
|
{'ToolState'} = $results->{'ToolState'};
|
||||||
|
$ToolName{ $results->{'UnitNameId'} . $results->{'ToolNameID'} }
|
||||||
|
{'LettureNodi'} = "--";
|
||||||
|
$ToolName{ $results->{'UnitNameId'} . $results->{'ToolNameID'} }
|
||||||
|
{'LettureCatene'} = "--";
|
||||||
|
$ToolName{ $results->{'UnitNameId'} . $results->{'ToolNameID'} }
|
||||||
|
{'NumeroNodi'} = "--";
|
||||||
|
$ToolName{ $results->{'UnitNameId'} . $results->{'ToolNameID'} }
|
||||||
|
{'ErroriTotaliLetture'} = "--";
|
||||||
|
|
||||||
if ( $results->{'ToolState'} eq "Active" ) {
|
if ( $results->{'ToolState'} eq "Active" ) {
|
||||||
$act_tool++;
|
$act_tool++;
|
||||||
}
|
}
|
||||||
@@ -140,24 +170,30 @@ if ($sth->rows < 0) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$sth->finish;
|
$sth->finish;
|
||||||
|
|
||||||
# Disconnect
|
# Disconnect
|
||||||
$dbh->disconnect;
|
$dbh->disconnect;
|
||||||
|
|
||||||
$dbh = DBI->connect( "DBI:mysql:$database;host=$server", $username, $password )
|
$dbh = DBI->connect( "DBI:mysql:$database;host=$server", $username, $password )
|
||||||
or die getLoggingTime() . " - Could not connect to database: $DBI::errstr";
|
or die getLoggingTime() . " - Could not connect to database: $DBI::errstr";
|
||||||
|
|
||||||
my $stw = $dbh->prepare("select UnitName, ToolNameID, EventDate, count(EventTime) 'LettureNodi', truncate(Count(EventTime)/max(NodeNum),1) 'LettureCatene', max(NodeNum) 'NumeroNodi' from RAWDATACOR where EventDate = subdate(curdate(),1) group by UnitName, ToolNameID")
|
my $stw = $dbh->prepare(
|
||||||
or die getLoggingTime() . " - $DBI::errstr";
|
"select UnitName, ToolNameID, EventDate, count(EventTime) 'LettureNodi', truncate(Count(EventTime)/max(NodeNum),1) 'LettureCatene', max(NodeNum) 'NumeroNodi' from RAWDATACOR where EventDate = subdate(curdate(),1) group by UnitName, ToolNameID"
|
||||||
|
) or die getLoggingTime() . " - $DBI::errstr";
|
||||||
$stw->execute();
|
$stw->execute();
|
||||||
|
|
||||||
if ( $stw->rows == 0 ) {
|
if ( $stw->rows == 0 ) {
|
||||||
print getLoggingTime() . " - No rows from RAWDATACOR.\n";
|
print getLoggingTime() . " - No rows from RAWDATACOR.\n";
|
||||||
$email_obj = "ATTENZIONE: nessuna lettura pervenuta!!!"
|
$email_obj = "ATTENZIONE: nessuna lettura pervenuta!!!";
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
while ( my $results = $stw->fetchrow_hashref ) {
|
while ( my $results = $stw->fetchrow_hashref ) {
|
||||||
$ToolName{$results->{'UnitName'} . $results->{'ToolNameID'}}{'LettureNodi'} = $results->{'LettureNodi'};
|
$ToolName{ $results->{'UnitName'} . $results->{'ToolNameID'} }
|
||||||
$ToolName{$results->{'UnitName'} . $results->{'ToolNameID'}}{'LettureCatene'} = $results->{'LettureCatene'};
|
{'LettureNodi'} = $results->{'LettureNodi'};
|
||||||
$ToolName{$results->{'UnitName'} . $results->{'ToolNameID'}}{'NumeroNodi'} = $results->{'NumeroNodi'};
|
$ToolName{ $results->{'UnitName'} . $results->{'ToolNameID'} }
|
||||||
|
{'LettureCatene'} = $results->{'LettureCatene'};
|
||||||
|
$ToolName{ $results->{'UnitName'} . $results->{'ToolNameID'} }
|
||||||
|
{'NumeroNodi'} = $results->{'NumeroNodi'};
|
||||||
$ref_date = $results->{'EventDate'};
|
$ref_date = $results->{'EventDate'};
|
||||||
if ( substr( $results->{'ToolNameID'}, 0, 2 ) eq "GD" ) {
|
if ( substr( $results->{'ToolNameID'}, 0, 2 ) eq "GD" ) {
|
||||||
$gd_tool++;
|
$gd_tool++;
|
||||||
@@ -165,20 +201,25 @@ if ($stw->rows == 0) {
|
|||||||
}
|
}
|
||||||
if ( ( $act_tool + $gd_tool ) == $stw->rows ) {
|
if ( ( $act_tool + $gd_tool ) == $stw->rows ) {
|
||||||
$email_obj = "Tutte le letture pervenute.";
|
$email_obj = "Tutte le letture pervenute.";
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
$email_obj = "ATTENZIONE: alcune letture non pervenute!!!";
|
$email_obj = "ATTENZIONE: alcune letture non pervenute!!!";
|
||||||
}
|
}
|
||||||
print getLoggingTime() . " - tool attivi: $act_tool - GD tool: $gd_tool - righe query: " . $stw->rows . ".\n";
|
print getLoggingTime()
|
||||||
|
. " - tool attivi: $act_tool - GD tool: $gd_tool - righe query: "
|
||||||
|
. $stw->rows . ".\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
$stw->finish;
|
$stw->finish;
|
||||||
|
|
||||||
my $ste = $dbh->prepare("select UnitName, ToolNameID, count(val0) 'ErroriTotaliLetture' from RAWDATACOR where EventDate = ? and val0 = 'Err1' group by ToolNameID")
|
my $ste = $dbh->prepare(
|
||||||
or die getLoggingTime() . " - $DBI::errstr";
|
"select UnitName, ToolNameID, count(val0) 'ErroriTotaliLetture' from RAWDATACOR where EventDate = ? and val0 = 'Err1' group by ToolNameID"
|
||||||
|
) or die getLoggingTime() . " - $DBI::errstr";
|
||||||
$ste->bind_param( 1, $ref_date );
|
$ste->bind_param( 1, $ref_date );
|
||||||
$ste->execute();
|
$ste->execute();
|
||||||
while ( my $results = $ste->fetchrow_hashref ) {
|
while ( my $results = $ste->fetchrow_hashref ) {
|
||||||
$ToolName{$results->{'UnitName'} . $results->{'ToolNameID'}}{'ErroriTotaliLetture'} = $results->{'ErroriTotaliLetture'};
|
$ToolName{ $results->{'UnitName'} . $results->{'ToolNameID'} }
|
||||||
|
{'ErroriTotaliLetture'} = $results->{'ErroriTotaliLetture'};
|
||||||
}
|
}
|
||||||
|
|
||||||
$ste->finish;
|
$ste->finish;
|
||||||
@@ -193,18 +234,44 @@ foreach my $tool (sort grep { /DT|PT/ } keys %ToolName){
|
|||||||
next; #skip dei tool nello stato del test
|
next; #skip dei tool nello stato del test
|
||||||
}
|
}
|
||||||
|
|
||||||
my $tool_part = "<td>" . $ToolName{$tool}{'CompanyNameShort'} . "</td><td>". $ToolName{$tool}{'SiteName'} . "</td>";
|
my $tool_part = "<td>"
|
||||||
$tool_part .= "<td align=\"center\">" . $ToolName{$tool}{'UnitNameId'} . "</td><td align=\"center\">" . $ToolName{$tool}{ToolNameID} . "</td>";
|
. $ToolName{$tool}{'CompanyNameShort'}
|
||||||
$tool_part .= "<td align=\"right\">" . $ToolName{$tool}{'LettureNodi'} . "</td><td align=\"right\">" . $ToolName{$tool}{'NumeroNodi'} . "</td>";
|
. "</td><td>"
|
||||||
$tool_part .= "<td align=\"right\">" . $ToolName{$tool}{'LettureCatene'} . "</td>";
|
. $ToolName{$tool}{'SiteName'} . "</td>";
|
||||||
$tool_part .= "<td align=\"center\">" . $ToolName{$tool}{'ErroriTotaliLetture'} . "</td><td align=\"center\">" . $ToolName{$tool}{'ToolState'} . "</td></tr>\n";
|
$tool_part .=
|
||||||
|
"<td align=\"center\">"
|
||||||
|
. $ToolName{$tool}{'UnitNameId'}
|
||||||
|
. "</td><td align=\"center\">"
|
||||||
|
. $ToolName{$tool}{ToolNameID} . "</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 "--" ) {
|
if ( $ToolName{$tool}{'LettureNodi'} ne "--" ) {
|
||||||
$tools_info .= "<tr>";
|
$tools_info .= "<tr>";
|
||||||
} elsif ( $ToolName{$tool}{'ToolState'} eq 'Active' ) {
|
}
|
||||||
$Tools_infoCompany{$ToolName{$tool}{'idCompany'}} .= "<tr bgcolor=\"#fddada\">" . $tool_part;
|
elsif ( $ToolName{$tool}{'ToolState'} eq 'Active' ) {
|
||||||
|
if ( $ToolName{ $ToolName{$tool}{'idSite'} }{'SiteAlarmEmail'} eq "" ) {
|
||||||
|
$Tools_infoCompany{ $ToolName{$tool}{'idCompany'} } .=
|
||||||
|
"<tr bgcolor=\"#fddada\">" . $tool_part;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$Tools_infoCompanySite{ $ToolName{$tool}{'idSite'} } .=
|
||||||
|
"<tr bgcolor=\"#fddada\">" . $tool_part;
|
||||||
|
}
|
||||||
$tools_info .= "<tr bgcolor=\"#fddada\">";
|
$tools_info .= "<tr bgcolor=\"#fddada\">";
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
$tools_info .= "<tr bgcolor=\"#cfe5e0\">";
|
$tools_info .= "<tr bgcolor=\"#cfe5e0\">";
|
||||||
}
|
}
|
||||||
$tools_info .= $tool_part;
|
$tools_info .= $tool_part;
|
||||||
@@ -212,15 +279,35 @@ foreach my $tool (sort grep { /DT|PT/ } keys %ToolName){
|
|||||||
makeEmailMsg();
|
makeEmailMsg();
|
||||||
emailSend();
|
emailSend();
|
||||||
|
|
||||||
|
#print $email_msg;print "OOOOOOOOOOOOOO=====================OOOOOOOOOOOOOO\n";
|
||||||
|
|
||||||
foreach my $comp ( keys %Tools_infoCompany ) {
|
foreach my $comp ( keys %Tools_infoCompany ) {
|
||||||
$tools_info = $Tools_infoCompany{$comp};
|
$tools_info = $Tools_infoCompany{$comp};
|
||||||
my $comp_email = $ToolName{$comp}{'CompanyEmail'};
|
my $comp_email = $ToolName{$comp}{'CompanyEmail'};
|
||||||
my $send_alarm = $ToolName{$comp}{'SendAlarm'};
|
my $send_alarm = $ToolName{$comp}{'SendAlarm'};
|
||||||
print getLoggingTime() . " - Company email: $comp_email - Send Alarm: $send_alarm\n";
|
print getLoggingTime()
|
||||||
|
. " - Company email: $comp_email - Send Alarm: $send_alarm\n";
|
||||||
if ( $send_alarm == 1 ) {
|
if ( $send_alarm == 1 ) {
|
||||||
$email_addr = $ToolName{$comp}{'CompanyEmail'};
|
$email_addr = $ToolName{$comp}{'CompanyEmail'};
|
||||||
makeEmailMsg();
|
makeEmailMsg();
|
||||||
emailSend();
|
emailSend();
|
||||||
|
|
||||||
|
#print $email_msg;print "XXXXXXXXXXXXX=====================XXXXXXXXXXXXX\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach my $site ( keys %Tools_infoCompanySite ) {
|
||||||
|
$tools_info = $Tools_infoCompanySite{$site};
|
||||||
|
my $site_email = $ToolName{$site}{'SiteAlarmEmail'};
|
||||||
|
my $send_alarm = $ToolName{$site}{'SendAlarm'};
|
||||||
|
print getLoggingTime()
|
||||||
|
. " - Site email: $site_email - Send Alarm: $send_alarm\n";
|
||||||
|
if ( $send_alarm == 1 ) {
|
||||||
|
$email_addr = $site_email;
|
||||||
|
makeEmailMsg();
|
||||||
|
emailSend();
|
||||||
|
|
||||||
|
#print $email_msg;print "|||||||||||||||=====================|||||||||||||||\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ my $matlab_rt = "/usr/local/MATLAB/MATLAB_Runtime/v93";
|
|||||||
my $matlab_func_dir = "/usr/local/matlab_func/";
|
my $matlab_func_dir = "/usr/local/matlab_func/";
|
||||||
my $matlab_error = '';
|
my $matlab_error = '';
|
||||||
my $matlab_timeout = '1800';
|
my $matlab_timeout = '1800';
|
||||||
my $email_addr = 'andrea.carri@aseltd.eu,roberto.savi@aseltd.eu,edoardo.cavalca@aseltd.eu,alessandro.battilani@gmail.com,alessandro.valletta@aseltd.eu';
|
my $email_addr = 'andrea.carri@aseltd.eu,edoardo.cavalca@aseltd.eu,alessandro.battilani@gmail.com,alessandro.valletta@aseltd.eu';
|
||||||
my ($email_msg, $email_obj);
|
my ($email_msg, $email_obj);
|
||||||
my $G201_fcheck = 0;
|
my $G201_fcheck = 0;
|
||||||
|
|
||||||
|
|||||||
162
SendElabData.pl
Executable file
162
SendElabData.pl
Executable file
@@ -0,0 +1,162 @@
|
|||||||
|
#!/usr/bin/perl
|
||||||
|
use warnings;
|
||||||
|
use strict;
|
||||||
|
use DBI;
|
||||||
|
use Getopt::Long;
|
||||||
|
use Net::FTP;
|
||||||
|
use File::Basename qw( fileparse );
|
||||||
|
use List::Util qw( max );
|
||||||
|
use Data::Dumper qw(Dumper);
|
||||||
|
use Cwd;
|
||||||
|
|
||||||
|
$|++; # Autoflush
|
||||||
|
|
||||||
|
my $filename;
|
||||||
|
my $username = 'ase_lar';
|
||||||
|
my $password = 'laravel';
|
||||||
|
my $db_lar = 'ase_lar';
|
||||||
|
my $db_sp = 'ase_lar';
|
||||||
|
my $server = 'localhost';
|
||||||
|
my $db_name = "ase_lar";
|
||||||
|
my @sql;
|
||||||
|
my ($unit, $tool);
|
||||||
|
my $matlab_timestamp = "";
|
||||||
|
my $ftp_addrs;
|
||||||
|
my $ftp_user;
|
||||||
|
my $ftp_passwd;
|
||||||
|
my $ftp_parm;
|
||||||
|
my $ftp_filename;
|
||||||
|
my $ftp_target;
|
||||||
|
my $duedate = '';
|
||||||
|
|
||||||
|
|
||||||
|
sub getTimeStamp{ # parm [ts] => timestamp for filename; log => timestamp for log
|
||||||
|
my $format = "%04d%02d%02d%02d%02d%02d";
|
||||||
|
my ($p1) = @_;
|
||||||
|
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime(time());
|
||||||
|
if ( defined $p1 and $p1 eq "log" ) {
|
||||||
|
$format = "%04d%02d%02d %02d:%02d:%02d";
|
||||||
|
}
|
||||||
|
if ( defined $p1 and $p1 eq "db_ts" ) {
|
||||||
|
( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime(time() - 36000);
|
||||||
|
$format = "%04d-%02d-%02d %02d:%02d:%02d";
|
||||||
|
}
|
||||||
|
|
||||||
|
my $timestamp =sprintf( $format, $year + 1900, $mon + 1, $mday, $hour, $min, $sec );
|
||||||
|
return $timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
sub trxelab {
|
||||||
|
my $dbh =DBI->connect( "DBI:mysql:$db_lar;host=$server", $username, $password )
|
||||||
|
or die getTimeStamp("log"). " - pid $$ >> Could not connect to database: $DBI::errstr";
|
||||||
|
|
||||||
|
my $sth = $dbh->prepare(
|
||||||
|
'select ftp_addrs, ftp_user, ftp_passwd, ftp_parm, ftp_filename, ftp_target, duedate from units
|
||||||
|
where name = "' . $unit . '";'
|
||||||
|
) or die getTimeStamp("log") . " - pid $$ >> $DBI::errstr";
|
||||||
|
|
||||||
|
$sth->execute();
|
||||||
|
if ( $sth->rows eq 0 ) {
|
||||||
|
die getTimeStamp("log") . " - pid $$ >> 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'};
|
||||||
|
$duedate = $results->{'duedate'};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$sth->finish;
|
||||||
|
|
||||||
|
# Disconnect
|
||||||
|
|
||||||
|
my $fileelab;
|
||||||
|
if ( $ftp_filename eq "" ) {
|
||||||
|
$fileelab ='/var/lib/mysql-files/'. $tool . '_'. getTimeStamp(). '.csv'; #mettere quello che si prende da query *** aggiungere $unit
|
||||||
|
}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',
|
||||||
|
'speed', 'speed_local', 'acceleration', 'acceleration_local', 'T_node', 'water_level', 'pressure', 'load_value', 'AlfaX', 'AlfaY', 'CalcErr'
|
||||||
|
union all
|
||||||
|
select ToolNameID, EventDate, EventTime, NodeNum, NodeType, NodeDepth,
|
||||||
|
XShift, YShift, ZShift , X, Y, Z, HShift, HShiftDir, HShift_local,
|
||||||
|
speed, speed_local, acceleration, acceleration_local, T_node, water_level, pressure, load_value, AlfaX, AlfaY, calcerr
|
||||||
|
from elabdataview
|
||||||
|
where UnitName = '$unit' and ToolNameID = '$tool' and updated_at > '$matlab_timestamp'
|
||||||
|
order by ToolNameID DESC, EventDate, EventTime, convert(`NodeNum`, UNSIGNED INTEGER) DESC
|
||||||
|
) resulting_set
|
||||||
|
into outfile '$fileelab'
|
||||||
|
fields terminated by ',' optionally enclosed by '"' lines terminated by '\n'}
|
||||||
|
) or die getTimeStamp("log") . " - pid $$ >> $DBI::errstr";
|
||||||
|
|
||||||
|
$dbh->disconnect;
|
||||||
|
$ftp_parm //= "";
|
||||||
|
$ftp_parm =~ s/\s//g;
|
||||||
|
|
||||||
|
my %ftp_parms = ('Host' => $ftp_addrs,);
|
||||||
|
|
||||||
|
foreach my $parm (split(/,/, $ftp_parm)){
|
||||||
|
my ($key, $val) = split(/=>/, $parm);
|
||||||
|
$ftp_parms{$key} = $val;
|
||||||
|
}
|
||||||
|
$ftp_parms{'Debug'} = 0;
|
||||||
|
$ftp_parms{'Passive'} = 1;
|
||||||
|
|
||||||
|
print getTimeStamp("log") . " - pid $$ >> $unit - $tool ftp parm:\n" . Dumper(%ftp_parms);
|
||||||
|
|
||||||
|
if ( !defined $duedate
|
||||||
|
or($duedate eq '0000-00-00 00:00:00')
|
||||||
|
or($duedate eq '')
|
||||||
|
or($duedate gt getTimeStamp("db_ts") )) {
|
||||||
|
my $ftp = Net::FTP->new(%ftp_parms)
|
||||||
|
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";
|
||||||
|
|
||||||
|
print getTimeStamp("log"). " - pid $$ >> ftp target lists ". scalar @{ $ftp->ls() }. " elements.\n";
|
||||||
|
|
||||||
|
$ftp->quit;
|
||||||
|
print getTimeStamp("log"). " - pid $$ >> $unit - $tool: $fileelab ftp put executed.\n";
|
||||||
|
} else {
|
||||||
|
print getTimeStamp("log"). " - pid $$ >> $unit - $tool: $fileelab ftp put didn't executed because due date reached.\n";
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
my $starttime = getTimeStamp("log");
|
||||||
|
print "$starttime - pid $$ >> Start execution.\n";
|
||||||
|
|
||||||
|
GetOptions(
|
||||||
|
"unit=s" => \$unit,
|
||||||
|
"tool=s" => \$tool
|
||||||
|
) or die("Error in command line arguments\n");
|
||||||
|
die("ERROR: unit must be specified.") unless defined $unit;
|
||||||
|
die("ERROR: tool must be specified.") unless defined $tool;
|
||||||
|
|
||||||
|
$matlab_timestamp = getTimeStamp("db_ts");
|
||||||
|
trxelab();
|
||||||
|
|
||||||
|
print getTimeStamp("log") . " - pid $$ >> Execution ended.\n";
|
||||||
|
exit;
|
||||||
Reference in New Issue
Block a user