Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local cur, old = {}, {}
- function stat(rx_tx)
- local stat = io.open('/sys/devices/pci0000:00/0000:00:1c.4/0000:03:00.0/net/enp3s0/statistics/'..rx_tx..'_bytes')
- stat:setvbuf('no')
- local result = stat:read()
- stat:close()
- return result
- end
- function net_speed()
- old.rx, old.tx, old.time = stat('rx'), stat('tx'), os.time()
- os.execute("sleep 1")
- cur.rx, cur.tx, cur.time = stat('rx'), stat('tx'), os.time()
- local time_diff = (cur.time - old.time)
- -- local rx_diff, tx_diff, rx_rate, tx_rate = 0, 0, 0, 0
- if time_diff > 0 then
- local rx_diff = (cur.rx - old.rx)
- local tx_diff = (cur.tx - old.tx)
- local rx_rate = (rx_diff / time_diff)
- local tx_rate = (tx_diff / time_diff)
- if rx_rate > (1024^2) then
- rx_kib = (rx_rate / (1024^2))
- rx_rate = string.format('%3.2f %s', rx_kib, 'M')
- else
- rx_rate = string.format('%3.2f %s', rx_kib, 'K')
- end
- -- tx_kib = (tx_rate >> 10)
- if tx_rate > (1024^2) then
- local tx_kib = (tx_rate / (1024^2))
- tx_rate = string.format('%3.2f %s', tx_kib, 'M')
- else
- tx_rate = string.format('%3.2f %s', tx_kib, 'K')
- end
- else
- rx_rate = string.format('%s', '???')
- tx_rate = string.format('%s', '???')
- end
- local result = rx_rate..' '..tx_rate
- return result
- end
- net_speed()
- --[[
- widget = {
- plugin = 'timer',
- opts = {period = 2},
- cb = function()
- return {full_text=net_speed(), color='#00e0ff'}
- end,
- }]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement