2021-07-29 22:39:38 -04:00
|
|
|
local util = require 'util'
|
2021-07-29 22:18:29 -04:00
|
|
|
local common = require 'common'
|
|
|
|
local geometry = require 'geometry'
|
2021-07-23 01:41:06 -04:00
|
|
|
local func = require 'func'
|
2017-07-19 00:36:15 -04:00
|
|
|
|
2021-07-18 00:03:44 -04:00
|
|
|
return function(update_freq)
|
|
|
|
local PLOT_SEC_BREAK = 20
|
|
|
|
local PLOT_HEIGHT = 56
|
2021-07-18 19:27:41 -04:00
|
|
|
|
|
|
|
local get_interfaces = function()
|
2021-07-29 22:39:38 -04:00
|
|
|
local s = util.execute_cmd('realpath /sys/class/net/* | grep -v virtual')
|
2021-07-18 19:27:41 -04:00
|
|
|
local interfaces = {}
|
|
|
|
for iface in string.gmatch(s, '/([^/\n]+)\n') do
|
|
|
|
interfaces[#interfaces + 1] = iface
|
|
|
|
end
|
|
|
|
return interfaces
|
|
|
|
end
|
|
|
|
|
|
|
|
local INTERFACES = get_interfaces()
|
2021-07-17 16:43:00 -04:00
|
|
|
|
2021-07-23 01:41:06 -04:00
|
|
|
local INTERFACE_PATHS = func.map(
|
|
|
|
function(s)
|
|
|
|
local dir = string.format('/sys/class/net/%s/statistics/', s)
|
|
|
|
return {rx = dir..'rx_bytes', tx = dir..'tx_bytes'}
|
|
|
|
end,
|
|
|
|
INTERFACES
|
|
|
|
)
|
2021-07-17 16:43:00 -04:00
|
|
|
|
2021-07-19 23:58:58 -04:00
|
|
|
local get_bits = function(path)
|
2021-07-29 22:39:38 -04:00
|
|
|
return util.read_file(path, nil, '*n') * 8
|
2021-07-19 23:58:58 -04:00
|
|
|
end
|
2021-07-17 16:43:00 -04:00
|
|
|
|
2021-07-19 23:58:58 -04:00
|
|
|
local read_interfaces = function()
|
|
|
|
local rx = 0
|
|
|
|
local tx = 0
|
|
|
|
for i = 1, #INTERFACE_PATHS do
|
|
|
|
local p = INTERFACE_PATHS[i]
|
|
|
|
rx = rx + get_bits(p.rx)
|
|
|
|
tx = tx + get_bits(p.tx)
|
|
|
|
end
|
|
|
|
return rx, tx
|
|
|
|
end
|
|
|
|
|
|
|
|
local init_rx_bits, init_tx_bits = read_interfaces()
|
2021-07-05 23:27:43 -04:00
|
|
|
|
2021-07-18 00:03:44 -04:00
|
|
|
local value_format_function = function(bits)
|
2021-07-29 22:39:38 -04:00
|
|
|
local unit, value = util.convert_data_val(bits)
|
|
|
|
return util.precision_round_to_string(value, 3)..' '..unit..'b/s'
|
2021-07-18 00:03:44 -04:00
|
|
|
end
|
|
|
|
|
2021-07-29 23:04:39 -04:00
|
|
|
local make_plot = function(y, label, init)
|
|
|
|
return common.make_rate_timeseries(
|
2021-07-29 22:18:29 -04:00
|
|
|
geometry.CENTER_RIGHT_X,
|
2021-07-18 00:03:44 -04:00
|
|
|
y,
|
2021-07-29 22:18:29 -04:00
|
|
|
geometry.SECTION_WIDTH,
|
2021-07-18 00:03:44 -04:00
|
|
|
PLOT_HEIGHT,
|
|
|
|
value_format_function,
|
2021-07-29 22:18:29 -04:00
|
|
|
common.converted_y_label_format_generator('b'),
|
2021-07-18 00:03:44 -04:00
|
|
|
PLOT_SEC_BREAK,
|
|
|
|
label,
|
|
|
|
2,
|
2021-07-19 23:58:58 -04:00
|
|
|
update_freq,
|
|
|
|
init
|
2021-07-18 00:03:44 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2021-07-19 23:58:58 -04:00
|
|
|
-- header
|
2021-07-18 00:03:44 -04:00
|
|
|
|
2021-07-29 23:04:39 -04:00
|
|
|
local header = common.make_header(
|
2021-07-29 22:18:29 -04:00
|
|
|
geometry.CENTER_RIGHT_X,
|
|
|
|
geometry.TOP_Y,
|
|
|
|
geometry.SECTION_WIDTH,
|
2021-07-19 23:58:58 -04:00
|
|
|
'NETWORK'
|
2021-07-17 16:43:00 -04:00
|
|
|
)
|
2017-07-19 00:36:15 -04:00
|
|
|
|
2021-07-18 00:03:44 -04:00
|
|
|
-----------------------------------------------------------------------------
|
2021-07-19 23:58:58 -04:00
|
|
|
-- download plot
|
2021-07-18 01:17:45 -04:00
|
|
|
|
2021-07-29 23:04:39 -04:00
|
|
|
local rx = make_plot(header.bottom_y, 'Download', init_rx_bits)
|
2021-07-18 00:03:44 -04:00
|
|
|
|
2021-07-19 23:58:58 -04:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- upload plot
|
2021-07-18 01:17:45 -04:00
|
|
|
|
2021-07-19 23:58:58 -04:00
|
|
|
local TX_Y = header.bottom_y + PLOT_HEIGHT + PLOT_SEC_BREAK * 2
|
2021-07-29 23:04:39 -04:00
|
|
|
local tx = make_plot(TX_Y, 'Upload', init_tx_bits)
|
2021-07-18 01:31:51 -04:00
|
|
|
|
2021-07-19 23:58:58 -04:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- main drawing functions
|
2021-07-18 00:03:44 -04:00
|
|
|
|
2021-07-26 23:45:54 -04:00
|
|
|
local update = function()
|
2021-07-18 01:17:45 -04:00
|
|
|
local rx_bits, tx_bits = read_interfaces()
|
2021-07-29 22:18:29 -04:00
|
|
|
common.update_rate_timeseries(rx, rx_bits)
|
|
|
|
common.update_rate_timeseries(tx, tx_bits)
|
2021-07-17 00:17:22 -04:00
|
|
|
end
|
2017-07-19 00:36:15 -04:00
|
|
|
|
2021-07-17 16:43:00 -04:00
|
|
|
local draw_static = function(cr)
|
2021-07-29 23:04:39 -04:00
|
|
|
common.draw_header(cr, header)
|
2021-07-30 22:46:20 -04:00
|
|
|
common.tagged_scaled_timeseries_draw_static(rx, cr)
|
|
|
|
common.tagged_scaled_timeseries_draw_static(tx, cr)
|
2021-07-17 16:43:00 -04:00
|
|
|
end
|
|
|
|
|
2021-07-17 00:17:22 -04:00
|
|
|
local draw_dynamic = function(cr)
|
2021-07-30 22:46:20 -04:00
|
|
|
common.tagged_scaled_timeseries_draw_dynamic(rx, cr)
|
|
|
|
common.tagged_scaled_timeseries_draw_dynamic(tx, cr)
|
2021-07-17 00:17:22 -04:00
|
|
|
end
|
2018-08-05 11:22:07 -04:00
|
|
|
|
2021-07-27 23:47:26 -04:00
|
|
|
return {static = draw_static, dynamic = draw_dynamic, update = update}
|
2021-07-17 00:17:22 -04:00
|
|
|
end
|