Advertisement
Guest User

Untitled

a guest
Mar 15th, 2017
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. local cur, old = {}, {}
  2.  
  3. function stat(rx_tx)
  4. local stat = io.open('/sys/devices/pci0000:00/0000:00:1c.4/0000:03:00.0/net/enp3s0/statistics/'..rx_tx..'_bytes')
  5. stat:setvbuf('no')
  6. stat:seek('set', 0)
  7. local result = stat:read()
  8. stat:close()
  9. return result
  10. end
  11.  
  12. function net_speed()
  13. if cur.rx==nil then
  14. old.rx, old.tx, old.time = 0,0,0
  15. else
  16. old.rx, old.tx, old.time = cur.rx, cur.tx, cur.time
  17. end
  18. -- os.execute("sleep 1")
  19. cur.rx, cur.tx, cur.time = stat('rx'), stat('tx'), os.time()
  20.  
  21. local time_diff = (cur.time - old.time)
  22. local rx_kib, tx_kib = 0, 0
  23.  
  24. if time_diff > 0 then
  25. local rx_diff = (cur.rx - old.rx)
  26. local tx_diff = (cur.tx - old.tx)
  27. rx_kib = (rx_diff / time_diff / 1024)
  28. tx_kib = (tx_diff / time_diff / 1024)
  29.  
  30. if rx_kib > 1024 then
  31. rx_kib = (rx_kib / 1024)
  32. rx_kib = string.format('%3.1f %s', rx_kib*8, 'Mb')
  33. else
  34. rx_kib = string.format('%3.1f %s', rx_kib*8, 'Kb')
  35. end
  36.  
  37. if tx_kib > 1024 then
  38. tx_kib = (tx_kib / 1024)
  39. tx_kib = string.format('%3.1f %s', tx_kib*8, 'Mb')
  40. else
  41. tx_kib = string.format('%3.1f %s', tx_kib*8, 'Kb')
  42. end
  43. else
  44. rx_rate = '???'
  45. tx_rate = '???'
  46. end
  47. local result = rx_kib..' '..tx_kib
  48. return result
  49. end
  50.  
  51.  
  52. --[[
  53. widget = {
  54. plugin = 'timer',
  55. opts = {period = 2},
  56. cb = function()
  57. return {full_text=net_speed(), color='#00e0ff'}
  58. end,
  59. }]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement