diff --git a/AlertNotReceived.pl b/AlertNotReceived.pl index 6d7672d..89ac197 100644 --- a/AlertNotReceived.pl +++ b/AlertNotReceived.pl @@ -8,26 +8,33 @@ use Mail::Sender; # Variables my $username = 'aseuser'; my $password = 'Mums0001!'; -#my $database = 'ase_mums'; + +#my $database = 'ase_mums'; my $database = 'ase_lar'; -my $db_lar = 'ase_lar'; -my $server = 'localhost'; -my ($email_msg, $email_obj, $tools_info, $ref_date); +my $db_lar = 'ase_lar'; +my $server = 'localhost'; +my ( $email_msg, $email_obj, $tools_info, $ref_date ); my $act_tool = 0; -my $gd_tool = 0; +my $gd_tool = 0; + #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; 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); + 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"; + $email_msg = <<"END_MSG";
@@ -80,105 +87,139 @@ END_MSG } sub emailSend { - my $sender = new Mail::Sender { - auth => 'PLAIN', - authid => 'ase.mums.alert@gmail.com', - authpwd => 'ASE@mums2013F!', - smtp => 'smtp.gmail.com', - port => 587, - from => 'ASE Alert System', - to => $email_addr, - cc => $cc_email_addr, - subject => $email_obj, - msg => $email_msg, - ctype => "text/html", + my $sender = new Mail::Sender { + auth => 'PLAIN', + authid => 'ase.mums.alert@gmail.com', + authpwd => 'ASE@mums2013F!', + smtp => 'smtp.gmail.com', + port => 587, + from => 'ASE Alert System', + to => $email_addr, + cc => $cc_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 $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 = (); +my %ToolName = (); +my %Tools_infoCompany = (); +my %Tools_infoCompanySite = (); 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, SendAlarm, UnitNameId, ToolNameID, ToolState from mail_view;') - or die getLoggingTime() . " - $DBI::errstr"; +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, idSite, SiteName, CompanyEmail, SiteAlarmEmail, SendAlarm, UnitNameId, ToolNameID, ToolState from mail_view;' +) or die getLoggingTime() . " - $DBI::errstr"; $sth->execute(); my $ToolNum = $sth->rows; -if ($sth->rows < 0) { +if ( $sth->rows < 0 ) { print getLoggingTime() . " - No rows from TOOLS & UNITS.\n"; -} else { - while (my $results = $sth->fetchrow_hashref) { - $ToolName{$results->{UnitNameId} . $results->{'ToolNameID'}}{'idCompany'} = $results->{'idCompany'}; - $ToolName{$results->{UnitNameId} . $results->{'ToolNameID'}}{'CompanyNameShort'} = $results->{'CompanyNameShort'}; - $ToolName{$results->{UnitNameId} . $results->{'ToolNameID'}}{'SiteName'} = $results->{'SiteName'}; - $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" ){ - $act_tool++; - } +} +else { + while ( my $results = $sth->fetchrow_hashref ) { + $ToolName{ $results->{'UnitNameId'} . $results->{'ToolNameID'} } + {'idCompany'} = $results->{'idCompany'}; + $ToolName{ $results->{'UnitNameId'} . $results->{'ToolNameID'} } + {'CompanyNameShort'} = $results->{'CompanyNameShort'}; + $ToolName{ $results->{'UnitNameId'} . $results->{'ToolNameID'} } + {'idSite'} = $results->{'idSite'}; + $ToolName{ $results->{'UnitNameId'} . $results->{'ToolNameID'} } + {'SiteName'} = $results->{'SiteName'}; + $ToolName{ $results->{'idSite'} }{'SiteAlarmEmail'} = + $results->{'SiteAlarmEmail'}; + $ToolName{ $results->{'idSite'} }{'SendAlarm'} = + $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" ) { + $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 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"; +$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 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(); -if ($stw->rows == 0) { +if ( $stw->rows == 0 ) { print getLoggingTime() . " - No rows from RAWDATACOR.\n"; - $email_obj = "ATTENZIONE: nessuna lettura pervenuta!!!" -} else { - while (my $results = $stw->fetchrow_hashref) { - $ToolName{$results->{'UnitName'} . $results->{'ToolNameID'}}{'LettureNodi'} = $results->{'LettureNodi'}; - $ToolName{$results->{'UnitName'} . $results->{'ToolNameID'}}{'LettureCatene'} = $results->{'LettureCatene'}; - $ToolName{$results->{'UnitName'} . $results->{'ToolNameID'}}{'NumeroNodi'} = $results->{'NumeroNodi'}; - $ref_date = $results->{'EventDate'}; - if (substr($results->{'ToolNameID'},0,2) eq "GD" ){ - $gd_tool++; - } + $email_obj = "ATTENZIONE: nessuna lettura pervenuta!!!"; +} +else { + while ( my $results = $stw->fetchrow_hashref ) { + $ToolName{ $results->{'UnitName'} . $results->{'ToolNameID'} } + {'LettureNodi'} = $results->{'LettureNodi'}; + $ToolName{ $results->{'UnitName'} . $results->{'ToolNameID'} } + {'LettureCatene'} = $results->{'LettureCatene'}; + $ToolName{ $results->{'UnitName'} . $results->{'ToolNameID'} } + {'NumeroNodi'} = $results->{'NumeroNodi'}; + $ref_date = $results->{'EventDate'}; + if ( substr( $results->{'ToolNameID'}, 0, 2 ) eq "GD" ) { + $gd_tool++; + } } - if (($act_tool + $gd_tool) == $stw->rows) { - $email_obj = "Tutte le letture pervenute."; - } else { - $email_obj = "ATTENZIONE: alcune letture non pervenute!!!"; - } - print getLoggingTime() . " - tool attivi: $act_tool - GD tool: $gd_tool - righe query: " . $stw->rows . ".\n"; + if ( ( $act_tool + $gd_tool ) == $stw->rows ) { + $email_obj = "Tutte le letture pervenute."; + } + else { + $email_obj = "ATTENZIONE: alcune letture non pervenute!!!"; + } + print getLoggingTime() + . " - tool attivi: $act_tool - GD tool: $gd_tool - righe query: " + . $stw->rows . ".\n"; } $stw->finish; -my $ste = $dbh->prepare("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); +my $ste = $dbh->prepare( +"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->execute(); -while (my $results = $ste->fetchrow_hashref) { - $ToolName{$results->{'UnitName'} . $results->{'ToolNameID'}}{'ErroriTotaliLetture'} = $results->{'ErroriTotaliLetture'}; +while ( my $results = $ste->fetchrow_hashref ) { + $ToolName{ $results->{'UnitName'} . $results->{'ToolNameID'} } + {'ErroriTotaliLetture'} = $results->{'ErroriTotaliLetture'}; } $ste->finish; @@ -187,41 +228,87 @@ $ste->finish; $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 = "