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