optimize Network.lua
This commit is contained in:
parent
84e18d97ba
commit
14062c2cbd
|
@ -10,10 +10,6 @@ local __string_gmatch = string.gmatch
|
||||||
local _PLOT_SEC_BREAK_ = 20
|
local _PLOT_SEC_BREAK_ = 20
|
||||||
local _PLOT_HEIGHT_ = 56
|
local _PLOT_HEIGHT_ = 56
|
||||||
|
|
||||||
local SYSFS_NET = '/sys/class/net/'
|
|
||||||
local STATS_RX = '/statistics/rx_bytes'
|
|
||||||
local STATS_TX = '/statistics/tx_bytes'
|
|
||||||
|
|
||||||
local network_label_function = function(bytes)
|
local network_label_function = function(bytes)
|
||||||
local new_unit = util.get_unit(bytes)
|
local new_unit = util.get_unit(bytes)
|
||||||
|
|
||||||
|
@ -77,55 +73,45 @@ local upload = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
local interfaces = {}
|
local interface_counters_tbl = {}
|
||||||
|
|
||||||
local add_interface = function(iface)
|
|
||||||
local rx_path = SYSFS_NET..iface..STATS_RX
|
|
||||||
local tx_path = SYSFS_NET..iface..STATS_TX
|
|
||||||
|
|
||||||
interfaces[iface] = {
|
|
||||||
rx_path = rx_path,
|
|
||||||
tx_path = tx_path,
|
|
||||||
rx_cumulative_bytes = 0,
|
|
||||||
tx_cumulative_bytes = 0,
|
|
||||||
prev_rx_cumulative_bytes = util.read_file(rx_path, nil, '*n'),
|
|
||||||
prev_tx_cumulative_bytes = util.read_file(tx_path, nil, '*n'),
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
for iface in io.popen('ls -1 '..SYSFS_NET):lines() do
|
|
||||||
add_interface(iface)
|
|
||||||
end
|
|
||||||
|
|
||||||
local update = function(cr, update_frequency)
|
local update = function(cr, update_frequency)
|
||||||
local dspeed, uspeed = 0, 0
|
local dspeed, uspeed = 0, 0
|
||||||
local glob = util.execute_cmd('ip route show')
|
|
||||||
|
|
||||||
local rx_bps, tx_bps
|
local rx_delta, tx_delta
|
||||||
|
|
||||||
for iface in __string_gmatch(glob, 'default via %d+%.%d+%.%d+%.%d+ dev (%w+) ') do
|
-- iterate through the route file and filter on interfaces that are gateways (flag = 0003)
|
||||||
local current_iface = interfaces[iface]
|
local iterator = __string_gmatch(util.read_file('/proc/net/route'),
|
||||||
|
'(%w+)%s+%w+%s+%w+%s+0003%s+')
|
||||||
|
|
||||||
if not current_iface then
|
for interface in iterator do
|
||||||
add_interface(iface)
|
local interface_counters = interface_counters_tbl[interface]
|
||||||
current_iface = interfaces[iface]
|
|
||||||
|
if not interface_counters then
|
||||||
|
local rx_path = '/sys/class/net/'..interface..'/statistics/rx_bytes'
|
||||||
|
local tx_path = '/sys/class/net/'..interface..'/statistics/tx_bytes'
|
||||||
|
|
||||||
|
interface_counters = {
|
||||||
|
rx_path = rx_path,
|
||||||
|
tx_path = tx_path,
|
||||||
|
prev_rx_byte_cnt = util.read_file(rx_path, nil, '*n'),
|
||||||
|
prev_tx_byte_cnt = util.read_file(tx_path, nil, '*n'),
|
||||||
|
}
|
||||||
|
interface_counters_tbl[interface] = interface_counters
|
||||||
end
|
end
|
||||||
|
|
||||||
local new_rx_cumulative_bytes = util.read_file(current_iface.rx_path, nil, '*n')
|
local rx_byte_cnt = util.read_file(interface_counters.rx_path, nil, '*n')
|
||||||
local new_tx_cumulative_bytes = util.read_file(current_iface.tx_path, nil, '*n')
|
local tx_byte_cnt = util.read_file(interface_counters.tx_path, nil, '*n')
|
||||||
|
|
||||||
rx_bps = (new_rx_cumulative_bytes - current_iface.prev_rx_cumulative_bytes) * update_frequency
|
rx_delta = rx_byte_cnt - interface_counters.prev_rx_byte_cnt
|
||||||
tx_bps = (new_tx_cumulative_bytes - current_iface.prev_tx_cumulative_bytes) * update_frequency
|
tx_delta = tx_byte_cnt - interface_counters.prev_tx_byte_cnt
|
||||||
|
|
||||||
current_iface.prev_rx_cumulative_bytes = new_rx_cumulative_bytes
|
interface_counters.prev_rx_byte_cnt = rx_byte_cnt
|
||||||
current_iface.prev_tx_cumulative_bytes = new_tx_cumulative_bytes
|
interface_counters.prev_tx_byte_cnt = tx_byte_cnt
|
||||||
|
|
||||||
--mask overflow
|
-- mask overflow
|
||||||
if rx_bps < 0 then rx_bps = 0 end
|
if rx_delta > 0 then dspeed = dspeed + rx_delta * update_frequency end
|
||||||
if tx_bps < 0 then tx_bps = 0 end
|
if tx_delta > 0 then uspeed = uspeed + tx_delta * update_frequency end
|
||||||
|
|
||||||
dspeed = dspeed + rx_bps
|
|
||||||
uspeed = uspeed + tx_bps
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local dspeed_unit = util.get_unit(dspeed)
|
local dspeed_unit = util.get_unit(dspeed)
|
||||||
|
|
Loading…
Reference in New Issue