From 820f11932f548182ebb32704e1df83b78e69b19e Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Fri, 17 Jul 2009 20:27:05 +0000 Subject: [PATCH] add tools to scan network for any device, or for sip devices, and output device information, thanks cshore --- .../luasrc/controller/luci_diag/devinfo_common.lua | 193 +++++++++++++++++++++ .../controller/luci_diag/luci_diag_devinfo.lua | 69 ++++++++ .../controller/luci_diag/netdiscover_common.lua | 99 +++++++++++ .../luasrc/controller/luci_diag/smap_common.lua | 109 ++++++++++++ .../luasrc/model/cbi/luci_diag/mactodevinfo.lua | 36 ++++ .../model/cbi/luci_diag/netdiscover_devinfo.lua | 31 ++++ .../cbi/luci_diag/netdiscover_devinfo_config.lua | 30 ++++ .../luci_diag/netdiscover_devinfo_config_mini.lua | 30 ++++ .../cbi/luci_diag/netdiscover_devinfo_mini.lua | 31 ++++ .../luasrc/model/cbi/luci_diag/smap_devinfo.lua | 31 ++++ .../model/cbi/luci_diag/smap_devinfo_config.lua | 35 ++++ .../cbi/luci_diag/smap_devinfo_config_mini.lua | 34 ++++ .../model/cbi/luci_diag/smap_devinfo_mini.lua | 31 ++++ .../luasrc/view/diag/smapsection.htm | 137 +++++++++++++++ .../luasrc/view/diag/smapvalue.htm | 21 +++ 15 files changed, 917 insertions(+) create mode 100644 applications/luci-diag-devinfo/luasrc/controller/luci_diag/devinfo_common.lua create mode 100644 applications/luci-diag-devinfo/luasrc/controller/luci_diag/luci_diag_devinfo.lua create mode 100644 applications/luci-diag-devinfo/luasrc/controller/luci_diag/netdiscover_common.lua create mode 100644 applications/luci-diag-devinfo/luasrc/controller/luci_diag/smap_common.lua create mode 100644 applications/luci-diag-devinfo/luasrc/model/cbi/luci_diag/mactodevinfo.lua create mode 100644 applications/luci-diag-devinfo/luasrc/model/cbi/luci_diag/netdiscover_devinfo.lua create mode 100644 applications/luci-diag-devinfo/luasrc/model/cbi/luci_diag/netdiscover_devinfo_config.lua create mode 100644 applications/luci-diag-devinfo/luasrc/model/cbi/luci_diag/netdiscover_devinfo_config_mini.lua create mode 100644 applications/luci-diag-devinfo/luasrc/model/cbi/luci_diag/netdiscover_devinfo_mini.lua create mode 100644 applications/luci-diag-devinfo/luasrc/model/cbi/luci_diag/smap_devinfo.lua create mode 100644 applications/luci-diag-devinfo/luasrc/model/cbi/luci_diag/smap_devinfo_config.lua create mode 100644 applications/luci-diag-devinfo/luasrc/model/cbi/luci_diag/smap_devinfo_config_mini.lua create mode 100644 applications/luci-diag-devinfo/luasrc/model/cbi/luci_diag/smap_devinfo_mini.lua create mode 100644 applications/luci-diag-devinfo/luasrc/view/diag/smapsection.htm create mode 100644 applications/luci-diag-devinfo/luasrc/view/diag/smapvalue.htm (limited to 'applications/luci-diag-devinfo/luasrc') diff --git a/applications/luci-diag-devinfo/luasrc/controller/luci_diag/devinfo_common.lua b/applications/luci-diag-devinfo/luasrc/controller/luci_diag/devinfo_common.lua new file mode 100644 index 000000000..58009c596 --- /dev/null +++ b/applications/luci-diag-devinfo/luasrc/controller/luci_diag/devinfo_common.lua @@ -0,0 +1,193 @@ +--[[ + +Luci diag - Diagnostics controller module +(c) 2009 Daniel Dickinson + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +]]-- + +module("luci.controller.luci_diag.devinfo_common", package.seeall) + +require("luci.i18n") +require("luci.util") +require("luci.sys") +require("luci.cbi") +require("luci.model.uci") + +local translate = luci.i18n.translate +local DummyValue = luci.cbi.DummyValue +local SimpleSection = luci.cbi.SimpleSection + +function run_processes(outnets, cmdfunc) + i = next(outnets, nil) + while (i) do + outnets[i]["output"] = luci.sys.exec(cmdfunc(outnets, i)) + i = next(outnets, i) + end +end + +function parse_output(devmap, outnets, haslink, type, mini, debug) + local curnet = next(outnets, nil) + + luci.i18n.loadc("diag_devinfo") + + while (curnet) do + local output = outnets[curnet]["output"] + local subnet = outnets[curnet]["subnet"] + local ports = outnets[curnet]["ports"] + local interface = outnets[curnet]["interface"] + local netdevs = {} + devlines = luci.util.split(output) + if not devlines then + devlines = {} + table.insert(devlines, output) + end + + local j = nil + j = next(devlines, j) + + local found_a_device = false + + while (j) do + if devlines[j] and ( devlines[j] ~= "" ) then + found_a_device = true + local devtable + local row = {} + devtable = luci.util.split(devlines[j], ' | ') + row["ip"] = devtable[1] + if (not mini) then + row["mac"] = devtable[2] + end + if ( devtable[4] == 'unknown' ) then + row["vendor"] = devtable[3] + else + row["vendor"] = devtable[4] + end + row["type"] = devtable[5] + if (not mini) then + row["model"] = devtable[6] + end + if (haslink) then + row["config_page"] = devtable[7] + end + + if (debug) then + row["raw"] = devlines[j] + end + table.insert(netdevs, row) + end + j = next(devlines, j) + end + if not found_a_device then + local row = {} + row["ip"] = curnet + if (not mini) then + row["mac"] = "" + end + if (type == "smap") then + row["vendor"] = luci.i18n.translate("l_d_d_dc_nosipdev") + else + row["vendor"] = luci.i18n.translate("l_d_d_dc_nodev") + end + row["type"] = luci.i18n.translate("l_d_d_dc_checkothernet") + if (not mini) then + row["model"] = "" + end + if (haslink) then + row["config_page"] = "" + end + if (debug) then + row["raw"] = output + end + table.insert(netdevs, row) + end + local s + if (type == "smap") then + if (mini) then + s = devmap:section(luci.cbi.Table, netdevs, luci.i18n.translate("l_d_d_dc_sipdev_found_mini") .. " " .. curnet) + else + local interfacestring = "" + if ( interface ~= "" ) then + interfacestring = ", " .. interface + end + s = devmap:section(luci.cbi.Table, netdevs, luci.i18n.translate("l_d_d_dc_sipdev_found_admin") .. " " .. curnet .. " (" .. subnet .. ":" .. ports .. interfacestring .. ")") + end + s.template = "diag/smapsection" + else + if (mini) then + s = devmap:section(luci.cbi.Table, netdevs, luci.i18n.translate("l_dd_dc_netdev_found_mini") .. " " .. curnet) + else + local interfacestring = "" + if ( interface ~= "" ) then + interfacestring = ", " .. interface + end + s = devmap:section(luci.cbi.Table, netdevs, luci.i18n.translate("l_d_d_dc_netdev_found_admin", "Devices discovered for") .. " " .. curnet .. " (" .. subnet .. interfacestring .. ")") + end + end + s:option(DummyValue, "ip", translate("l_d_d_dc_mac_ip")) + if (not mini) then + s:option(DummyValue, "mac", translate("l_d_d_dc_mac_address")) + end + s:option(DummyValue, "vendor", translate("l_d_d_dc_mac_vendor")) + s:option(DummyValue, "type", translate("l_d_d_dc_mac_device_type")) + if (not mini) then + s:option(DummyValue, "model", translate("l_d_d_dc_mac_model")) + end + if (haslink) then + s:option(DummyValue, "config_page", translate("l_d_d_dc_config_page")) + end + if (debug) then + s:option(DummyValue, "raw", translate("l_d_d_dc_raw")) + end + curnet = next(outnets, curnet) + end +end + +function get_network_device(interface) + local state = luci.model.uci.cursor_state() + state:load("network") + local dev + + return state:get("network", interface, "ifname") +end + + +function cbi_add_networks(field) + uci.cursor():foreach("network", "interface", + function (section) + if section[".name"] ~= "loopback" then + field:value(section[".name"]) + end + end + ) + field.titleref = luci.dispatcher.build_url("admin", "network", "network") +end + +function config_devinfo_scan(map, scannet) + local o + o = scannet:option(luci.cbi.Flag, "enable", translate("l_d_d_dc_enable")) + o.optional = false + o.rmempty = false + + o = scannet:option(luci.cbi.Value, "interface", translate("l_d_d_dc_interface")) + o.optional = false + luci.controller.luci_diag.devinfo_common.cbi_add_networks(o) + + local scansubnet + scansubnet = scannet:option(luci.cbi.Value, "subnet", translate("l_d_d_dc_subnet")) + scansubnet.optional = false + + o = scannet:option(luci.cbi.Value, "timeout", translate("l_d_d_dc_timeout"), translate("l_d_d_dc_timeout_descr")) + o.optional = true + + o = scannet:option(luci.cbi.Value, "repeat_count", translate("l_d_d_dc_repeat_count"), translate("l_d_d_dc_repeat_count_descr")) + o.optional = true + + o = scannet:option(luci.cbi.Value, "sleepreq", translate("l_d_d_dc_sleep"), translate("l_d_d_dc_sleep_desc")) + o.optional = true +end \ No newline at end of file diff --git a/applications/luci-diag-devinfo/luasrc/controller/luci_diag/luci_diag_devinfo.lua b/applications/luci-diag-devinfo/luasrc/controller/luci_diag/luci_diag_devinfo.lua new file mode 100644 index 000000000..0d8da66b6 --- /dev/null +++ b/applications/luci-diag-devinfo/luasrc/controller/luci_diag/luci_diag_devinfo.lua @@ -0,0 +1,69 @@ +--[[ + +Luci diag - Diagnostics controller module +(c) 2009 Daniel Dickinson + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +]]-- + + +module("luci.controller.luci_diag.luci_diag_devinfo", package.seeall) + +function index() + require("luci.i18n") + luci.i18n.loadc("diag_devinfo") + + local e = entry({"admin", "voice", "diag", "phones"}, arcombine(cbi("luci_diag/smap_devinfo"), cbi("luci_diag/smap_devinfo_config")), luci.i18n.translate("l_d_d_ldd_diag_smap_devinfo"), 10) + e.leaf = true + e.subindex = true + e.i18n = "diag_devinfo" + e.dependent = true + + e = entry({"admin", "voice", "diag", "phones", "config"}, cbi("luci_diag/smap_devinfo_config"), luci.i18n.translate("l_d_d_ldd_config"), 10) + e.i18n = "diag_devinfo" + + e = entry({"admin", "status", "smap_devinfo"}, cbi("luci_diag/smap_devinfo"), luci.i18n.translate("l_d_d_ldd_smap_devinfo_config"), 120) + e.leaf = true + e.i18n = "diag_devinfo" + e.dependent = true + + e = entry({"admin", "network", "diag_config", "netdiscover_devinfo_config"}, cbi("luci_diag/netdiscover_devinfo_config"), luci.i18n.translate("l_d_d_ldd_netdiscover_devinfo_scan"), 100) + e.leaf = true + e.i18n = "diag_devinfo" + e.dependent = true + + e = entry({"admin", "network", "diag_config", "smap_devinfo_config"}, cbi("luci_diag/smap_devinfo_config"), luci.i18n.translate("l_d_d_ldd_smap_devinfo_scan_config")) + e.leaf = true + e.i18n = "diag_devinfo" + e.dependent = true + + e = entry({"admin", "status", "netdiscover_devinfo"}, cbi("luci_diag/netdiscover_devinfo"), luci.i18n.translate("l_d_d_ldd_netdiscover_devinfo"), 90) + e.i18n = "diag_devinfo" + e.dependent = true + + e = entry({"admin", "network", "mactodevinfo"}, cbi("luci_diag/mactodevinfo"), luci.i18n.translate("l_d_d_ldd_mac_to_devinfo_overrides"), 190) + e.i18n = "diag_devinfo" + e.dependent = true + + e = entry({"mini", "diag", "phone_scan"}, cbi("luci_diag/smap_devinfo_mini"), luci.i18n.translate("l_d_d_ldd_diag_smap_devinfo_scan_mini"), 100) + e.i18n = "diag_devinfo" + e.dependent = true + + e = entry({"mini", "voice", "phones", "phone_scan_config"}, cbi("luci_diag/smap_devinfo_config_mini"), luci.i18n.translate("l_d_d_ldd_smap_devinfo_config_scan_miini"), 90) + e.i18n = "diag_devinfo" + e.dependent = true + + e = entry({"mini", "diag", "netdiscover_devinfo"}, cbi("luci_diag/netdiscover_devinfo_mini"), luci.i18n.translate("l_d_d_ldd_diag_netdiscover_devinfo_scan_mini"), 10) + e.i18n = "diag_devinfo" + e.dependent = true + + e = entry({"mini", "network", "netdiscover_devinfo_config"}, cbi("luci_diag/netdiscover_devinfo_config_mini"), luci.i18n.translate("l_d_d_ldd_netdiscover_devinfo_scan_config_mini")) + e.i18n = "diag_devinfo" + e.dependent = true + +end diff --git a/applications/luci-diag-devinfo/luasrc/controller/luci_diag/netdiscover_common.lua b/applications/luci-diag-devinfo/luasrc/controller/luci_diag/netdiscover_common.lua new file mode 100644 index 000000000..71dc34081 --- /dev/null +++ b/applications/luci-diag-devinfo/luasrc/controller/luci_diag/netdiscover_common.lua @@ -0,0 +1,99 @@ +--[[ + +Luci diag - Diagnostics controller module +(c) 2009 Daniel Dickinson + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +]]-- + +module("luci.controller.luci_diag.netdiscover_common", package.seeall) + +require("luci.i18n") +require("luci.util") +require("luci.sys") +require("luci.cbi") +require("luci.model.uci") + +local translate = luci.i18n.translate +local DummyValue = luci.cbi.DummyValue +local SimpleSection = luci.cbi.SimpleSection + + +function get_params() + + local netdiscover_uci = luci.model.uci.cursor() + netdiscover_uci:load("luci_devinfo") + local nettable = netdiscover_uci:get_all("luci_devinfo") + + local i + local subnet + local netdout + + local outnets = {} + + i = next(nettable, nil) + + while (i) do + if (netdiscover_uci:get("luci_devinfo", i) == "netdiscover_scannet") then + local scannet = netdiscover_uci:get_all("luci_devinfo", i) + if scannet["subnet"] and (scannet["subnet"] ~= "") and scannet["enable"] and ( scannet["enable"] == "1") then + local output = "" + local outrow = {} + outrow["interface"] = scannet["interface"] + outrow["timeout"] = 10 + local timeout = tonumber(scannet["timeout"]) + if timeout and ( timeout > 0 ) then + outrow["timeout"] = scannet["timeout"] + end + + outrow["repeat_count"] = 1 + local repcount = tonumber(scannet["repeat_count"]) + if repcount and ( repcount > 0 ) then + outrow["repeat_count"] = scannet["repeat_count"] + end + + outrow["sleepreq"] = 100 + local repcount = tonumber(scannet["sleepreq"]) + if repcount and ( repcount > 0 ) then + outrow["sleepreq"] = scannet["sleepreq"] + end + + outrow["subnet"] = scannet["subnet"] + outrow["output"] = output + outnets[i] = outrow + end + end + i = next(nettable, i) + end + return outnets +end + +function command_function(outnets, i) + local interface = luci.controller.luci_diag.devinfo_common.get_network_device(outnets[i]["interface"]) + + return "/usr/bin/netdiscover-to-devinfo " .. outnets[i]["subnet"] .. " " .. interface .. " " .. outnets[i]["timeout"] .. " -r " .. outnets[i]["repeat_count"] .. " -s " .. outnets[i]["sleepreq"] .. " 0 ) then + outrow["timeout"] = scannet["timeout"] + end + + outrow["repeat_count"] = 1 + local repcount = tonumber(scannet["repeat_count"]) + if repcount and ( repcount > 0 ) then + outrow["repeat_count"] = scannet["repeat_count"] + end + + outrow["sleepreq"] = 100 + local repcount = tonumber(scannet["sleepreq"]) + if repcount and ( repcount > 0 ) then + outrow["sleepreq"] = scannet["sleepreq"] + end + + if scannet["interface"] and ( scannet["interface"] ~= "" ) then + outrow["interface"] = scannet["interface"] + else + outrow["interface"] = "" + end + + outrow["ports"] = ports + outrow["output"] = output + outnets[i] = outrow + end + end + i = next(nettable, i) + end + return outnets +end + +function command_function(outnets, i) + + local interface = luci.controller.luci_diag.devinfo_common.get_network_device(outnets[i]["interface"]) + + return "/usr/bin/netsmap-to-devinfo -r " .. outnets[i]["subnet"] .. " -t " .. outnets[i]["timeout"] .. " -i " .. interface .. " -x -p " .. outnets[i]["ports"] .. " -c " .. outnets[i]["repeat_count"] .. " -s " .. outnets[i]["sleepreq"] .. " +<%- +local rowcnt = 1 +function rowstyle() + rowcnt = rowcnt + 1 + return (rowcnt % 2) + 1 +end +-%> + + +
+ <% if self.title and #self.title > 0 then -%> + <%=self.title%> + <%- end %> +
<%=self.description%>
+
+ <%- local count = 0 -%> + + + <%- if not self.anonymous then -%> + <%- if self.sectionhead then -%> + + <%- else -%> + + <%- end -%> + <%- end -%> + <%- for i, k in pairs(self.children) do if not k.optional then -%> + + <%- count = count + 1; end; end; if self.extedit or self.addremove then -%> + + <%- count = count + 1; end -%> + + + <%- if not self.anonymous then -%> + <%- if self.sectiondesc then -%> + + <%- else -%> + + <%- end -%> + <%- end -%> + <%- for i, k in pairs(self.children) do if not k.optional then -%> + + <%- end; end; if self.extedit or self.addremove then -%> + + <%- end -%> + + <%- local isempty = true + for i, k in ipairs(self:cfgsections()) do + section = k + isempty = false + scope = { valueheader = "cbi/cell_valueheader", valuefooter = "cbi/cell_valuefooter" } + -%> + + <% if not self.anonymous then -%> + + <%- end %> + + <%- for k, node in ipairs(self.children) do -%> + <%- if not node.optional then -%> + <%- nodevalue = node:cfgvalue(section) -%> + <%- if nodevalue and ( nodevalue ~= "" ) and string.find(nodevalue, 'http://', 1, plain) then + node.href = nodevalue + node.template = "diag/smapvalue" + end + -%> + <%- node:render(section, scope or {}) -%> + <%- end -%> + <%- end -%> + + <%- if self.extedit or self.addremove then -%> + + <%- end -%> + + <%- end -%> + + <%- if isempty then -%> + + + + <%- end -%> +
<%=self.sectionhead%>  + <%- if k.titleref then -%><%- end -%> + <%-=k.title-%> + <%- if k.titleref then -%><%- end -%> +  
<%=self.sectiondesc%><%=k.description%>

