<?php
/**********************************************************/
-// This PHP script acquires syslog warning message and sends mail.
+// This PHP script acquires SYSLOG warning message and sends mail to the corresponding user.
+
// Add in syslog.conf as: local1.=warning <TAB> | /path/sendreportmail.php
-// The following warning message is reported when a MAC address registered in
-// watchlist table is detected by opengatemd.
-// "Sep 29 12:34:56 opengate01 opengatemd[1234]: WARN: find mac=11:22:33:44:55:66 ip=192.168.0.10"
+// The warning message is reported by SYSLOG when a MAC address in watchlist table
+// is detected by opengatemd.
+
+// As this script should be used only by the administrators,
+// it should be protected by some access control method.
+
+// Following parameters should be modified properly.
/**********************************************************/
/*** parameters ***/
// open syslog
openlog('sendreportmail', LOG_PID, LOG_LOCAL1);
-// get mac address and others from syslog message
+// get mac address and others from syslog warning message such as
+// "Sep 29 12:34:56 opengate01 opengatemd[1234]: WARN: find mac=11:22:33:44:55:66 ip=192.168.0.10"
list($timestamp, $gatewayName, $macAddress, $ipAddress)=getDataFromSyslog();
if($timestamp=="?") return;
// prepare database
if(!($link=prepareMysql($mysqlServer, $mysqlUser, $mysqlPassword))) return;
-if(!$link)return;
-// get mail address and others from mysql db
+// get mail address and others relating to the mac address from mysql db
if(!(list($device, $mailAddress)=getDataFromMysql($macAddress))){
- mysql_close($link);
+ mysqli_close($link);
return;
}
-// if recent report exists, skip to send report
-if(skipReporting($macAddress, $gatewayName, $reportInterval)){
- mysql_close($link);
+// if reported recently, skip reporting
+if(skipReporting($link, $macAddress, $gatewayName, $reportInterval)){
+ mysqli_close($link);
return;
}
// close database
-mysql_close($link);
+mysqli_close($link);
// send mail to the user
sendMailToUser($mailSender, $mailAddress, $device, $gatewayName,
$macAddress="?";
$ipAddress="?";
- // syslog message is acqiured from STDIN
+ // syslog message is acquired from STDIN (piped to syslog output)
if(($message=fgets(STDIN))==FALSE){
syslog(LOG_INFO, 'ERR: Fail to read from stdin');
return FALSE;
function prepareMysql($mysqlServer, $mysqlUser, $mysqlPassword){
// connect and access to MySql DB
- $link = mysql_connect($mysqlServer, $mysqlUser, $mysqlPassword);
+ $link = mysqli_connect($mysqlServer, $mysqlUser, $mysqlPassword);
if (!$link){
- syslog(LOG_INFO, 'ERR: Cannot connect DB '.mysql_error());
+ syslog(LOG_INFO, 'ERR: Cannot connect DB '.mysqli_error());
return FALSE;
}
// use opengatem database
- $db_selected = mysql_select_db('opengatem', $link);
+ $db_selected = mysqli_select_db($link, 'opengatem');
if (!$db_selected){
- syslog(LOG_INFO, 'ERR: Cannot select DB '.mysql_error());
+ syslog(LOG_INFO, 'ERR: Cannot select DB '.mysqli_error());
return FALSE;
}
- mysql_set_charset('utf8');
+ mysqli_set_charset($link, 'utf8');
return $link;
}
$mailAddress="?";
// query
- $result = mysql_query('SELECT device, mailAddress FROM macaddrs
+ $result = mysqli_query($link, 'SELECT device, mailAddress FROM macaddrs
WHERE macAddress="'.$macAddress.'" AND status!="D"');
if (!$result){
- syslog(LOG_INFO, 'ERR: Fail DB query '.mysql_error());
+ syslog(LOG_INFO, 'ERR: Fail DB query '.mysqli_error());
return FALSE;
}
// get result
- if($row = mysql_fetch_row($result)){
+ if($row = mysqli_fetch_row($result)){
$device = $row[0];
$mailAddress = $row[1];
}else{
}
/***
-to avoid to send too many mails,
-skip if there are recent logs having same macaddress, and same gateway
-PLEASE MODIFY to control the report frequency.
+To avoid to send too many mails,
+ skip if there are recent logs having same macaddress and same gateway.
+If you want to change the report period, $reportPeriod is defined at the top of this file.
***/
-function skipReporting($macAddress, $gatewayName, $reportInterval){
+function skipReporting($link, $macAddress, $gatewayName, $reportInterval){
// query
- $result = mysql_query('SELECT count(*) FROM sessionmd '
+ $result = mysqli_query($link, 'SELECT count(*) FROM sessionmd '
.'WHERE EXISTS (SELECT * FROM sessionmd '
.'WHERE macAddress="'.$macAddress.'" '
.'AND gatewayName LIKE "'.$gatewayName.'.%" '
);
if (!$result){
- syslog(LOG_INFO, 'ERR: Fail query '.mysql_error());
+ syslog(LOG_INFO, 'ERR: Fail query '.mysqli_error());
return TRUE;
}
// get data
- if($row = mysql_fetch_row($result)) $count = $row[0];
+ if($row = mysqli_fetch_row($result)) $count = $row[0];
else $count = 0;
// if recent logs exist, skip is true
}
/***
-send mail to the mail address
+send mail to the user mail address
***/
function sendMailToUser($mailSender, $mailAddress, $device, $gatewayName,
$ipAddress, $timestamp){