OSDN Git Service

Fixed link error
[opengatem/opengatem.git] / phpsrc / sendreportmail.php
1 #!/usr/local/bin/php
2
3 <?php
4 /**********************************************************/
5 // This PHP script acquires SYSLOG warning message and sends mail to the corresponding user.
6
7 // Add in syslog.conf as: local1.=warning <TAB> | /path/sendreportmail.php
8 // The warning message is reported by SYSLOG when a MAC address in watchlist table 
9 // is detected by opengatemd.
10
11 // As this script should be used only by the administrators, 
12 // it should be protected by some access control method.
13
14 // Following parameters should be modified properly.
15 /**********************************************************/
16
17 /*** parameters ***/
18 $mysqlServer="localhost";
19 $mysqlUser="root";
20 $mysqlPassword="";
21 $mailSender="opengate@cc.saga-u.ac.jp";
22 $reportInterval="6 HOUR";
23
24 // open syslog
25 openlog('sendreportmail', LOG_PID, LOG_LOCAL1);
26
27 // get mac address and others from syslog warning message such as 
28 // "Sep 29 12:34:56 opengate01 opengatemd[1234]: WARN: find mac=11:22:33:44:55:66 ip=192.168.0.10"
29 list($timestamp, $gatewayName, $macAddress, $ipAddress)=getDataFromSyslog();
30 if($timestamp=="?") return;
31
32 // prepare database 
33 if(!($link=prepareMysql($mysqlServer, $mysqlUser, $mysqlPassword))) return;
34
35 // get mail address and others relating to the mac address from mysql db
36 if(!(list($device, $mailAddress)=getDataFromMysql($macAddress))){
37         mysqli_close($link);
38         return;
39 }
40
41 // if reported recently, skip reporting
42 if(skipReporting($link, $macAddress, $gatewayName, $reportInterval)){
43         mysqli_close($link);
44         return;
45 }
46
47 // close database
48 mysqli_close($link);
49
50 // send mail to the user
51 sendMailToUser($mailSender, $mailAddress, $device, $gatewayName, 
52                 $ipAddress, $timestamp);
53 return;
54
55
56 /***
57 get MAC address and others from syslog 
58 ***/
59 function getDataFromSyslog(){
60         $timestamp="?";
61         $gatewayName="?";
62         $macAddress="?";
63         $ipAddress="?";
64
65         // syslog message is acquired from STDIN (piped to syslog output)
66         if(($message=fgets(STDIN))==FALSE){
67                 syslog(LOG_INFO, 'ERR: Fail to read from stdin');
68                 return FALSE;
69         }
70
71         // extract timestamp, gateway and macaddress by regular expression
72         if(preg_match('/^(.*) (.*) .* WARN: find mac=(.*) ip=(.*)/',
73          $message, $matches)==1){
74                 $timestamp = $matches[1];
75                 $gatewayName = $matches[2];
76                 $macAddress = $matches[3];
77                 $ipAddress = $matches[4];
78         }else{
79                 syslog(LOG_INFO, 'ERR: Fail to analyze syslog message');
80         }
81         return array($timestamp, $gatewayName, $macAddress, $ipAddress);
82 }
83
84 /***
85 prepare mysql connection
86 ***/
87 function prepareMysql($mysqlServer, $mysqlUser, $mysqlPassword){
88
89         // connect and access to MySql DB
90         $link = mysqli_connect($mysqlServer, $mysqlUser, $mysqlPassword);
91         if (!$link){
92                 syslog(LOG_INFO, 'ERR: Cannot connect DB '.mysqli_error());
93                 return FALSE;
94         }
95
96         // use opengatem database
97         $db_selected = mysqli_select_db($link, 'opengatem');
98         if (!$db_selected){
99                 syslog(LOG_INFO, 'ERR: Cannot select DB '.mysqli_error());
100                 return FALSE;
101         }
102         mysqli_set_charset($link, 'utf8');
103         return $link;
104 }
105
106 /***
107 get mail address and others corresponding to the MAC address from mysql 
108 ***/
109 function getDataFromMysql($macAddress){
110         $device="?";
111         $mailAddress="?";
112
113         // query
114         $result = mysqli_query($link, 'SELECT device, mailAddress FROM macaddrs 
115                 WHERE macAddress="'.$macAddress.'" AND status!="D"');
116         if (!$result){
117                 syslog(LOG_INFO, 'ERR: Fail DB query '.mysqli_error());
118                 return FALSE;
119         }
120
121         // get result
122         if($row = mysqli_fetch_row($result)){
123                 $device = $row[0];
124                 $mailAddress = $row[1];
125         }else{
126                 syslog(LOG_INFO, 'ERR: Fail to get mail address from DB');
127                 return FALSE;
128         }
129
130         return array($device, $mailAddress);
131 }
132
133 /***
134 To avoid to send too many mails, 
135  skip if there are recent logs having same macaddress and same gateway. 
136 If you want to change the report period, $reportPeriod is defined at the top of this file.
137 ***/
138 function skipReporting($link, $macAddress, $gatewayName, $reportInterval){
139         
140         // query
141         $result = mysqli_query($link, 'SELECT count(*) FROM sessionmd '
142                 .'WHERE EXISTS (SELECT * FROM sessionmd '
143                 .'WHERE macAddress="'.$macAddress.'" '
144                 .'AND gatewayName LIKE "'.$gatewayName.'.%" '
145                 .'AND openTime > NOW() - INTERVAL '.$reportInterval.' '
146                 .'AND openTime < NOW() - INTERVAL 1 MINUTE '
147                 .')'
148                 );
149
150         if (!$result){
151                 return FALSE; // if table doesn't exist, no skip 
152         }
153
154         // get data
155         if($row = mysqli_fetch_row($result)) $count = $row[0];
156         else    $count = 0;
157
158         // if recent logs exist, skip is true
159         if($count>0)return TRUE;
160         else return FALSE;
161 }
162
163 /***
164 send mail to the user mail address
165 ***/
166 function sendMailToUser($mailSender, $mailAddress, $device, $gatewayName, 
167                 $ipAddress, $timestamp){
168         
169         $to=$mailAddress;
170         $subject="Your device is detected";
171         $message="Your device ".$device
172         ." is detected as ip=".$ipAddress
173         ." on the subnet under ".$gatewayName
174         ." at ".$timestamp
175         .". "
176         ." If it is not your use, please contact to the administrator.";
177         $headers="From: ".$mailSender."\n";
178         $parameters="-f ".$mailSender;
179
180         if(mb_send_mail($to, $subject, $message, $headers, $parameters)){
181                 syslog(LOG_INFO, 'INFO: Success to send mail');
182                 return TRUE;
183         }else{
184                 syslog(LOG_INFO, 'ERR: Fail to send mail');
185                 return FALSE;
186         }
187 }
188 ?>