2
0

status.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. -- Copyright 2011 Jo-Philipp Wich <jow@openwrt.org>
  2. -- Licensed to the public under the Apache License 2.0.
  3. module("luci.tools.status", package.seeall)
  4. local uci = require "luci.model.uci".cursor()
  5. local function dhcp_leases_common(family)
  6. local rv = { }
  7. local nfs = require "nixio.fs"
  8. local leasefile = "/tmp/dhcp.leases"
  9. uci:foreach("dhcp", "dnsmasq",
  10. function(s)
  11. if s.leasefile and nfs.access(s.leasefile) then
  12. leasefile = s.leasefile
  13. return false
  14. end
  15. end)
  16. local fd = io.open(leasefile, "r")
  17. if fd then
  18. while true do
  19. local ln = fd:read("*l")
  20. if not ln then
  21. break
  22. else
  23. local ts, mac, ip, name, duid = ln:match("^(%d+) (%S+) (%S+) (%S+) (%S+)")
  24. if ts and mac and ip and name and duid then
  25. if family == 4 and not ip:match(":") then
  26. rv[#rv+1] = {
  27. expires = os.difftime(tonumber(ts) or 0, os.time()),
  28. macaddr = mac,
  29. ipaddr = ip,
  30. hostname = (name ~= "*") and name
  31. }
  32. elseif family == 6 and ip:match(":") then
  33. rv[#rv+1] = {
  34. expires = os.difftime(tonumber(ts) or 0, os.time()),
  35. ip6addr = ip,
  36. duid = (duid ~= "*") and duid,
  37. hostname = (name ~= "*") and name
  38. }
  39. end
  40. end
  41. end
  42. end
  43. fd:close()
  44. end
  45. local lease6file = "/tmp/hosts/odhcpd"
  46. uci:foreach("dhcp", "odhcpd",
  47. function(t)
  48. if t.leasefile and nfs.access(t.leasefile) then
  49. lease6file = t.leasefile
  50. return false
  51. end
  52. end)
  53. local fd = io.open(lease6file, "r")
  54. if fd then
  55. while true do
  56. local ln = fd:read("*l")
  57. if not ln then
  58. break
  59. else
  60. local iface, duid, iaid, name, ts, id, length, ip = ln:match("^# (%S+) (%S+) (%S+) (%S+) (-?%d+) (%S+) (%S+) (.*)")
  61. local expire = tonumber(ts) or 0
  62. if ip and iaid ~= "ipv4" and family == 6 then
  63. rv[#rv+1] = {
  64. expires = (expire >= 0) and os.difftime(expire, os.time()),
  65. duid = duid,
  66. ip6addr = ip,
  67. hostname = (name ~= "-") and name
  68. }
  69. elseif ip and iaid == "ipv4" and family == 4 then
  70. rv[#rv+1] = {
  71. expires = (expire >= 0) and os.difftime(expire, os.time()),
  72. macaddr = duid,
  73. ipaddr = ip,
  74. hostname = (name ~= "-") and name
  75. }
  76. end
  77. end
  78. end
  79. fd:close()
  80. end
  81. return rv
  82. end
  83. function dhcp_leases()
  84. return dhcp_leases_common(4)
  85. end
  86. function dhcp6_leases()
  87. return dhcp_leases_common(6)
  88. end
  89. function wifi_networks()
  90. local rv = { }
  91. local ntm = require "luci.model.network".init()
  92. local dev
  93. for _, dev in ipairs(ntm:get_wifidevs()) do
  94. local rd = {
  95. up = dev:is_up(),
  96. device = dev:name(),
  97. name = dev:get_i18n(),
  98. networks = { }
  99. }
  100. local net
  101. for _, net in ipairs(dev:get_wifinets()) do
  102. rd.networks[#rd.networks+1] = {
  103. name = net:shortname(),
  104. link = net:adminlink(),
  105. up = net:is_up(),
  106. mode = net:active_mode(),
  107. ssid = net:active_ssid(),
  108. bssid = net:active_bssid(),
  109. encryption = net:active_encryption(),
  110. frequency = net:frequency(),
  111. channel = net:channel(),
  112. signal = net:signal(),
  113. quality = net:signal_percent(),
  114. noise = net:noise(),
  115. bitrate = net:bitrate(),
  116. ifname = net:ifname(),
  117. assoclist = net:assoclist(),
  118. country = net:country(),
  119. txpower = net:txpower(),
  120. txpoweroff = net:txpower_offset(),
  121. disabled = (dev:get("disabled") == "1" or
  122. net:get("disabled") == "1")
  123. }
  124. end
  125. rv[#rv+1] = rd
  126. end
  127. return rv
  128. end
  129. function wifi_network(id)
  130. local ntm = require "luci.model.network".init()
  131. local net = ntm:get_wifinet(id)
  132. if net then
  133. local dev = net:get_device()
  134. if dev then
  135. return {
  136. id = id,
  137. name = net:shortname(),
  138. link = net:adminlink(),
  139. up = net:is_up(),
  140. mode = net:active_mode(),
  141. ssid = net:active_ssid(),
  142. bssid = net:active_bssid(),
  143. encryption = net:active_encryption(),
  144. frequency = net:frequency(),
  145. channel = net:channel(),
  146. signal = net:signal(),
  147. quality = net:signal_percent(),
  148. noise = net:noise(),
  149. bitrate = net:bitrate(),
  150. ifname = net:ifname(),
  151. assoclist = net:assoclist(),
  152. country = net:country(),
  153. txpower = net:txpower(),
  154. txpoweroff = net:txpower_offset(),
  155. disabled = (dev:get("disabled") == "1" or
  156. net:get("disabled") == "1"),
  157. device = {
  158. up = dev:is_up(),
  159. device = dev:name(),
  160. name = dev:get_i18n()
  161. }
  162. }
  163. end
  164. end
  165. return { }
  166. end
  167. function switch_status(devs)
  168. local dev
  169. local switches = { }
  170. for dev in devs:gmatch("[^%s,]+") do
  171. local ports = { }
  172. local swc = io.popen("swconfig dev %q show" % dev, "r")
  173. if swc then
  174. local l
  175. repeat
  176. l = swc:read("*l")
  177. if l then
  178. local port, up = l:match("port:(%d+) link:(%w+)")
  179. if port then
  180. local speed = l:match(" speed:(%d+)")
  181. local duplex = l:match(" (%w+)-duplex")
  182. local txflow = l:match(" (txflow)")
  183. local rxflow = l:match(" (rxflow)")
  184. local auto = l:match(" (auto)")
  185. ports[#ports+1] = {
  186. port = tonumber(port) or 0,
  187. speed = tonumber(speed) or 0,
  188. link = (up == "up"),
  189. duplex = (duplex == "full"),
  190. rxflow = (not not rxflow),
  191. txflow = (not not txflow),
  192. auto = (not not auto)
  193. }
  194. end
  195. end
  196. until not l
  197. swc:close()
  198. end
  199. switches[dev] = ports
  200. end
  201. return switches
  202. end