From 277ffb6ac50010e17d2daff6aad72672ff2ecdef Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Tue, 23 Feb 2021 16:21:25 +0100 Subject: [PATCH] Reporting / Traffic: change api output to combined in/out per row and change user interface to handle new format. closes https://github.com/opnsense/core/issues/4724 --- .../views/OPNsense/Diagnostics/traffic.volt | 36 +++++++++---------- .../scripts/interfaces/traffic_top.py | 29 ++++++++------- 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/src/opnsense/mvc/app/views/OPNsense/Diagnostics/traffic.volt b/src/opnsense/mvc/app/views/OPNsense/Diagnostics/traffic.volt index e58282cd2..3ee3d6bf4 100644 --- a/src/opnsense/mvc/app/views/OPNsense/Diagnostics/traffic.volt +++ b/src/opnsense/mvc/app/views/OPNsense/Diagnostics/traffic.volt @@ -236,9 +236,8 @@ POSSIBILITY OF SUCH DAMAGE. let update_stamp_iso = (new Date()).toISOString(); Object.keys(data).forEach(function(intf) { let intf_label = $("#interfaces > option[value="+intf+"]").data('content'); - ['in', 'out'].forEach(function(dir) { - for (var i=0; i < data[intf][dir].length ; i++) { - let item = data[intf][dir][i]; + for (var i=0; i < data[intf]['records'].length ; i++) { + let item = data[intf]['records'][i]; let tr = target.find("tr[data-address='"+item.address+"']"); if (tr.length === 0) { tr = $(""); @@ -257,18 +256,19 @@ POSSIBILITY OF SUCH DAMAGE. tr.append($("")); target.append(tr); } - tr.data('bps_'+dir, item.rate_bits); - tr.data('total_'+ dir, tr.data('total_'+ dir) + item.cumulative_bytes); - tr.data('last_seen', update_stamp); - tr.find('td.last_seen').text(update_stamp_iso); - if (parseInt(tr.data('bps_max_'+dir)) < item.rate_bits) { - tr.data('bps_max_'+dir, item.rate_bits); - tr.find('td.bps_max_'+dir).text(item.rate); - } - tr.find('td.bps_'+dir).text(item.rate); - tr.find('td.total_'+dir).text(byteFormat(tr.data('total_'+ dir))); + ['in', 'out'].forEach(function(dir) { + tr.data('bps_'+dir, item['rate_bits'+dir]); + tr.data('total_'+ dir, tr.data('total_'+ dir) + item.cumulative_bytes); + tr.data('last_seen', update_stamp); + tr.find('td.last_seen').text(update_stamp_iso); + if (parseInt(tr.data('bps_max_'+dir)) < item['rate_bits_'+dir]) { + tr.data('bps_max_'+dir, item['rate_bits_'+dir]); + tr.find('td.bps_max_'+dir).text(item['rate_'+dir]); + } + tr.find('td.bps_'+dir).text(item['rate_'+dir]); + tr.find('td.total_'+dir).text(byteFormat(tr.data('total_'+ dir))); + }); } - }); }); let ttl = 120; // keep visible for ttl seconds target.find('tr').each(function(){ @@ -374,14 +374,14 @@ POSSIBILITY OF SUCH DAMAGE. Object.keys(data).forEach(function(intf) { this_chart.config.data.datasets.forEach(function(dataset) { if (dataset.intf == intf) { - let calc_data = data[intf][dataset.src_field]; + let calc_data = data[intf]['records']; dataset.hidden = !$("#interfaces").val().includes(intf); - for (var i=0; i < data[intf][dataset.src_field].length ; ++i) { + for (var i=0; i < data[intf]['records'].length ; ++i) { dataset.data.push({ x: Date.now(), - y: data[intf][dataset.src_field][i]['rate_bits'], + y: data[intf]['records'][i]['rate_bits_' + dataset.src_field], r: 4, - address: data[intf][dataset.src_field][i]['address'] + address: data[intf]['records'][i]['address'] }); } return; diff --git a/src/opnsense/scripts/interfaces/traffic_top.py b/src/opnsense/scripts/interfaces/traffic_top.py index a697fe046..98657a3b7 100755 --- a/src/opnsense/scripts/interfaces/traffic_top.py +++ b/src/opnsense/scripts/interfaces/traffic_top.py @@ -94,7 +94,7 @@ if __name__ == '__main__': for intf in iftop_data: agg_results = dict() - result[intf] = {'in' : [], 'out': []} + result[intf] = {'records' : []} if iftop_data[intf] is None: result[intf]['status'] = 'timeout' continue @@ -133,29 +133,32 @@ if __name__ == '__main__': 'address': item['address'], 'rate_bits_in': 0, 'rate_bits_out': 0, + 'rate_bits': 0, 'cumulative_bytes_in': 0, 'cumulative_bytes_out': 0, + 'cumulative_bytes': 0, 'tags': item['tags'], 'details': [] } agg_results[item['address']]['rate_bits_out'] += item['rate_bits'] agg_results[item['address']]['rate_bits_in'] += other_end['rate_bits'] + agg_results[item['address']]['rate_bits'] += (item['rate_bits'] + other_end['rate_bits']) agg_results[item['address']]['cumulative_bytes_out'] += item['cumulative_bytes'] agg_results[item['address']]['cumulative_bytes_in'] += other_end['cumulative_bytes'] + agg_results[item['address']]['cumulative_bytes'] += ( + item['cumulative_bytes'] + other_end['cumulative_bytes'] + ) agg_results[item['address']]['details'].append(other_end) # XXX: sort output, limit output results to max 200 (safety precaution) - for direction in ['in', 'out']: - # XXX split in/out, we should probably change the output type later\ - top_hosts = sorted(agg_results.values(), key=lambda x: x['rate_bits_%s' % direction], reverse=True)[:200] - for host in top_hosts: - result[intf][direction].append({ - 'address': host['address'], - 'rate': to_bformat(host['rate_bits_%s' % direction]), - 'rate_bits': host['rate_bits_%s' % direction], - 'cumulative_bytes': host['cumulative_bytes_%s' % direction], - 'cumulative': to_bformat(host['cumulative_bytes_%s' % direction]), - 'tags': host['tags'], - }) + top_hosts = sorted(agg_results.values(), key=lambda x: x['rate_bits'], reverse=True)[:200] + for host in top_hosts: + host['rate_in'] = to_bformat(host['rate_bits_in']) + host['rate_out'] = to_bformat(host['rate_bits_out']) + host['rate'] = to_bformat(host['rate_bits']) + host['cumulative_in'] = to_bformat(host['cumulative_bytes_in']) + host['cumulative_out'] = to_bformat(host['cumulative_bytes_out']) + host['cumulative'] = to_bformat(host['cumulative_bytes']) + result[intf]['records'].append(host) print(ujson.dumps(result))