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