Merge pull request #278 from nmav/ocserv
[project/luci.git] / modules / freifunk / luasrc / view / freifunk / public_status.htm
1 <%
2 local sys = require "luci.sys"
3 local twa = require "luci.tools.webadmin"
4
5 -- System
6 local model, system, memtotal, memcached, membuffers, memfree, bogomips = sys.sysinfo()
7 local uptime = twa.date_format(tonumber(sys.uptime()))
8 local time = os.date("%a, %d %b %Y, %H:%M:%S")
9 local load1, load5, load15 = sys.loadavg()
10 local load = string.format("%.2f, %.2f, %.2f", load1, load5, load15)
11
12 local mem = string.format(
13         "%.2f MB (%.2f %s, %.2f %s, %.2f %s, %.2f %s)",
14         tonumber(memtotal) / 1024,
15         tonumber(memtotal - memfree) / 1024,
16         tostring(i18n.translate("used")),
17         memfree / 1024,
18         tostring(i18n.translate("free")),
19         memcached / 1024,
20         tostring(i18n.translate("cached")),
21         membuffers / 1024,
22         tostring(i18n.translate("buffered"))
23 )
24
25 -- update interval
26 local bogomips = bogomips or 100
27 local interval = 10
28 if bogomips > 350 then
29         interval = 5
30 end
31
32 -- wireless
33 local ntm = require "luci.model.network".init()
34 local devices  = ntm:get_wifidevs()
35 local netlist = { }
36 local netdevs = { }
37 local dev
38 for _, dev in ipairs(devices) do
39         local net
40         for _, net in ipairs(dev:get_wifinets()) do
41                 netlist[#netlist+1] = net:ifname()
42                 netdevs[net:ifname()] = dev:name()
43         end
44 end
45 local has_iwinfo = pcall(require, "iwinfo")
46
47 -- Routes
48 local defroutev4 = sys.net.defaultroute()
49 local defroutev6 = sys.net.defaultroute6()
50
51 if defroutev4 then
52         defroutev4.dest = defroutev4.dest:string()
53         defroutev4.gateway = defroutev4.gateway:string()
54 else
55         -- probably policy routing activated, try olsr-default table
56         local dr4 = sys.exec("ip r s t olsr-default")
57         if dr4 then
58                 defroutev4 = { }
59                 defroutev4.dest, defroutev4.gateway, defroutev4.device, defroutev4.metric = dr4:match("^(%w+) via (%d+.%d+.%d+.%d+) dev ([%w-]+) +metric (%d+)")
60         end
61 end
62
63 if defroutev6 then
64         defroutev6.dest = defroutev6.dest:string()
65         defroutev6.nexthop = defroutev6.nexthop:string()
66 end
67
68 if luci.http.formvalue("status") == "1" then
69         local rv = { }
70         for dev in pairs(netdevs) do
71                 local j = { id = dev }
72                 local iw = luci.sys.wifi.getiwinfo(dev)
73                 if iw then
74                         local f
75                         for _, f in ipairs({
76                                 "channel", "txpower", "bitrate", "signal", "noise",
77                                 "quality", "quality_max", "mode", "ssid", "bssid", "encryption", "ifname"
78                         }) do
79                                 j[f] = iw[f]
80                         end
81                 end
82                 rv[#rv+1] = j
83         end
84
85         if defroutev6 then
86                 def6 = {
87                         gateway = defroutev6.nexthop,
88                         dest = defroutev6.dest,
89                         dev = defroutev6.device,
90                         metr = defroutev6.metric
91                 }
92         end
93
94         if defroutev4 then
95                 def4 = {
96                         gateway = defroutev4.gateway,
97                         dest = defroutev4.dest,
98                         dev = defroutev4.device,
99                         metr = defroutev4.metric
100                 }
101         end
102
103         rv[#rv+1] = {
104                 time = time,
105                 uptime = uptime,
106                 load = load,
107                 mem = mem,
108                 defroutev4 = def4,
109                 defroutev6 = def6
110         }
111
112         luci.http.prepare_content("application/json")
113         luci.http.write_json(rv)
114         return
115 end
116 -%>
117
118 <%+header%>
119
120 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
121
122 <script type="text/javascript">//<![CDATA[
123          XHR.poll(<%=interval%> , '<%=REQUEST_URI%>', { status: 1 },
124                 function(x, st)
125                 {
126                         if (st)
127                         {
128                                 for( var i = 0; i < st.length; i++ )
129                                 {
130                                         var iw = st[i];
131                                         var is_assoc = (iw.bssid && iw.channel);
132                                         var p = (100 / iw.quality_max * iw.quality);
133                                         var q = is_assoc ? p : -1;
134
135                                         var icon;
136                                         if (q < 0)
137                                                 icon = "<%=resource%>/icons/signal-none.png";
138                                         else if (q == 0)
139                                                 icon = "<%=resource%>/icons/signal-0.png";
140                                         else if (q < 25)
141                                                 icon = "<%=resource%>/icons/signal-0-25.png";
142                                         else if (q < 50)
143                                                 icon = "<%=resource%>/icons/signal-25-50.png";
144                                         else if (q < 75)
145                                                 icon = "<%=resource%>/icons/signal-50-75.png";
146                                         else
147                                                 icon = "<%=resource%>/icons/signal-75-100.png";
148
149                                         var power = document.getElementById(iw.id + '-txpower');
150                                         if (power)
151                                                 power.innerHTML = String.format('%s dbm', iw.txpower);
152
153                                         var signal = document.getElementById(iw.id + '-signal');
154                                         if (signal)
155                                                 signal.innerHTML = String.format(
156                                                         '<img src="%s" title="Signal: %s db / Noise: %s db" alt="Signal Quality" />',
157                                                         icon, iw.signal, iw.noise
158                                                 );
159
160                                         var bitrate = document.getElementById(iw.id + '-bitrate');
161                                         if (bitrate)
162                                                 bitrate.innerHTML = String.format('%s Mb/s', iw.bitrate ? iw.bitrate / 1000 : '?');
163
164                                         var ssid = document.getElementById(iw.id + '-ssid');
165                                         if (ssid)
166                                                 ssid.innerHTML = iw.ssid;
167
168                                         var bssid = document.getElementById(iw.id + '-bssid');
169                                         if (bssid)
170                                                 bssid.innerHTML = iw.bssid;
171
172                                         var channel = document.getElementById(iw.id + '-channel');
173                                         if (channel)
174                                                 channel.innerHTML = iw.channel;
175
176                                         var mode = document.getElementById(iw.id + '-mode');
177                                         if (mode)
178                                         mode.innerHTML = iw.mode;
179                                 }
180
181                                 i = st.length - 1
182                                 var u
183
184                                 if (u = document.getElementById('dynuptime'))
185                                         u.innerHTML = st[i].uptime;
186
187                                 if (u = document.getElementById('dynload'))
188                                         u.innerHTML = st[i].load;
189
190                                 if (u = document.getElementById('dynmem'))
191                                         u.innerHTML = st[i].mem;
192
193                                 if (u = document.getElementById('dyntime'))
194                                         u.innerHTML = st[i].time;
195
196                                 if (st[i].defroutev4)
197                                 {
198                                         if (u = document.getElementById('v4dst'))
199                                                 u.innerHTML = st[i].defroutev4.dest;
200
201                                         if (u = document.getElementById('v4gw'))
202                                                 u.innerHTML = st[i].defroutev4.gateway;
203
204                                         if (u = document.getElementById('v4dev'))
205                                                 u.innerHTML = st[i].defroutev4.dev;
206
207                                         if (u = document.getElementById('v4metr'))
208                                                 u.innerHTML = st[i].defroutev4.metr;
209                                 }
210
211                                 if (st[i].defroutev6)
212                                 {
213                                         if (u = document.getElementById('v6dst'))
214                                                 u.innerHTML = st[i].defroutev6.dest;
215
216                                         if (u = document.getElementById('v6gw'))
217                                                 u.innerHTML = st[i].defroutev6.gateway;
218
219                                         if (u = document.getElementById('v6dev'))
220                                                 u.innerHTML = st[i].defroutev6.dev;
221
222                                         if (u = document.getElementById('v6metr'))
223                                                 u.innerHTML = st[i].defroutev6.metr;
224                                 }
225                         }
226                 }
227         );
228 //]]></script>
229
230 <div class="cbi-map">
231         <h2><%:System%></h2>
232         <div class="cbi-section-node">
233                 <div class="cbi-value"><label class="cbi-value-title"><%:System%></label><div class="cbi-value-field"><%=system%></div></div>
234                 <div class="cbi-value"><label class="cbi-value-title"><%:Processor%></label><div class="cbi-value-field"><%=model%></div></div>
235                 <div class="cbi-value"><label class="cbi-value-title"><%:Load%></label><div class="cbi-value-field" id="dynload"><%=load%></div></div>
236                 <div class="cbi-value"><label class="cbi-value-title"><%:Memory%></label><div class="cbi-value-field" id="dynmem"><%=mem%></div></div>
237                 <div class="cbi-value"><label class="cbi-value-title"><%:Local Time%></label><div class="cbi-value-field" id="dyntime"><%=time%></div></div>
238                 <div class="cbi-value"><label class="cbi-value-title"><%:Uptime%></label><div class="cbi-value-field" id="dynuptime"><%=uptime%></div></div>
239         </div>
240 </div>
241
242 <% if devices[1] then %>
243
244 <div class="cbi-map">
245         <h2><%:Wireless Overview%></h2>
246
247                 <% if not has_iwinfo then %>
248                         <div class="errorbox">
249                                 <strong><%:Package libiwinfo required!%></strong><br />
250                                 <%_The <em>libiwinfo</em> package is not installed. You must install this component for working wireless configuration!%>
251                         </div>
252                 <% end %>
253
254                 <div class="cbi-section">
255                         <div class="cbi-section-node">
256                                 <table class="cbi-section-table">
257                                         <tr class="cbi-section-table-titles">
258                                                 <th class="cbi-section-table-cell"><%:Signal%></th>
259                                                 <th class="cbi-section-table-cell"><%:Bitrate%></th>
260                                                 <th class="cbi-section-table-cell"><%:SSID%></th>
261                                                 <th class="cbi-section-table-cell"><%:BSSID%></th>
262                                                 <th class="cbi-section-table-cell"><%:Channel%></th>
263                                                 <th class="cbi-section-table-cell"><%:Mode%></th>
264                                                 <th class="cbi-section-table-cell"><%:TX%>-<%:Power%></th>
265                                                 <th class="cbi-section-table-cell"><%:Interface%></th>
266                                         </tr>
267         <%
268         for _, dev in ipairs(devices) do
269         local net
270                 for _, net in ipairs(dev:get_wifinets()) do
271                         netlist[#netlist+1] = net:ifname()
272                         netdevs[net:ifname()] = dev:name()
273
274                         if net.iwdata.device then
275                                 local signal = net.iwinfo.signal or "N/A"
276                                 local noise = net.iwinfo.noise or "N/A"
277                                 local q = net.iwinfo.quality or "0"
278                                 local qmax = net.iwinfo.quality_max or "100"
279                                 local qperc = q / qmax * 100
280
281                                 if qperc == 0 then
282                                         icon = "signal-none.png"
283                                 elseif qperc < 26 then
284                                         icon = "signal-0-25.png"
285                                 elseif qperc < 51 then
286                                         icon = "signal-25-50.png"
287                                 elseif qperc < 76 then
288                                         icon = "signal-50-75.png"
289                                 elseif qperc < 100 then
290                                         icon = "signal-75-100.png"
291                                 else
292                                         icon = "signal-0.png"
293                                 end
294
295                                 signal_string = "<img src='"..resource.."/icons/"..icon.."' title='Signal: "..signal.." db / Noise: "..noise.." db' alt='Signal Quality'></img>"
296
297                                 local ssid = net.iwinfo.ssid or "N/A"
298                                 local bssid = net.iwinfo.bssid or "N/A"
299                                 local chan = net.iwinfo.channel or "N/A"
300                                 local mode = net.iwinfo.mode or "N/A"
301                                 local txpwr = net.iwinfo.txpower or "N/A"
302                                 if txpwr ~= "N/A" then
303                                         txpwr = txpwr.." dbm"
304                                 end
305                                 local bitrate = net.iwinfo.bitrate or "N/A"
306                                 if bitrate ~= "N/A" then
307                                         bitrate = ( bitrate / 1000 ).."Mb/s"
308                                 end
309                                 local interface = net.iwdata.ifname or "N/A"
310         %>
311                                                 <tr class="cbi-section-table-row cbi-rowstyle-1">
312                                                 <td class="cbi-value-field" id="<%=net:ifname()%>-signal"><%=signal_string%></td>
313                                                 <td class="cbi-value-field" id="<%=net:ifname()%>-bitrate"><%=bitrate%></td>
314                                                 <td class="cbi-value-field" id="<%=net:ifname()%>-ssid"><%=ssid%></td>
315                                                 <td class="cbi-value-field" id="<%=net:ifname()%>-bssid"><%=bssid%></td>
316                                                 <td class="cbi-value-field" id="<%=net:ifname()%>-channel"><%=chan%></td>
317                                                 <td class="cbi-value-field" id="<%=net:ifname()%>-mode"><%=mode%></td>
318                                                 <td class="cbi-value-field" id="<%=net:ifname()%>-txpower"><%=txpwr%></td>
319                                                 <td class="cbi-value-field"><%=interface%></td>
320                                         </tr>
321                         <% end
322                 end
323         end %>
324                         </table>
325                 </div>
326         </div>
327 </div>
328 <% end %>
329
330 <div class="cbi-map">
331         <h2><%:Default routes%></h2>
332                 <div class="cbi-section">
333                         <div class="cbi-section-node">
334
335 <% if not defroutev4 and not defroutev6 then %>
336         <%:No default routes known.%>
337 <%else%>
338                                 <table class="cbi-section-table">
339                                                 <tr class="cbi-section-table-titles">
340                                                         <th class="cbi-section-table-cell"><%:Network%></th>
341                                                         <th class="cbi-section-table-cell"><%:Interface%></th>
342                                                         <th class="cbi-section-table-cell"><%:Gateway%></th>
343                                                         <th class="cbi-section-table-cell"><%:Metric%></th>
344                                                 </tr>
345
346         <% if defroutev4 then %>
347
348                                                 <tr class="cbi-section-table-row cbi-rowstyle-1">
349                                                         <td class="cbi-value-field" id="v4dst"><%=defroutev4.dest%></td>
350                                                         <td class="cbi-value-field" id="v4dev"><%=defroutev4.device%></td>
351                                                         <td class="cbi-value-field" id="v4gw"><%=defroutev4.gateway%></td>
352                                                         <td class="cbi-value-field" id="v4metr"><%=defroutev4.metric%></td>
353                                                 </tr>
354
355         <% end
356         if defroutev6 then %>
357
358                                                 <tr class="cbi-section-table-row cbi-rowstyle-2">
359                                                         <td class="cbi-value-field" id="v6dst"><%=defroutev6.dest%></td>
360                                                         <td class="cbi-value-field" id="v6dev"><%=defroutev6.device%></td>
361                                                         <td class="cbi-value-field" id="v6gw"><%=defroutev6.nexthop%></td>
362                                                         <td class="cbi-value-field" id="v6metr"><%=defroutev6.metric%></td>
363                                                 </tr>
364
365         <% end %>
366
367                                 </table>
368 <% end %>
369                 </div>
370         </div>
371 </div>
372 <%+footer%>