1
0
Fork 0
mirror of https://github.com/Ysurac/openmptcprouter-feeds.git synced 2025-03-09 15:40:03 +00:00

add a total polyline to accumulative total traffic of all interfaces and display in the graph

This commit is contained in:
libin 2018-05-17 10:39:11 +08:00
parent 927bb0df77
commit 76771db0e2
2 changed files with 218 additions and 111 deletions

View file

@ -28,19 +28,100 @@ function interface_bandwidth(iface)
end end
end end
function string.split(input, delimiter)
input = tostring(input)
delimiter = tostring(delimiter)
if (delimiter=='') then return false end
local pos,arr = 0, {}
-- for each divider found
for st,sp in function() return string.find(input, delimiter, pos, true) end do
table.insert(arr, string.sub(input, pos, st - 1))
pos = sp + 1
end
table.insert(arr, string.sub(input, pos))
return arr
end
function multipath_bandwidth() function multipath_bandwidth()
local result = { }; local result = { };
local uci = luci.model.uci.cursor() local uci = luci.model.uci.cursor()
local multipath="";
local proto="";
local res={ };
local str="";
local tmpstr="";
for _, dev in luci.util.vspairs(luci.sys.net.devices()) do for _, dev in luci.util.vspairs(luci.sys.net.devices()) do
if dev ~= "lo" then if dev ~= "lo" then
local multipath = uci:get("network", dev, "multipath") if dev == "eth0.2" then
multipath = uci:get("network", "wan", "multipath")
elseif dev == "4g-wwan0" then
multipath = uci:get("network", "wwan0", "multipath")
else
multipath = uci:get("network", dev, "multipath")
end
if multipath == "on" or multipath == "master" or multipath == "backup" or multipath == "handover" then if multipath == "on" or multipath == "master" or multipath == "backup" or multipath == "handover" then
result[dev] = "[" .. string.gsub((luci.sys.exec("luci-bwc -i %q 2>/dev/null" % dev)), '[\r\n]', '') .. "]" result[dev] = "[" .. string.gsub((luci.sys.exec("luci-bwc -i %q 2>/dev/null" % dev)), '[\r\n]', '') .. "]"
end end
end end
end end
---先初始化求和数组
res["total"]={ };
for i=1,60 do
res["total"][i]={}
for j=1,5 do
res["total"][i][j]=0
end
end
--遍历所有接口表求和
for key,value in pairs(result) do
res[key]={}
value=(string.gsub(value, "^%[%[", ""))
value=(string.gsub(value, "%]%]", ""))
local temp1 = string.split(value, "],")
res[key][1]=temp1[1]
for i=2,60 do
res[key][i]={}
res[key][i]=(string.gsub(temp1[i], "%[", " "))
end
for i=1,60 do
res[key][i] = string.split(res[key][i], ",")
for j=1,5 do
if "string"== type(res[key][i][j]) then
res[key][i][j]= tonumber(res[key][i][j])
end
if "string"==type(res["total"][i][j]) then
res["total"][i][j]= tonumber(res["total"][i][j])
end
if j ==1 then
res["total"][i][j] = res[key][i][j]
else
res["total"][i][j] = res["total"][i][j] + res[key][i][j]
end
end
end
end
---数值类型转成字符串
for i=1,60 do
for j=1,5 do
if "number"== type(res["total"][i][j]) then
res["total"][i][j]= tostring(res["total"][i][j])
end
end
end
---数组转成字符串
for i=1,60 do
if i == 60 then
tmpstr = "["..table.concat(res["total"][i], ",")
else
tmpstr = "["..table.concat(res["total"][i], ",").."],"
end
str = str..tmpstr
end
str = "["..str.."]]"
result["total"]=str
luci.http.prepare_content("application/json") luci.http.prepare_content("application/json")
luci.http.write_json(result) luci.http.write_json(result)
end end

View file

