status 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #!/usr/bin/lua
  2. local util = require("luci.util")
  3. local fs = require("nixio.fs")
  4. local ltn12 = require 'luci.ltn12'
  5. local sys = require("luci.sys")
  6. local json = require("luci.jsonc")
  7. local nixio = require 'nixio'
  8. local platform_info = require("platform_info")
  9. local site = require 'gluon.site_config'
  10. local ip = require 'luci.ip'
  11. local uci = require('luci.model.uci').cursor()
  12. local hostname = sys.hostname()
  13. local model = platform_info.get_model()
  14. local release = util.trim(fs.readfile("/lib/gluon/release") or "")
  15. local primary_mac = require('gluon.sysconfig').primary_mac
  16. local nodeid = require('gluon.util').node_id()
  17. local contact = uci:get_first('gluon-node-info', 'owner', 'contact', '')
  18. if contact == '' then
  19. contact = "none"
  20. end
  21. local autoupdater = uci:get('autoupdater', 'settings', 'branch')
  22. if uci:get_bool('autoupdater', 'settings', 'enabled') == false then
  23. autoupdater = "disabled (" .. autoupdater .. ")"
  24. end
  25. local addresses = ""
  26. for line in io.lines('/proc/net/if_inet6') do
  27. local matches = { line:match('^' .. string.rep('(%x%x%x%x)', 8) .. string.rep(' %x%x', 4) .. '%s+([^%s]+)$') }
  28. if matches[9] == 'br-client' then
  29. addresses = addresses .. " " .. ip.IPv6(string.format('%s:%s:%s:%s:%s:%s:%s:%s', unpack(matches))):string():lower() .. "\n"
  30. end
  31. end
  32. local data = io.open('/proc/meminfo'):read('*a')
  33. local fields = {}
  34. for k, v in data:gmatch('([^\n:]+):%s*(%d+) kB') do
  35. fields[k] = tonumber(v)
  36. end
  37. function location()
  38. local text = 'none'
  39. local locationid = uci:get_first('gluon-node-info', 'location')
  40. if locationid then
  41. local location = uci:get_all('gluon-node-info', locationid)
  42. if uci:get_bool('gluon-node-info', locationid, 'share_location') and location.latitude and location.longitude then
  43. text = location.latitude .. ', ' .. location.longitude
  44. end
  45. end
  46. if (site.status_page or {}).location_link then
  47. text = '<a href=' .. site.status_page.location_link .. nodeid .. '>' .. text .. '</a>'
  48. end
  49. return text
  50. end
  51. function escape_html(s)
  52. return (s:gsub('&', '&amp;'):gsub('<', '&lt;'):gsub('>', '&gt;'):gsub('"', '&quot;'))
  53. end
  54. function neighbours(ifname)
  55. local info = util.exec("gluon-neighbour-info -d ff02::2:1001 -p 1001 -r nodeinfo -t 3 -i " .. ifname)
  56. local macs = {}
  57. for _, line in ipairs(util.split(info)) do
  58. local data = json.parse(line)
  59. if data then
  60. local function add_macs(list)
  61. if list then
  62. for _, mac in ipairs(list) do
  63. macs[mac] = data
  64. end
  65. end
  66. end
  67. if data["network"] then
  68. add_macs(data["network"]["mesh_interfaces"])
  69. if data["network"]["mesh"] and data["network"]["mesh"]["bat0"] and
  70. data["network"]["mesh"]["bat0"]["interfaces"] then
  71. local interfaces = data["network"]["mesh"]["bat0"]["interfaces"]
  72. add_macs(interfaces["other"])
  73. add_macs(interfaces["wireless"])
  74. add_macs(interfaces["tunnel"])
  75. end
  76. end
  77. end
  78. end
  79. return macs
  80. end
  81. io.write("Content-type: text/html\n\n")
  82. io.write("<!DOCTYPE html>\n")
  83. io.write("<html>")
  84. io.write("<head>")
  85. io.write("<meta charset=\"utf-8\"/>")
  86. io.write("<script src=\"/status.js\"></script>")
  87. io.write("<title>" .. escape_html(hostname) .. "</title>")
  88. io.write("</head>")
  89. io.write("<body>")
  90. io.write("<h1>" .. escape_html(hostname) .. "</h1>")
  91. io.write("<pre>")
  92. io.write("Community: " .. escape_html(site.site_name) .. "\n")
  93. io.write("Model: " .. escape_html(model) .. "\n")
  94. io.write("Firmware: " .. escape_html(release) .. "\n")
  95. io.write("MAC: " .. escape_html(primary_mac) .. "\n")
  96. io.write("Contact: " .. escape_html(contact) .. "\n")
  97. io.write("Uptime: " .. escape_html(util.trim(sys.exec("uptime | sed 's/^ \+//'"))) .. "\n")
  98. io.write("Autoupdater: " .. escape_html(autoupdater) .. "\n")
  99. io.write("Location: " .. location() .. "\n")
  100. io.write("IPs: " .. escape_html(util.trim(addresses)) .. "\n")
  101. io.write("Memory: " .. string.format("%.1f %% used, %.1f %% free",(fields.MemTotal-fields.MemFree)/fields.MemTotal*100,fields.MemFree/fields.MemTotal*100) .. "\n")
  102. io.write("</pre>")
  103. io.write("<h2>Neighbours</h2>")
  104. local interfaces = util.split(util.trim(util.exec("iw dev | egrep 'type IBSS|type mesh' -B 5 | grep Interface | cut -d' ' -f2")))
  105. for _, ifname in ipairs(interfaces) do
  106. io.write("<h3>" .. escape_html(ifname) .. "</h3>")
  107. io.write("<pre>")
  108. local peer=false
  109. for _, line in ipairs(util.split(util.exec("iw dev " .. ifname .. " station dump"))) do
  110. local mac = line:match("^Station (.*) %(on ")
  111. if mac then
  112. io.write("Station <a id=\"" .. escape_html(ifname) .. "-" .. mac .. "\">" .. mac .. "</a> (on " .. escape_html(ifname) .. ")\n")
  113. peer = true
  114. else
  115. io.write(escape_html(line) .. "\n")
  116. end
  117. end
  118. if peer == false then
  119. io.write("no peers connected")
  120. end
  121. io.write("</pre>")
  122. end
  123. io.write("<h2>VPN status</h2>")
  124. io.write("<pre>")
  125. if string.len(util.exec("ip -f inet address show dev br-wan | grep global")) >= 2 then
  126. io.write("IPv4 configured\n")
  127. else
  128. io.write("IPv4 not configured\n")
  129. end
  130. if string.len(util.exec("ip -f inet6 address show dev br-wan | grep global")) >= 2 then
  131. io.write("IPv6 configured\n")
  132. else
  133. io.write("IPv6 not configured\n")
  134. end
  135. local stat, fastd_status = pcall(
  136. function()
  137. local fastd_sock = nixio.socket('unix', 'stream')
  138. assert(fastd_sock:connect('/var/run/fastd.mesh_vpn.socket'))
  139. decoder = json.new()
  140. ltn12.pump.all(ltn12.source.file(fastd_sock), decoder:sink())
  141. return decoder:get()
  142. end
  143. )
  144. if stat then
  145. io.write(string.format("fastd running for %.3f seconds\n\n", fastd_status.uptime/1000))
  146. local peers = 0
  147. local connections = 0
  148. for key, peer in pairs(fastd_status.peers) do
  149. peers = peers+1
  150. if peer.connection then
  151. connections = connections+1
  152. end
  153. end
  154. io.write(string.format("There are %i peers configured, of which %i are connected:\n", peers, connections))
  155. for key, peer in pairs(fastd_status.peers) do
  156. io.write(string.format("%s: ", escape_html(peer.name)))
  157. if peer.connection then
  158. io.write(string.format("connected for %.3f seconds\n", peer.connection.established/1000))
  159. else
  160. io.write("not connected\n")
  161. end
  162. end
  163. else
  164. io.write("fastd not running")
  165. end
  166. io.write("</pre>")
  167. io.write("<script>")
  168. for _, ifname in ipairs(interfaces) do
  169. local macs = neighbours(ifname)
  170. for mac, node in pairs(macs) do
  171. local hostname = node["hostname"]
  172. local ip
  173. if node["network"] and node["network"]["addresses"] then
  174. for _, myip in ipairs(node["network"]["addresses"]) do
  175. if ip == nil and myip:sub(1, 5) ~= "fe80:" then
  176. ip = myip
  177. end
  178. end
  179. end
  180. if ip and hostname then
  181. io.write("update_node(\"" .. escape_html(ifname) .. "-" .. mac .. "\", \"" .. escape_html(ip) .. "\", \"" .. escape_html(hostname) .. "\");")
  182. end
  183. end
  184. end
  185. io.write("</script>")
  186. io.write("</body>")
  187. io.write("</html>")