status.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 = "/var/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 fd = io.open("/tmp/hosts/odhcpd", "r")
  46. if fd then
  47. while true do
  48. local ln = fd:read("*l")
  49. if not ln then
  50. break
  51. else
  52. local iface, duid, iaid, name, ts, id, length, ip = ln:match("^# (%S+) (%S+) (%S+) (%S+) (%d+) (%S+) (%S+) (.*)")
  53. if ip and iaid ~= "ipv4" and family == 6 then
  54. rv[#rv+1] = {
  55. expires = os.difftime(tonumber(ts) or 0, os.time()),
  56. duid = duid,
  57. ip6addr = ip,
  58. hostname = (name ~= "-") and name
  59. }
  60. elseif ip and iaid == "ipv4" and family == 4 then
  61. rv[#rv+1] = {
  62. expires = os.difftime(tonumber(ts) or 0, os.time()),
  63. macaddr = duid,
  64. ipaddr = ip,
  65. hostname = (name ~= "-") and name
  66. }
  67. end
  68. end
  69. end
  70. fd:close()
  71. end
  72. return rv
  73. end
  74. function dhcp_leases()
  75. return dhcp_leases_common(4)
  76. end
  77. function dhcp6_leases()
  78. return dhcp_leases_common(6)
  79. end
  80. function wifi_networks()
  81. local rv = { }
  82. local ntm = require "luci.model.network".init()
  83. local dev
  84. for _, dev in ipairs(ntm:get_wifidevs()) do
  85. local rd = {
  86. up = dev:is_up(),
  87. device = dev:name(),
  88. name = dev:get_i18n(),
  89. networks = { }
  90. }
  91. local net
  92. for _, net in ipairs(dev:get_wifinets()) do
  93. rd.networks[#rd.networks+1] = {
  94. name = net:shortname(),
  95. link = net:adminlink(),
  96. up = net:is_up(),
  97. mode = net:active_mode(),
  98. ssid = net:active_ssid(),
  99. bssid = net:active_bssid(),
  100. encryption = net:active_encryption(),
  101. frequency = net:frequency(),
  102. channel = net:channel(),
  103. signal = net:signal(),
  104. quality = net:signal_percent(),
  105. noise = net:noise(),
  106. bitrate = net:bitrate(),
  107. ifname = net:ifname(),
  108. assoclist = net:assoclist(),
  109. country = net:country(),
  110. txpower = net:txpower(),
  111. txpoweroff = net:txpower_offset(),
  112. disabled = (dev:get("disabled") == "1" or
  113. net:get("disabled") == "1")
  114. }
  115. end
  116. rv[#rv+1] = rd
  117. end
  118. return rv
  119. end
  120. function wifi_network(id)
  121. local ntm = require "luci.model.network".init()
  122. local net = ntm:get_wifinet(id)
  123. if net then
  124. local dev = net:get_device()
  125. if dev then
  126. return {
  127. id = id,
  128. name = net:shortname(),
  129. link = net:adminlink(),
  130. up = net:is_up(),
  131. mode = net:active_mode(),
  132. ssid = net:active_ssid(),
  133. bssid = net:active_bssid(),
  134. encryption = net:active_encryption(),
  135. frequency = net:frequency(),
  136. channel = net:channel(),
  137. signal = net:signal(),
  138. quality = net:signal_percent(),
  139. noise = net:noise(),
  140. bitrate = net:bitrate(),
  141. ifname = net:ifname(),
  142. assoclist = net:assoclist(),
  143. country = net:country(),
  144. txpower = net:txpower(),
  145. txpoweroff = net:txpower_offset(),
  146. disabled = (dev:get("disabled") == "1" or
  147. net:get("disabled") == "1"),
  148. device = {
  149. up = dev:is_up(),
  150. device = dev:name(),
  151. name = dev:get_i18n()
  152. }
  153. }
  154. end
  155. end
  156. return { }
  157. end
  158. function switch_status(devs)
  159. local dev
  160. local switches = { }
  161. for dev in devs:gmatch("[^%s,]+") do
  162. local ports = { }
  163. local swc = io.popen("swconfig dev %q show" % dev, "r")
  164. if swc then
  165. local l
  166. repeat
  167. l = swc:read("*l")
  168. if l then
  169. local port, up = l:match("port:(%d+) link:(%w+)")
  170. if port then
  171. local speed = l:match(" speed:(%d+)")
  172. local duplex = l:match(" (%w+)-duplex")
  173. local txflow = l:match(" (txflow)")
  174. local rxflow = l:match(" (rxflow)")
  175. local auto = l:match(" (auto)")
  176. ports[#ports+1] = {
  177. port = tonumber(port) or 0,
  178. speed = tonumber(speed) or 0,
  179. link = (up == "up"),
  180. duplex = (duplex == "full"),
  181. rxflow = (not not rxflow),
  182. txflow = (not not txflow),
  183. auto = (not not auto)
  184. }
  185. end
  186. end
  187. until not l
  188. swc:close()
  189. end
  190. switches[dev] = ports
  191. end
  192. return switches
  193. end