OSDN Git Service

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