ffho-debug 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #!/usr/bin/lua
  2. debugdata = ""
  3. ATH9K_DEBUGFS_DIR="/sys/kernel/debug/ieee80211/phy0/ath9k"
  4. PATH_DBG_REPORT='/tmp/debug-report.txt'
  5. local nixio = require('nixio'), require('nixio.util')
  6. local util = require("luci.util")
  7. local fs = require("nixio.fs")
  8. local ltn12 = require 'luci.ltn12'
  9. local sys = require("luci.sys")
  10. local json = require("luci.jsonc")
  11. local platform_info = require("platform_info")
  12. local site = require 'gluon.site_config'
  13. local ip = require 'luci.ip'
  14. local uci = require('luci.model.uci').cursor()
  15. -- some usefull functions
  16. local hostname = sys.hostname()
  17. local model = platform_info.get_model()
  18. local release = util.trim(fs.readfile("/lib/gluon/release") or "")
  19. local version = util.trim(fs.readfile("/lib/gluon/gluon-version") or "")
  20. local primary_mac = require('gluon.sysconfig').primary_mac
  21. local contact = uci:get_first('gluon-node-info', 'owner', 'contact', '')
  22. if contact == '' then
  23. contact = "none"
  24. end
  25. local autoupdater = uci:get('autoupdater', 'settings', 'branch')
  26. if uci:get_bool('autoupdater', 'settings', 'enabled') == false then
  27. autoupdater = "disabled (" .. autoupdater .. ")"
  28. end
  29. local addresses = ""
  30. for line in io.lines('/proc/net/if_inet6') do
  31. local matches = { line:match('^' .. string.rep('(%x%x%x%x)', 8) .. string.rep(' %x%x', 4) .. '%s+([^%s]+)$') }
  32. if matches[9] == 'br-client' then
  33. addresses = addresses .. " " .. ip.IPv6(string.format('%s:%s:%s:%s:%s:%s:%s:%s', unpack(matches))):string():lower() .. "\n"
  34. end
  35. end
  36. local data = io.open('/proc/meminfo'):read('*a')
  37. local fields = {}
  38. for k, v in data:gmatch('([^\n:]+):%s*(%d+) kB') do
  39. fields[k] = tonumber(v)
  40. end
  41. local function location()
  42. local text = 'none'
  43. local locationid = uci:get_first('gluon-node-info', 'location')
  44. if locationid then
  45. local location = uci:get_all('gluon-node-info', locationid)
  46. if uci:get_bool('gluon-node-info', locationid, 'share_location') and location.latitude and location.longitude then
  47. text = location.latitude .. ', ' .. location.longitude
  48. end
  49. end
  50. return text
  51. end
  52. local function ip_proto(address)
  53. if address:match("%.") then
  54. return "IPv4"
  55. end
  56. if address:match(":") then
  57. return "IPv6"
  58. end
  59. return "???"
  60. end
  61. -- wrapper for calling systemcommands
  62. local function cmd(_command)
  63. local f = io.popen(_command)
  64. local l = f:read("*a")
  65. f:close()
  66. return "# " .. _command .. ": \n" .. l .. "\n"
  67. end
  68. -- read contents of a given file
  69. local function readFile(_file)
  70. local f = io.open(_file, "r")
  71. if f~=nil then
  72. local l = f:read("*a")
  73. f:close()
  74. return "--- Content of file " .. _file .. " ---\n" .. l .. "\n\n"
  75. else
  76. return ""
  77. end
  78. end
  79. -- Should we enter local mode?
  80. localMode = false
  81. if arg[1] == '-l' then
  82. localMode = true
  83. end
  84. -- search for existing debug report
  85. local oldReport = nixio.open(PATH_DBG_REPORT, "r")
  86. if oldReport==nil then
  87. -- no existing debugreport, let's generate a new one
  88. -- inform the User ;)
  89. print('-- Hello, Mr. Dillinger. Thank you for coming back early.')
  90. print('-- Sit right there; make yourself comfortable. Remember the time we spent play chess together?')
  91. -- first of all, collect some generic information about the system
  92. debugdata = debugdata .. "---- BEGIN SYSTEM INFORMATION ----\n"
  93. debugdata = debugdata .. "Hostname: " .. hostname .. "\n"
  94. debugdata = debugdata .. "Community: " .. site.site_name .. "\n"
  95. debugdata = debugdata .. "Model: " .. model .. "\n"
  96. debugdata = debugdata .. "Firmware: " .. release .. " / " .. version .. "\n"
  97. debugdata = debugdata .. "MAC: " .. primary_mac .. "\n"
  98. debugdata = debugdata .. "Contact: " .. contact .. "\n"
  99. debugdata = debugdata .. "Uptime: " .. util.trim(sys.exec("uptime | sed 's/^ \+//'")) .. "\n"
  100. debugdata = debugdata .. "Autoupdater: " .. autoupdater .. "\n"
  101. debugdata = debugdata .. "Location: " .. location() .. "\n"
  102. debugdata = debugdata .. "IPs: " .. util.trim(addresses) .. "\n"
  103. debugdata = debugdata .. "Memory: " .. string.format("%.1f %% used, %.1f %% free\n",(fields.MemTotal-fields.MemFree)/fields.MemTotal*100,fields.MemFree/fields.MemTotal*100)
  104. debugdata = debugdata .. cmd("ps w")
  105. debugdata = debugdata .. "---- END SYSTEM INFORMATION ----\n\n"
  106. -- show uci variables
  107. debugdata = debugdata .. "---- BEGIN UCI VARIABLES ----\n"
  108. debugdata = debugdata .. cmd("uci show | grep -v '\.key' | grep -v '\.secret'")
  109. debugdata = debugdata .. "---- END UCI VARIABLES ----\n\n"
  110. -- show cron jobs
  111. debugdata = debugdata .. "---- BEGIN CRON JOBS ----\n"
  112. debugdata = debugdata .. cmd("grep -r \"\" /usr/lib/micron.d/ | sed -e \"s/:/:\\n/\"")
  113. debugdata = debugdata .. "---- END CRON JOBS ----\n\n"
  114. -- now get some information about the network status
  115. debugdata = debugdata .. "---- BEGIN IP AND ROUTUNG INFORMATION ----\n"
  116. debugdata = debugdata .. cmd("ip addr show")
  117. debugdata = debugdata .. cmd("ip route show")
  118. debugdata = debugdata .. cmd("ip -6 route show")
  119. debugdata = debugdata .. "---- BEGIN IP AND ROUTUNG INFORMATION ----\n\n"
  120. -- get wireless status
  121. debugdata = debugdata .. "---- BEGIN WIRELESS INFORMATION ----\n"
  122. debugdata = debugdata .. cmd("iwinfo ibss0 info 2>&1")
  123. debugdata = debugdata .. cmd("iwinfo client0 info 2>&1")
  124. debugdata = debugdata .. cmd("iwinfo ibss1 info 2>&1")
  125. debugdata = debugdata .. cmd("iwinfo client1 info 2>&1")
  126. debugdata = debugdata .. cmd("iwinfo ibss0 assoclist 2>&1")
  127. debugdata = debugdata .. cmd("iwinfo ibss1 assoclist 2>&1")
  128. debugdata = debugdata .. "---- END WIRELESS INFORMATION ----\n\n"
  129. -- get batman status
  130. debugdata = debugdata .. "---- BEGIN BATMAN STATUS ----\n"
  131. debugdata = debugdata .. cmd("batctl gwl")
  132. debugdata = debugdata .. cmd("batctl tl")
  133. debugdata = debugdata .. "---- END BATMAN STATUS ----\n\n"
  134. -- get fastd status
  135. debugdata = debugdata .. "---- BEGIN FASTD STATUS ----\n"
  136. if string.len(util.exec("ip -f inet address show dev br-wan | grep global")) >= 2 then
  137. debugdata = debugdata .. "IPv4 configured\n"
  138. else
  139. debugdata = debugdata .. "IPv4 not configured\n"
  140. end
  141. if string.len(util.exec("ip -f inet6 address show dev br-wan | grep global")) >= 2 then
  142. debugdata = debugdata .. "IPv6 configured\n"
  143. else
  144. debugdata = debugdata .. "IPv6 not configured\n"
  145. end
  146. local stat, fastd_status = pcall(
  147. function()
  148. local fastd_sock = nixio.socket('unix', 'stream')
  149. assert(fastd_sock:connect('/var/run/fastd.mesh_vpn.socket'))
  150. decoder = json.new()
  151. ltn12.pump.all(ltn12.source.file(fastd_sock), decoder:sink())
  152. return decoder:get()
  153. end
  154. )
  155. if stat then
  156. debugdata = debugdata .. string.format("fastd running for %.3f seconds\n", fastd_status.uptime/1000)
  157. local peers = 0
  158. local connections = 0
  159. for key, peer in pairs(fastd_status.peers) do
  160. peers = peers+1
  161. if peer.connection then
  162. connections = connections+1
  163. end
  164. end
  165. debugdata = debugdata .. string.format("There are %i peers configured, of which %i are connected:\n", peers, connections)
  166. for key, peer in pairs(fastd_status.peers) do
  167. debugdata = debugdata .. peer.name .. ": "
  168. if peer.connection then
  169. debugdata = debugdata .. string.format("connected for %.3f seconds via %s\n", peer.connection.established/1000, ip_proto(peer.address))
  170. else
  171. debugdata = debugdata .. "not connected\n"
  172. end
  173. end
  174. else
  175. debugdata = debugdata .. "fastd not running\n"
  176. end
  177. debugdata = debugdata .. "---- END FASTD STATUS ----\n\n"
  178. -- get log
  179. debugdata = debugdata .. "---- BEGIN LOGREAD ----\n"
  180. debugdata = debugdata .. cmd("logread")
  181. debugdata = debugdata .. "---- END LOGREAD ----\n\n"
  182. debugdata = debugdata .. "---- BEGIN DMESG KERNEL LOG ----\n"
  183. debugdata = debugdata .. cmd("dmesg")
  184. debugdata = debugdata .. "---- END DMESG KERNEL LOG ----\n\n"
  185. else
  186. print('Orphaned debug-report file found.')
  187. print('-- You wouldn\'t want me to dig up Flynn\'s file and read it up on a VDT at The Times, would you?')
  188. debugdata = oldReport:readall()
  189. oldReport:close()
  190. end
  191. -- if local mode is requested print the report, otherwise send it to the admin team
  192. siteConfig = require("gluon.site_config")
  193. if localMode then
  194. print('Omitting to send the report data to a report-server')
  195. print('-- Sark! All of my functions are now yours. Take them!:')
  196. print(debugdata)
  197. nixio.fs.unlink(PATH_DBG_REPORT)
  198. else
  199. local nixio = require('nixio'), require('nixio.util')
  200. print('-- My User has information that could... that could make this a free system again!')
  201. local sent = 0
  202. local reportname = nil
  203. local port = siteConfig.debugserver.port
  204. for host in ipairs(siteConfig.debugserver.host) do
  205. print('Trying to deliver debug-report to: ' .. host)
  206. local sock = nixio.connect(host, port, "inet6", "stream")
  207. if sock then
  208. sock:setopt('socket', 'sndtimeo', 30.0)
  209. sock:setopt('socket', 'rcvtimeo', 30.0)
  210. sent = sock:writeall(debugdata)
  211. if sent == debugdata:len() then
  212. -- half-side close to indicate the end of our transmission
  213. sock:shutdown('wr')
  214. print('Transmission succeeded. Waiting for report-name.')
  215. reportname = sock:readall(256)
  216. end
  217. sock:close()
  218. if reportname ~= nil then break end
  219. end
  220. end
  221. if reportname ~= nil then
  222. print('\nYour report has been stored at the debug-server with the name: ' .. reportname)
  223. print('-- With the information I can access, I can run things 900 to 1200 times better than any human.')
  224. else
  225. print('Sorry, I couldn\'t send the report.')
  226. print('-- If you are a User, then everything you\'ve done so far has been according to a plan, right?')
  227. print('-- I will try it again the next time you run me. See you soon ...')
  228. local f = nixio.open(PATH_DBG_REPORT, 'w')
  229. f:writeall(debugdata)
  230. f:close()
  231. end
  232. end
  233. print("-- You've almost reached your decision gate, and I cannot spare you any more time. End of Line.")
  234. os.exit(0)