@ -25,10 +25,18 @@
local dev local dev
local devices = { "all" } local devices = { "all" }
local multipath = ""
for _, dev in luci.util.vspairs(luci.sys.net.devices()) do for _, dev in luci.util.vspairs(luci.sys.net.devices()) do
if dev ~= "lo" and not ntm:ignore_interface(dev) then if dev ~= "lo" and not ntm:ignore_interface(dev) then
local multipath = uci:get("network", dev, "multipath") if dev == "eth0.2" then
if multipath == "on" or multipath == "master" or multipath == "backup" or multipath == "handover" then multipath = uci:get("network", "wan", "multipath")
elseif dev == "4g-wwan0" then
multipath = uci:get("network", "wwan0", "multipath")
else
multipath = uci:get("network", dev, "multipath")
end
if multipath == "on" or multipath == "master" or multipath == "backup" or multipath== "handover" then
devices[#devices+1] = dev devices[#devices+1] = dev
end end
end end
@ -48,6 +56,8 @@
<script type="text/javascript">//<![CDATA[ <script type="text/javascript">//<![CDATA[
function stringToColour(str) { function stringToColour(str) {
if(str == "total")
return "OrangeRed" ;
if(str == "free1") if(str == "free1")
return "FireBrick"; return "FireBrick";
if(str == "ovh1") if(str == "ovh1")
@ -159,33 +169,15 @@
height = dnsvg.offsetHeight - 2; height = dnsvg.offsetHeight - 2;
data_wanted = Math.ceil(width / step); data_wanted = Math.ceil(width / step);
/* prefill datasets
for (var i = 0; i < data_wanted; i++)
{
data_tx[i] = 0;
}
*/
/* find svg elements */ /* find svg elements */
labeldn_25 = Gdn.getElementById('label_25'); labeldn_25 = Gdn.getElementById('label_25');
labeldn_50 = Gdn.getElementById('label_50'); labeldn_50 = Gdn.getElementById('label_50');
labeldn_75 = Gdn.getElementById('label_75'); labeldn_75 = Gdn.getElementById('label_75');
labelup_25 = Gup.getElementById('label_25'); labelup_25 = Gup.getElementById('label_25');
labelup_50 = Gup.getElementById('label_50'); labelup_50 = Gup.getElementById('label_50');
labelup_75 = Gup.getElementById('label_75'); labelup_75 = Gup.getElementById('label_75');
/*
label_rx_cur = document.getElementById('rx_bw_cur');
label_rx_avg = document.getElementById('rx_bw_avg');
label_rx_peak = document.getElementById('rx_bw_peak');
label_tx_cur = document.getElementById('tx_bw_cur');
label_tx_avg = document.getElementById('tx_bw_avg');
label_tx_peak = document.getElementById('tx_bw_peak');
*/
labeldn_scale = document.getElementById('dnscale'); labeldn_scale = document.getElementById('dnscale');
labelup_scale = document.getElementById('upscale'); labelup_scale = document.getElementById('upscale');
@ -209,21 +201,21 @@
labeldn_25.parentNode.appendChild(linedn); labeldn_25.parentNode.appendChild(linedn);
labeldn_25.parentNode.appendChild(textdn); labeldn_25.parentNode.appendChild(textdn);
var lineup = Gup.createElementNS('http://www.w3.org/2000/svg', 'line'); var lineup = Gup.createElementNS('http://www.w3.org/2000/svg', 'line');
lineup.setAttribute('x1', i); lineup.setAttribute('x1', i);
lineup.setAttribute('y1', 0); lineup.setAttribute('y1', 0);
lineup.setAttribute('x2', i); lineup.setAttribute('x2', i);
lineup.setAttribute('y2', '100%'); lineup.setAttribute('y2', '100%');
lineup.setAttribute('style', 'stroke:black;stroke-width:0.1'); lineup.setAttribute('style', 'stroke:black;stroke-width:0.1');
var textup = Gup.createElementNS('http://www.w3.org/2000/svg', 'text'); var textup = Gup.createElementNS('http://www.w3.org/2000/svg', 'text');
textup.setAttribute('x', i + 5); textup.setAttribute('x', i + 5);
textup.setAttribute('y', 15); textup.setAttribute('y', 15);
textup.setAttribute('style', 'fill:#999999; font-size:9pt'); textup.setAttribute('style', 'fill:#999999; font-size:9pt');
textup.appendChild(Gup.createTextNode(Math.round((width - i) / step / 60) + 'm')); textup.appendChild(Gup.createTextNode(Math.round((width - i) / step / 60) + 'm'));
labelup_25.parentNode.appendChild(lineup); labelup_25.parentNode.appendChild(lineup);
labelup_25.parentNode.appendChild(textup); labelup_25.parentNode.appendChild(textup);
} }
@ -246,9 +238,10 @@
var data_tx_peak = 0; var data_tx_peak = 0;
var data = {}; var data = {};
var pre_itf = "";
for(var itf in dataarray) for(var itf in dataarray)
{ {
// convert json string to json object // convert json string to json object
data[itf] = JSON.parse(dataarray[itf]); data[itf] = JSON.parse(dataarray[itf]);
// Create lines for each interfaces // Create lines for each interfaces
@ -258,19 +251,33 @@
// Create a new polygon to draw the bandwith // Create a new polygon to draw the bandwith
var dnline = Gdn.createElementNS('http://www.w3.org/2000/svg', 'polyline'); var dnline = Gdn.createElementNS('http://www.w3.org/2000/svg', 'polyline');
dnline.setAttributeNS(null, 'id', 'rx_' + itf); dnline.setAttributeNS(null, 'id', 'rx_' + itf);
dnline.setAttributeNS(null, 'style', 'fill:' + color + ';fill-opacity:0.4;stroke:black;stroke-width:0.1'); if(itf != "total")
{
dnline.setAttributeNS(null, 'style', 'fill:' + color + ';fill-opacity:0.4;stroke:black;stroke-width:0.1');
}
else
{
dnline.setAttributeNS(null, 'style', 'fill: none;stroke:OrangeRed;stroke-width:1');
}
Gdn.getElementById('rx').parentNode.appendChild(dnline); Gdn.getElementById('rx').parentNode.appendChild(dnline);
dnPolygons[itf] = Gdn.getElementById('rx_' + itf); dnPolygons[itf] = Gdn.getElementById('rx_' + itf);
/* prefill datasets */ /* prefill datasets */
dndata[itf] = [ ]; dndata[itf] = [ ];
for (var i = 0; i < data_wanted; i++) for (var i = 0; i < data_wanted; i++)
{ {
dndata[itf][i] = 0; dndata[itf][i] = 0;
} }
// Create legend for this connextion // Create legend for this connextion
var table = document.getElementById('download_stats'); var table = document.getElementById('download_stats');
var tr = table.insertRow(); if(pre_itf == "total") //insert before total
{
var tr = table.insertRow(0);
}
else //insert other interface affer,default value -1
{
var tr = table.insertRow();
}
tr.setAttribute('id', itf + '_download'); tr.setAttribute('id', itf + '_download');
// Create cells of the table // Create cells of the table
var itflabel = tr.insertCell(0); var itflabel = tr.insertCell(0);
@ -295,43 +302,59 @@
} }
if(typeof upPolygons[itf] != "object") if(typeof upPolygons[itf] != "object")
{ {
var upline = Gup.createElementNS('http://www.w3.org/2000/svg', 'polyline'); var upline = Gup.createElementNS('http://www.w3.org/2000/svg', 'polyline');
upline.setAttributeNS(null, 'id', 'tx_' + itf); upline.setAttributeNS(null, 'id', 'tx_' + itf);
upline.setAttributeNS(null, 'style', 'fill:' + color + ';fill-opacity:0.4;stroke:black;stroke-width:0.1'); if(itf != "total")
{
upline.setAttributeNS(null, 'style', 'fill:' + color + ';fill-opacity:0.4;stroke:black;stroke-width:0.1');
}
else
{
upline.setAttributeNS(null, 'style', 'fill: none;stroke:OrangeRed;stroke-width:1');
}
Gup.getElementById('tx').parentNode.appendChild(upline); Gup.getElementById('tx').parentNode.appendChild(upline);
upPolygons[itf] = Gup.getElementById('tx_' + itf); upPolygons[itf] = Gup.getElementById('tx_' + itf);
/* prefill datasets */ /* prefill datasets */
updata[itf] = [ ]; updata[itf] = [ ];
for (var i = 0; i < data_wanted; i++) for (var i = 0; i < data_wanted; i++)
{ {
updata[itf][i] = 0; updata[itf][i] = 0;
} }
// Create legend for this connextion // Create legend for this connextion
var table = document.getElementById('upload_stats'); var table = document.getElementById('upload_stats');
var tr = table.insertRow(); if(pre_itf == "total") //insert before total
tr.setAttribute('id', itf + '_upload'); {
// Create cells of the table var tr = table.insertRow(0);
var itflabel = tr.insertCell(0); }
// Crete itf legend else //insert other interface affer,default value -1
var strong = document.createElement('strong') {
strong.appendChild(document.createTextNode(itf)); var tr = table.insertRow();
strong.setAttribute('style', 'border-bottom:2px solid ' + color); }
itflabel.appendChild(strong); tr.setAttribute('id', itf + '_upload');
// Create cells of the table
var itflabel = tr.insertCell(0);
// Crete itf legend
var strong = document.createElement('strong')
strong.appendChild(document.createTextNode(itf));
strong.setAttribute('style', 'border-bottom:2px solid ' + color);
itflabel.appendChild(strong);
// Create label for stats // Create label for stats
tr.insertCell(1).appendChild(document.createTextNode('<%:Current:%>')); tr.insertCell(1).appendChild(document.createTextNode('<%:Current:%>'));
var itfcur = tr.insertCell(2); var itfcur = tr.insertCell(2);
itfcur.setAttribute('id', itf + '_upload_cur'); itfcur.setAttribute('id', itf + '_upload_cur');
itfcur.appendChild(document.createTextNode('0 <%:kbit/s%> (0 <%:kB/s%>)')); itfcur.appendChild(document.createTextNode('0 <%:kbit/s%> (0 <%:kB/s%>)'));
tr.insertCell(3).appendChild(document.createTextNode('<%:Average:%>')); tr.insertCell(3).appendChild(document.createTextNode('<%:Average:%>'));
var itfavg = tr.insertCell(4); var itfavg = tr.insertCell(4);
itfavg.setAttribute('id', itf + '_upload_avg'); itfavg.setAttribute('id', itf + '_upload_avg');
itfavg.appendChild(document.createTextNode('0 <%:kbit/s%> (0 <%:kB/s%>)')); itfavg.appendChild(document.createTextNode('0 <%:kbit/s%> (0 <%:kB/s%>)'));
tr.insertCell(5).appendChild(document.createTextNode('<%:Peak:%>')); tr.insertCell(5).appendChild(document.createTextNode('<%:Peak:%>'));
var itfpeak = tr.insertCell(6); var itfpeak = tr.insertCell(6);
itfpeak.setAttribute('id', itf + '_upload_peak'); itfpeak.setAttribute('id', itf + '_upload_peak');
itfpeak.appendChild(document.createTextNode('0 <%:kbit/s%> (0 <%:kB/s%>)')); itfpeak.appendChild(document.createTextNode('0 <%:kbit/s%> (0 <%:kB/s%>)'));
} }
pre_itf = itf;
} }
for (var itf in data) for (var itf in data)
@ -367,15 +390,17 @@
var el = Gdn.getElementById('rx').parentNode; var el = Gdn.getElementById('rx').parentNode;
// intialize upline datas for download and upload,the downline should be zero. // intialize upline and downline datas for download and upload
var uplineDnl = { }; var uplineDnl = { };
var uplineUpl = { }; var uplineUpl = { };
// fetch upline and downline datas // fetch upline and downline datas
var toadditf = [ ]; var toadditf = [ ];
for (var itf in data) for (var itf in data)
{ {
uplineDnl[itf] = [ ]; uplineDnl[itf] = [ ];
uplineUpl[itf] = [ ]; uplineUpl[itf] = [ ];
// build point sets // build point sets
var data_tx_avg; var data_tx_avg;
var data_rx_avg; var data_rx_avg;
@ -398,16 +423,17 @@
data_tx_peak = Math.max(data_tx_peak, updata[itf][i]); data_tx_peak = Math.max(data_tx_peak, updata[itf][i]);
data_max_upl = Math.max(data_max_upl, uplineUpl[itf][i]); data_max_upl = Math.max(data_max_upl, uplineUpl[itf][i]);
if (i > 0) if (i > 0)
{ {
data_rx_avg = (data_rx_avg + dndata[itf][i]) / 2; data_rx_avg = (data_rx_avg + dndata[itf][i]) / 2;
data_tx_avg = (data_tx_avg + updata[itf][i]) / 2; data_tx_avg = (data_tx_avg + updata[itf][i]) / 2;
} }
else else
{ {
data_rx_avg = dndata[itf][i]; data_rx_avg = dndata[itf][i];
data_tx_avg = updata[itf][i]; data_tx_avg = updata[itf][i];
} }
} }
toadditf.push(itf); toadditf.push(itf);
// Update stats labels // Update stats labels
@ -416,26 +442,26 @@
var label = document.getElementById(itf + '_download_cur'); var label = document.getElementById(itf + '_download_cur');
label.innerHTML = bandwidth_label(dndata[itf][dndata[itf].length-1], true); label.innerHTML = bandwidth_label(dndata[itf][dndata[itf].length-1], true);
} }
if(document.getElementById(itf + '_upload_cur')) if(document.getElementById(itf + '_upload_cur'))
{ {
var label = document.getElementById(itf + '_upload_cur'); var label = document.getElementById(itf + '_upload_cur');
label.innerHTML = bandwidth_label(updata[itf][updata[itf].length-1], true); label.innerHTML = bandwidth_label(updata[itf][updata[itf].length-1], true);
} }
if(label = document.getElementById(itf + '_download_avg')) if(label = document.getElementById(itf + '_download_avg'))
{ {
label.innerHTML = bandwidth_label(data_rx_avg, true); label.innerHTML = bandwidth_label(data_rx_avg, true);
} }
if(label = document.getElementById(itf + '_upload_avg')) if(label = document.getElementById(itf + '_upload_avg'))
{ {
label.innerHTML = bandwidth_label(data_tx_avg, true); label.innerHTML = bandwidth_label(data_tx_avg, true);
} }
if(document.getElementById(itf + '_download_peak')) if(document.getElementById(itf + '_download_peak'))
{ {
var label = document.getElementById(itf + '_download_peak'); var label = document.getElementById(itf + '_download_peak');
label.innerHTML = bandwidth_label(data_rx_peak, true); label.innerHTML = bandwidth_label(data_rx_peak, true);
} }
if(document.getElementById(itf + '_upload_peak')) if(document.getElementById(itf + '_upload_peak'))
{ {
var label = document.getElementById(itf + '_upload_peak'); var label = document.getElementById(itf + '_upload_peak');
label.innerHTML = bandwidth_label(data_tx_peak, true); label.innerHTML = bandwidth_label(data_tx_peak, true);
} }
@ -451,7 +477,7 @@
/* plot data */ /* plot data */
var pt_rx = '0,' + height; var pt_rx = '0,' + height;
var pt_tx = '0,' + height; var pt_tx = '0,' + height;
// draw polygon // draw polygon
for (var i = 0; i < uplineDnl[itf].length; i++) for (var i = 0; i < uplineDnl[itf].length; i++)
{ {
@ -459,18 +485,18 @@
y_rx = height - Math.floor((uplineDnl[itf][i]) * data_scale_dnl); y_rx = height - Math.floor((uplineDnl[itf][i]) * data_scale_dnl);
y_tx = height - Math.floor((uplineUpl[itf][i]) * data_scale_upl); y_tx = height - Math.floor((uplineUpl[itf][i]) * data_scale_upl);
pt_rx += ' ' + x + ',' + y_rx; pt_rx += ' ' + x + ',' + y_rx;
pt_tx += ' ' + x + ',' + y_tx; pt_tx += ' ' + x + ',' + y_tx;
} }
// draw last tangant point in upline graph // draw last tangant point in upline graph
pt_rx += ' ' + width + ',' + y_rx; pt_rx += ' ' + width + ',' + y_rx;
pt_rx += ' ' + width + ',' + height; pt_rx += ' ' + width + ',' + height;
pt_tx += ' ' + width + ',' + y_tx; pt_tx += ' ' + width + ',' + y_tx;
pt_tx += ' ' + width + ',' + height; pt_tx += ' ' + width + ',' + height;
// Draw polygon // Draw polygon
dnPolygons[itf].setAttribute('points', pt_rx); dnPolygons[itf].setAttribute('points', pt_rx);
upPolygons[itf].setAttribute('points', pt_tx); upPolygons[itf].setAttribute('points', pt_tx);
@ -478,9 +504,9 @@
labeldn_50.firstChild.data = bandwidth_label(1.1 * 0.50 * data_max_dnl); labeldn_50.firstChild.data = bandwidth_label(1.1 * 0.50 * data_max_dnl);
labeldn_75.firstChild.data = bandwidth_label(1.1 * 0.75 * data_max_dnl); labeldn_75.firstChild.data = bandwidth_label(1.1 * 0.75 * data_max_dnl);
labelup_25.firstChild.data = bandwidth_label(1.1 * 0.25 * data_max_upl); labelup_25.firstChild.data = bandwidth_label(1.1 * 0.25 * data_max_upl);
labelup_50.firstChild.data = bandwidth_label(1.1 * 0.50 * data_max_upl); labelup_50.firstChild.data = bandwidth_label(1.1 * 0.50 * data_max_upl);
labelup_75.firstChild.data = bandwidth_label(1.1 * 0.75 * data_max_upl); labelup_75.firstChild.data = bandwidth_label(1.1 * 0.75 * data_max_upl);
} }
} }
); );