#!/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 = 'aselar'; my $password = 'laravel'; my $db_lar = 'ase_lar'; my $db_sp = 'ase_lar'; my $server = '85.235.153.201'; 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'; 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( "
", @errors ); $MatlabWarnings = join( "
", 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"; Alert from ASE
ASE

Alert from ASE:

Matlab function $matlab_cmd failed on unit => $unit - tool => $tool

$matlab_error

$MatlabErrors
$MatlabWarnings
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;