OpengateM Source Repository
Revisão | 47d56ffb1096a0892887036b51eca061ee185982 (tree) |
---|---|
Hora | 2016-06-24 11:05:36 |
Autor | watanaby <watanaby@user...> |
Commiter | watanaby |
restored optional log table in management db
@@ -92,6 +92,9 @@ | ||
92 | 92 | <!-- SQLite busy timeout (milli-seconds) --> |
93 | 93 | <SqliteBusyTimeout>100</SqliteBusyTimeout> |
94 | 94 | |
95 | + <!-- Save Log of session to Management DB (1:YES/0:NO) --> | |
96 | + <SaveLogToMngDb>1</SaveLogToMngDb> | |
97 | + | |
95 | 98 | <!-- SQLite database file --> |
96 | 99 | <!-- for opengatemd work --> |
97 | 100 | <SqliteDbMd>/tmp/opengatemd.db</SqliteDbMd> |
@@ -80,7 +80,7 @@ | ||
80 | 80 | |
81 | 81 | <!-- SQLite busy timeout (milli-seconds) --> |
82 | 82 | <SqliteBusyTimeout>100</SqliteBusyTimeout> |
83 | - | |
83 | + | |
84 | 84 | <!-- SQLite database file --> |
85 | 85 | <!-- opengatemd work db --> |
86 | 86 | <SqliteDbMd>/tmp/opengatemd.db</SqliteDbMd> |
@@ -273,8 +273,12 @@ OpengateM History</H3> | ||
273 | 273 | </DD> |
274 | 274 | <DT>Ver.1.1.0 at 2016.6.22 |
275 | 275 | </DT><DD> |
276 | - Modified code to prevent inflation of Databases (drop table macippair&sessionmd:MySQL, delete old records of macmodify:MySQL and macinfo:SQLite3). | |
277 | -w </DD> | |
276 | + Modified code to prevent inflation of Databases (drop table macippair/sessionmd:MySQL, delete old records of macmodify:MySQL and macinfo:SQLite3). | |
277 | + </DD> | |
278 | + <DT>Ver.1.1.1 at 2016.6.24 | |
279 | + </DT><DD> | |
280 | + Restored table sessionmd in management db (It holds the log expired after 1 month. Saving-log can be disabled in opengatemd.conf). | |
281 | + </DD> | |
278 | 282 | </DL> |
279 | 283 | <P> |
280 | 284 | <B>Please see GIT on osdn.jp to check the differences between versions.</B> |
@@ -2,7 +2,21 @@ | ||
2 | 2 | <head> |
3 | 3 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
4 | 4 | <title></title> |
5 | +<script type="text/javascript"> | |
6 | +<!-- | |
7 | +var loglist={ | |
8 | +%%USAGELOG%% | |
9 | +}; | |
10 | +--> | |
11 | +</script> | |
12 | +<script type="text/javascript" src="/opengate/showlog.js"></script> | |
5 | 13 | <script type="text/javascript" src="/opengate/editstring.js"></script> |
14 | +<script> | |
15 | +function toggleDisplay(id){ | |
16 | + if (document.getElementById(id).style.display=="none") document.getElementById(id).style.display="block"; | |
17 | + else document.getElementById(id).style.display="none"; | |
18 | +} | |
19 | +</script> | |
6 | 20 | </head> |
7 | 21 | <body> |
8 | 22 | <center> |
@@ -32,6 +46,7 @@ action="%%CGINAME%%?lang=en&redirectedurl=%%REDIRECTEDURL%%"> | ||
32 | 46 | %%MACLIST%% |
33 | 47 | </table> |
34 | 48 | <p><input type="submit" value=" Send "> |
49 | + <span style="color:blue;text-decoration:underline;cursor:pointer;" onclick="toggleDisplay('help1')"> help </span> | |
35 | 50 | </p> |
36 | 51 | </form> |
37 | 52 |
@@ -41,7 +56,7 @@ action="%%CGINAME%%?lang=en&redirectedurl=%%REDIRECTEDURL%%"> | ||
41 | 56 | </center> |
42 | 57 | |
43 | 58 | <div style="width:640;margin:0 auto;"> |
44 | -<ul> | |
59 | +<ul id="help1" style="display:none"> | |
45 | 60 | <li><p> |
46 | 61 | Select <strong>"Renew"</strong> to renew the registration (resume |
47 | 62 | and extend the limit date). |
@@ -64,5 +79,32 @@ action="%%CGINAME%%?lang=en&redirectedurl=%%REDIRECTEDURL%%"> | ||
64 | 79 | </ul> |
65 | 80 | </div> |
66 | 81 | |
82 | +<div style="%%LOGDISPLAYSTYLE%%"> | |
83 | +<center> | |
84 | + <p><font size="5"> Usage Log </font> | |
85 | + <span style="color:blue;text-decoration:underline;cursor:pointer;" onclick="toggleDisplay('help2')"> help </span> | |
86 | + <br><font size="2"> | |
87 | + (To prevent hijacking, please confirm your log.)</font> | |
88 | +</p> | |
89 | +</center> | |
90 | + | |
91 | +<div style="width:640;margin:0 auto;"> | |
92 | +<ul id="help2" style="display:none"> | |
93 | +<li>The color-bars in the chart show the time range when you used your devices.</li> | |
94 | +<li>If there is a use without your memory, please inform it to the administrator.</li> | |
95 | +<li>The red frame indicates that one device has plural logs. Please check it carefully.</li> | |
96 | +<li>Even if you do not intend to use the device, the automatic access might be done.</li> | |
97 | +<li>The closing of the network has time delay after finishing the use.</li> | |
98 | +</ul> | |
99 | +</div> | |
100 | + | |
101 | +<center> | |
102 | +<div id="usagelog"></div> | |
103 | +<div id="popup"></div> | |
104 | +<p><br><br></p> | |
105 | +</center> | |
106 | + | |
107 | +</div> | |
108 | + | |
67 | 109 | </body> |
68 | 110 | </html> |
@@ -2,7 +2,21 @@ | ||
2 | 2 | <head> |
3 | 3 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
4 | 4 | <title></title> |
5 | +<script type="text/javascript"> | |
6 | +<!-- | |
7 | +var loglist={ | |
8 | +%%USAGELOG%% | |
9 | +}; | |
10 | +--> | |
11 | +</script> | |
12 | +<script type="text/javascript" src="/opengate/showlog.js"></script> | |
5 | 13 | <script type="text/javascript" src="/opengate/editstring.js"></script> |
14 | +<script> | |
15 | +function toggleDisplay(id){ | |
16 | + if (document.getElementById(id).style.display=="none") document.getElementById(id).style.display="block"; | |
17 | + else document.getElementById(id).style.display="none"; | |
18 | +} | |
19 | +</script> | |
6 | 20 | </head> |
7 | 21 | <body> |
8 | 22 | <center> |
@@ -32,6 +46,7 @@ action="%%CGINAME%%?lang=ja&redirectedurl=%%REDIRECTEDURL%%"> | ||
32 | 46 | </table> |
33 | 47 | |
34 | 48 | <p><input type="submit" value=" 送信 "> |
49 | + <span style="color:blue;text-decoration:underline;cursor:pointer;" onclick="toggleDisplay('help1')"> help </span> | |
35 | 50 | </p> |
36 | 51 | </form> |
37 | 52 |
@@ -41,7 +56,7 @@ action="%%CGINAME%%?lang=ja&redirectedurl=%%REDIRECTEDURL%%"> | ||
41 | 56 | </center> |
42 | 57 | |
43 | 58 | <div style="width:640;margin:0 auto;"> |
44 | - <ul> | |
59 | +<ul id="help1" style="display:none"> | |
45 | 60 | <li><p> |
46 | 61 | <strong>「更新」</strong>をチェックすると、機器の登録を更新して利用期限を延長します。 |
47 | 62 | </p></li> |
@@ -60,5 +75,29 @@ action="%%CGINAME%%?lang=ja&redirectedurl=%%REDIRECTEDURL%%"> | ||
60 | 75 | </ul> |
61 | 76 | </div> |
62 | 77 | |
78 | +<div id="logheader" style="display:block"> | |
79 | +<center> | |
80 | + <p><font size="5">利用履歴</font> | |
81 | +<span style="color:blue;text-decoration:underline;cursor:pointer;" onclick="toggleDisplay('help2')"> help </span> | |
82 | + <br> | |
83 | + <font size="2">(不正利用を防ぐため、妥当か確認して下さい。)</font> | |
84 | +</p> | |
85 | +</center> | |
86 | + | |
87 | +<div style="width:640;margin:0 auto;"> | |
88 | +<ul id="help2" style="display:none"> | |
89 | +<li>利用された時間帯を1時間ごとに色付け表示しています。覚えの無い履歴があれば管理者までお知らせください。</li> | |
90 | +<li>赤枠の時間帯は1つの機器に対する2つ以上の履歴を含んでいます。特に注意下さい。</li> | |
91 | +<li>意図して利用しなくても、自動通信による利用がありえます。また使い終わってしばらくしてから閉鎖します。</li> | |
92 | +</ul> | |
93 | +</div> | |
94 | + | |
95 | +<center> | |
96 | +<div id="usagelog"></div> | |
97 | +<div id="popup"></div> | |
98 | +<p><br><br></p> | |
99 | +</center> | |
100 | +</div> | |
101 | + | |
63 | 102 | </body> |
64 | 103 | </html> |
@@ -0,0 +1,324 @@ | ||
1 | +// Program to show chart type opengatem usage log in web page | |
2 | +// used in macupdate.html in lang(ja/en) directory | |
3 | +// | |
4 | +// Programmed by Yoshiaki Watanabe, 2013 | |
5 | +// Revised by Minoru Kishira, 2013 | |
6 | +// | |
7 | +// canvas.js by Satoru Takabayashi is used partially. | |
8 | +// http://0xcc.net/blog/archives/canvas.js | |
9 | +// | |
10 | +// You can redistribute it and/or modify it under the terms of | |
11 | +// the GNU General Public License version 2. | |
12 | +// | |
13 | + | |
14 | +// variables | |
15 | +var nTime=24; // time division per day | |
16 | +var beginTime; | |
17 | +var endTime; | |
18 | +var cellWidth="20px"; | |
19 | +var cellHeight="8px"; | |
20 | +var deviceList=[]; // devices used | |
21 | + | |
22 | +var colorList={0:"LimeGreen", 1:"Orange", 2:"SkyBlue", 3:"Tan", 4:"Thistle", | |
23 | + "none":"WhiteSmoke", "sunday":"Pink","saturday":"LightBlue", "blank":"white", "popup":"yellow"}; | |
24 | +var weekString = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat"); | |
25 | +var today = new Date(); | |
26 | +var weekday; | |
27 | +var monthday; | |
28 | + | |
29 | +//////////////////////////// | |
30 | +// make handler of mouseover | |
31 | +function onmouseoverHandler(element, logItemNo) { | |
32 | + | |
33 | + // save local values | |
34 | + var day=monthday; | |
35 | + var week=weekday; | |
36 | + | |
37 | + // return the mandler | |
38 | + return function(e) { | |
39 | + | |
40 | + // if log item is found, show the item as popup | |
41 | + if(logItemNo[0]>=0){ | |
42 | + | |
43 | + // popup style | |
44 | + popup.style.position = "fixed"; | |
45 | + popup.style.left = e.clientX+20; | |
46 | + popup.style.top = e.clientY+10; | |
47 | + popup.style.background = colorList["popup"]; | |
48 | + popup.style.display = "block"; | |
49 | + | |
50 | + // popup string | |
51 | + popup.innerHTML=loglist[logItemNo[0]].device +" ["+loglist[logItemNo[0]].macAddress+"]<BR>"; | |
52 | + for(var i=0; i<logItemNo.length; i++){ | |
53 | + popup.innerHTML+="Used at "+ loglist[logItemNo[i]].gateway+"<BR>" | |
54 | + +"( "+loglist[logItemNo[i]].openTime+" -> "+loglist[logItemNo[i]].closeTime+" )<BR>"; | |
55 | + } | |
56 | + } | |
57 | + } | |
58 | +} | |
59 | + | |
60 | +//////////////////////////// | |
61 | +// make handler of mouseout | |
62 | +function onmouseoutHandler(element) { | |
63 | + | |
64 | + // off the display | |
65 | + return function() { | |
66 | + popup.style.display = "none"; | |
67 | + } | |
68 | +} | |
69 | + | |
70 | +//////////////////////////// | |
71 | +// make cells to show every date-time range | |
72 | +function makeCells() { | |
73 | + | |
74 | + // make html table | |
75 | + var table = document.createElement("table"); | |
76 | + var tbody = document.createElement("tbody"); | |
77 | + table.border ="2px #808080 solid"; | |
78 | + | |
79 | + // grouping columns (title, 0-5, 6-11, 12-17, 18-23 hours) | |
80 | + table.rules ="groups"; | |
81 | + cgroup0=document.createElement("colgroup"); cgroup0.span=1; table.appendChild(cgroup0); | |
82 | + cgroup1=document.createElement("colgroup"); cgroup1.span=6; table.appendChild(cgroup1); | |
83 | + cgroup2=document.createElement("colgroup"); cgroup2.span=6; table.appendChild(cgroup2); | |
84 | + cgroup3=document.createElement("colgroup"); cgroup3.span=6; table.appendChild(cgroup3); | |
85 | + cgroup4=document.createElement("colgroup"); cgroup4.span=6; table.appendChild(cgroup4); | |
86 | + | |
87 | + // add header to display the hours | |
88 | + tbody.appendChild(makeHeaderRow()); | |
89 | + | |
90 | + // make row for each day and each device | |
91 | + var nDate=loglist["info"].dateCount; | |
92 | + var nDevice=deviceList.length; | |
93 | + for (var date = nDate-1; date >= 0; date--) { | |
94 | + for(var iDevice = 0; iDevice < nDevice+1; iDevice++){ | |
95 | + var tr = document.createElement("tr"); | |
96 | + | |
97 | + // make leftmost title column spanning 6 rows | |
98 | + if(iDevice==0) tr.appendChild(makeHeaderColumn(date)); | |
99 | + | |
100 | + // make column representing 0 hour to 23 hour | |
101 | + for (var time = 0; time < nTime; time++) { | |
102 | + var td = document.createElement("td"); | |
103 | + setupCellProperties(td, date, time, iDevice); | |
104 | + if(iDevice==nDevice) td.style.background=colorList["blank"]; | |
105 | + tr.appendChild(td); | |
106 | + } | |
107 | + | |
108 | + tbody.appendChild(tr); | |
109 | + } | |
110 | + } | |
111 | + table.appendChild(tbody); | |
112 | + return table; | |
113 | +} | |
114 | + | |
115 | +//////////////////////////// | |
116 | +// make the top title row to display hours | |
117 | +function makeHeaderRow(){ | |
118 | + var tr = document.createElement("tr"); | |
119 | + var td = document.createElement("td"); | |
120 | + tr.appendChild(td); | |
121 | + for (var time = 0; time < nTime; time++) { | |
122 | + var td = document.createElement("td"); | |
123 | + var text = document.createTextNode(time); | |
124 | + td.appendChild(text); | |
125 | + tr.appendChild(td); | |
126 | + } | |
127 | + return tr; | |
128 | +} | |
129 | + | |
130 | +//////////////////////////// | |
131 | +// make the leftmost title comumn spanning to all devices row | |
132 | +function makeHeaderColumn(date){ | |
133 | + var td = document.createElement("td"); | |
134 | + var nDevice=deviceList.length; | |
135 | + | |
136 | + td.rowSpan=nDevice+1; | |
137 | + dateStr=getRealDate(date); | |
138 | + var text = document.createTextNode(dateStr); | |
139 | + td.appendChild(text); | |
140 | + if(dateStr.indexOf(weekString[0])!=-1) | |
141 | + td.style.background=colorList["sunday"]; | |
142 | + else if(dateStr.indexOf(weekString[6])!=-1) | |
143 | + td.style.background=colorList["saturday"]; | |
144 | + return td; | |
145 | +} | |
146 | + | |
147 | +//////////////////////////// | |
148 | +// get the real date string from date number(0 means begin date) | |
149 | +function getRealDate(date){ | |
150 | + var beginTime=new Date(loglist["info"].beginTime.replace(/-/g, "/")); | |
151 | + var y=beginTime.getFullYear(); | |
152 | + var m=beginTime.getMonth(); | |
153 | + var d=beginTime.getDate(); | |
154 | + var realDate = new Date(y, m, d+date); | |
155 | + return (realDate.getMonth()+1)+"/"+realDate.getDate()+" "+ weekString[realDate.getDay()]; | |
156 | +} | |
157 | + | |
158 | +//////////////////////////// | |
159 | +// setup cell properties | |
160 | +function setupCellProperties(td, date, time, iDevice){ | |
161 | + | |
162 | + td.width=cellWidth; td.height=cellHeight; | |
163 | + | |
164 | + // search log for the time range; | |
165 | + var logItemNo = searchLog(date, time, iDevice); | |
166 | + | |
167 | + td.onmouseover = onmouseoverHandler(td, logItemNo); | |
168 | + td.onmouseout = onmouseoutHandler(td); | |
169 | + | |
170 | + //set background color of the cell | |
171 | + //if two or more items are found, display red border | |
172 | + if(logItemNo.length > 1){ | |
173 | + td.style.background=colorList[iDevice%5]; | |
174 | + td.style.border="3px solid Red"; | |
175 | + } | |
176 | + // one item is found (if the item number == -1, no log is matched) | |
177 | + else if(logItemNo.length == 1){ | |
178 | + if(logItemNo[0] == -1) td.style.background=colorList["none"]; | |
179 | + else td.style.background=colorList[iDevice%5]; | |
180 | + } | |
181 | + // no item | |
182 | + else td.style.background=colorList["blank"]; | |
183 | +} | |
184 | + | |
185 | +//////////////////////////// | |
186 | +// search the usage log corresponding to to the time range and the device number | |
187 | +function searchLog(date, hour, iDevice){ | |
188 | + | |
189 | + var y=beginTime.getFullYear(); | |
190 | + var m=beginTime.getMonth(); | |
191 | + var d=beginTime.getDate(); | |
192 | + | |
193 | + var timeRangeBegin = new Date(y, m, d+date, hour); | |
194 | + var timeRangeEnd = new Date(y, m, d+date, hour+1); | |
195 | + | |
196 | + // omit the range before beginTime or after endTime | |
197 | + if(timeRangeEnd < beginTime || endTime < timeRangeBegin) return -2; | |
198 | + | |
199 | + // make the week number and month/date string | |
200 | + weekday=timeRangeBegin.getDay(); | |
201 | + monthday=timeRangeBegin.getFullYear()+"/"+(timeRangeBegin.getMonth()+1)+"/"+timeRangeBegin.getDate(); | |
202 | + | |
203 | + // search the device in loglist | |
204 | + var logItemNoArray = new Array(); | |
205 | + | |
206 | + nList=loglist["info"].listCount; | |
207 | + for (var i=0; i<nList; i++){ | |
208 | + if(deviceList[iDevice] == loglist[i].device){ | |
209 | + | |
210 | + // loglist includes '2000-10-10 00:00:00' but Date() reads '2000/10/10 00:00:00' | |
211 | + var openTime = new Date(loglist[i].openTime.replace(/-/g, "/")); | |
212 | + var closeTime = new Date(loglist[i].closeTime.replace(/-/g, "/")); | |
213 | + | |
214 | + // if the device is used in the time range, return the log item number | |
215 | + // second line check the device not closed yet | |
216 | + if(timeRangeBegin < closeTime && openTime < timeRangeEnd) logItemNoArray.push(i); | |
217 | + | |
218 | + if(loglist[i].closeTime=="0000-00-00 00:00:00" && openTime < timeRangeEnd) logItemNoArray.push(i); | |
219 | + } | |
220 | + } | |
221 | + | |
222 | + // cannot found | |
223 | + if(logItemNoArray.length==0) logItemNoArray.push(-1); | |
224 | + return logItemNoArray; | |
225 | +} | |
226 | + | |
227 | +//////////////////////////// | |
228 | +// make table for the legend of devices | |
229 | +function makeDevicesLegend() { | |
230 | + | |
231 | + var table = document.createElement("table"); | |
232 | + table.border ="2px #808080 solid"; | |
233 | + var tbody = document.createElement("tbody"); | |
234 | + var nDevice=deviceList.length; | |
235 | + | |
236 | + if(nDevice>0){ | |
237 | + for (var iDevice = 0; iDevice<nDevice; iDevice++) { | |
238 | + var tr = document.createElement("tr"); | |
239 | + var td = document.createElement("td"); | |
240 | + td.innerHTML=deviceList[iDevice]; | |
241 | + tr.appendChild(td); | |
242 | + var td = document.createElement("td"); | |
243 | + td.width=50; | |
244 | + td.style.background=colorList[iDevice%5]; | |
245 | + tr.appendChild(td); | |
246 | + tbody.appendChild(tr); | |
247 | + } | |
248 | + }else{ | |
249 | + var tr = document.createElement("tr"); | |
250 | + var td = document.createElement("td"); | |
251 | + td.innerHTML="No device is used in this period"; | |
252 | + tr.appendChild(td); | |
253 | + tbody.appendChild(tr); | |
254 | + } | |
255 | + | |
256 | + table.appendChild(tbody); | |
257 | + return table; | |
258 | +} | |
259 | + | |
260 | +//////////////////////////// | |
261 | +// get the device names in loglist | |
262 | +function getDeviceList(){ | |
263 | + | |
264 | + var nList=loglist["info"].listCount; | |
265 | + var deviceList = new Array(); | |
266 | + for (var i=0; i<nList; i++){ | |
267 | + logItem=loglist[i]; | |
268 | + if(deviceList.indexOf(logItem.device)==-1){ | |
269 | + deviceList.push(logItem.device); | |
270 | + } | |
271 | + } | |
272 | + return deviceList; | |
273 | +} | |
274 | + | |
275 | +// make canvas to display usage log | |
276 | +function makeCanvas() { | |
277 | + | |
278 | + // if no log in loglist, hide the log header and skip chart creation | |
279 | + if(loglist["info"].listCount==0){ | |
280 | + document.getElementById('logheader').style.display="none"; | |
281 | + return; | |
282 | + } | |
283 | + | |
284 | + // make canvas for drawing chart | |
285 | + var canvas = document.getElementById("usagelog"); | |
286 | + if (canvas) { | |
287 | + var cell = makeCells(); | |
288 | + canvas.appendChild(cell); | |
289 | + var devicesLegend = makeDevicesLegend(); | |
290 | + canvas.appendChild(devicesLegend); | |
291 | + } | |
292 | +} | |
293 | + | |
294 | +//////////////////////////// | |
295 | +// set up event listner | |
296 | +function addOnLoadEvent(func) { | |
297 | + if (window.addEventListener) { | |
298 | + window.addEventListener("load", func, false); | |
299 | + } else if (window.attachEvent) { // for IE | |
300 | + window.attachEvent("onload", func); | |
301 | + } else { | |
302 | + window.onload = func; | |
303 | + } | |
304 | +} | |
305 | + | |
306 | +//////////////////////////// | |
307 | +// initialize | |
308 | +function init(){ | |
309 | + | |
310 | + // get loglist info | |
311 | + beginTime=new Date(loglist["info"].beginTime.replace(/-/g, "/")); | |
312 | + endTime=new Date(loglist["info"].endTime.replace(/-/g, "/")); | |
313 | + | |
314 | + // get device names in loglist | |
315 | + deviceList=getDeviceList(); | |
316 | + | |
317 | + // set up onLoad event listner | |
318 | + addOnLoadEvent(makeCanvas); | |
319 | +} | |
320 | + | |
321 | +//////////////////////////// | |
322 | +// startup from here | |
323 | +init(); | |
324 | + |
@@ -28,8 +28,8 @@ Email: watanaby@is.saga-u.ac.jp | ||
28 | 28 | #include "opengatemd.h" |
29 | 29 | #include <mysql.h> |
30 | 30 | |
31 | - | |
32 | -MYSQL mysql; | |
31 | +static MYSQL mysql; | |
32 | +static int saveLogToMngDb=0; | |
33 | 33 | |
34 | 34 | /****************************************** |
35 | 35 | initialize management db |
@@ -41,6 +41,7 @@ int initMngDb(void){ | ||
41 | 41 | char *user = GetConfValue("MySqlDb/User"); |
42 | 42 | char *password = GetConfValue("MySqlDb/Password"); |
43 | 43 | char *database = GetConfValue("MySqlDb/Database"); |
44 | + char *saveLogStr = GetConfValue("SaveLogToMngDb"); | |
44 | 45 | my_bool reconnect; |
45 | 46 | |
46 | 47 | /* initialize mysql */ |
@@ -61,7 +62,11 @@ int initMngDb(void){ | ||
61 | 62 | |
62 | 63 | /* set auto-reconnect true */ |
63 | 64 | reconnect = TRUE; |
64 | - mysql_options(&mysql, MYSQL_OPT_RECONNECT, &reconnect); | |
65 | + mysql_options(&mysql, MYSQL_OPT_RECONNECT, &reconnect); | |
66 | + | |
67 | + /* read a flag from conf file and hold in a variable */ | |
68 | + if(isNull(saveLogStr)) saveLogToMngDb = 0; | |
69 | + else saveLogToMngDb = strtol(saveLogStr, NULL, 2); | |
65 | 70 | |
66 | 71 | return TRUE; |
67 | 72 | } |
@@ -120,6 +125,82 @@ int queryMacFromMngDb(char* macAddress, char* userId, char* extraId){ | ||
120 | 125 | return found; |
121 | 126 | } |
122 | 127 | |
128 | +/****************************************** | |
129 | +put gate-open log to management db | |
130 | +******************************************/ | |
131 | +int putOpenToMngDb(char* macAddress){ | |
132 | + | |
133 | + char queryStr[BUFFMAXLN]; | |
134 | + struct utsname uts; | |
135 | + | |
136 | + /* if session log table is not used, return */ | |
137 | + if(!saveLogToMngDb) return FALSE; | |
138 | + | |
139 | + /* get domain name */ | |
140 | + uname(&uts); | |
141 | + | |
142 | + /* prepare query string */ | |
143 | + snprintf(queryStr, BUFFMAXLN, | |
144 | + "insert into sessionmd " | |
145 | + "(macAddress, gatewayName, openTime, closeTime) " | |
146 | + "values ('%s','%s', now(), 0)", | |
147 | + macAddress, uts.nodename); | |
148 | + | |
149 | + /* send SQL query */ | |
150 | + if (mysql_query(&mysql, queryStr)){ | |
151 | + err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__, | |
152 | + mysql_error(&mysql)); | |
153 | + return FALSE; | |
154 | + } | |
155 | + | |
156 | + return TRUE; | |
157 | +} | |
158 | + | |
159 | +/****************************************** | |
160 | +put gate-close log to management db | |
161 | +******************************************/ | |
162 | +int putCloseToMngDb(char* macAddress){ | |
163 | + | |
164 | + char queryStr[BUFFMAXLN]; | |
165 | + | |
166 | + /* if session log table is not used, return */ | |
167 | + if(!saveLogToMngDb) return FALSE; | |
168 | + | |
169 | + /* prepare query string */ | |
170 | + snprintf(queryStr, BUFFMAXLN, | |
171 | + "update sessionmd set closeTime=now() " | |
172 | + "where macAddress='%s' and closeTime=0", macAddress); | |
173 | + | |
174 | + /* send SQL query */ | |
175 | + if (mysql_query(&mysql, queryStr)){ | |
176 | + err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__, | |
177 | + mysql_error(&mysql)); | |
178 | + return FALSE; | |
179 | + } | |
180 | + | |
181 | + return TRUE; | |
182 | + | |
183 | +} | |
184 | + | |
185 | +/****************************************** | |
186 | +delete old session log in management DB | |
187 | +******************************************/ | |
188 | +int delOldSessionLogInMngDb(void){ | |
189 | + | |
190 | + char* queryStr="delete from sessionmd where closeTime>0 and closeTime<adddate(now(),interval -1 month)"; | |
191 | + | |
192 | + /* if session log table is not used, return */ | |
193 | + if(!saveLogToMngDb) return FALSE; | |
194 | + | |
195 | + /* send SQL query */ | |
196 | + if (mysql_query(&mysql, queryStr)){ | |
197 | + err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__, | |
198 | + mysql_error(&mysql)); | |
199 | + return FALSE; | |
200 | + } | |
201 | + | |
202 | + return TRUE; | |
203 | +} | |
123 | 204 | |
124 | 205 | /******************************************* |
125 | 206 | get next mac address from watchlist table in management db |
@@ -239,6 +320,22 @@ void CloseMngDb(void){ | ||
239 | 320 | if(debug>1) err_msg("DEBUG:<=closeMngDb()"); |
240 | 321 | } |
241 | 322 | |
323 | +int PutCloseToMngDb(char* macAddress){ | |
324 | + int ret; | |
325 | + if(debug>1) err_msg("DEBUG:=>putCloseToMngDb(%s)", macAddress); | |
326 | + ret = putCloseToMngDb(macAddress); | |
327 | + if(debug>1) err_msg("DEBUG:(%d)<=putCloseToMngDb()",ret); | |
328 | + return ret; | |
329 | +} | |
330 | + | |
331 | +int PutOpenToMngDb(char* macAddress){ | |
332 | + int ret; | |
333 | + if(debug>1) err_msg("DEBUG:=>putOpenToMngDb(%s)", macAddress); | |
334 | + ret = putOpenToMngDb(macAddress); | |
335 | + if(debug>1) err_msg("DEBUG:(%d)<=putOpenToMngDb()",ret); | |
336 | + return ret; | |
337 | +} | |
338 | + | |
242 | 339 | int GetNextRecordFromWatchlistTableInMngDb(char* macAddress){ |
243 | 340 | int ret; |
244 | 341 | if(debug>1) err_msg("DEBUG:=>getNextRecordFromWatchlisttableInMngDb()"); |
@@ -256,3 +353,10 @@ int IsAllFoundInWatchlistTable(void){ | ||
256 | 353 | return ret; |
257 | 354 | } |
258 | 355 | |
356 | +int DelOldSessionLogInMngDb(void){ | |
357 | + int ret; | |
358 | + if(debug>1) err_msg("DEBUG:=>delOldSessionLogInMngDb()"); | |
359 | + ret = delOldSessionLogInMngDb(); | |
360 | + if(debug>1) err_msg("DEBUG:(%d)<=delOldSessionLogInMngDb()", ret); | |
361 | + return ret; | |
362 | +} |
@@ -182,8 +182,9 @@ int main(int argc, char **argv) | ||
182 | 182 | checkInterval=atoi(GetConfValue("UselessCheckInterval")); |
183 | 183 | uselessCheckTime=time(NULL); |
184 | 184 | DelUselessSessions(); |
185 | + DelOldMacInfoInWorkDb(); | |
186 | + DelOldSessionLogInMngDb(); | |
185 | 187 | |
186 | - DelOldMacInfoInWorkDb(); /**********************************/ | |
187 | 188 | /*** enter infinite loop of packet inspection ***/ |
188 | 189 | while(1){ |
189 | 190 |
@@ -218,6 +219,7 @@ int main(int argc, char **argv) | ||
218 | 219 | uselessCheckTime = time(NULL); |
219 | 220 | DelUselessSessions(); |
220 | 221 | DelOldMacInfoInWorkDb(); |
222 | + DelOldSessionLogInMngDb(); | |
221 | 223 | } |
222 | 224 | |
223 | 225 | /* and return to loop top */ |
@@ -313,6 +315,7 @@ int main(int argc, char **argv) | ||
313 | 315 | uselessCheckTime = time(NULL); |
314 | 316 | DelUselessSessions(); |
315 | 317 | DelOldMacInfoInWorkDb(); |
318 | + DelOldSessionLogInMngDb(); | |
316 | 319 | } |
317 | 320 | } |
318 | 321 | /*** end of infinite loop ***/ |
@@ -173,8 +173,11 @@ int ReFormatMacAddr(char* macAddr); | ||
173 | 173 | int InitMngDb(void); |
174 | 174 | int QueryMacFromMngDb(char* macAddress, char* userid, char* extraid); |
175 | 175 | void CloseMngDb(void); |
176 | +int PutCloseToMngDb(char* macAddress); | |
177 | +int PutOpenToMngDb(char* macAddress); | |
176 | 178 | int GetNextRecordFromWatchlistTableInMngDb(char* macAddress); |
177 | 179 | int IsAllFoundInWatchlistTable(void); |
180 | +int DelOldSessionLogInMngDb(void); | |
178 | 181 | |
179 | 182 | /* workdb.c */ |
180 | 183 | int SetupSqliteBusyTimeoutValue(void); |
@@ -87,6 +87,9 @@ int addSession(char* macAddress, char* userId, char* extraId){ | ||
87 | 87 | /* write open log to syslog */ |
88 | 88 | WriteOpenToSyslog(userId, extraId, macAddress); |
89 | 89 | |
90 | + /* write log to management db */ | |
91 | + PutOpenToMngDb(macAddress); | |
92 | + | |
90 | 93 | return TRUE; |
91 | 94 | } |
92 | 95 |
@@ -119,6 +122,9 @@ void delSession(char* macAddress){ | ||
119 | 122 | WriteCloseToSyslog(userId, extraId, macAddress, openTime); |
120 | 123 | if(debug>0) WriteSessionInfoToSyslog(userId, extraId, |
121 | 124 | macAddress, ruleNumber); |
125 | + | |
126 | + /* write close log to management db */ | |
127 | + PutCloseToMngDb(macAddress); | |
122 | 128 | |
123 | 129 | /* reset the memory of MAC IP pair */ |
124 | 130 | ResetMacIpPairs(macAddress); |
@@ -175,6 +181,9 @@ int closeSession(void* pParam, int argc, char *argv[], char* colName[]){ | ||
175 | 181 | WriteCloseToSyslog(userId, extraId, macAddress, openTime); |
176 | 182 | if(debug>0) WriteSessionInfoToSyslog(userId, extraId, macAddress, ruleNumber); |
177 | 183 | |
184 | + /* write close log to management db */ | |
185 | + PutCloseToMngDb(macAddress); | |
186 | + | |
178 | 187 | /* reset the memory of MAC IP pair */ |
179 | 188 | ResetMacIpPairs(macAddress); |
180 | 189 |
@@ -330,6 +339,7 @@ void removeSessionUnmatchedToIpfwRule(DB* ruleTable, DB* sessionTable){ | ||
330 | 339 | err_msg("ERR at %s#%d: fail to get session info",__FILE__,__LINE__); |
331 | 340 | }else{ |
332 | 341 | WriteCloseToSyslog(userId, extraId, macAddress, openTime); |
342 | + PutCloseToMngDb(macAddress); | |
333 | 343 | DelSessionFromWorkDb(macAddress); |
334 | 344 | } |
335 | 345 |
@@ -387,6 +397,7 @@ void removeIpfwRuleUnmatchedToSession(DB* ruleTable, DB* sessionTable){ | ||
387 | 397 | |
388 | 398 | /* write log */ |
389 | 399 | WriteCloseToSyslog("?", "", macAddress, time(NULL)); |
400 | + PutCloseToMngDb(macAddress); | |
390 | 401 | |
391 | 402 | /* write session info to syslog (no user info)*/ |
392 | 403 | if(debug>0) WriteSessionInfoToSyslog("?","", macAddress, ruleNumber); |
@@ -1171,6 +1171,10 @@ int putUpdatePageToClient(char *language, char* userId, char* extraId, int ownUp | ||
1171 | 1171 | if(strstr(buff, "%%MACLIST%%")!=NULL){ |
1172 | 1172 | PutMacListToClient(userId,extraId); |
1173 | 1173 | } |
1174 | + else if(strstr(buff, "%%USAGELOG%%")!=NULL){ | |
1175 | + PutUsageLogToClient(userId, extraId, language); | |
1176 | + } | |
1177 | + | |
1174 | 1178 | else if(strstr(buff, "%%ERRORLIST%%")!=NULL){ |
1175 | 1179 | InsertMessageToPage(language); |
1176 | 1180 | } |
@@ -1241,6 +1245,55 @@ void putMacListToClient(char* userId, char* extraId){ | ||
1241 | 1245 | } |
1242 | 1246 | } |
1243 | 1247 | |
1248 | +/*********************************************/ | |
1249 | +/* put usage log to the client */ | |
1250 | +/*********************************************/ | |
1251 | +void putUsageLogToClient(char *userId, char* extraId, char* language){ | |
1252 | + | |
1253 | + char macAddr[ADDRMAXLN]=""; | |
1254 | + char deviceName[WORDMAXLN]=""; | |
1255 | + char openTime[WORDMAXLN]=""; | |
1256 | + char closeTime[WORDMAXLN]=""; | |
1257 | + char gatewayName[WORDMAXLN]=""; | |
1258 | + char beginTime[WORDMAXLN]=""; | |
1259 | + char endTime[WORDMAXLN]=""; | |
1260 | + int dateCount=0; | |
1261 | + char* p; | |
1262 | + int count=0; | |
1263 | + | |
1264 | + /*** get usage log and insert it to html doc as JSON format */ | |
1265 | + /* get one log form management db */ | |
1266 | + while(GetNextUsageLogFromMngDb(userId,extraId,macAddr,deviceName, | |
1267 | + openTime,closeTime,gatewayName)){ | |
1268 | + | |
1269 | + /* split hostname only (opengate01.example.com -> opengate01) */ | |
1270 | + if((p=strchr(gatewayName,'.'))!=NULL) *p='\0'; | |
1271 | + | |
1272 | + /* put out the log as a JSON item */ | |
1273 | + /* table skeleton is prepared in template file */ | |
1274 | + printf("\t %d:{\n", count); | |
1275 | + printf("\t\t macAddress: '%s',\n", macAddr); | |
1276 | + printf("\t\t device: '%s',\n",deviceName); | |
1277 | + printf("\t\t openTime: '%s',\n",openTime); | |
1278 | + printf("\t\t closeTime: '%s',\n",closeTime); | |
1279 | + printf("\t\t gateway: '%s'\n",gatewayName); | |
1280 | + printf("\t },\n"); | |
1281 | + | |
1282 | + /* increment log counter */ | |
1283 | + count++; | |
1284 | + } | |
1285 | + | |
1286 | + /* put out the JSON item for summary info of the list */ | |
1287 | + GetTimeRangeToShowLog(beginTime, endTime, &dateCount); | |
1288 | + printf("\t info:{\n"); | |
1289 | + printf("\t\t beginTime: '%s',\n", beginTime); | |
1290 | + printf("\t\t endTime: '%s',\n", endTime); | |
1291 | + printf("\t\t listCount: %d,\n", count); | |
1292 | + printf("\t\t dateCount: %d\n", dateCount); | |
1293 | + printf("\t }\n"); | |
1294 | +} | |
1295 | + | |
1296 | + | |
1244 | 1297 | /********************************************************/ |
1245 | 1298 | /* the client is an allowable http-user-agent or not */ |
1246 | 1299 | /* the allowable http-user-agent can be defined in conf */ |
@@ -1479,6 +1532,12 @@ void PutMacListToClient(char *userId, char* extraId){ | ||
1479 | 1532 | if(debug>1) err_msg("DEBUG:<=putMacListToClient( )"); |
1480 | 1533 | } |
1481 | 1534 | |
1535 | +void PutUsageLogToClient(char *userId, char* extraId, char* language){ | |
1536 | + if(debug>1) err_msg("DEBUG:=>putUsageLogToClient(%s,%s,%s)",userId,extraId,language); | |
1537 | + putUsageLogToClient(userId,extraId,language); | |
1538 | + if(debug>1) err_msg("DEBUG:<=putUsageLogToClient( )"); | |
1539 | +} | |
1540 | + | |
1482 | 1541 | int IsAccessedFromAllowableAgent(void){ |
1483 | 1542 | int ret; |
1484 | 1543 | if(debug>1) err_msg("DEBUG:=>isAccessedFromAllowableAgent( )"); |
@@ -480,6 +480,66 @@ int pauseMacAddrInMngDb(char* macAddr){ | ||
480 | 480 | } |
481 | 481 | |
482 | 482 | /******************************************* |
483 | + get next next usage log from management db | |
484 | + if end of list, return false | |
485 | +*******************************************/ | |
486 | +int getNextUsageLogFromMngDb(char* userId, char* extraId, char* macAddr, char* deviceName, char* openTime, char* closeTime, char* gatewayName){ | |
487 | + static MYSQL_RES *res=NULL; | |
488 | + MYSQL_ROW row; | |
489 | + char queryStr[BUFFMAXLN]; | |
490 | + | |
491 | + /* set default values */ | |
492 | + macAddr[0]=deviceName[0]=openTime[0]=gatewayName[0]='\0'; | |
493 | + | |
494 | + /* if do not get result yet */ | |
495 | + if(res==NULL){ | |
496 | + | |
497 | + /* prepare query string */ | |
498 | + /* get log where addr.entry < session.open < addr.limit */ | |
499 | + /* (the same device may be registered by other users in old days) */ | |
500 | + snprintf(queryStr, BUFFMAXLN, | |
501 | + "select macaddrs.macAddress, device, openTime, closeTime, " | |
502 | + " gatewayName " | |
503 | + " from macaddrs, sessionmd " | |
504 | + " where macaddrs.macAddress=sessionmd.macAddress " | |
505 | + " and entryDate < openTime and openTime < limitDate " | |
506 | + " and (%s<closeTime or closeTime=0) " | |
507 | + " and userId='%s' and extraId='%s'", | |
508 | + GetConfValue("ShowLogAfter"), userId, extraId); | |
509 | + | |
510 | + /* send SQL query */ | |
511 | + if (mysql_query(&mysql, queryStr)){ | |
512 | + | |
513 | + /* query error */ | |
514 | + /* might be caused by <SaveLogToMngDb> setting in opengatemd.conf */ | |
515 | + return FALSE; | |
516 | + } | |
517 | + | |
518 | + /* store result */ | |
519 | + res = mysql_store_result(&mysql); | |
520 | + } | |
521 | + | |
522 | + /* output table name */ | |
523 | + row = mysql_fetch_row(res); | |
524 | + | |
525 | + /* if found, return values */ | |
526 | + if(row!=NULL){ | |
527 | + strlcpy(macAddr, row[0],ADDRMAXLN); | |
528 | + strlcpy(deviceName,row[1],WORDMAXLN); | |
529 | + strlcpy(openTime,row[2],WORDMAXLN); | |
530 | + strlcpy(closeTime,row[3],WORDMAXLN); | |
531 | + strlcpy(gatewayName,row[4],WORDMAXLN); | |
532 | + return TRUE; | |
533 | + } | |
534 | + /* if not found, free memory area */ | |
535 | + else{ | |
536 | + mysql_free_result(res); | |
537 | + return FALSE; | |
538 | + } | |
539 | +} | |
540 | + | |
541 | + | |
542 | +/******************************************* | |
483 | 543 | get next mail address near limit date |
484 | 544 | from management db |
485 | 545 | *******************************************/ |
@@ -860,6 +920,17 @@ int PauseMacAddrInMngDb(char* macAddr){ | ||
860 | 920 | return ret; |
861 | 921 | } |
862 | 922 | |
923 | +int GetNextUsageLogFromMngDb(char* userId, char* extraId, char* macAddr, | |
924 | + char* deviceName, char* openTime, char* closeTime, char* gatewayName){ | |
925 | + int ret; | |
926 | + if(debug>1) err_msg("DEBUG:=>getNextUsageLogFromMngDb(%s,%s)",userId,extraId); | |
927 | + ret=getNextUsageLogFromMngDb(userId,extraId,macAddr,deviceName, | |
928 | + openTime,closeTime,gatewayName); | |
929 | + if(debug>1) err_msg("DEBUG:(%d)<=getNextUsageLogFromMngDb(,,%s,%s,%s,%s,%s)", | |
930 | + ret,macAddr,deviceName,openTime,closeTime,gatewayName); | |
931 | + return ret; | |
932 | +} | |
933 | + | |
863 | 934 | int GetNextMailAddressFromMngDb(char* mailAddress, char* limitDate, char*device){ |
864 | 935 | int ret; |
865 | 936 | if(debug>1) err_msg("DEBUG:=>getnextMailAddressFromMngDb( )"); |
@@ -189,6 +189,7 @@ int CountMacModifyPerDayInMngDb(char* userId, char* extraId, char* macAddress); | ||
189 | 189 | int DelMacAddrFromMngDb(char* macAddr); |
190 | 190 | int RenewMacAddrInMngDb(char* macAddr); |
191 | 191 | int PauseMacAddrInMngDb(char* macAddr); |
192 | +int GetNextUsageLogFromMngDb(char* userId, char* extraId, char* macAddr, char* deviceName, char* openTime, char* closeTime, char* gatewayName); | |
192 | 193 | int GetNextMailAddressFromMngDb(char* mailAddress, char* limitDate, char*device); |
193 | 194 | int GetTimeRangeToShowLog(char* beginTime, char* endTime, int* dateCount); |
194 | 195 | int RenameDeviceNameInMngDb(char* macAddr, char* nameStr); |
@@ -244,6 +245,7 @@ int IsCorrectCookie(char* cookie, int userType); | ||
244 | 245 | int AnalyzeUpdateRequestAndExecute(char *content, char* userId, char* extraId); |
245 | 246 | int PutUpdatePageToClient(char *language, char* userId, char* extraId, int ownUpdate, char* redirectedUrl); |
246 | 247 | void PutMacListToClient(char *userId, char* extraId); |
248 | +void PutUsageLogToClient(char *userId, char* extraId, char* language); | |
247 | 249 | int IsAccessedFromAllowableAgent(void); |
248 | 250 | |
249 | 251 | /* messages.c */ |
@@ -38,6 +38,12 @@ if(!(list($device, $mailAddress)=getDataFromMysql($macAddress))){ | ||
38 | 38 | return; |
39 | 39 | } |
40 | 40 | |
41 | +// if reported recently, skip reporting | |
42 | +if(skipReporting($link, $macAddress, $gatewayName, $reportInterval)){ | |
43 | + mysqli_close($link); | |
44 | + return; | |
45 | +} | |
46 | + | |
41 | 47 | // close database |
42 | 48 | mysqli_close($link); |
43 | 49 |
@@ -125,6 +131,36 @@ function getDataFromMysql($macAddress){ | ||
125 | 131 | } |
126 | 132 | |
127 | 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 | +/*** | |
128 | 164 | send mail to the user mail address |
129 | 165 | ***/ |
130 | 166 | function sendMailToUser($mailSender, $mailAddress, $device, $gatewayName, |
@@ -7,6 +7,7 @@ | ||
7 | 7 | <h2>Show Tables</h2> |
8 | 8 | <p> |
9 | 9 | <a href='showtable.php?table=macaddrs'>macaddrs</a> |
10 | +<a href='showtable.php?table=sessionmd'>sessionmd</a> | |
10 | 11 | <a href='showtable.php?table=nicvendors'>nicvendors</a> <br> |
11 | 12 | (Show max 1000 rows of selected table). |
12 | 13 | </p> |
@@ -15,6 +15,17 @@ create table if not exists macaddrs( | ||
15 | 15 | renewDate DATETIME, |
16 | 16 | limitDate DATETIME); |
17 | 17 | |
18 | +create table if not exists sessionmd( | |
19 | + macAddress CHAR(18), | |
20 | + gatewayName TINYTEXT, | |
21 | + openTime DATETIME, | |
22 | + closeTime DATETIME); | |
23 | + | |
24 | +create table if not exists macippair( | |
25 | + macAddress CHAR(18), | |
26 | + ipAddress TINYTEXT, | |
27 | + findTime DATETIME); | |
28 | + | |
18 | 29 | create table if not exists macmodify( |
19 | 30 | userId TINYTEXT, |
20 | 31 | extraId TINYTEXT, |