<%=k%>

+ <%- if self.extedit then -%> + + <%=self.extedit:format(section)%> + <%- elseif type(self.extedit) == "function" then -%> + <%=self:extedit(section)%> + <%- end -%> + " title="<%:edit%>"><%:edit%> + <%- end; if self.addremove then %> + + <%- end -%> +

<%:sectempty%>
+ + <% if self.error then %> +
+
    <% for _, c in pairs(self.error) do for _, e in ipairs(c) do -%> +
  • <%=luci.util.pcdata(e):gsub("\n","
    ")%>
  • + <%- end end %>
+
+ <% end %> + + <%- if self.addremove then -%> + <% if self.template_addremove then include(self.template_addremove) else -%> +
+ <% if self.anonymous then %> + + <% else %> + <% if self.invalid_cts then -%>
<% end %> + + + <% if self.invalid_cts then -%> +
<%:cbi_invalid%>
+ <%- end %> + <% end %> +
+ <%- end %> + <%- end -%> +
+
+ diff --git a/applications/luci-diag-devinfo/luasrc/view/diag/smapvalue.htm b/applications/luci-diag-devinfo/luasrc/view/diag/smapvalue.htm new file mode 100644 index 000000000..60aa70a30 --- /dev/null +++ b/applications/luci-diag-devinfo/luasrc/view/diag/smapvalue.htm @@ -0,0 +1,21 @@ +<%# +LuCI - Lua Configuration Interface +(c) 2009 Daniel Dickinson + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +$Id$ + +-%> + +<%+cbi/valueheader%> +<% if self.href then %><% end -%> + <%=luci.util.pcdata(self:cfgvalue(section))%> +<%- if self.href then %><%end%> +  + +<%+cbi/valuefooter%> -- cgit v1.2.3