361 lines
12 KiB
Perl
361 lines
12 KiB
Perl
#!/usr/bin/perl
|
|
|
|
use warnings;
|
|
use strict;
|
|
use DBI;
|
|
use MIME::Lite;
|
|
use Data::Dumper;
|
|
|
|
# Variables
|
|
my $username = 'aseuser';
|
|
my $password = 'Mums0001!';
|
|
|
|
#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 $act_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,alessandro.valletta@aseltd.eu,alessandro.battilani@aseltd.eu,corrado@geielettronica.it,luca.chiapponi@aseltd.eu';
|
|
my $cc_email_addr = $email_addr;
|
|
my $from = 'ASE Alert System<alert@aseltd.eu>';
|
|
|
|
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
|
|
);
|
|
return $nice_timestamp;
|
|
}
|
|
|
|
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>
|
|
<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 border="0" cellpadding="0" cellspacing="0" width="100%">
|
|
<tr>
|
|
<td>
|
|
<table align="center" border="0" cellpadding="0" cellspacing="0" width="600" style="border-collapse: collapse;">
|
|
<tr>
|
|
<td align="center" bgcolor="#fff" style="padding: 10px 0 10px 0;">
|
|
<img src="https://www2.aseltd.eu/static/img/logo_ASE_small.png" alt="ASE"body style="display: block;" />
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td bgcolor="#ffffff" style="padding: 10px 10px 10px 10px;">
|
|
<table border="0" cellpadding="0" cellspacing="0" width="100%">
|
|
<tr>
|
|
<td style="padding: 10px 10px 10px 10px;">
|
|
Alert from ASE: data received on $ref_date.
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td>
|
|
|
|
<table border="0" cellpadding="4" cellspacing="0" width="100%">
|
|
<tr bgcolor="#64b7ff" align="center" style="color:white">
|
|
<td><b>Company</b></td><td><b>Sito</b></td><td><b>ID Centralina</b></td><td><b>ID Tools</b></td>
|
|
<td><b>Letture Nodi</b></td><td><b>Numero Nodi</b></td><td><b>Letture Tool</b></td>
|
|
<td><b>Letture Nodi in Errore</b></td><td><b>Stato Tool</b></td>
|
|
</tr>
|
|
$tools_info
|
|
</table>
|
|
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</body>
|
|
</html>
|
|
END_MSG
|
|
}
|
|
|
|
sub emailSend {
|
|
my $msg = MIME::Lite->new(
|
|
From => $from,
|
|
To => $email_addr,
|
|
Cc => $cc_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 getLoggingTime() . " - $email_obj\n";
|
|
print getLoggingTime() . " - Mail sent to: $email_addr.\n";
|
|
}
|
|
else {
|
|
print getLoggingTime() . " - Error in sending mail to: $email_addr.\n";
|
|
}
|
|
}
|
|
|
|
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, idSite, SiteName, CompanyEmail, SiteAlarmEmail, SendAlarm, UnitNameId, ToolNameID, ToolState, ToolType from mail_view where SiteName <> "Vobarno";'
|
|
) or die getLoggingTime() . " - $DBI::errstr";
|
|
|
|
$sth->execute();
|
|
my $ToolNum = $sth->rows;
|
|
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'} }
|
|
{'idSite'} = $results->{'idSite'};
|
|
$ToolName{ $results->{'UnitNameId'} . $results->{'ToolNameID'} }
|
|
{'SiteName'} = $results->{'SiteName'};
|
|
$ToolName{ 'site' . $results->{'idSite'} }{'SiteAlarmEmail'} =
|
|
$results->{'SiteAlarmEmail'};
|
|
$ToolName{ 'site' . $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";
|
|
$stw->execute();
|
|
|
|
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++;
|
|
}
|
|
}
|
|
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 );
|
|
$ste->execute();
|
|
while ( my $results = $ste->fetchrow_hashref ) {
|
|
$ToolName{ $results->{'UnitName'} . $results->{'ToolNameID'} }
|
|
{'ErroriTotaliLetture'} = $results->{'ErroriTotaliLetture'};
|
|
}
|
|
|
|
$ste->finish;
|
|
|
|
# Inizio G-FLOW
|
|
|
|
my $stwg = $dbh->prepare(
|
|
"select unit_name, tool_name, date(EventDateTime) as EventDate, count(EventDateTime) 'LettureNodi', Count(EventDateTime) 'LettureCatene', 1 'NumeroNodi' from ELABDATAGFLOW where date(EventDateTime) = subdate(curdate(),1) group by unit_name, tool_name"
|
|
) or die getLoggingTime() . " - $DBI::errstr";
|
|
$stwg->execute();
|
|
|
|
while ( my $results = $stwg->fetchrow_hashref ) {
|
|
$ToolName{ $results->{'unit_name'} . $results->{'tool_name'} }
|
|
{'LettureNodi'} = $results->{'LettureNodi'};
|
|
$ToolName{ $results->{'unit_name'} . $results->{'tool_name'} }
|
|
{'LettureCatene'} = $results->{'LettureCatene'};
|
|
$ToolName{ $results->{'unit_name'} . $results->{'tool_name'} }{'NumeroNodi'}
|
|
= $results->{'NumeroNodi'};
|
|
$ref_date = $results->{'EventDate'};
|
|
}
|
|
|
|
$stwg->finish;
|
|
|
|
my $steg = $dbh->prepare(
|
|
"select unit_name, tool_name, count(xacc) 'ErroriTotaliLetture' from ELABDATAGFLOW
|
|
where date(EventDateTime) = ? and xacc = 'Err1' group by tool_name"
|
|
) or die getLoggingTime() . " - $DBI::errstr";
|
|
$steg->bind_param( 1, $ref_date );
|
|
$steg->execute();
|
|
while ( my $results = $steg->fetchrow_hashref ) {
|
|
$ToolName{ $results->{'unit_name'} . $results->{'tool_name'} }
|
|
{'ErroriTotaliLetture'} = $results->{'ErroriTotaliLetture'};
|
|
}
|
|
|
|
$steg->finish;
|
|
|
|
# Fine G-FLOW
|
|
|
|
# Disconnect
|
|
$dbh->disconnect;
|
|
|
|
# Ciclo x email totale rilevazioni
|
|
foreach my $tool ( sort grep { /DT|PT/ } keys %ToolName ) {
|
|
|
|
if ( !defined $ToolName{$tool}{'ToolState'}
|
|
or ( $ToolName{$tool}{'ToolState'} eq 'Monitoring Completed' ) )
|
|
{
|
|
next; #skip dei tool nello stato del test
|
|
}
|
|
|
|
my $tool_part = "<td>"
|
|
. $ToolName{$tool}{'CompanyNameShort'}
|
|
. "</td><td>"
|
|
. $ToolName{$tool}{'SiteName'} . "</td>";
|
|
$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 "--" ) {
|
|
$tools_info .= "<tr>";
|
|
}
|
|
elsif ( $ToolName{$tool}{'ToolState'} eq 'Active' ) {
|
|
if ( $ToolName{ 'site' . $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\">";
|
|
}
|
|
else {
|
|
$tools_info .= "<tr bgcolor=\"#cfe5e0\">";
|
|
}
|
|
$tools_info .= $tool_part;
|
|
}
|
|
makeEmailMsg();
|
|
emailSend();
|
|
|
|
#print $email_msg;print "OOOOOOOOOOOOOO=====================OOOOOOOOOOOOOO\n";
|
|
|
|
#print Dumper(\%ToolName);
|
|
|
|
foreach my $comp ( keys %Tools_infoCompany ) {
|
|
$tools_info = $Tools_infoCompany{$comp};
|
|
my $comp_email = $ToolName{$comp}{'CompanyEmail'};
|
|
my $send_alarm = $ToolName{$comp}{'SendAlarm'};
|
|
print getLoggingTime()
|
|
. " - Company email: $comp_email - Send Alarm: $send_alarm\n";
|
|
if ( $send_alarm == 1 ) {
|
|
$email_addr = $ToolName{$comp}{'CompanyEmail'};
|
|
makeEmailMsg();
|
|
emailSend();
|
|
|
|
#print $email_msg;print "XXXXXXXXXXXXX=====================XXXXXXXXXXXXX\n";
|
|
}
|
|
}
|
|
|
|
foreach my $site ( keys %Tools_infoCompanySite ) {
|
|
$tools_info = $Tools_infoCompanySite{$site};
|
|
my $site_email = $ToolName{ 'site' . $site }{'SiteAlarmEmail'};
|
|
my $send_alarm = $ToolName{ 'site' . $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";
|
|
}
|
|
}
|
|
|
|
print getLoggingTime() . " - pid $$ >> Execution ended.\n";
|