280 lines
8.3 KiB
Perl
Executable File
280 lines
8.3 KiB
Perl
Executable File
#!/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;
|
|
use MIME::Lite;
|
|
|
|
$|++; # Autoflush
|
|
|
|
my ( $filename, $sqlout );
|
|
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 $table = "RAWDATACOR";
|
|
my ( @CSVData, @sql );
|
|
my $outcount = 0;
|
|
my $outfilenum = 0;
|
|
my $maxsqllines = 10000;
|
|
my (
|
|
$tool, $toolbatt, $tooltemp, $tooltype, @channels,
|
|
@NewData, @ain, @din, @nodetype, $GDEventDate
|
|
);
|
|
|
|
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 $duedate = '';
|
|
my ( $unit_id, $unit, $unittype );
|
|
my ( $fileDate, $fileTime );
|
|
my $matlab_rt = "/usr/local/MATLAB/MATLAB_Runtime/v93";
|
|
my $matlab_func_dir = "/usr/local/matlab_func/";
|
|
my $matlab_error = '';
|
|
my $matlab_timeout = '1800';
|
|
my $email_addr =
|
|
'andrea.carri@aseltd.eu,alessandro.battilani@gmail.com,alessandro.valletta@aseltd.eu';
|
|
my $from = 'ASE Alert System<alert@aseltd.eu>';
|
|
my ( $email_msg, $email_obj );
|
|
my $G201_fcheck = 0;
|
|
my $D2W_fcheck = 0;
|
|
my $G301_fcheck = 0;
|
|
my $FtpToCustomerCmd = 'SendFtpElabData.pl';
|
|
my ( $scriptname, $scriptpath );
|
|
my $MatlabErrorFilename = "/tmp/";
|
|
my @matlabOutputErrorArray;
|
|
my @errors;
|
|
my @warnings;
|
|
my $MatlabErrors = '';
|
|
my $MatlabWarnings = '';
|
|
my $tool_status = '';
|
|
|
|
sub uniq {
|
|
my %seen;
|
|
grep !$seen{$_}++, @_;
|
|
}
|
|
|
|
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 matlabCalc {
|
|
|
|
$matlab_timestamp = getTimeStamp("db_ts");
|
|
print getTimeStamp("log")
|
|
. " - pid $$ >> $unit - $tool MatLab calc started...\n";
|
|
|
|
$matlab_cmd = './run_' . $matlab_cmd . '.sh';
|
|
$MatlabErrorFilename .= "${unit}${tool}_output_error.txt";
|
|
|
|
#print $ENV{"HOME"} . "\n";
|
|
$ENV{"HOME"} = "/" . $ENV{"HOME"};
|
|
chdir($matlab_func_dir)
|
|
or die "cannot change: $!\n";
|
|
print getTimeStamp("log") . " - pid $$ >> current dir: $matlab_func_dir\n";
|
|
my @args =
|
|
( 'timeout', $matlab_timeout, $matlab_cmd, $matlab_rt, $unit, $tool );
|
|
if ( system(@args) != 0 ) {
|
|
open( my $fh, '<', $MatlabErrorFilename )
|
|
or warn print getTimeStamp("log")
|
|
. " - pid $$ >> Cannot open Matlab output error file: ${MatlabErrorFilename}\n";
|
|
{
|
|
chomp( @matlabOutputErrorArray = <$fh> );
|
|
}
|
|
close($fh);
|
|
@matlabOutputErrorArray = grep( /\S/, @matlabOutputErrorArray );
|
|
@errors = grep( /^Error/, @matlabOutputErrorArray );
|
|
@warnings = grep( !/^Error/, @matlabOutputErrorArray );
|
|
$MatlabErrors = join( "<br/>", @errors );
|
|
$MatlabWarnings = join( "<br/>", uniq(@warnings) );
|
|
my $exit_value = $? >> 8;
|
|
|
|
if ( $exit_value == 124 ) {
|
|
print getTimeStamp("log")
|
|
. " - pid $$ >> system @args excessive duration: killed after $matlab_timeout seconds\n";
|
|
$matlab_error =
|
|
"Matlab elab excessive duration: killed after $matlab_timeout seconds";
|
|
}
|
|
else {
|
|
print getTimeStamp("log")
|
|
. " - pid $$ >> system @args failed: return code $exit_value - $?\n";
|
|
$matlab_error = "Matlab elab failed: $exit_value - $?";
|
|
}
|
|
makeEmailMsg();
|
|
emailSend();
|
|
print getTimeStamp("log") . " - pid $$ >> Execution ended.\n";
|
|
exit(1);
|
|
}
|
|
|
|
print getTimeStamp("log")
|
|
. " - pid $$ >> $unit - $tool MatLab calc executed.\n";
|
|
|
|
return;
|
|
}
|
|
|
|
sub getMatlabCmd {
|
|
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 m.matcall, t.ftp_send , t.unit_id, s.`desc` as statustools from matfuncs as m
|
|
inner join tools as t on t.matfunc = m.id
|
|
inner join units as u on u.id = t.unit_id
|
|
inner join statustools as s on t.statustool_id = s.id
|
|
where t.name = "' . $tool . '" and u.name = "' . $unit . '";'
|
|
) or die getTimeStamp("log") . " - pid $$ >> $DBI::errstr";
|
|
|
|
$sth->execute();
|
|
if ( $sth->rows eq 0 ) {
|
|
die getTimeStamp("log")
|
|
. " - pid $$ >> No tool's matlab function selected.\n";
|
|
}
|
|
else {
|
|
while ( my $results = $sth->fetchrow_hashref ) {
|
|
$matlab_cmd = $results->{'matcall'};
|
|
$ftp_send = $results->{'ftp_send'};
|
|
$unit_id = $results->{'unit_id'};
|
|
$tool_status = $results->{'statustools'};
|
|
}
|
|
}
|
|
|
|
$sth->finish;
|
|
|
|
# Disconnect
|
|
$dbh->disconnect;
|
|
return;
|
|
}
|
|
|
|
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 xmlns="http://www.w3.org/1999/xhtml">
|
|
|
|
<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 bgcolor="#ffffff" border="0" cellpadding="0" cellspacing="0" width="100%">
|
|
<tr>
|
|
<td align="center">
|
|
<img src="https://www2.aseltd.eu/static/img/logo_ASE_small.png" alt="ASE" style="display: block;" />
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center">
|
|
<h1 style="margin: 5px;">Alert from ASE:</h1>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center">
|
|
<h3 style="margin: 5px;">Matlab function $matlab_cmd failed on unit => $unit - tool => $tool</h3>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center">
|
|
<h4 style="margin: 5px;">$matlab_error</h4>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td style="padding: 20px; padding-bottom: 0px; color: red">
|
|
$MatlabErrors
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td style="padding: 20px;">
|
|
$MatlabWarnings
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</body>
|
|
</html>
|
|
END_MSG
|
|
}
|
|
|
|
sub emailSend {
|
|
$email_obj = 'Matlab error on unit ' . $unit . ' - tool ' . $tool;
|
|
my $msg = MIME::Lite->new(
|
|
From => $from,
|
|
To => $email_addr,
|
|
Subject => $email_obj,
|
|
Data => $email_msg
|
|
);
|
|
|
|
$msg->attr( "content-type" => "text/html" );
|
|
if (
|
|
$msg->send(
|
|
'smtp', "smtps.aruba.it",
|
|
AuthUser => "alert\@aseltd.eu",
|
|
AuthPass => "Ase#2013!20\@bat",
|
|
Port => 465,
|
|
SSL => 1,
|
|
Debug => 0
|
|
)
|
|
)
|
|
{
|
|
print getTimeStamp("log") . " - pid $$ >> $email_obj\n";
|
|
print getTimeStamp("log") . " - pid $$ >> Mail sent to: $email_addr.\n";
|
|
}
|
|
else {
|
|
print getTimeStamp("log")
|
|
. " - pid $$ >> Error in sending mail to: $email_addr.\n";
|
|
}
|
|
}
|
|
|
|
my $starttime = getTimeStamp("log");
|
|
print "$starttime - pid $$ >> Start execution.\n";
|
|
|
|
GetOptions(
|
|
"tool=s" => \$tool,
|
|
"unit=s" => \$unit
|
|
) or die("Error in command line arguments\n");
|
|
|
|
( $scriptname, $scriptpath ) = fileparse($0);
|
|
|
|
$db_name = 'ase_lar';
|
|
|
|
getMatlabCmd();
|
|
print getTimeStamp("log")
|
|
. " - pid $$ >> $unit - $tool - Status $tool_status.\n";
|
|
if ( $tool_status eq 'Monitoring Completed' ) {
|
|
print getTimeStamp("log")
|
|
. " - pid $$ >> $unit - $tool - Monitoring completed: MatLab calc by-passed.\n";
|
|
}
|
|
else {
|
|
matlabCalc();
|
|
}
|
|
|
|
print getTimeStamp("log") . " - pid $$ >> Execution ended.\n";
|
|
exit;
|