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