[PATCH] add tools to scan network for any device, or for sip devices, and output...
[project/luci.git] / applications / luci-diag-devinfo / luasrc / controller / luci_diag / devinfo_common.lua
1 --[[
2
3 Luci diag - Diagnostics controller module
4 (c) 2009 Daniel Dickinson
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 ]]--
13
14 module("luci.controller.luci_diag.devinfo_common", package.seeall)
15
16 require("luci.i18n")
17 require("luci.util")
18 require("luci.sys")
19 require("luci.cbi")
20 require("luci.model.uci")
21
22 local translate = luci.i18n.translate
23 local DummyValue = luci.cbi.DummyValue
24 local SimpleSection = luci.cbi.SimpleSection
25
26 function run_processes(outnets, cmdfunc)
27 i = next(outnets, nil)
28 while (i) do
29 outnets[i]["output"] = luci.sys.exec(cmdfunc(outnets, i))
30 i = next(outnets, i)
31 end
32 end
33
34 function parse_output(devmap, outnets, haslink, type, mini, debug)
35 local curnet = next(outnets, nil)
36
37 luci.i18n.loadc("diag_devinfo")
38
39 while (curnet) do
40 local output = outnets[curnet]["output"]
41 local subnet = outnets[curnet]["subnet"]
42 local ports = outnets[curnet]["ports"]
43 local interface = outnets[curnet]["interface"]
44 local netdevs = {}
45 devlines = luci.util.split(output)
46 if not devlines then
47 devlines = {}
48 table.insert(devlines, output)
49 end
50
51 local j = nil
52 j = next(devlines, j)
53
54 local found_a_device = false
55
56 while (j) do
57 if devlines[j] and ( devlines[j] ~= "" ) then
58 found_a_device = true
59 local devtable
60 local row = {}
61 devtable = luci.util.split(devlines[j], ' | ')
62 row["ip"] = devtable[1]
63 if (not mini) then
64 row["mac"] = devtable[2]
65 end
66 if ( devtable[4] == 'unknown' ) then
67 row["vendor"] = devtable[3]
68 else
69 row["vendor"] = devtable[4]
70 end
71 row["type"] = devtable[5]
72 if (not mini) then
73 row["model"] = devtable[6]
74 end
75 if (haslink) then
76 row["config_page"] = devtable[7]
77 end
78
79 if (debug) then
80 row["raw"] = devlines[j]
81 end
82 table.insert(netdevs, row)
83 end
84 j = next(devlines, j)
85 end
86 if not found_a_device then
87 local row = {}
88 row["ip"] = curnet
89 if (not mini) then
90 row["mac"] = ""
91 end
92 if (type == "smap") then
93 row["vendor"] = luci.i18n.translate("l_d_d_dc_nosipdev")
94 else
95 row["vendor"] = luci.i18n.translate("l_d_d_dc_nodev")
96 end
97 row["type"] = luci.i18n.translate("l_d_d_dc_checkothernet")
98 if (not mini) then
99 row["model"] = ""
100 end
101 if (haslink) then
102 row["config_page"] = ""
103 end
104 if (debug) then
105 row["raw"] = output
106 end
107 table.insert(netdevs, row)
108 end
109 local s
110 if (type == "smap") then
111 if (mini) then
112 s = devmap:section(luci.cbi.Table, netdevs, luci.i18n.translate("l_d_d_dc_sipdev_found_mini") .. " " .. curnet)
113 else
114 local interfacestring = ""
115 if ( interface ~= "" ) then
116 interfacestring = ", " .. interface
117 end
118 s = devmap:section(luci.cbi.Table, netdevs, luci.i18n.translate("l_d_d_dc_sipdev_found_admin") .. " " .. curnet .. " (" .. subnet .. ":" .. ports .. interfacestring .. ")")
119 end
120 s.template = "diag/smapsection"
121 else
122 if (mini) then
123 s = devmap:section(luci.cbi.Table, netdevs, luci.i18n.translate("l_dd_dc_netdev_found_mini") .. " " .. curnet)
124 else
125 local interfacestring = ""
126 if ( interface ~= "" ) then
127 interfacestring = ", " .. interface
128 end
129 s = devmap:section(luci.cbi.Table, netdevs, luci.i18n.translate("l_d_d_dc_netdev_found_admin", "Devices discovered for") .. " " .. curnet .. " (" .. subnet .. interfacestring .. ")")
130 end
131 end
132 s:option(DummyValue, "ip", translate("l_d_d_dc_mac_ip"))
133 if (not mini) then
134 s:option(DummyValue, "mac", translate("l_d_d_dc_mac_address"))
135 end
136 s:option(DummyValue, "vendor", translate("l_d_d_dc_mac_vendor"))
137 s:option(DummyValue, "type", translate("l_d_d_dc_mac_device_type"))
138 if (not mini) then
139 s:option(DummyValue, "model", translate("l_d_d_dc_mac_model"))
140 end
141 if (haslink) then
142 s:option(DummyValue, "config_page", translate("l_d_d_dc_config_page"))
143 end
144 if (debug) then
145 s:option(DummyValue, "raw", translate("l_d_d_dc_raw"))
146 end
147 curnet = next(outnets, curnet)
148 end
149 end
150
151 function get_network_device(interface)
152 local state = luci.model.uci.cursor_state()
153 state:load("network")
154 local dev
155
156 return state:get("network", interface, "ifname")
157 end
158
159
160 function cbi_add_networks(field)
161 uci.cursor():foreach("network", "interface",
162 function (section)
163 if section[".name"] ~= "loopback" then
164 field:value(section[".name"])
165 end
166 end
167 )
168 field.titleref = luci.dispatcher.build_url("admin", "network", "network")
169 end
170
171 function config_devinfo_scan(map, scannet)
172 local o
173 o = scannet:option(luci.cbi.Flag, "enable", translate("l_d_d_dc_enable"))
174 o.optional = false
175 o.rmempty = false
176
177 o = scannet:option(luci.cbi.Value, "interface", translate("l_d_d_dc_interface"))
178 o.optional = false
179 luci.controller.luci_diag.devinfo_common.cbi_add_networks(o)
180
181 local scansubnet
182 scansubnet = scannet:option(luci.cbi.Value, "subnet", translate("l_d_d_dc_subnet"))
183 scansubnet.optional = false
184
185 o = scannet:option(luci.cbi.Value, "timeout", translate("l_d_d_dc_timeout"), translate("l_d_d_dc_timeout_descr"))
186 o.optional = true
187
188 o = scannet:option(luci.cbi.Value, "repeat_count", translate("l_d_d_dc_repeat_count"), translate("l_d_d_dc_repeat_count_descr"))
189 o.optional = true
190
191 o = scannet:option(luci.cbi.Value, "sleepreq", translate("l_d_d_dc_sleep"), translate("l_d_d_dc_sleep_desc"))
192 o.optional = true
193 end