devinfo_common.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. -- Copyright 2009 Daniel Dickinson
  2. -- Licensed to the public under the Apache License 2.0.
  3. module("luci.controller.luci_diag.devinfo_common", package.seeall)
  4. require("luci.i18n")
  5. require("luci.util")
  6. require("luci.sys")
  7. require("luci.cbi")
  8. require("luci.model.uci")
  9. local translate = luci.i18n.translate
  10. local DummyValue = luci.cbi.DummyValue
  11. local SimpleSection = luci.cbi.SimpleSection
  12. function index()
  13. return -- no-op
  14. end
  15. function run_processes(outnets, cmdfunc)
  16. i = next(outnets, nil)
  17. while (i) do
  18. outnets[i]["output"] = luci.sys.exec(cmdfunc(outnets, i))
  19. i = next(outnets, i)
  20. end
  21. end
  22. function parse_output(devmap, outnets, haslink, type, mini, debug)
  23. local curnet = next(outnets, nil)
  24. while (curnet) do
  25. local output = outnets[curnet]["output"]
  26. local subnet = outnets[curnet]["subnet"]
  27. local ports = outnets[curnet]["ports"]
  28. local interface = outnets[curnet]["interface"]
  29. local netdevs = {}
  30. devlines = luci.util.split(output)
  31. if not devlines then
  32. devlines = {}
  33. table.insert(devlines, output)
  34. end
  35. local j = nil
  36. j = next(devlines, j)
  37. local found_a_device = false
  38. while (j) do
  39. if devlines[j] and ( devlines[j] ~= "" ) then
  40. found_a_device = true
  41. local devtable
  42. local row = {}
  43. devtable = luci.util.split(devlines[j], ' | ')
  44. row["ip"] = devtable[1]
  45. if (not mini) then
  46. row["mac"] = devtable[2]
  47. end
  48. if ( devtable[4] == 'unknown' ) then
  49. row["vendor"] = devtable[3]
  50. else
  51. row["vendor"] = devtable[4]
  52. end
  53. row["type"] = devtable[5]
  54. if (not mini) then
  55. row["model"] = devtable[6]
  56. end
  57. if (haslink) then
  58. row["config_page"] = devtable[7]
  59. end
  60. if (debug) then
  61. row["raw"] = devlines[j]
  62. end
  63. table.insert(netdevs, row)
  64. end
  65. j = next(devlines, j)
  66. end
  67. if not found_a_device then
  68. local row = {}
  69. row["ip"] = curnet
  70. if (not mini) then
  71. row["mac"] = ""
  72. end
  73. if (type == "smap") then
  74. row["vendor"] = luci.i18n.translate("No SIP devices")
  75. else
  76. row["vendor"] = luci.i18n.translate("No devices detected")
  77. end
  78. row["type"] = luci.i18n.translate("check other networks")
  79. if (not mini) then
  80. row["model"] = ""
  81. end
  82. if (haslink) then
  83. row["config_page"] = ""
  84. end
  85. if (debug) then
  86. row["raw"] = output
  87. end
  88. table.insert(netdevs, row)
  89. end
  90. local s
  91. if (type == "smap") then
  92. if (mini) then
  93. s = devmap:section(luci.cbi.Table, netdevs, luci.i18n.translate("SIP devices discovered for") .. " " .. curnet)
  94. else
  95. local interfacestring = ""
  96. if ( interface ~= "" ) then
  97. interfacestring = ", " .. interface
  98. end
  99. s = devmap:section(luci.cbi.Table, netdevs, luci.i18n.translate("SIP devices discovered for") .. " " .. curnet .. " (" .. subnet .. ":" .. ports .. interfacestring .. ")")
  100. end
  101. s.template = "diag/smapsection"
  102. else
  103. if (mini) then
  104. s = devmap:section(luci.cbi.Table, netdevs, luci.i18n.translate("Devices discovered for") .. " " .. curnet)
  105. else
  106. local interfacestring = ""
  107. if ( interface ~= "" ) then
  108. interfacestring = ", " .. interface
  109. end
  110. s = devmap:section(luci.cbi.Table, netdevs, luci.i18n.translate("Devices discovered for") .. " " .. curnet .. " (" .. subnet .. interfacestring .. ")")
  111. end
  112. end
  113. s:option(DummyValue, "ip", translate("IP Address"))
  114. if (not mini) then
  115. s:option(DummyValue, "mac", translate("MAC Address"))
  116. end
  117. s:option(DummyValue, "vendor", translate("Vendor"))
  118. s:option(DummyValue, "type", translate("Device Type"))
  119. if (not mini) then
  120. s:option(DummyValue, "model", translate("Model"))
  121. end
  122. if (haslink) then
  123. s:option(DummyValue, "config_page", translate("Link to Device"))
  124. end
  125. if (debug) then
  126. s:option(DummyValue, "raw", translate("Raw"))
  127. end
  128. curnet = next(outnets, curnet)
  129. end
  130. end
  131. function get_network_device(interface)
  132. local state = luci.model.uci.cursor_state()
  133. state:load("network")
  134. local dev
  135. return state:get("network", interface, "ifname")
  136. end
  137. function cbi_add_networks(field)
  138. uci.cursor():foreach("network", "interface",
  139. function (section)
  140. if section[".name"] ~= "loopback" then
  141. field:value(section[".name"])
  142. end
  143. end
  144. )
  145. field.titleref = luci.dispatcher.build_url("admin", "network", "network")
  146. end
  147. function config_devinfo_scan(map, scannet)
  148. local o
  149. o = scannet:option(luci.cbi.Flag, "enable", translate("Enable"))
  150. o.optional = false
  151. o.rmempty = false
  152. o = scannet:option(luci.cbi.Value, "interface", translate("Interface"))
  153. o.optional = false
  154. luci.controller.luci_diag.devinfo_common.cbi_add_networks(o)
  155. local scansubnet
  156. scansubnet = scannet:option(luci.cbi.Value, "subnet", translate("Subnet"))
  157. scansubnet.optional = false
  158. o = scannet:option(luci.cbi.Value, "timeout", translate("Timeout"), translate("Time to wait for responses in seconds (default 10)"))
  159. o.optional = true
  160. o = scannet:option(luci.cbi.Value, "repeat_count", translate("Repeat Count"), translate("Number of times to send requests (default 1)"))
  161. o.optional = true
  162. o = scannet:option(luci.cbi.Value, "sleepreq", translate("Sleep Between Requests"), translate("Milliseconds to sleep between requests (default 100)"))
  163. o.optional = true
  164. end