ffho-debug 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #!/usr/bin/lua
  2. debugdata = ""
  3. PATH_DBG_REPORT='/tmp/debug-report.txt'
  4. local nixio = require('nixio'), require('nixio.util')
  5. local util = require("luci.util")
  6. local fs = require("nixio.fs")
  7. local ltn12 = require 'luci.ltn12'
  8. local sys = require("luci.sys")
  9. local json = require("luci.jsonc")
  10. local platform_info = require("platform_info")
  11. local site = require 'gluon.site_config'
  12. local ip = require 'luci.ip'
  13. local uci = require('luci.model.uci').cursor()
  14. -- some usefull functions
  15. local hostname = sys.hostname()
  16. local model = platform_info.get_model()
  17. local release = util.trim(fs.readfile("/lib/gluon/release") or "")
  18. local version = util.trim(fs.readfile("/lib/gluon/gluon-version") or "")
  19. local primary_mac = require('gluon.sysconfig').primary_mac
  20. local contact = uci:get_first('gluon-node-info', 'owner', 'contact', '')
  21. if contact == '' then
  22. contact = "none"
  23. end
  24. local autoupdater = uci:get('autoupdater', 'settings', 'branch')
  25. if uci:get_bool('autoupdater', 'settings', 'enabled') == false then
  26. autoupdater = "disabled (" .. autoupdater .. ")"
  27. end
  28. local addresses = ""
  29. for line in io.lines('/proc/net/if_inet6') do
  30. local matches = { line:match('^' .. string.rep('(%x%x%x%x)', 8) .. string.rep(' %x%x', 4) .. '%s+([^%s]+)$') }
  31. if matches[9] == 'local-node' then
  32. addresses = addresses .. " " .. ip.IPv6(string.format('%s:%s:%s:%s:%s:%s:%s:%s', unpack(matches))):string():lower() .. "\n"
  33. end
  34. end
  35. local data = io.open('/proc/meminfo'):read('*a')
  36. local fields = {}
  37. for k, v in data:gmatch('([^\n:]+):%s*(%d+) kB') do
  38. fields[k] = tonumber(v)
  39. end
  40. local function location()
  41. local text = 'none'
  42. local locationid = uci:get_first('gluon-node-info', 'location')
  43. if locationid then
  44. local location = uci:get_all('gluon-node-info', locationid)
  45. if uci:get_bool('gluon-node-info', locationid, 'share_location') and location.latitude and location.longitude then
  46. text = location.latitude .. ', ' .. location.longitude
  47. end
  48. end
  49. return text
  50. end
  51. local function ip_proto(address)
  52. if address:match("%.") then
  53. return "IPv4"
  54. end
  55. if address:match(":") then
  56. return "IPv6"
  57. end
  58. return "???"
  59. end
  60. -- wrapper for calling systemcommands
  61. local function cmd(_command)
  62. local f = io.popen(_command)
  63. local l = f:read("*a")
  64. f:close()
  65. return "# " .. _command .. ": \n" .. l .. "\n"
  66. end
  67. -- read contents of a given file
  68. local function readFile(_file)
  69. local f = io.open(_file, "r")
  70. if f~=nil then
  71. local l = f:read("*a")
  72. f:close()
  73. return "--- Content of file " .. _file .. " ---\n" .. l .. "\n\n"
  74. else
  75. return ""
  76. end
  77. end
  78. -- Should we enter local mode?
  79. localMode = false
  80. if arg[1] == '-l' then
  81. localMode = true
  82. end
  83. -- search for existing debug report
  84. local oldReport = nixio.open(PATH_DBG_REPORT, "r")
  85. if oldReport==nil then
  86. -- no existing debugreport, let's generate a new one
  87. -- inform the User ;)
  88. print('-- Hello, Mr. Dillinger. Thank you for coming back early.')
  89. print('-- Sit right there; make yourself comfortable. Remember the time we spent play chess together?')
  90. -- first of all, collect some generic information about the system
  91. debugdata = debugdata .. "---- BEGIN SYSTEM INFORMATION ----\n"
  92. debugdata = debugdata .. "Hostname: " .. hostname .. "\n"
  93. debugdata = debugdata .. "Community: " .. site.site_name .. "\n"
  94. debugdata = debugdata .. "Model: " .. model .. "\n"
  95. debugdata = debugdata .. "Firmware: " .. release .. " / " .. version .. "\n"
  96. debugdata = debugdata .. "MAC: " .. primary_mac .. "\n"
  97. debugdata = debugdata .. "Contact: " .. contact .. "\n"
  98. debugdata = debugdata .. "Uptime: " .. util.trim(sys.exec("uptime | sed 's/^ \+//'")) .. "\n"
  99. debugdata = debugdata .. "Autoupdater: " .. autoupdater .. "\n"
  100. debugdata = debugdata .. "Location: " .. location() .. "\n"
  101. debugdata = debugdata .. "IPs: " .. util.trim(addresses) .. "\n"
  102. debugdata = debugdata .. "Memory: " .. string.format("%.1f %% used, %.1f %% free\n",(fields.MemTotal-fields.MemFree)/fields.MemTotal*100,fields.MemFree/fields.MemTotal*100)
  103. debugdata = debugdata .. cmd("ps w")
  104. debugdata = debugdata .. "---- END SYSTEM INFORMATION ----\n\n"
  105. -- show uci variables
  106. debugdata = debugdata .. "---- BEGIN UCI VARIABLES ----\n"
  107. debugdata = debugdata .. cmd("uci show | grep -v '\.key' | grep -v '\.secret'")
  108. debugdata = debugdata .. "---- END UCI VARIABLES ----\n\n"
  109. -- show cron jobs
  110. debugdata = debugdata .. "---- BEGIN CRON JOBS ----\n"
  111. debugdata = debugdata .. cmd("grep -r \"\" /usr/lib/micron.d/ | sed -e \"s/:/:\\n/\"")
  112. debugdata = debugdata .. "---- END CRON JOBS ----\n\n"
  113. -- now get some information about the network status
  114. debugdata = debugdata .. "---- BEGIN IP AND ROUTUNG INFORMATION ----\n"
  115. debugdata = debugdata .. cmd("ip addr show")
  116. debugdata = debugdata .. cmd("ip route show")
  117. debugdata = debugdata .. cmd("ip -6 route show")
  118. debugdata = debugdata .. "---- BEGIN IP AND ROUTUNG INFORMATION ----\n\n"
  119. -- get wireless status
  120. debugdata = debugdata .. "---- BEGIN WIRELESS INFORMATION ----\n"
  121. local interfaces = util.split(util.trim(util.exec("iw dev | grep Interface | cut -d' ' -f2")))
  122. for _, ifname in ipairs(interfaces) do
  123. debugdata = debugdata .. cmd("iwinfo " .. ifname .. " info 2>&1")
  124. if ifname:match('ibss') or ifname:match('mesh') then
  125. debugdata = debugdata .. cmd("iwinfo " .. ifname .. " assoclist 2>&1")
  126. end
  127. end
  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. if localMode then
  193. print('Omitting to send the report data to a report-server')
  194. print('-- Sark! All of my functions are now yours. Take them!:')
  195. print(debugdata)
  196. nixio.fs.unlink(PATH_DBG_REPORT)
  197. else
  198. local nixio = require('nixio'), require('nixio.util')
  199. print('-- My User has information that could... that could make this a free system again!')
  200. local sent = 0
  201. local reportname = nil
  202. local port = site.debugserver.port
  203. for _, host in ipairs(site.debugserver.host) do
  204. print('Trying to deliver debug-report to: ' .. host)
  205. local sock = nixio.connect(host, port, "inet6", "stream")
  206. if sock then
  207. sock:setopt('socket', 'sndtimeo', 30.0)
  208. sock:setopt('socket', 'rcvtimeo', 30.0)
  209. sent = sock:writeall(debugdata)
  210. if sent == debugdata:len() then
  211. -- half-side close to indicate the end of our transmission
  212. sock:shutdown('wr')
  213. print('Transmission succeeded. Waiting for report-name.')
  214. reportname = sock:readall(256)
  215. end
  216. sock:close()
  217. if reportname ~= nil then break end
  218. end
  219. end
  220. if reportname ~= nil then
  221. print('\nYour report has been stored at the debug-server with the name: ' .. reportname)
  222. print('-- With the information I can access, I can run things 900 to 1200 times better than any human.')
  223. else
  224. print('Sorry, I couldn\'t send the report.')
  225. print('-- If you are a User, then everything you\'ve done so far has been according to a plan, right?')
  226. print('-- I will try it again the next time you run me. See you soon ...')
  227. local f = nixio.open(PATH_DBG_REPORT, 'w')
  228. f:writeall(debugdata)
  229. f:close()
  230. end
  231. end
  232. print("-- You've almost reached your decision gate, and I cannot spare you any more time. End of Line.")
  233. os.exit(0)