display.htm 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. <%#
  2. Copyright 2017 Jo-Philipp Wich <jo@mein.io>
  3. Licensed to the public under the Apache License 2.0.
  4. -%>
  5. <% css = [[
  6. #chartjs-tooltip {
  7. opacity: 0;
  8. position: absolute;
  9. background: rgba(0, 0, 0, .7);
  10. color: white;
  11. padding: 3px;
  12. border-radius: 3px;
  13. -webkit-transition: all .1s ease;
  14. transition: all .1s ease;
  15. pointer-events: none;
  16. -webkit-transform: translate(-50%, 0);
  17. transform: translate(-50%, 0);
  18. z-index: 200;
  19. }
  20. #chartjs-tooltip.above {
  21. -webkit-transform: translate(-50%, -100%);
  22. transform: translate(-50%, -100%);
  23. }
  24. #chartjs-tooltip.above:before {
  25. border: solid;
  26. border-color: #111 transparent;
  27. border-color: rgba(0, 0, 0, .8) transparent;
  28. border-width: 8px 8px 0 8px;
  29. bottom: 1em;
  30. content: "";
  31. display: block;
  32. left: 50%;
  33. top: 100%;
  34. position: absolute;
  35. z-index: 99;
  36. -webkit-transform: translate(-50%, 0);
  37. transform: translate(-50%, 0);
  38. }
  39. table {
  40. border: 1px solid #999;
  41. border-collapse: collapse;
  42. margin: 0 0 2px !important;
  43. }
  44. th, td, table table td {
  45. border: 1px solid #999;
  46. text-align: right;
  47. padding: 1px 3px !important;
  48. white-space: nowrap;
  49. }
  50. tbody td {
  51. border-bottom-color: #ccc;
  52. }
  53. tbody td[rowspan] {
  54. border-bottom-color: #999;
  55. }
  56. tbody tr:last-child td {
  57. border-bottom-color: #999;
  58. }
  59. .pie {
  60. width: 200px;
  61. display: inline-block;
  62. margin: 20px;
  63. }
  64. .pie label {
  65. font-weight: bold;
  66. font-size: 14px;
  67. display: block;
  68. margin-bottom: 10px;
  69. text-align: center;
  70. }
  71. .kpi {
  72. display: inline-block;
  73. margin: 80px 20px 20px;
  74. vertical-align: top;
  75. }
  76. .kpi ul {
  77. list-style: none;
  78. }
  79. .kpi li {
  80. margin: 10px;
  81. display: none;
  82. }
  83. .kpi big {
  84. font-weight: bold;
  85. }
  86. #detail-bubble {
  87. position: absolute;
  88. opacity: 0;
  89. visibility: hidden;
  90. }
  91. #detail-bubble.in {
  92. opacity: 1;
  93. visibility: visible;
  94. transition: opacity 0.5s;
  95. }
  96. #detail-bubble > div {
  97. border: 1px solid #ccc;
  98. border-radius: 2px;
  99. padding: 5px;
  100. background: #fcfcfc;
  101. }
  102. #detail-bubble .head {
  103. text-align: center;
  104. white-space: nowrap;
  105. position: relative;
  106. }
  107. #detail-bubble .head .dismiss {
  108. top: 0;
  109. right: 0;
  110. width: 20px;
  111. line-height: 20px;
  112. text-align: center;
  113. text-decoration: none;
  114. font-weight: bold;
  115. color: #000;
  116. position: absolute;
  117. font-size: 20px;
  118. }
  119. #detail-bubble .pie {
  120. width: 100px;
  121. margin: 5px;
  122. }
  123. #detail-bubble .kpi {
  124. margin: 40px 5px 5px;
  125. font-size: smaller;
  126. text-align: left;
  127. }
  128. #detail-bubble .kpi ul {
  129. margin: 0;
  130. }
  131. #bubble-arrow {
  132. border: 1px solid #ccc;
  133. border-width: 1px 0 0 1px;
  134. background: #fcfcfc;
  135. width: 15px;
  136. height: 15px;
  137. position: absolute;
  138. left: 0;
  139. top: -8px;
  140. transform: rotate(45deg);
  141. margin: 0 0 0 -8px;
  142. }
  143. tr.active > td {
  144. border-bottom: 2px solid red;
  145. }
  146. tr.active > td.active {
  147. border: 2px solid red;
  148. border-bottom: none;
  149. }
  150. td.detail {
  151. border: 2px solid red;
  152. border-top: none;
  153. opacity: 0;
  154. transition: opacity 0.5s;
  155. }
  156. td.detail.in {
  157. opacity: 1;
  158. }
  159. th.hostname,
  160. td.hostname {
  161. text-align: left;
  162. }
  163. ]] -%>
  164. <%+header%>
  165. <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
  166. <script type="text/javascript" src="<%=resource%>/nlbw.chart.min.js"></script>
  167. <script type="text/javascript">//<![CDATA[
  168. var chartRegistry = {},
  169. trafficPeriods = [],
  170. trafficData = { columns: [], data: [] },
  171. hostNames = {},
  172. hostInfo = <%=luci.util.serialize_json(luci.sys.net.host_hints())%>,
  173. ouiData = [];
  174. function off(elem)
  175. {
  176. var val = [0, 0];
  177. do {
  178. if (!isNaN(elem.offsetLeft) && !isNaN(elem.offsetTop)) {
  179. val[0] += elem.offsetLeft;
  180. val[1] += elem.offsetTop;
  181. }
  182. }
  183. while ((elem = elem.offsetParent) != null);
  184. return val;
  185. }
  186. Chart.defaults.global.customTooltips = function(tooltip) {
  187. var tooltipEl = document.getElementById('chartjs-tooltip');
  188. if (!tooltipEl) {
  189. tooltipEl = document.createElement('div');
  190. tooltipEl.setAttribute('id', 'chartjs-tooltip');
  191. document.body.appendChild(tooltipEl);
  192. }
  193. if (!tooltip) {
  194. if (tooltipEl.row)
  195. tooltipEl.row.style.backgroundColor = '';
  196. tooltipEl.style.opacity = 0;
  197. return;
  198. }
  199. var pos = off(tooltip.chart.canvas);
  200. tooltipEl.className = tooltip.yAlign;
  201. tooltipEl.innerHTML = tooltip.text[0];
  202. tooltipEl.style.opacity = 1;
  203. tooltipEl.style.left = pos[0] + tooltip.x + 'px';
  204. tooltipEl.style.top = pos[1] + tooltip.y - tooltip.caretHeight - tooltip.caretPadding + 'px';
  205. var row = tooltip.text[1],
  206. hue = tooltip.text[2];
  207. if (row && !isNaN(hue)) {
  208. row.style.backgroundColor = 'hsl(%u, 100%%, 80%%)'.format(hue);
  209. tooltipEl.row = row;
  210. }
  211. };
  212. Chart.defaults.global.tooltipFontSize = 10;
  213. Chart.defaults.global.tooltipTemplate = function(tip) {
  214. tip.label[0] = tip.label[0].format(tip.value);
  215. return tip.label;
  216. };
  217. function kpi(id, val1, val2, val3)
  218. {
  219. var e = document.getElementById(id);
  220. if (val1 && val2 && val3)
  221. e.innerHTML = '<%:%s, %s and %s%>'.format(val1, val2, val3);
  222. else if (val1 && val2)
  223. e.innerHTML = '<%:%s and %s%>'.format(val1, val2);
  224. else if (val1)
  225. e.innerHTML = val1;
  226. e.parentNode.style.display = val1 ? 'list-item' : '';
  227. }
  228. function pie(id, data)
  229. {
  230. data.sort(function(a, b) { return b.value - a.value });
  231. if (data.length === 0 || (data.length === 1 && data[0].value === 0))
  232. data[0] = {
  233. value: 1,
  234. color: '#cccccc',
  235. label: [ '<%:no traffic%>' ]
  236. };
  237. for (var i = 0; i < data.length; i++) {
  238. if (!data[i].color) {
  239. var hue = 120 / (data.length-1) * i;
  240. data[i].color = 'hsl(%u, 80%%, 50%%)'.format(hue);
  241. data[i].label.push(hue);
  242. }
  243. }
  244. var ctx = document.getElementById(id).getContext('2d');
  245. if (chartRegistry.hasOwnProperty(id))
  246. chartRegistry[id].destroy();
  247. chartRegistry[id] = new Chart(ctx).Doughnut(data, {
  248. segmentStrokeWidth: 1,
  249. percentageInnerCutout: 30
  250. });
  251. return chartRegistry[id];
  252. }
  253. function query(filter, group, order)
  254. {
  255. var keys = [], columns = {}, records = {}, result = [];
  256. if (typeof(group) !== 'function' && typeof(group) !== 'object')
  257. group = ['mac'];
  258. for (var i = 0; i < trafficData.columns.length; i++)
  259. columns[trafficData.columns[i]] = i;
  260. for (var i = 0; i < trafficData.data.length; i++) {
  261. var record = trafficData.data[i];
  262. if (typeof(filter) === 'function' && filter(columns, record) !== true)
  263. continue;
  264. var key;
  265. if (typeof(group) === 'function') {
  266. key = group(columns, record);
  267. }
  268. else {
  269. key = [];
  270. for (var j = 0; j < group.length; j++)
  271. if (columns.hasOwnProperty(group[j]))
  272. key.push(record[columns[group[j]]]);
  273. key = key.join(',');
  274. }
  275. if (!records.hasOwnProperty(key)) {
  276. var rec = {};
  277. for (var col in columns)
  278. rec[col] = record[columns[col]];
  279. records[key] = rec;
  280. result.push(rec);
  281. }
  282. else {
  283. records[key].conns += record[columns.conns];
  284. records[key].rx_bytes += record[columns.rx_bytes];
  285. records[key].rx_pkts += record[columns.rx_pkts];
  286. records[key].tx_bytes += record[columns.tx_bytes];
  287. records[key].tx_pkts += record[columns.tx_pkts];
  288. }
  289. }
  290. if (typeof(order) === 'function')
  291. result.sort(order);
  292. return result;
  293. }
  294. function oui(mac) {
  295. var m, l = 0, r = ouiData.length / 3 - 1;
  296. var mac1 = parseInt(mac.replace(/[^a-fA-F0-9]/g, ''), 16);
  297. while (l <= r) {
  298. m = l + Math.floor((r - l) / 2);
  299. var mask = (0xffffffffffff -
  300. (Math.pow(2, 48 - ouiData[m * 3 + 1]) - 1));
  301. var mac1_hi = ((mac1 / 0x10000) & (mask / 0x10000)) >>> 0;
  302. var mac1_lo = ((mac1 & 0xffff) & (mask & 0xffff)) >>> 0;
  303. var mac2 = parseInt(ouiData[m * 3], 16);
  304. var mac2_hi = (mac2 / 0x10000) >>> 0;
  305. var mac2_lo = (mac2 & 0xffff) >>> 0;
  306. if (mac1_hi === mac2_hi && mac1_lo === mac2_lo)
  307. return ouiData[m * 3 + 2];
  308. if (mac2_hi > mac1_hi ||
  309. (mac2_hi === mac1_hi && mac2_lo > mac1_lo))
  310. r = m - 1;
  311. else
  312. l = m + 1;
  313. }
  314. return null;
  315. }
  316. function fetchData(period)
  317. {
  318. XHR.get('<%=url("admin/nlbw/data")%>', { period: period, group_by: 'family,mac,ip,layer7', order_by: '-rx_bytes,-tx_bytes' }, function(xhr, res) {
  319. if (res !== null && typeof(res) === 'object' && typeof(res.columns) === 'object' && typeof(res.data) === 'object')
  320. trafficData = res;
  321. var addrs = query(null, ['ip'], null);
  322. var ipAddrs = [];
  323. for (var i = 0; i < addrs.length; i++)
  324. if (ipAddrs.indexOf(addrs[i].ip) < 0)
  325. ipAddrs.push(addrs[i].ip);
  326. renderHostData();
  327. renderLayer7Data();
  328. renderIPv6Data();
  329. XHR.get('<%=url("admin/nlbw/ptr")%>/' + ipAddrs.join('/'), null, function(xhr, res) {
  330. if (res !== null && typeof(res) === 'object')
  331. hostNames = res;
  332. });
  333. });
  334. }
  335. function switchTab(tab)
  336. {
  337. bubbleDismiss();
  338. return cbi_t_switch('nlbw', tab);
  339. }
  340. function renderPeriods()
  341. {
  342. var sel = document.getElementById('nlbw.period');
  343. for (var e, i = trafficPeriods.length - 1; e = trafficPeriods[i]; i--) {
  344. var d1 = new Date(e);
  345. var d2, pd;
  346. if (i) {
  347. d2 = new Date(trafficPeriods[i - 1]);
  348. d2.setDate(d2.getDate() - 1);
  349. pd = '%04d-%02d-%02d'.format(d1.getFullYear(), d1.getMonth() + 1, d1.getDate());
  350. }
  351. else {
  352. d2 = new Date();
  353. pd = '';
  354. }
  355. var opt = document.createElement('option');
  356. opt.setAttribute('data-duration', (d2.getTime() - d1.getTime()) / 1000);
  357. opt.value = pd;
  358. opt.text = '%04d-%02d-%02d - %04d-%02d-%02d'.format(
  359. d1.getFullYear(), d1.getMonth() + 1, d1.getDate(),
  360. d2.getFullYear(), d2.getMonth() + 1, d2.getDate());
  361. sel.appendChild(opt);
  362. }
  363. sel.selectedIndex = sel.childNodes.length - 1;
  364. sel.style.display = '';
  365. sel.onchange = function() {
  366. bubbleDismiss();
  367. fetchData(sel.options[sel.selectedIndex].value);
  368. }
  369. }
  370. function renderHostDetail()
  371. {
  372. var key = this.getAttribute('href').substr(1),
  373. col = this.getAttribute('data-col'),
  374. label = this.getAttribute('data-label'),
  375. bubble = document.getElementById('detail-bubble'),
  376. arrow = document.getElementById('bubble-arrow'),
  377. table = document.getElementById('bubble-table');
  378. bubbleDismiss();
  379. var detailData = query(
  380. function(c, r) {
  381. return ((r[c.mac] === key || r[c.ip] === key) &&
  382. (r[c.rx_bytes] > 0 || r[c.tx_bytes] > 0));
  383. },
  384. [col],
  385. function(r1, r2) {
  386. return ((r2.rx_bytes + r2.tx_bytes) - (r1.rx_bytes + r1.tx_bytes));
  387. }
  388. );
  389. var rxData = [], txData = [];
  390. table.innerHTML = '<tr>' +
  391. '<th>%s</th>'.format(label || col) +
  392. '<th><%:Conn.%></th>' +
  393. '<th colspan="2"><%:Down. (Bytes / Pkts.)%></th>' +
  394. '<th colspan="2"><%:Up. (Bytes / Pkts.)%></th>' +
  395. '</tr>';
  396. for (var i = 0; i < detailData.length; i++) {
  397. var rec = detailData[i],
  398. row = table.insertRow(-1);
  399. row.insertCell(-1).innerHTML = rec[col] || '<%:other%>';
  400. row.insertCell(-1).innerHTML = "%1000.2m".format(rec.conns);
  401. row.insertCell(-1).innerHTML = "%1024.2mB".format(rec.rx_bytes);
  402. row.insertCell(-1).innerHTML = "%1000.2mP".format(rec.rx_pkts);
  403. row.insertCell(-1).innerHTML = "%1024.2mB".format(rec.tx_bytes);
  404. row.insertCell(-1).innerHTML = "%1000.2mP".format(rec.tx_pkts);
  405. rxData.push({
  406. label: ['%s: %%1024.2mB'.format(rec[col] || '<%:other%>'), row],
  407. value: rec.rx_bytes
  408. });
  409. txData.push({
  410. label: ['%s: %%1024.2mB'.format(rec[col] || '<%:other%>'), row],
  411. value: rec.tx_bytes
  412. });
  413. }
  414. pie('bubble-pie1', rxData);
  415. pie('bubble-pie2', txData);
  416. var mac = key.toUpperCase();
  417. var name = hostInfo.hasOwnProperty(mac) ? hostInfo[mac].name : null;
  418. if (!name)
  419. for (var i = 0; i < detailData.length; i++)
  420. if ((name = hostNames[detailData[i].ip]) !== undefined)
  421. break;
  422. if (mac !== '00:00:00:00:00:00') {
  423. kpi('bubble-hostname', name);
  424. kpi('bubble-vendor', oui(mac));
  425. }
  426. else {
  427. kpi('bubble-hostname');
  428. kpi('bubble-vendor');
  429. }
  430. var tr = this.parentNode.parentNode,
  431. xy = off(tr),
  432. xy2 = off(this);
  433. bubble.style.width = tr.offsetWidth + 'px';
  434. bubble.style.left = xy[0] + 'px';
  435. bubble.style.top = (xy[1] + tr.offsetHeight) + 'px';
  436. arrow.style.left = Math.floor(xy2[0] + this.offsetWidth / 2 - xy[0]) + 'px';
  437. bubble.className = 'in';
  438. return false;
  439. }
  440. function formatHostname(dns)
  441. {
  442. if (dns === undefined || dns === null || dns === '')
  443. return '-';
  444. dns = dns.split('.')[0];
  445. if (dns.length > 12)
  446. return '<span title="%q">%h…</span>'.format(dns, dns.substr(0, 12));
  447. return '%h'.format(dns);
  448. }
  449. function renderHostData()
  450. {
  451. var trafData = [], connData = [];
  452. var rx_total = 0, tx_total = 0, conn_total = 0;
  453. var table = document.getElementById('host-data');
  454. var hostData = query(
  455. function(c, r) {
  456. return (r[c.rx_bytes] > 0 || r[c.tx_bytes] > 0);
  457. },
  458. ['mac'],
  459. //function(c, r) {
  460. // return (r[c.mac] !== '00:00:00:00:00:00') ? r[c.mac] : r[c.ip];
  461. //},
  462. function(r1, r2) {
  463. return ((r2.rx_bytes + r2.tx_bytes) - (r1.rx_bytes + r1.tx_bytes));
  464. }
  465. );
  466. while (table.rows.length > 1)
  467. table.deleteRow(1);
  468. for (var i = 0; i < hostData.length; i++) {
  469. var row = table.insertRow(-1),
  470. cell = row.insertCell(-1),
  471. rec = hostData[i],
  472. mac = rec.mac.toUpperCase(),
  473. key = (mac !== '00:00:00:00:00:00') ? mac : rec.ip,
  474. dns = hostInfo[mac] ? hostInfo[mac].name : null;
  475. var link1 = document.createElement('a');
  476. link1.onclick = renderHostDetail;
  477. link1.href = '#' + rec.mac;
  478. link1.setAttribute('data-col', 'ip');
  479. link1.setAttribute('data-label', '<%:Source IP%>');
  480. link1.innerHTML = (mac !== '00:00:00:00:00:00') ? mac : '<%:other%>';
  481. var link2 = document.createElement('a');
  482. link2.onclick = renderHostDetail;
  483. link2.href = '#' + rec.mac;
  484. link2.setAttribute('data-col', 'layer7');
  485. link2.setAttribute('data-label', '<%:Protocol%>');
  486. link2.innerHTML = "%1000.2m".format(rec.conns);
  487. cell.innerHTML = formatHostname(dns);
  488. cell.className = 'hostname';
  489. row.insertCell(-1).appendChild(link1);
  490. row.insertCell(-1).appendChild(link2);
  491. row.insertCell(-1).innerHTML = "%1024.2mB".format(rec.rx_bytes);
  492. row.insertCell(-1).innerHTML = "%1000.2mP".format(rec.rx_pkts);
  493. row.insertCell(-1).innerHTML = "%1024.2mB".format(rec.tx_bytes);
  494. row.insertCell(-1).innerHTML = "%1000.2mP".format(rec.tx_pkts);
  495. trafData.push({
  496. value: rec.rx_bytes + rec.tx_bytes,
  497. label: ["%s: %%.2mB".format(key), row]
  498. });
  499. connData.push({
  500. value: rec.conns,
  501. label: ["%s: %%.2m".format(key), row]
  502. });
  503. rx_total += rec.rx_bytes;
  504. tx_total += rec.tx_bytes;
  505. conn_total += rec.conns;
  506. }
  507. if (table.rows.length === 1) {
  508. var cell = table.insertRow(-1).insertCell(-1);
  509. cell.setAttribute('colspan', 6);
  510. cell.innerHTML = '<em><%:No data recorded yet.%> <a href="<%=url("admin/nlbw/commit")%>"><%:Force reload…%></a></em>';
  511. }
  512. pie('traf-pie', trafData);
  513. pie('conn-pie', connData);
  514. kpi('rx-total', '%1024.2mB'.format(rx_total));
  515. kpi('tx-total', '%1024.2mB'.format(tx_total));
  516. kpi('conn-total', '%1000m'.format(conn_total));
  517. kpi('host-total', '%u'.format(hostData.length));
  518. }
  519. function renderLayer7Data()
  520. {
  521. var rxData = [], txData = [];
  522. var topConn = [[0],[0],[0]], topRx = [[0],[0],[0]], topTx = [[0],[0],[0]];
  523. var table = document.getElementById('layer7-data');
  524. var layer7Data = query(
  525. null, ['layer7'],
  526. function(r1, r2) {
  527. return ((r2.rx_bytes + r2.tx_bytes) - (r1.rx_bytes + r1.tx_bytes));
  528. }
  529. );
  530. while (table.rows.length > 1)
  531. table.deleteRow(1);
  532. for (var i = 0, c = 0; i < layer7Data.length; i++) {
  533. var rec = layer7Data[i],
  534. row = table.insertRow(-1);
  535. rxData.push({
  536. value: rec.rx_bytes,
  537. label: ["%s: %%.2mB".format(rec.layer7 || '<%:other%>'), row]
  538. });
  539. txData.push({
  540. value: rec.tx_bytes,
  541. label: ["%s: %%.2mB".format(rec.layer7 || '<%:other%>'), row]
  542. });
  543. row.insertCell(-1).innerHTML = rec.layer7 || '<%:other%>';
  544. row.insertCell(-1).innerHTML = "%1000m".format(rec.conns);
  545. row.insertCell(-1).innerHTML = "%1024.2mB".format(rec.rx_bytes);
  546. row.insertCell(-1).innerHTML = "%1000.2mP".format(rec.rx_pkts);
  547. row.insertCell(-1).innerHTML = "%1024.2mB".format(rec.tx_bytes);
  548. row.insertCell(-1).innerHTML = "%1000.2mP".format(rec.tx_pkts);
  549. if (rec.layer7) {
  550. topRx.push([rec.rx_bytes, rec.layer7]);
  551. topTx.push([rec.tx_bytes, rec.layer7]);
  552. topConn.push([rec.conns, rec.layer7]);
  553. }
  554. }
  555. if (table.rows.length === 1) {
  556. var cell = table.insertRow(-1).insertCell(-1);
  557. cell.setAttribute('colspan', 6);
  558. cell.innerHTML = '<em><%:No data recorded yet.%> <a href="<%=url("admin/nlbw/commit")%>"><%:Force reload…%></a></em>';
  559. }
  560. pie('layer7-rx-pie', rxData);
  561. pie('layer7-tx-pie', txData);
  562. topRx.sort(function(a, b) { return b[0] - a[0] });
  563. topTx.sort(function(a, b) { return b[0] - a[0] });
  564. topConn.sort(function(a, b) { return b[0] - a[0] });
  565. kpi('layer7-total', layer7Data.length);
  566. kpi('layer7-most-rx', topRx[0][1], topRx[1][1], topRx[2][1]);
  567. kpi('layer7-most-tx', topTx[0][1], topTx[1][1], topTx[2][1]);
  568. kpi('layer7-most-conn', topConn[0][1], topConn[1][1], topConn[2][1]);
  569. }
  570. function renderIPv6Data()
  571. {
  572. var table = document.getElementById('ipv6-data'),
  573. col = { },
  574. rx4_total = 0,
  575. tx4_total = 0,
  576. rx6_total = 0,
  577. tx6_total = 0,
  578. v4_total = 0,
  579. v6_total = 0,
  580. ds_total = 0,
  581. families = { },
  582. records = { };
  583. ipv6Data = query(
  584. null, ['family', 'mac'],
  585. function(r1, r2) {
  586. return ((r2.rx_bytes + r2.tx_bytes) - (r1.rx_bytes + r1.tx_bytes));
  587. }
  588. );
  589. for (var i = 0, c = 0; i < ipv6Data.length; i++) {
  590. var rec = ipv6Data[i],
  591. mac = rec.mac.toUpperCase(),
  592. ip = rec.ip,
  593. fam = families[mac] || 0,
  594. recs = records[mac] || {};
  595. if (rec.family == 4) {
  596. rx4_total += rec.rx_bytes;
  597. tx4_total += rec.tx_bytes;
  598. fam |= 1;
  599. }
  600. else {
  601. rx6_total += rec.rx_bytes;
  602. tx6_total += rec.tx_bytes;
  603. fam |= 2;
  604. }
  605. recs[rec.family] = rec;
  606. records[mac] = recs;
  607. families[mac] = fam;
  608. }
  609. for (var mac in families) {
  610. switch (families[mac])
  611. {
  612. case 3:
  613. ds_total++;
  614. break;
  615. case 2:
  616. v6_total++;
  617. break;
  618. case 1:
  619. v4_total++;
  620. break;
  621. }
  622. }
  623. while (table.rows.length > 1)
  624. table.deleteRow(1);
  625. for (var mac in records) {
  626. if (mac === '00:00:00:00:00:00')
  627. continue;
  628. var row = table.insertRow(-1),
  629. cell1 = row.insertCell(-1),
  630. cell2 = row.insertCell(-1),
  631. dns = hostInfo[mac] ? hostInfo[mac].name : null,
  632. rec4 = records[mac][4],
  633. rec6 = records[mac][6];
  634. cell1.setAttribute('rowspan', 2);
  635. cell1.innerHTML = formatHostname(dns);
  636. cell1.className = 'hostname';
  637. cell2.setAttribute('rowspan', 2);
  638. cell2.innerHTML = mac;
  639. row.insertCell(-1).innerHTML = 'IPv4';
  640. row.insertCell(-1).innerHTML = rec4 ? "%1024.2mB".format(rec4.rx_bytes) : '-';
  641. row.insertCell(-1).innerHTML = rec4 ? "%1000.2mP".format(rec4.rx_pkts) : '-';
  642. row.insertCell(-1).innerHTML = rec4 ? "%1024.2mB".format(rec4.tx_bytes) : '-';
  643. row.insertCell(-1).innerHTML = rec4 ? "%1000.2mP".format(rec4.tx_pkts) : '-';
  644. row = table.insertRow(-1);
  645. row.insertCell(-1).innerHTML = 'IPv6';
  646. row.insertCell(-1).innerHTML = rec6 ? "%1024.2mB".format(rec6.rx_bytes) : '-';
  647. row.insertCell(-1).innerHTML = rec6 ? "%1000.2mP".format(rec6.rx_pkts) : '-';
  648. row.insertCell(-1).innerHTML = rec6 ? "%1024.2mB".format(rec6.tx_bytes) : '-';
  649. row.insertCell(-1).innerHTML = rec6 ? "%1000.2mP".format(rec6.tx_pkts) : '-';
  650. }
  651. if (table.rows.length === 1) {
  652. var cell = table.insertRow(-1).insertCell(-1);
  653. cell.setAttribute('colspan', 7);
  654. cell.innerHTML = '<em><%:No data recorded yet.%> <a href="<%=url("admin/nlbw/commit")%>"><%:Force reload…%></a></em>';
  655. }
  656. var shareData = [], hostsData = [];
  657. if (rx4_total > 0 || tx4_total > 0)
  658. shareData.push({
  659. value: rx4_total + tx4_total,
  660. label: ["IPv4: %.2mB"],
  661. color: 'hsl(140, 100%, 50%)'
  662. });
  663. if (rx6_total > 0 || tx6_total > 0)
  664. shareData.push({
  665. value: rx6_total + tx6_total,
  666. label: ["IPv6: %.2mB"],
  667. color: 'hsl(180, 100%, 50%)'
  668. });
  669. if (v4_total > 0)
  670. hostsData.push({
  671. value: v4_total,
  672. label: ["<%:%d IPv4-only hosts%>"],
  673. color: 'hsl(140, 100%, 50%)'
  674. });
  675. if (v6_total > 0)
  676. hostsData.push({
  677. value: v6_total,
  678. label: ["<%:%d IPv6-only hosts%>"],
  679. color: 'hsl(180, 100%, 50%)'
  680. });
  681. if (ds_total > 0)
  682. hostsData.push({
  683. value: ds_total,
  684. label: ["<%:%d dual-stack hosts%>"],
  685. color: 'hsl(50, 100%, 50%)'
  686. });
  687. pie('ipv6-share-pie', shareData);
  688. pie('ipv6-hosts-pie', hostsData);
  689. kpi('ipv6-hosts', '%.2f%%'.format(100 / (ds_total + v4_total + v6_total) * (ds_total + v6_total)));
  690. kpi('ipv6-share', '%.2f%%'.format(100 / (rx4_total + rx6_total + tx4_total + tx6_total) * (rx6_total + tx6_total)));
  691. kpi('ipv6-rx', '%1024.2mB'.format(rx6_total));
  692. kpi('ipv6-tx', '%1024.2mB'.format(tx6_total));
  693. }
  694. function bubbleDismiss()
  695. {
  696. var bubble = document.getElementById('detail-bubble');
  697. bubble.className = '';
  698. document.body.appendChild(bubble);
  699. return false;
  700. }
  701. //]]></script>
  702. <h2 name="content"><%:Netlink Bandwidth Monitor%></h2>
  703. <div id="detail-bubble">
  704. <span id="bubble-arrow"></span>
  705. <div>
  706. <div class="head">
  707. <a class="dismiss" href="#" onclick="this.blur(); return bubbleDismiss()">×</a>
  708. <div class="pie">
  709. <label>Download</label>
  710. <canvas id="bubble-pie1" width="100" height="100"></canvas>
  711. </div>
  712. <div class="pie">
  713. <label>Upload</label>
  714. <canvas id="bubble-pie2" width="100" height="100"></canvas>
  715. </div>
  716. <div class="kpi">
  717. <ul>
  718. <li><%_Hostname: <big id="bubble-hostname">example.org</big>%></li>
  719. <li><%_Vendor: <big id="bubble-vendor">Example Corp.</big>%></li>
  720. </ul>
  721. </div>
  722. </div>
  723. <table id="bubble-table"></table>
  724. </div>
  725. </div>
  726. <hr />
  727. <p>
  728. <%:Select accounting period:%>
  729. <select id="nlbw.period" style="display:none"></select>
  730. </p>
  731. <hr />
  732. <ul class="cbi-tabmenu">
  733. <li id="tab.nlbw.traffic" class="cbi-tab"><a href="#" onclick="return switchTab('traffic')"><%:Traffic Distribution%></a></li>
  734. <li id="tab.nlbw.layer7" class="cbi-tab-disabled"><a href="#" onclick="return switchTab('layer7')"><%:Application Protocols%></a></li>
  735. <li id="tab.nlbw.ipv6" class="cbi-tab-disabled"><a href="#" onclick="return switchTab('ipv6')"><%:IPv6%></a></li>
  736. <li id="tab.nlbw.export" class="cbi-tab-disabled"><a href="#" onclick="return switchTab('export')"><%:Export%></a></li>
  737. </ul>
  738. <div class="cbi-section" id="container.nlbw.traffic">
  739. <div>
  740. <div class="pie">
  741. <label><%:Traffic / Host%></label>
  742. <canvas id="traf-pie" width="200" height="200"></canvas>
  743. </div>
  744. <div class="pie">
  745. <label><%:Connections / Host%></label>
  746. <canvas id="conn-pie" width="200" height="200"></canvas>
  747. </div>
  748. <div class="kpi">
  749. <ul>
  750. <li><%_<big id="host-total">0</big> hosts%></li>
  751. <li><%_<big id="rx-total">0</big> download%></li>
  752. <li><%_<big id="tx-total">0</big> upload%></li>
  753. <li><%_<big id="conn-total">0</big> connections%></li>
  754. </ul>
  755. </div>
  756. </div>
  757. <table id="host-data">
  758. <tr>
  759. <th width="10%" class="hostname"><%:Host%></th>
  760. <th width="5%"><%:MAC%></th>
  761. <th width="5%"><%:Connections%></th>
  762. <th width="30%" colspan="2"><%:Download (Bytes / Packets)%></th>
  763. <th width="30%" colspan="2"><%:Upload (Bytes / Packets)%></th>
  764. </tr>
  765. </table>
  766. </div>
  767. <div class="cbi-section" id="container.nlbw.layer7" style="display:none">
  768. <div>
  769. <div class="pie">
  770. <label><%:Download / Application%></label>
  771. <canvas id="layer7-rx-pie" width="200" height="200"></canvas>
  772. </div>
  773. <div class="pie">
  774. <label><%:Upload / Application%></label>
  775. <canvas id="layer7-tx-pie" width="200" height="200"></canvas>
  776. </div>
  777. <div class="kpi">
  778. <ul>
  779. <li><%_<big id="layer7-total">0</big> different application protocols%></li>
  780. <li><%_<big id="layer7-most-rx">0</big> cause the most download%></li>
  781. <li><%_<big id="layer7-most-tx">0</big> cause the most upload%></li>
  782. <li><%_<big id="layer7-most-conn">0</big> cause the most connections%></li>
  783. </ul>
  784. </div>
  785. </div>
  786. <table id="layer7-data">
  787. <tr>
  788. <th width="20%"><%:Application%></th>
  789. <th width="10%"><%:Connections%></th>
  790. <th width="30%" colspan="2"><%:Download (Bytes / Packets)%></th>
  791. <th width="30%" colspan="2"><%:Upload (Bytes / Packets)%></th>
  792. </tr>
  793. </table>
  794. </div>
  795. <div class="cbi-section" id="container.nlbw.ipv6" style="display:none">
  796. <div>
  797. <div class="pie">
  798. <label><%:IPv4 vs. IPv6%></label>
  799. <canvas id="ipv6-share-pie" width="200" height="200"></canvas>
  800. </div>
  801. <div class="pie">
  802. <label><%:Dualstack enabled hosts%></label>
  803. <canvas id="ipv6-hosts-pie" width="200" height="200"></canvas>
  804. </div>
  805. <div class="kpi">
  806. <ul>
  807. <li><%_<big id="ipv6-hosts">0%</big> IPv6 support rate among hosts%></li>
  808. <li><%_<big id="ipv6-share">0%</big> of the total traffic is IPv6%></li>
  809. <li><%_<big id="ipv6-rx">0B</big> total IPv6 download%></li>
  810. <li><%_<big id="ipv6-tx">0B</big> total IPv6 upload%></li>
  811. </ul>
  812. </div>
  813. </div>
  814. <table id="ipv6-data">
  815. <tr>
  816. <th width="10%" class="hostname"><%:Host%></th>
  817. <th width="5%"><%:MAC%></th>
  818. <th width="5%"><%:Family%></th>
  819. <th width="40%" colspan="2"><%:Download (Bytes / Packets)%></th>
  820. <th width="40%" colspan="2"><%:Upload (Bytes / Packets)%></th>
  821. </tr>
  822. </table>
  823. </div>
  824. <div class="cbi-section" id="container.nlbw.export" style="display:none">
  825. <ul>
  826. <li><a href="<%=url('admin/nlbw/data')%>?type=csv&#38;group_by=mac&#38;order_by=-rx,-tx"><%:CSV, grouped by MAC%></a></li>
  827. <li><a href="<%=url('admin/nlbw/data')%>?type=csv&#38;group_by=ip&#38;order_by=-rx,-tx"><%:CSV, grouped by IP%></a></li>
  828. <li><a href="<%=url('admin/nlbw/data')%>?type=csv&#38;group_by=layer7&#38;order_by=-rx,-tx"><%:CSV, grouped by protocol%></a></li>
  829. <li><a href="<%=url('admin/nlbw/data')%>?type=json"><%:JSON dump%></a></li>
  830. </ul>
  831. </div>
  832. <script type="text/javascript">//<![CDATA[
  833. cbi_t_add('nlbw', 'traffic');
  834. cbi_t_add('nlbw', 'layer7');
  835. cbi_t_add('nlbw', 'ipv6');
  836. cbi_t_add('nlbw', 'export');
  837. XHR.get('<%=url("admin/nlbw/list")%>', null, function(xhr, res) {
  838. if (res !== null && typeof(res) === 'object' && res.length > 0) {
  839. trafficPeriods = res;
  840. renderPeriods();
  841. }
  842. xhr.open('GET', 'https://raw.githubusercontent.com/jow-/oui-database/master/oui.json', true);
  843. xhr.onreadystatechange = function() {
  844. if (xhr.readyState === 4) {
  845. try { res = JSON.parse(xhr.responseText); }
  846. catch(e) { res = null; }
  847. if (res !== null && typeof(res) === 'object' && (res.length % 3) === 0)
  848. ouiData = res;
  849. fetchData('');
  850. }
  851. };
  852. xhr.send(null);
  853. });
  854. //]]></script>
  855. <%+footer%>