ea4ec3f5f0ef2ab669c6759a095afe89369639cf
[project/luci.git] / modules / admin-full / luasrc / view / admin_status / index.htm
1 <%#
2 LuCI - Lua Configuration Interface
3 Copyright 2008 Steven Barth <steven@midlink.org>
4 Copyright 2008-2011 Jo-Philipp Wich <xm@subsignal.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13
14 -%>
15
16 <%
17 require "luci.fs"
18 require "luci.tools.status"
19
20 local has_ipv6 = luci.fs.access("/proc/net/ipv6_route")
21 local has_dhcp = luci.fs.access("/etc/config/dhcp")
22 local has_wifi = luci.fs.stat("/etc/config/wireless")
23 has_wifi = has_wifi and has_wifi.size > 0
24
25 if luci.http.formvalue("status") == "1" then
26 local ntm = require "luci.model.network".init()
27 local dr4 = luci.sys.net.defaultroute()
28 local dr6 = luci.sys.net.defaultroute6()
29 local wan, wan6
30
31 if dr4 and dr4.device then
32 wan = ntm:get_interface(dr4.device)
33 wan = wan and wan:get_network()
34 end
35
36 if dr6 and dr6.device then
37 wan6 = ntm:get_interface(dr6.device)
38 wan6 = wan6 and wan6:get_network()
39 end
40
41 local _, _, memtotal, memcached, membuffers, memfree = luci.sys.sysinfo()
42
43 local conn_count = tonumber((
44 luci.sys.exec("wc -l /proc/net/nf_conntrack") or
45 luci.sys.exec("wc -l /proc/net/ip_conntrack") or
46 ""):match("%d+")) or 0
47
48 local conn_max = tonumber((
49 luci.sys.exec("sysctl net.nf_conntrack_max") or
50 luci.sys.exec("sysctl net.ipv4.netfilter.ip_conntrack_max") or
51 ""):match("%d+")) or 4096
52
53 local rv = {
54 uptime = luci.sys.uptime(),
55 localtime = os.date(),
56 loadavg = { luci.sys.loadavg() },
57 memtotal = memtotal,
58 memcached = memcached,
59 membuffers = membuffers,
60 memfree = memfree,
61 connmax = conn_max,
62 conncount = conn_count,
63 leases = luci.tools.status.dhcp_leases(),
64 wifinets = luci.tools.status.wifi_networks()
65 }
66
67 if wan then
68 rv.wan = {
69 ipaddr = wan:ipaddr(),
70 gwaddr = wan:gwaddr(),
71 netmask = wan:netmask(),
72 dns = wan:dnsaddrs(),
73 expires = wan:expires(),
74 uptime = wan:uptime(),
75 proto = wan:proto(),
76 ifname = wan:ifname(),
77 link = wan:adminlink()
78 }
79 end
80
81 if wan6 then
82 rv.wan6 = {
83 ip6addr = wan6:ip6addr(),
84 gw6addr = wan6:gw6addr(),
85 dns = wan6:dns6addrs(),
86 uptime = wan6:uptime(),
87 ifname = wan6:ifname(),
88 link = wan6:adminlink()
89 }
90 end
91
92 luci.http.prepare_content("application/json")
93 luci.http.write_json(rv)
94
95 return
96 end
97
98 local system, model = luci.sys.sysinfo()
99 -%>
100
101 <%+header%>
102
103 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
104 <script type="text/javascript">//<![CDATA[
105 function progressbar(v, m)
106 {
107 var vn = parseInt(v) || 0;
108 var mn = parseInt(m) || 100;
109 var pc = Math.floor((100 / mn) * vn);
110
111 return String.format(
112 '<div style="width:200px; position:relative; border:1px solid #999999">' +
113 '<div style="background-color:#CCCCCC; width:%d%%; height:15px">' +
114 '<div style="position:absolute; left:0; top:0; text-align:center; width:100%%; color:#000000">' +
115 '<small>%s / %s (%d%%)</small>' +
116 '</div>' +
117 '</div>' +
118 '</div>', pc, v, m, pc
119 );
120 }
121
122 var iwxhr = new XHR();
123 var wifidevs = <%=luci.http.write_json(netdevs)%>;
124 var arptable = <%=luci.http.write_json(arpcache)%>;
125
126 var update_status = function() {
127 iwxhr.get('<%=REQUEST_URI%>', { status: 1 },
128 function(x, info)
129 {
130 var si = document.getElementById('wan4_i');
131 var ss = document.getElementById('wan4_s');
132 var ifc = info.wan;
133
134 if (ifc && ifc.ifname && ifc.proto != 'none')
135 {
136 var s = String.format(
137 '<strong><%:Type%>: </strong>%s<br />' +
138 '<strong><%:Address%>: </strong>%s<br />' +
139 '<strong><%:Netmask%>: </strong>%s<br />' +
140 '<strong><%:Gateway%>: </strong>%s<br />',
141 ifc.proto,
142 (ifc.ipaddr) ? ifc.ipaddr : '0.0.0.0',
143 (ifc.netmask != ifc.ipaddr) ? ifc.netmask : '255.255.255.255',
144 (ifc.gwaddr) ? ifc.gwaddr : '0.0.0.0'
145 );
146
147 for (var i = 0; i < ifc.dns.length; i++)
148 {
149 s += String.format(
150 '<strong><%:DNS%> %d: </strong>%s<br />',
151 i + 1, ifc.dns[i]
152 );
153 }
154
155 if (ifc.expires > -1)
156 {
157 s += String.format(
158 '<strong><%:Expires%>: </strong>%t<br />',
159 ifc.expires
160 );
161 }
162
163 if (ifc.uptime > 0)
164 {
165 s += String.format(
166 '<strong><%:Connected%>: </strong>%t<br />',
167 ifc.uptime
168 );
169 }
170
171 ss.innerHTML = String.format('<small>%s</small>', s);
172 si.innerHTML = String.format(
173 '<img src="<%=resource%>/icons/ethernet.png" />' +
174 '<br /><small><a href="%s">%s</a></small>',
175 ifc.link, ifc.ifname
176 );
177 }
178 else
179 {
180 si.innerHTML = '<img src="<%=resource%>/icons/ethernet_disabled.png" /><br /><small>?</small>';
181 ss.innerHTML = '<em>Not connected</em>';
182 }
183
184 <% if has_ipv6 then %>
185 var si6 = document.getElementById('wan6_i');
186 var ss6 = document.getElementById('wan6_s');
187 var ifc6 = info.wan6;
188
189 if (ifc6 && ifc6.ifname && ifc6.proto != 'none')
190 {
191 var s = String.format(
192 '<strong><%:Address%>: </strong>%s<br />' +
193 '<strong><%:Gateway%>: </strong>%s<br />',
194 (ifc6.ip6addr) ? ifc6.ip6addr : '::',
195 (ifc6.gw6addr) ? ifc6.gw6addr : '::'
196 );
197
198 for (var i = 0; i < ifc6.dns.length; i++)
199 {
200 s += String.format(
201 '<strong><%:DNS%> %d: </strong>%s<br />',
202 i + 1, ifc6.dns[i]
203 );
204 }
205
206 if (ifc6.uptime > 0)
207 {
208 s += String.format(
209 '<strong><%:Connected%>: </strong>%t<br />',
210 ifc6.uptime
211 );
212 }
213
214 ss6.innerHTML = String.format('<small>%s</small>', s);
215 si6.innerHTML = String.format(
216 '<img src="<%=resource%>/icons/ethernet.png" />' +
217 '<br /><small><a href="%s">%s</a></small>',
218 ifc6.link, ifc6.ifname
219 );
220 }
221 else
222 {
223 si6.innerHTML = '<img src="<%=resource%>/icons/ethernet_disabled.png" /><br /><small>?</small>';
224 ss6.innerHTML = '<em>Not connected</em>';
225 }
226 <% end %>
227
228 <% if has_dhcp then %>
229 var ls = document.getElementById('lease_status_table');
230 if (ls)
231 {
232 /* clear all rows */
233 while( ls.rows.length > 1 )
234 ls.rows[0].parentNode.deleteRow(1);
235
236 for( var i = 0; i < info.leases.length; i++ )
237 {
238 var timestr;
239
240 if (info.leases[i].expires <= 0)
241 timestr = '<em><%:expired%></em>';
242 else
243 timestr = String.format('%t', info.leases[i].expires);
244
245 var tr = ls.rows[0].parentNode.insertRow(-1);
246 tr.className = 'cbi-section-table-row cbi-rowstyle-' + ((i % 2) + 1);
247
248 tr.insertCell(-1).innerHTML = info.leases[i].hostname ? info.leases[i].hostname : '?';
249 tr.insertCell(-1).innerHTML = info.leases[i].ipaddr;
250 tr.insertCell(-1).innerHTML = info.leases[i].macaddr;
251 tr.insertCell(-1).innerHTML = timestr;
252 }
253
254 if( ls.rows.length == 1 )
255 {
256 var tr = ls.rows[0].parentNode.insertRow(-1);
257 tr.className = 'cbi-section-table-row';
258
259 var td = tr.insertCell(-1);
260 td.colSpan = 4;
261 td.innerHTML = '<em><br /><%:There are no active leases.%></em>';
262 }
263 }
264 <% end %>
265
266 <% if has_wifi then %>
267 var assoclist = [ ];
268
269 var ws = document.getElementById('wifi_status_table');
270 if (ws)
271 {
272 var wsbody = ws.rows[0].parentNode;
273 while (ws.rows.length > 0)
274 wsbody.deleteRow(0);
275
276 for (var didx = 0; didx < info.wifinets.length; didx++)
277 {
278 var dev = info.wifinets[didx];
279
280 var tr = wsbody.insertRow(-1);
281 var td;
282
283 td = tr.insertCell(-1);
284 td.width = "33%";
285 td.innerHTML = dev.name;
286 td.style.verticalAlign = "top";
287
288 td = tr.insertCell(-1);
289
290 var s = '';
291
292 for (var nidx = 0; nidx < dev.networks.length; nidx++)
293 {
294 var net = dev.networks[nidx];
295 var is_assoc = (net.bssid != '00:00:00:00:00:00' && net.channel);
296
297 var icon;
298 if (!is_assoc)
299 icon = "<%=resource%>/icons/signal-none.png";
300 else if (net.quality == 0)
301 icon = "<%=resource%>/icons/signal-0.png";
302 else if (net.quality < 25)
303 icon = "<%=resource%>/icons/signal-0-25.png";
304 else if (net.quality < 50)
305 icon = "<%=resource%>/icons/signal-25-50.png";
306 else if (net.quality < 75)
307 icon = "<%=resource%>/icons/signal-50-75.png";
308 else
309 icon = "<%=resource%>/icons/signal-75-100.png";
310
311 s += String.format(
312 '<table><tr><td style="text-align:center; width:32px; padding:3px">' +
313 '<img src="%s" title="<%:Signal%>: %d dBm / <%:Noise%>: %d dBm" />' +
314 '<br /><small>%d%%</small>' +
315 '</td><td style="text-align:left; padding:3px"><small>' +
316 '<strong><%:SSID%>:</strong> <a href="%s">%h</a><br />' +
317 '<strong><%:Mode%>:</strong> %s<br />' +
318 '<strong><%:Channel%>:</strong> %d (%.2f GHz)<br />' +
319 '<strong><%:Bitrate%>:</strong> %s Mb/s<br />',
320 icon, net.signal, net.noise,
321 net.quality,
322 net.link, net.ssid,
323 net.mode,
324 net.channel, net.frequency,
325 net.bitrate || '?'
326 );
327
328 if (is_assoc)
329 {
330 s += String.format(
331 '<strong><%:BSSID%>:</strong> %s<br />' +
332 '<strong><%:Encryption%>:</strong> %s',
333 net.bssid,
334 net.encryption
335 );
336 }
337 else
338 {
339 s += '<em><%:Wireless is disabled or not associated%></em>';
340 }
341
342 s += '</small></td></tr></table>';
343
344 for (var bssid in net.assoclist)
345 {
346 assoclist.push({
347 bssid: bssid,
348 signal: net.assoclist[bssid].signal,
349 noise: net.assoclist[bssid].noise,
350 link: net.link,
351 name: net.name
352 });
353 }
354 }
355
356 if (!s)
357 s = '<em><%:No information available%></em>';
358
359 td.innerHTML = s;
360 }
361 }
362
363 var ac = document.getElementById('wifi_assoc_table');
364 if (ac)
365 {
366 /* clear all rows */
367 while( ac.rows.length > 1 )
368 ac.rows[0].parentNode.deleteRow(1);
369
370 assoclist.sort(function(a, b) {
371 return (a.name == b.name)
372 ? (a.bssid < b.bssid)
373 : (a.name > b.name )
374 ;
375 });
376
377 for( var i = 0; i < assoclist.length; i++ )
378 {
379 var tr = ac.rows[0].parentNode.insertRow(-1);
380 tr.className = 'cbi-section-table-row cbi-rowstyle-' + (1 + (i % 2));
381
382 var icon;
383 var q = (-1 * (assoclist[i].noise - assoclist[i].signal)) / 5;
384 if (q < 1)
385 icon = "<%=resource%>/icons/signal-0.png";
386 else if (q < 2)
387 icon = "<%=resource%>/icons/signal-0-25.png";
388 else if (q < 3)
389 icon = "<%=resource%>/icons/signal-25-50.png";
390 else if (q < 4)
391 icon = "<%=resource%>/icons/signal-50-75.png";
392 else
393 icon = "<%=resource%>/icons/signal-75-100.png";
394
395 tr.insertCell(-1).innerHTML = String.format(
396 '<img src="%s" title="<%:Signal%>: %d dBm / <%:Noise%>: %d dBm" />',
397 icon, assoclist[i].signal, assoclist[i].noise
398 );
399
400 tr.insertCell(-1).innerHTML = assoclist[i].bssid;
401
402 tr.insertCell(-1).innerHTML = String.format(
403 '<a href="%s">%h</a>',
404 assoclist[i].link,
405 assoclist[i].name
406 );
407
408 tr.insertCell(-1).innerHTML = String.format('%d dBm', assoclist[i].signal);
409 tr.insertCell(-1).innerHTML = String.format('%d dBm', assoclist[i].noise);
410 }
411
412 if (ac.rows.length == 0)
413 {
414 var tr = ac.rows[0].parentNode.insertRow(-1);
415 tr.className = 'cbi-section-table-row';
416
417 var td = tr.insertCell(-1);
418 td.colSpan = 5;
419 td.innerHTML = '<br /><em><%:No information available%></em>';
420 }
421 }
422 <% end %>
423
424 var e;
425
426 if (e = document.getElementById('localtime'))
427 e.innerHTML = info.localtime;
428
429 if (e = document.getElementById('uptime'))
430 e.innerHTML = String.format('%t', info.uptime);
431
432 if (e = document.getElementById('loadavg'))
433 e.innerHTML = String.format('%.02f, %.02f, %.02f',
434 info.loadavg[0], info.loadavg[1], info.loadavg[2]);
435
436 if (e = document.getElementById('memtotal'))
437 e.innerHTML = progressbar(
438 (info.memfree + info.membuffers + info.memcached) + " kB",
439 info.memtotal + " kB"
440 );
441
442 if (e = document.getElementById('memfree'))
443 e.innerHTML = progressbar(
444 info.memfree + " kB", info.memtotal + " kB"
445 );
446
447 if (e = document.getElementById('memcache'))
448 e.innerHTML = progressbar(
449 info.memcached + " kB", info.memtotal + " kB"
450 );
451
452 if (e = document.getElementById('membuff'))
453 e.innerHTML = progressbar(
454 info.membuffers + " kB", info.memtotal + " kB"
455 );
456
457 if (e = document.getElementById('conns'))
458 e.innerHTML = progressbar(info.conncount, info.connmax);
459
460 window.setTimeout(update_status, 5000);
461 }
462 )
463 };
464
465 update_status();
466 //]]></script>
467
468 <h2><a id="content" name="content"><%:Status%></a></h2>
469
470 <fieldset class="cbi-section">
471 <legend><%:System%></legend>
472
473 <table width="100%" cellspacing="10">
474 <tr><td width="33%"><%:Router Name%></td><td><%=luci.sys.hostname() or "?"%></td></tr>
475 <tr><td width="33%"><%:Router Model%></td><td><%=pcdata(model or "?")%></td></tr>
476 <tr><td width="33%"><%:Firmware Version%></td><td>
477 <%=pcdata(luci.version.distname)%> <%=pcdata(luci.version.distversion)%> /
478 <%=pcdata(luci.version.luciname)%> <%=pcdata(luci.version.luciversion)%>
479 </td></tr>
480 <tr><td width="33%"><%:Kernel Version%></td><td><%=luci.sys.exec("uname -r")%></td></tr>
481 <tr><td width="33%"><%:Local Time%></td><td id="localtime">-</td></tr>
482 <tr><td width="33%"><%:Uptime%></td><td id="uptime">-</td></tr>
483 <tr><td width="33%"><%:Load Average%></td><td id="loadavg">-</td></tr>
484 </table>
485 </fieldset>
486
487 <fieldset class="cbi-section">
488 <legend><%:Memory%></legend>
489
490 <table width="100%" cellspacing="10">
491 <tr><td width="33%"><%:Total Available%></td><td id="memtotal">-</td></tr>
492 <tr><td width="33%"><%:Free%></td><td id="memfree">-</td></tr>
493 <tr><td width="33%"><%:Cached%></td><td id="memcache">-</td></tr>
494 <tr><td width="33%"><%:Buffered%></td><td id="membuff">-</td></tr>
495 </table>
496 </fieldset>
497
498 <fieldset class="cbi-section">
499 <legend><%:Network%></legend>
500
501 <table width="100%" cellspacing="10">
502 <tr><td width="33%" style="vertical-align:top"><%:IPv4 WAN Status%></td><td>
503 <table><tr>
504 <td id="wan4_i" style="width:16px; text-align:center; padding:3px"><img src="<%=resource%>/icons/ethernet_disabled.png" /><br /><small>?</small></td>
505 <td id="wan4_s" style="vertical-align:middle; padding: 3px"><em><%:Collecting data...%></em></td>
506 </tr></table>
507 </td></tr>
508 <% if has_ipv6 then %>
509 <tr><td width="33%" style="vertical-align:top"><%:IPv6 WAN Status%></td><td>
510 <table><tr>
511 <td id="wan6_i" style="width:16px; text-align:center; padding:3px"><img src="<%=resource%>/icons/ethernet_disabled.png" /><br /><small>?</small></td>
512 <td id="wan6_s" style="vertical-align:middle; padding: 3px"><em><%:Collecting data...%></em></td>
513 </tr></table>
514 </td></tr>
515 <% end %>
516 <tr><td width="33%"><%:Active IP Connections%></td><td id="conns">-</td></tr>
517 </table>
518 </fieldset>
519
520 <% if has_dhcp then %>
521 <fieldset class="cbi-section">
522 <legend><%:DHCP Leases%></legend>
523
524 <table class="cbi-section-table" id="lease_status_table">
525 <tr class="cbi-section-table-titles">
526 <th class="cbi-section-table-cell"><%:Hostname%></th>
527 <th class="cbi-section-table-cell"><%:IPv4-Address%></th>
528 <th class="cbi-section-table-cell"><%:MAC-Address%></th>
529 <th class="cbi-section-table-cell"><%:Leasetime remaining%></th>
530 </tr>
531 <tr class="cbi-section-table-row">
532 <td colspan="4"><em><br /><%:Collecting data...%></em></td>
533 </tr>
534 </table>
535 </fieldset>
536 <% end %>
537
538 <% if has_wifi then %>
539 <fieldset class="cbi-section">
540 <legend><%:Wireless%></legend>
541
542 <table id="wifi_status_table" width="100%" cellspacing="10">
543 <tr><td><em><%:Collecting data...%></em></td></tr>
544 </table>
545 </fieldset>
546
547 <fieldset class="cbi-section">
548 <legend><%:Associated Stations%></legend>
549
550 <table class="cbi-section-table" id="wifi_assoc_table">
551 <tr class="cbi-section-table-titles">
552 <th class="cbi-section-table-cell">&#160;</th>
553 <th class="cbi-section-table-cell"><%:BSSID%></th>
554 <th class="cbi-section-table-cell"><%:Network%></th>
555 <th class="cbi-section-table-cell"><%:Signal%></th>
556 <th class="cbi-section-table-cell"><%:Noise%></th>
557 </tr>
558 <tr class="cbi-section-table-row">
559 <td colspan="5"><em><br /><%:Collecting data...%></em></td>
560 </tr>
561 </table>
562 </fieldset>
563 <% end %>
564
565 <%+footer%>