Advertisement
Guest User

Untitled

a guest
Apr 9th, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. -- Copy file here ==> /lib/gluon/announce/nodeinfo.d/wifistats
  2.  
  3. SAVE_FILE = "/tmp/wifiutilization.txt"
  4. INTERFACE = "mesh0"
  5.  
  6. function execute_command(command)
  7. cmd = assert (io.popen (command))
  8. local output = ""
  9. for line in cmd:lines() do
  10. output = output..'\n'..line
  11. end -- for loop
  12. cmd:close()
  13. return output
  14. end
  15.  
  16.  
  17. function get_last_data()
  18. local filehandle,err = io.open(SAVE_FILE,"r")
  19. if err then return false; end
  20.  
  21. local line = filehandle:read('*l')
  22. if line then
  23. local _, _, active_time, busy_time = string.find(line,"^([0-9]*) ([0-9]*)$")
  24. if active_time and busy_time then
  25. filehandle:close()
  26. return {active_time=tonumber(active_time), busy_time=tonumber(busy_time)}
  27. else
  28. filehandle:close()
  29. return false
  30. end
  31. else
  32. filehandle:close()
  33. return false
  34. end
  35. end
  36.  
  37.  
  38. function get_current_data()
  39. local output = execute_command("iw dev "..INTERFACE.." survey dump")
  40. local data = {}
  41. local _, _, active_time, busy_time = string.find(output,'%[.-%]\n.-\n.-([0-9]-) ms\n.-([0-9]-) ms')
  42. if active_time and busy_time then
  43. return {active_time=tonumber(active_time), busy_time=tonumber(busy_time)}
  44. else
  45. return false
  46. end
  47. -- retry / transmitted_frame_count * 100
  48. local output = execute_command("cat /sys/kernel/debug/ieee80211/phy0/statistics/transmitted_frame_count")
  49. local _, _, tx_frame_count = string.find(output,'^([0-9]-)$')
  50.  
  51. local output = execute_command("cat /sys/kernel/debug/ieee80211/phy0/statistics/retry_count")
  52. local _, _, retry_count = string.find(output,'^([0-9]-)$')
  53.  
  54. local output = execute_command("cat /sys/kernel/debug/ieee80211/phy0/statistics/multiple_retry_count")
  55. local _, _, multiple_retry_count = string.find(output,'^([0-9]-)$')
  56.  
  57. local output = execute_command("cat /sys/kernel/debug/ieee80211/phy0/statistics/failed_count")
  58. local _, _, failed_count = string.find(output,'^([0-9]-)$')
  59.  
  60. local output = execute_command("cat /sys/kernel/debug/ieee80211/phy0/statistics/frame_duplicate_count")
  61. local _, _, frame_duplicate_count = string.find(output,'^([0-9]-)$')
  62.  
  63. end
  64.  
  65.  
  66. function set_data(data)
  67. local filehandle,err = io.open(SAVE_FILE,"w+")
  68. if err then return false; end
  69.  
  70. filehandle:write(data["active_time"].." "..data["busy_time"].."\n")
  71. filehandle:flush()
  72. filehandle:close()
  73. return true
  74. end
  75.  
  76.  
  77.  
  78. current_data = get_current_data()
  79.  
  80. if current_data then
  81. last_data = get_last_data()
  82. if last_data then
  83. local last_minute = {active=(current_data["active_time"] - last_data["active_time"]), busy=(current_data["busy_time"] - last_data["busy_time"])}
  84. last_minute["utilization"] = string.format("%.2f", last_minute["busy"] / last_minute["active"])
  85. set_data(current_data)
  86. return{total={active=current_data["active_time"], busy=current_data["busy_time"], utilization=string.format("%.2f", current_data["busy_time"] / current_data["active_time"])}, last_minute=last_minute}
  87. end
  88. set_data(current_data)
  89. end
  90.  
  91.  
  92.  
  93.  
  94. --Sample data
  95. --Survey data from mesh0
  96. -- frequency: 2412 MHz [in use]
  97. -- noise: -95 dBm
  98. -- channel active time: 785305812 ms
  99. -- channel busy time: 197674023 ms
  100. -- channel receive time: 175420195 ms
  101. -- channel transmit time: 16634930 ms
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement