2 Copyright 2008 Steven Barth <steven@midlink.org>
3 Copyright 2008-2011 Jo-Philipp Wich <jow@openwrt.org>
4 Licensed to the public under the Apache License 2.0.
8 local fs = require "nixio.fs"
9 local util = require "luci.util"
10 local stat = require "luci.tools.status"
11 local ver = require "luci.version"
13 local has_ipv6 = fs.access("/proc/net/ipv6_route")
14 local has_dhcp = fs.access("/etc/config/dhcp")
15 local has_wifi = ((fs.stat("/etc/config/wireless", "size") or 0) > 0)
17 local sysinfo = luci.util.ubus("system", "info") or { }
18 local boardinfo = luci.util.ubus("system", "board") or { }
19 local unameinfo = nixio.uname() or { }
21 local meminfo = sysinfo.memory or {
28 local swapinfo = sysinfo.swap or {
33 local has_dsl = fs.access("/etc/init.d/dsl_control")
35 if luci.http.formvalue("status") == "1" then
36 local ntm = require "luci.model.network".init()
37 local wan = ntm:get_wannet()
38 local wan6 = ntm:get_wan6net()
40 local conn_count = tonumber((
41 luci.sys.exec("wc -l /proc/net/nf_conntrack") or
42 luci.sys.exec("wc -l /proc/net/ip_conntrack") or
43 ""):match("%d+")) or 0
45 local conn_max = tonumber((
46 luci.sys.exec("sysctl net.nf_conntrack_max") or
47 luci.sys.exec("sysctl net.ipv4.netfilter.ip_conntrack_max") or
48 ""):match("%d+")) or 4096
51 uptime = sysinfo.uptime or 0,
52 localtime = os.date(),
53 loadavg = sysinfo.load or { 0, 0, 0 },
57 conncount = conn_count,
58 leases = stat.dhcp_leases(),
59 leases6 = stat.dhcp6_leases(),
60 wifinets = stat.wifi_networks()
65 ipaddr = wan:ipaddr(),
66 gwaddr = wan:gwaddr(),
67 netmask = wan:netmask(),
69 expires = wan:expires(),
70 uptime = wan:uptime(),
72 ifname = wan:ifname(),
73 link = wan:adminlink()
79 ip6addr = wan6:ip6addr(),
80 gw6addr = wan6:gw6addr(),
81 dns = wan6:dns6addrs(),
82 uptime = wan6:uptime(),
83 ifname = wan6:ifname(),
84 link = wan6:adminlink()
89 local dsl_stat = luci.sys.exec("/etc/init.d/dsl_control lucistat")
90 local dsl_func = loadstring(dsl_stat)
96 luci.http.prepare_content("application/json")
97 luci.http.write_json(rv)
100 elseif luci.http.formvalue("hosts") == "1" then
101 luci.http.prepare_content("application/json")
102 luci.http.write_json(luci.sys.net.host_hints())
110 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
111 <script type="text/javascript">//<![CDATA[
112 function progressbar(v, m)
114 var vn = parseInt(v) || 0;
115 var mn = parseInt(m) || 100;
116 var pc = Math.floor((100 / mn) * vn);
118 return String.format(
119 '<div style="width:200px; position:relative; border:1px solid #999999">' +
120 '<div style="background-color:#CCCCCC; width:%d%%; height:15px">' +
121 '<div style="position:absolute; left:0; top:0; text-align:center; width:100%%; color:#000000">' +
122 '<small>%s / %s (%d%%)</small>' +
125 '</div>', pc, v, m, pc
129 function wifirate(bss, rx) {
130 var p = rx ? 'rx_' : 'tx_',
131 s = '%.1f <%:Mbit/s%>, %d<%:MHz%>'
132 .format(bss[p+'rate'] / 1000, bss[p+'mhz']),
133 ht = bss[p+'ht'], vht = bss[p+'vht'],
134 mhz = bss[p+'mhz'], nss = bss[p+'nss'],
135 mcs = bss[p+'mcs'], sgi = bss[p+'short_gi'];
138 if (vht) s += ', VHT-MCS %d'.format(mcs);
139 if (nss) s += ', VHT-NSS %d'.format(nss);
140 if (ht) s += ', MCS %s'.format(mcs);
141 if (sgi) s += ', <%:Short GI%>';
147 function duid2mac(duid) {
148 // DUID-LLT / Ethernet
149 if (duid.length === 28 && duid.substr(0, 8) === '00010001')
150 return duid.substr(16).replace(/(..)(?=..)/g, '$1:').toUpperCase();
152 // DUID-LL / Ethernet
153 if (duid.length === 24 && duid.substr(0, 8) === '00030001')
154 return duid.substr(8).replace(/(..)(?=..)/g, '$1:').toUpperCase();
160 var hosts = <%=luci.http.write_json(luci.sys.net.host_hints())%>;
162 function updateHosts() {
163 XHR.get('<%=REQUEST_URI%>', { hosts: 1 }, function(x, data) {
168 XHR.poll(5, '<%=REQUEST_URI%>', { status: 1 },
174 var si = document.getElementById('wan4_i');
175 var ss = document.getElementById('wan4_s');
178 if (ifc && ifc.ifname && ifc.proto != 'none')
180 var s = String.format(
181 '<strong><%:Type%>: </strong>%s<br />' +
182 '<strong><%:Address%>: </strong>%s<br />' +
183 '<strong><%:Netmask%>: </strong>%s<br />' +
184 '<strong><%:Gateway%>: </strong>%s<br />',
186 (ifc.ipaddr) ? ifc.ipaddr : '0.0.0.0',
187 (ifc.netmask && ifc.netmask != ifc.ipaddr) ? ifc.netmask : '255.255.255.255',
188 (ifc.gwaddr) ? ifc.gwaddr : '0.0.0.0'
191 for (var i = 0; i < ifc.dns.length; i++)
194 '<strong><%:DNS%> %d: </strong>%s<br />',
199 if (ifc.expires > -1)
202 '<strong><%:Expires%>: </strong>%t<br />',
210 '<strong><%:Connected%>: </strong>%t<br />',
215 ss.innerHTML = String.format('<small>%s</small>', s);
216 si.innerHTML = String.format(
217 '<img src="<%=resource%>/icons/ethernet.png" />' +
218 '<br /><small><a href="%s">%s</a></small>',
224 si.innerHTML = '<img src="<%=resource%>/icons/ethernet_disabled.png" /><br /><small>?</small>';
225 ss.innerHTML = '<em><%:Not connected%></em>';
228 <% if has_ipv6 then %>
229 var si6 = document.getElementById('wan6_i');
230 var ss6 = document.getElementById('wan6_s');
231 var ifc6 = info.wan6;
233 if (ifc6 && ifc6.ifname && ifc6.proto != 'none')
235 var s = String.format(
236 '<strong><%:Address%>: </strong>%s<br />' +
237 '<strong><%:Gateway%>: </strong>%s<br />',
238 (ifc6.ip6addr) ? ifc6.ip6addr : '::',
239 (ifc6.gw6addr) ? ifc6.gw6addr : '::'
242 for (var i = 0; i < ifc6.dns.length; i++)
245 '<strong><%:DNS%> %d: </strong>%s<br />',
253 '<strong><%:Connected%>: </strong>%t<br />',
258 ss6.innerHTML = String.format('<small>%s</small>', s);
259 si6.innerHTML = String.format(
260 '<img src="<%=resource%>/icons/ethernet.png" />' +
261 '<br /><small><a href="%s">%s</a></small>',
262 ifc6.link, ifc6.ifname
267 si6.innerHTML = '<img src="<%=resource%>/icons/ethernet_disabled.png" /><br /><small>?</small>';
268 ss6.innerHTML = '<em><%:Not connected%></em>';
272 <% if has_dsl then %>
273 var dsl_i = document.getElementById('dsl_i');
274 var dsl_s = document.getElementById('dsl_s');
276 var s = String.format(
277 '<strong><%:Status%>: </strong>%s<br />' +
278 '<strong><%:Line State%>: </strong>%s [0x%x]<br />' +
279 '<strong><%:Line Speed%>: </strong>%s/s / %s/s<br />' +
280 '<strong><%:Line Attenuation%>: </strong>%s dB / %s dB<br />' +
281 '<strong><%:Noise Margin%>: </strong>%s dB / %s dB<br />',
282 info.dsl.line_state, info.dsl.line_state_detail,
283 info.dsl.line_state_num,
284 info.dsl.data_rate_down_s, info.dsl.data_rate_up_s,
285 info.dsl.line_attenuation_down, info.dsl.line_attenuation_up,
286 info.dsl.noise_margin_down, info.dsl.noise_margin_up
289 dsl_s.innerHTML = String.format('<small>%s</small>', s);
290 dsl_i.innerHTML = String.format(
291 '<img src="<%=resource%>/icons/ethernet.png" />' +
292 '<br /><small>ADSL</small>'
296 <% if has_dhcp then %>
297 var ls = document.getElementById('lease_status_table');
301 while( ls.rows.length > 1 )
302 ls.rows[0].parentNode.deleteRow(1);
304 for( var i = 0; i < info.leases.length; i++ )
308 if (info.leases[i].expires <= 0)
309 timestr = '<em><%:expired%></em>';
311 timestr = String.format('%t', info.leases[i].expires);
313 var tr = ls.rows[0].parentNode.insertRow(-1);
314 tr.className = 'cbi-section-table-row cbi-rowstyle-' + ((i % 2) + 1);
316 tr.insertCell(-1).innerHTML = info.leases[i].hostname ? info.leases[i].hostname : '?';
317 tr.insertCell(-1).innerHTML = info.leases[i].ipaddr;
318 tr.insertCell(-1).innerHTML = info.leases[i].macaddr;
319 tr.insertCell(-1).innerHTML = timestr;
322 if( ls.rows.length == 1 )
324 var tr = ls.rows[0].parentNode.insertRow(-1);
325 tr.className = 'cbi-section-table-row';
327 var td = tr.insertCell(-1);
329 td.innerHTML = '<em><br /><%:There are no active leases.%></em>';
333 var ls6 = document.getElementById('lease6_status_table');
334 if (ls6 && info.leases6)
336 ls6.parentNode.style.display = 'block';
339 while( ls6.rows.length > 1 )
340 ls6.rows[0].parentNode.deleteRow(1);
342 for( var i = 0; i < info.leases6.length; i++ )
346 if (info.leases6[i].expires <= 0)
347 timestr = '<em><%:expired%></em>';
349 timestr = String.format('%t', info.leases6[i].expires);
351 var tr = ls6.rows[0].parentNode.insertRow(-1);
352 tr.className = 'cbi-section-table-row cbi-rowstyle-' + ((i % 2) + 1);
354 var host = hosts[duid2mac(info.leases6[i].duid)];
356 tr.insertCell(-1).innerHTML = String.format(
357 '<div style="max-width:200px;overflow:hidden;text-overflow:ellipsis">%s</div>',
358 ((host.name && (host.ipv4 || host.ipv6))
359 ? '%h (%s)'.format(host.name, host.ipv4 || host.ipv6)
360 : '%h'.format(host.name || host.ipv4 || host.ipv6)).nobr()
363 tr.insertCell(-1).innerHTML = info.leases6[i].hostname ? info.leases6[i].hostname : '?';
365 tr.insertCell(-1).innerHTML = info.leases6[i].ip6addr;
366 tr.insertCell(-1).innerHTML = info.leases6[i].duid;
367 tr.insertCell(-1).innerHTML = timestr;
370 if( ls6.rows.length == 1 )
372 var tr = ls6.rows[0].parentNode.insertRow(-1);
373 tr.className = 'cbi-section-table-row';
375 var td = tr.insertCell(-1);
377 td.innerHTML = '<em><br /><%:There are no active leases.%></em>';
382 <% if has_wifi then %>
385 var ws = document.getElementById('wifi_status_table');
388 var wsbody = ws.rows[0].parentNode;
389 while (ws.rows.length > 0)
392 for (var didx = 0; didx < info.wifinets.length; didx++)
394 var dev = info.wifinets[didx];
396 var tr = wsbody.insertRow(-1);
399 td = tr.insertCell(-1);
401 td.innerHTML = dev.name;
402 td.style.verticalAlign = "top";
404 td = tr.insertCell(-1);
408 for (var nidx = 0; nidx < dev.networks.length; nidx++)
410 var net = dev.networks[nidx];
411 var is_assoc = (net.bssid != '00:00:00:00:00:00' && net.channel && !net.disabled);
415 icon = "<%=resource%>/icons/signal-none.png";
416 else if (net.quality == 0)
417 icon = "<%=resource%>/icons/signal-0.png";
418 else if (net.quality < 25)
419 icon = "<%=resource%>/icons/signal-0-25.png";
420 else if (net.quality < 50)
421 icon = "<%=resource%>/icons/signal-25-50.png";
422 else if (net.quality < 75)
423 icon = "<%=resource%>/icons/signal-50-75.png";
425 icon = "<%=resource%>/icons/signal-75-100.png";
428 '<table><tr><td style="text-align:center; width:32px; padding:3px">' +
429 '<img src="%s" title="<%:Signal%>: %d dBm / <%:Noise%>: %d dBm" />' +
430 '<br /><small>%d%%</small>' +
431 '</td><td style="text-align:left; padding:3px"><small>' +
432 '<strong><%:SSID%>:</strong> <a href="%s">%h</a><br />' +
433 '<strong><%:Mode%>:</strong> %s<br />' +
434 '<strong><%:Channel%>:</strong> %d (%.3f <%:GHz%>)<br />' +
435 '<strong><%:Bitrate%>:</strong> %s <%:Mbit/s%><br />',
436 icon, net.signal, net.noise,
440 net.channel, net.frequency,
447 '<strong><%:BSSID%>:</strong> %s<br />' +
448 '<strong><%:Encryption%>:</strong> %s',
455 s += '<em><%:Wireless is disabled or not associated%></em>';
458 s += '</small></td></tr></table>';
460 for (var bssid in net.assoclist)
462 var bss = net.assoclist[bssid];
467 bss.ifname = net.ifname;
468 bss.radio = dev.name;
475 s = '<em><%:No information available%></em>';
481 var ac = document.getElementById('wifi_assoc_table');
485 while( ac.rows.length > 1 )
486 ac.rows[0].parentNode.deleteRow(1);
488 assoclist.sort(function(a, b) {
489 return (a.name == b.name)
490 ? (a.bssid < b.bssid)
495 for( var i = 0; i < assoclist.length; i++ )
497 var tr = ac.rows[0].parentNode.insertRow(-1);
498 tr.className = 'cbi-section-table-row cbi-rowstyle-' + (1 + (i % 2));
501 var q = (-1 * (assoclist[i].noise - assoclist[i].signal)) / 5;
503 icon = "<%=resource%>/icons/signal-0.png";
505 icon = "<%=resource%>/icons/signal-0-25.png";
507 icon = "<%=resource%>/icons/signal-25-50.png";
509 icon = "<%=resource%>/icons/signal-50-75.png";
511 icon = "<%=resource%>/icons/signal-75-100.png";
513 tr.insertCell(-1).innerHTML = String.format(
514 '<span class="ifacebadge" title="%q"><img src="<%=resource%>/icons/wifi.png" /> %h</span>',
515 assoclist[i].radio, assoclist[i].ifname
518 tr.insertCell(-1).innerHTML = String.format(
519 '<a href="%s">%s</a>',
521 '%h'.format(assoclist[i].name).nobr()
524 tr.insertCell(-1).innerHTML = assoclist[i].bssid;
526 var host = hosts[assoclist[i].bssid];
528 tr.insertCell(-1).innerHTML = String.format(
529 '<div style="max-width:200px;overflow:hidden;text-overflow:ellipsis">%s</div>',
530 ((host.name && (host.ipv4 || host.ipv6))
531 ? '%h (%s)'.format(host.name, host.ipv4 || host.ipv6)
532 : '%h'.format(host.name || host.ipv4 || host.ipv6)).nobr()
535 tr.insertCell(-1).innerHTML = '?';
537 tr.insertCell(-1).innerHTML = String.format(
538 '<span class="ifacebadge" title="<%:Signal%>: %d <%:dBm%> / <%:Noise%>: %d <%:dBm%> / <%:SNR%>: %d"><img src="%s" /> %d / %d <%:dBm%></span>',
539 assoclist[i].signal, assoclist[i].noise, assoclist[i].signal - assoclist[i].noise,
541 assoclist[i].signal, assoclist[i].noise
544 tr.insertCell(-1).innerHTML = wifirate(assoclist[i], true).nobr() + '<br />' + wifirate(assoclist[i], false).nobr();
547 if (ac.rows.length == 1)
549 var tr = ac.rows[0].parentNode.insertRow(-1);
550 tr.className = 'cbi-section-table-row';
552 var td = tr.insertCell(-1);
554 td.innerHTML = '<br /><em><%:No information available%></em>';
561 if (e = document.getElementById('localtime'))
562 e.innerHTML = info.localtime;
564 if (e = document.getElementById('uptime'))
565 e.innerHTML = String.format('%t', info.uptime);
567 if (e = document.getElementById('loadavg'))
568 e.innerHTML = String.format(
569 '%.02f, %.02f, %.02f',
570 info.loadavg[0] / 65535.0,
571 info.loadavg[1] / 65535.0,
572 info.loadavg[2] / 65535.0
575 if (e = document.getElementById('memtotal'))
576 e.innerHTML = progressbar(
577 ((info.memory.free + info.memory.buffered) / 1024) + " <%:kB%>",
578 (info.memory.total / 1024) + " <%:kB%>"
581 if (e = document.getElementById('memfree'))
582 e.innerHTML = progressbar(
583 (info.memory.free / 1024) + " <%:kB%>",
584 (info.memory.total / 1024) + " <%:kB%>"
587 if (e = document.getElementById('membuff'))
588 e.innerHTML = progressbar(
589 (info.memory.buffered / 1024) + " <%:kB%>",
590 (info.memory.total / 1024) + " <%:kB%>"
593 if (e = document.getElementById('swaptotal'))
594 e.innerHTML = progressbar(
595 (info.swap.free / 1024) + " <%:kB%>",
596 (info.swap.total / 1024) + " <%:kB%>"
599 if (e = document.getElementById('swapfree'))
600 e.innerHTML = progressbar(
601 (info.swap.free / 1024) + " <%:kB%>",
602 (info.swap.total / 1024) + " <%:kB%>"
605 if (e = document.getElementById('conns'))
606 e.innerHTML = progressbar(info.conncount, info.connmax);
612 <h2 name="content"><%:Status%></h2>
614 <fieldset class="cbi-section">
615 <legend><%:System%></legend>
617 <table width="100%" cellspacing="10">
618 <tr><td width="33%"><%:Hostname%></td><td><%=luci.sys.hostname() or "?"%></td></tr>
619 <tr><td width="33%"><%:Model%></td><td><%=pcdata(boardinfo.model or boardinfo.system or "?")%></td></tr>
620 <tr><td width="33%"><%:Firmware Version%></td><td>
621 <%=pcdata(ver.distname)%> <%=pcdata(ver.distversion)%> /
622 <%=pcdata(ver.luciname)%> (<%=pcdata(ver.luciversion)%>)
624 <tr><td width="33%"><%:Kernel Version%></td><td><%=unameinfo.release or "?"%></td></tr>
625 <tr><td width="33%"><%:Local Time%></td><td id="localtime">-</td></tr>
626 <tr><td width="33%"><%:Uptime%></td><td id="uptime">-</td></tr>
627 <tr><td width="33%"><%:Load Average%></td><td id="loadavg">-</td></tr>
631 <fieldset class="cbi-section">
632 <legend><%:Memory%></legend>
634 <table width="100%" cellspacing="10">
635 <tr><td width="33%"><%:Total Available%></td><td id="memtotal">-</td></tr>
636 <tr><td width="33%"><%:Free%></td><td id="memfree">-</td></tr>
637 <tr><td width="33%"><%:Buffered%></td><td id="membuff">-</td></tr>
641 <% if swapinfo.total > 0 then %>
642 <fieldset class="cbi-section">
643 <legend><%:Swap%></legend>
645 <table width="100%" cellspacing="10">
646 <tr><td width="33%"><%:Total Available%></td><td id="swaptotal">-</td></tr>
647 <tr><td width="33%"><%:Free%></td><td id="swapfree">-</td></tr>
652 <fieldset class="cbi-section">
653 <legend><%:Network%></legend>
655 <table width="100%" cellspacing="10">
656 <tr><td width="33%" style="vertical-align:top"><%:IPv4 WAN Status%></td><td>
658 <td id="wan4_i" style="width:16px; text-align:center; padding:3px"><img src="<%=resource%>/icons/ethernet_disabled.png" /><br /><small>?</small></td>
659 <td id="wan4_s" style="vertical-align:middle; padding: 3px"><em><%:Collecting data...%></em></td>
662 <% if has_ipv6 then %>
663 <tr><td width="33%" style="vertical-align:top"><%:IPv6 WAN Status%></td><td>
665 <td id="wan6_i" style="width:16px; text-align:center; padding:3px"><img src="<%=resource%>/icons/ethernet_disabled.png" /><br /><small>?</small></td>
666 <td id="wan6_s" style="vertical-align:middle; padding: 3px"><em><%:Collecting data...%></em></td>
670 <tr><td width="33%"><%:Active Connections%></td><td id="conns">-</td></tr>
674 <% if has_dhcp then %>
675 <fieldset class="cbi-section">
676 <legend><%:DHCP Leases%></legend>
678 <table class="cbi-section-table" id="lease_status_table">
679 <tr class="cbi-section-table-titles">
680 <th class="cbi-section-table-cell"><%:Hostname%></th>
681 <th class="cbi-section-table-cell"><%:IPv4-Address%></th>
682 <th class="cbi-section-table-cell"><%:MAC-Address%></th>
683 <th class="cbi-section-table-cell"><%:Leasetime remaining%></th>
685 <tr class="cbi-section-table-row">
686 <td colspan="4"><em><br /><%:Collecting data...%></em></td>
691 <fieldset class="cbi-section" style="display:none">
692 <legend><%:DHCPv6 Leases%></legend>
694 <table class="cbi-section-table" id="lease6_status_table">
695 <tr class="cbi-section-table-titles">
696 <th class="cbi-section-table-cell"><%:Host%></th>
697 <th class="cbi-section-table-cell"><%:IPv6-Address%></th>
698 <th class="cbi-section-table-cell"><%:DUID%></th>
699 <th class="cbi-section-table-cell"><%:Leasetime remaining%></th>
701 <tr class="cbi-section-table-row">
702 <td colspan="4"><em><br /><%:Collecting data...%></em></td>
708 <% if has_dsl then %>
709 <fieldset class="cbi-section">
710 <legend><%:ADSL%></legend>
711 <table width="100%" cellspacing="10">
712 <tr><td width="33%" style="vertical-align:top"><%:ADSL Status%></td><td>
714 <td id="dsl_i" style="width:16px; text-align:center; padding:3px"><img src="<%=resource%>/icons/ethernet_disabled.png" /><br /><small>?</small></td>
715 <td id="dsl_s" style="vertical-align:middle; padding: 3px"><em><%:Collecting data...%></em></td>
722 <% if has_wifi then %>
723 <fieldset class="cbi-section">
724 <legend><%:Wireless%></legend>
726 <table id="wifi_status_table" width="100%" cellspacing="10">
727 <tr><td><em><%:Collecting data...%></em></td></tr>
731 <fieldset class="cbi-section">
732 <legend><%:Associated Stations%></legend>
734 <table class="cbi-section-table valign-middle" id="wifi_assoc_table">
735 <tr class="cbi-section-table-titles">
736 <th class="cbi-section-table-cell"> </th>
737 <th class="cbi-section-table-cell"><%:Network%></th>
738 <th class="cbi-section-table-cell"><%:MAC-Address%></th>
739 <th class="cbi-section-table-cell"><%:Host%></th>
740 <th class="cbi-section-table-cell"><%:Signal%> / <%:Noise%></th>
741 <th class="cbi-section-table-cell"><%:RX Rate%> / <%:TX Rate%></th>
743 <tr class="cbi-section-table-row">
744 <td colspan="6"><em><br /><%:Collecting data...%></em></td>
751 local incdir = util.libpath() .. "/view/admin_status/index/"
752 if fs.access(incdir) then
754 for inc in fs.dir(incdir) do
755 if inc:match("%.htm$") then
756 include("admin_status/index/" .. inc:gsub("%.htm$", ""))