Advertisement
Guest User

Untitled

a guest
Mar 15th, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 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. local result = stat:read()
  7. stat:close()
  8. return result
  9. end
  10.  
  11. function net_speed()
  12.  
  13. old.rx, old.tx, old.time = stat('rx'), stat('tx'), os.time()
  14. os.execute("sleep 1")
  15. cur.rx, cur.tx, cur.time = stat('rx'), stat('tx'), os.time()
  16.  
  17. local time_diff = (cur.time - old.time)
  18.  
  19. -- local rx_diff, tx_diff, rx_rate, tx_rate = 0, 0, 0, 0
  20.  
  21.  
  22. if time_diff > 0 then
  23. local rx_diff = (cur.rx - old.rx)
  24. local tx_diff = (cur.tx - old.tx)
  25. local rx_rate = (rx_diff / time_diff)
  26. local tx_rate = (tx_diff / time_diff)
  27.  
  28. if rx_rate > (1024^2) then
  29. rx_kib = (rx_rate / (1024^2))
  30. rx_rate = string.format('%3.2f %s', rx_kib, 'M')
  31. else
  32. rx_rate = string.format('%3.2f %s', rx_kib, 'K')
  33. end
  34.  
  35. -- tx_kib = (tx_rate >> 10)
  36. if tx_rate > (1024^2) then
  37. local tx_kib = (tx_rate / (1024^2))
  38. tx_rate = string.format('%3.2f %s', tx_kib, 'M')
  39. else
  40. tx_rate = string.format('%3.2f %s', tx_kib, 'K')
  41. end
  42. else
  43. rx_rate = string.format('%s', '???')
  44. tx_rate = string.format('%s', '???')
  45. end
  46. local result = rx_rate..' '..tx_rate
  47. return result
  48. end
  49. net_speed()
  50.  
  51. --[[
  52. widget = {
  53. plugin = 'timer',
  54. opts = {period = 2},
  55. cb = function()
  56. return {full_text=net_speed(), color='#00e0ff'}
  57. end,
  58. }]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement