2017-07-19 00:43:44 -04:00
|
|
|
local Util = require 'Util'
|
2021-07-05 23:27:43 -04:00
|
|
|
local Common = require 'Common'
|
2021-07-16 23:25:44 -04:00
|
|
|
local Geometry = require 'Geometry'
|
2017-07-19 00:36:15 -04:00
|
|
|
|
|
|
|
local __string_gmatch = string.gmatch
|
2021-07-13 23:01:28 -04:00
|
|
|
local __math_floor = math.floor
|
2017-07-19 00:36:15 -04:00
|
|
|
|
|
|
|
local _PLOT_SEC_BREAK_ = 20
|
|
|
|
local _PLOT_HEIGHT_ = 56
|
|
|
|
|
2021-06-17 18:18:09 -04:00
|
|
|
local network_label_function = function(bits)
|
|
|
|
local new_unit, new_value = Util.convert_data_val(bits)
|
2021-07-13 23:01:28 -04:00
|
|
|
return __math_floor(new_value)..' '..new_unit..'b/s'
|
2017-07-19 00:36:15 -04:00
|
|
|
end
|
|
|
|
|
2021-07-11 20:26:02 -04:00
|
|
|
local value_format_function = function(bits)
|
|
|
|
local unit, value = Util.convert_data_val(bits)
|
|
|
|
return Util.precision_round_to_string(value, 3)..' '..unit..'b/s'
|
|
|
|
end
|
|
|
|
|
2021-07-17 16:43:00 -04:00
|
|
|
|
|
|
|
|
|
|
|
local interface_counters_tbl = {}
|
|
|
|
|
|
|
|
local get_bits = function(path)
|
|
|
|
return Util.read_file(path, nil, '*n') * 8
|
|
|
|
end
|
|
|
|
|
|
|
|
-- _PLOT_SEC_BREAK_ = nil
|
|
|
|
-- _PLOT_HEIGHT_ = nil
|
|
|
|
|
|
|
|
return function(update_freq)
|
|
|
|
local header = Common.Header(
|
|
|
|
Geometry.CENTER_RIGHT_X,
|
|
|
|
Geometry.TOP_Y,
|
|
|
|
Geometry.SECTION_WIDTH,
|
|
|
|
'NETWORK'
|
|
|
|
)
|
|
|
|
|
|
|
|
local dnload = Common.initLabeledScalePlot(
|
2021-07-16 23:25:44 -04:00
|
|
|
Geometry.CENTER_RIGHT_X,
|
2021-07-05 23:27:43 -04:00
|
|
|
header.bottom_y,
|
2021-07-16 23:25:44 -04:00
|
|
|
Geometry.SECTION_WIDTH,
|
2021-07-05 23:27:43 -04:00
|
|
|
_PLOT_HEIGHT_,
|
2021-07-11 20:26:02 -04:00
|
|
|
value_format_function,
|
2021-07-05 23:27:43 -04:00
|
|
|
network_label_function,
|
|
|
|
_PLOT_SEC_BREAK_,
|
2021-07-13 22:54:41 -04:00
|
|
|
'Download',
|
2021-07-17 16:43:00 -04:00
|
|
|
2,
|
|
|
|
update_freq
|
|
|
|
)
|
2021-07-05 23:27:43 -04:00
|
|
|
|
2021-07-17 16:43:00 -04:00
|
|
|
local upload = Common.initLabeledScalePlot(
|
2021-07-16 23:25:44 -04:00
|
|
|
Geometry.CENTER_RIGHT_X,
|
2021-07-05 23:27:43 -04:00
|
|
|
header.bottom_y + _PLOT_HEIGHT_ + _PLOT_SEC_BREAK_ * 2,
|
2021-07-16 23:25:44 -04:00
|
|
|
Geometry.SECTION_WIDTH,
|
2021-07-05 23:27:43 -04:00
|
|
|
_PLOT_HEIGHT_,
|
2021-07-11 20:26:02 -04:00
|
|
|
value_format_function,
|
2021-07-05 23:27:43 -04:00
|
|
|
network_label_function,
|
|
|
|
_PLOT_SEC_BREAK_,
|
2021-07-13 22:54:41 -04:00
|
|
|
'Upload',
|
2021-07-17 16:43:00 -04:00
|
|
|
2,
|
|
|
|
update_freq
|
|
|
|
)
|
2017-07-19 00:36:15 -04:00
|
|
|
|
2021-07-17 00:17:22 -04:00
|
|
|
local _update = function(cr)
|
|
|
|
local dspeed, uspeed = 0, 0
|
2017-07-19 00:36:15 -04:00
|
|
|
|
2021-07-17 00:17:22 -04:00
|
|
|
local rx_delta, tx_delta
|
2017-07-19 00:36:15 -04:00
|
|
|
|
2021-07-17 00:17:22 -04:00
|
|
|
-- iterate through the route file and filter on interfaces that are gateways (flag = 0003)
|
|
|
|
local iterator = __string_gmatch(Util.read_file('/proc/net/route'),
|
|
|
|
'(%w+)%s+%w+%s+%w+%s+0003%s+')
|
2017-07-19 00:36:15 -04:00
|
|
|
|
2021-07-17 00:17:22 -04:00
|
|
|
for interface in iterator do
|
|
|
|
local interface_counters = interface_counters_tbl[interface]
|
2021-06-17 18:18:09 -04:00
|
|
|
|
2021-07-17 00:17:22 -04:00
|
|
|
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'
|
2021-06-17 18:18:09 -04:00
|
|
|
|
2021-07-17 00:17:22 -04:00
|
|
|
interface_counters = {
|
|
|
|
rx_path = rx_path,
|
|
|
|
tx_path = tx_path,
|
|
|
|
prev_rx_byte_cnt = get_bits(rx_path, nil, '*n'),
|
|
|
|
prev_tx_byte_cnt = get_bits(tx_path, nil, '*n'),
|
|
|
|
}
|
|
|
|
interface_counters_tbl[interface] = interface_counters
|
|
|
|
end
|
2017-07-19 00:36:15 -04:00
|
|
|
|
2021-07-17 00:17:22 -04:00
|
|
|
local rx_byte_cnt = get_bits(interface_counters.rx_path, nil, '*n')
|
|
|
|
local tx_byte_cnt = get_bits(interface_counters.tx_path, nil, '*n')
|
2017-07-19 00:36:15 -04:00
|
|
|
|
2021-07-17 00:17:22 -04:00
|
|
|
rx_delta = rx_byte_cnt - interface_counters.prev_rx_byte_cnt
|
|
|
|
tx_delta = tx_byte_cnt - interface_counters.prev_tx_byte_cnt
|
2017-07-19 00:36:15 -04:00
|
|
|
|
2021-07-17 00:17:22 -04:00
|
|
|
interface_counters.prev_rx_byte_cnt = rx_byte_cnt
|
|
|
|
interface_counters.prev_tx_byte_cnt = tx_byte_cnt
|
2017-07-19 00:36:15 -04:00
|
|
|
|
2021-07-17 00:17:22 -04:00
|
|
|
-- mask overflow
|
|
|
|
if rx_delta > 0 then dspeed = dspeed + rx_delta * update_freq end
|
|
|
|
if tx_delta > 0 then uspeed = uspeed + tx_delta * update_freq end
|
|
|
|
end
|
2017-07-19 00:36:15 -04:00
|
|
|
|
2021-07-17 00:17:22 -04:00
|
|
|
Common.annotated_scale_plot_set(dnload, cr, dspeed)
|
|
|
|
Common.annotated_scale_plot_set(upload, cr, uspeed)
|
|
|
|
end
|
2017-07-19 00:36:15 -04:00
|
|
|
|
2021-07-17 16:43:00 -04:00
|
|
|
local draw_static = function(cr)
|
|
|
|
Common.drawHeader(cr, header)
|
|
|
|
Common.annotated_scale_plot_draw_static(dnload, cr)
|
|
|
|
Common.annotated_scale_plot_draw_static(upload, cr)
|
|
|
|
end
|
|
|
|
|
2021-07-17 00:17:22 -04:00
|
|
|
local draw_dynamic = function(cr)
|
|
|
|
_update(cr)
|
|
|
|
Common.annotated_scale_plot_draw_dynamic(dnload, cr)
|
|
|
|
Common.annotated_scale_plot_draw_dynamic(upload, cr)
|
|
|
|
end
|
2018-08-05 11:22:07 -04:00
|
|
|
|
2021-07-17 00:17:22 -04:00
|
|
|
return {static = draw_static, dynamic = draw_dynamic}
|
|
|
|
end
|