diff --git a/AlertNotReceived.pl b/AlertNotReceived.pl index 51270e8..f204246 100644 --- a/AlertNotReceived.pl +++ b/AlertNotReceived.pl @@ -41,7 +41,7 @@ $email_msg = <<"END_MSG"; diff --git a/LoadCSVData.pl b/LoadCSVData.pl index 21fd061..78640bf 100644 --- a/LoadCSVData.pl +++ b/LoadCSVData.pl @@ -8,6 +8,7 @@ use File::Basename qw( fileparse ); use List::Util qw( max ); use Data::Dumper qw(Dumper); use Cwd; +use Mail::Sender; $|++; # Autoflush @@ -41,6 +42,8 @@ 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 $email_addr = 'andrea.carri@aseltd.eu,alessandro.battilani@gmail.com'; +my ($email_msg, $email_obj); sub getTimeStamp { # parm [ts] => timestamp for filename; log => timestamp for log @@ -108,14 +111,21 @@ sub matlabCalc { chdir($matlab_func_dir) or die "cannot change: $!\n"; my @args = ( $matlab_cmd, $matlab_rt, $unit, $tool); - system(@args) == 0 - or die( getTimeStamp("log") . " - pid $$ >> system @args failed: $?\n" ); + if (system(@args) != 0) { + makeEmailMsg(); + emailSend(); + print getTimeStamp("log") . " - pid $$ >> system @args failed: $?\n"; + print getTimeStamp("log") . " - pid $$ >> Execution ended.\n"; + exit(1); + } } else { $matlab_cmd = 'matlab_' . uc $matlab_cmd . '.cmd'; my @out_matlab = `ssh1 $matlab_ip 'cmd /c $matlab_cmd ' $tool`; if (index($out_matlab[-1], "Failure") != -1) { print getTimeStamp("log") . " - pid $$ >> $tool MatLab calc failed.\n"; + makeEmailMsg(); + emailSend(); print getTimeStamp("log") . " - pid $$ >> Execution ended.\n"; exit(1); } @@ -177,11 +187,11 @@ sub trxelab { 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' + '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 + speed, speed_local, acceleration, acceleration_local, T_node, water_level, pressure, load_value, AlfaX, AlfaY, calcerr from ElabDataView where ToolNameID = '$tool' and updated_at > '$matlab_timestamp' order by ToolNameID DESC, EventDate, EventTime, convert(`NodeNum`, decimal) DESC @@ -338,6 +348,72 @@ sub getMatlabCmd { return; } +sub makeEmailMsg { +$email_msg = <<"END_MSG"; + + + + + Alert from ASE + + + +
- ASE + ASE
+ + + +
+ + + + + + + +
+ ASE +
+ + + + +
+ Alert from ASE:
+ Matlab function $matlab_cmd failed on unit => $unit - tool => $tool +
+
+
+ + +END_MSG +} + +sub emailSend { + $email_obj = 'Matlab error on unit ' . $unit . ' - tool ' . $tool; + + 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 getTimeStamp("log") . " - pid $$ >> $mail_res: $sender->{error_msg} \n"; + } else { + print getTimeStamp("log") . " - pid $$ >> $email_obj\n"; + print getTimeStamp("log") . " - pid $$ >> Mail sent to: $email_addr.\n"; + } +} + my $starttime = getTimeStamp("log"); print "$starttime - pid $$ >> Start execution.\n"; @@ -366,6 +442,7 @@ if ( $filecsvname =~ m/^(\d\d_\d\d\d\d_|)(DT\d\d\d\d|LOC\d.*|GD\d*)_\d*$/i ) { ($unittype, $unit ) = split( /\s/, uc ); $unit =~ s/;+$//; } elsif ( $filecsvname =~ m/^(\d\d_\d\d\d\d_|)(\d\d\d\d\d\d\d\d\d\d\d\d\d\d_)(G201_ID\d\d\d\d_DT\d\d\d\d)_\d*$/i ) { + # da mettere il controllo ^^^^^^^^^^^^^^^ se ha mese ed anno all'inivio my @strings = $filecsvname =~ /(.{1,4})(.{1,2})(.{1,2})(.{1,2})(.{1,2})(.{1,2})_(.{1,4})_(.{1,6})_(.{1,6}).*/; $fileDate = $strings[0] . "/" . $strings[1] . "/" . $strings[2];; $fileTime = $strings[3] . ":" . $strings[4] . ":" . $strings[5];