009054a86059559f6ab78cbbdf8514790b584aaf
[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 index()
27         return -- no-op
28 end
29
30 function run_processes(outnets, cmdfunc)
31    i = next(outnets, nil)
32    while (i) do
33       outnets[i]["output"] = luci.sys.exec(cmdfunc(outnets, i))
34       i = next(outnets, i)
35    end
36 end
37
38 function parse_output(devmap, outnets, haslink, type, mini, debug)
39    local curnet = next(outnets, nil)
40
41    luci.i18n.loadc("diag_devinfo")
42
43    while (curnet) do
44       local output = outnets[curnet]["output"]
45       local subnet = outnets[curnet]["subnet"]
46       local ports = outnets[curnet]["ports"]
47       local interface = outnets[curnet]["interface"]
48       local netdevs = {}
49       devlines = luci.util.split(output)
50       if not devlines then
51          devlines = {}
52          table.insert(devlines, output)
53       end
54             
55       local j = nil
56       j = next(devlines, j)
57       
58       local found_a_device = false
59       
60       while (j) do
61          if devlines[j] and ( devlines[j] ~= "" ) then
62             found_a_device = true
63             local devtable
64             local row = {}
65             devtable = luci.util.split(devlines[j], ' | ')
66             row["ip"] = devtable[1]
67             if (not mini) then
68                row["mac"] = devtable[2]
69             end
70             if ( devtable[4] == 'unknown' ) then 
71                row["vendor"] = devtable[3]
72             else
73                row["vendor"] = devtable[4]
74             end
75             row["type"] = devtable[5]
76             if (not mini) then
77                row["model"] = devtable[6]
78             end
79             if (haslink) then
80                row["config_page"] = devtable[7]
81             end
82             
83             if (debug) then
84                row["raw"] = devlines[j]
85             end
86             table.insert(netdevs, row)
87          end
88          j = next(devlines, j)
89       end
90       if not found_a_device then
91          local row = {}
92          row["ip"] = curnet
93          if (not mini) then
94             row["mac"] = ""
95          end
96          if (type == "smap") then
97             row["vendor"] = luci.i18n.translate("No SIP devices")
98          else
99             row["vendor"] = luci.i18n.translate("No devices detected")
100          end
101          row["type"] = luci.i18n.translate("check other networks")
102          if (not mini) then
103             row["model"] = ""
104          end
105          if (haslink) then
106             row["config_page"] = ""
107          end
108          if (debug) then
109             row["raw"] = output
110          end
111          table.insert(netdevs, row)
112       end        
113       local s
114       if (type == "smap") then
115          if (mini) then
116             s = devmap:section(luci.cbi.Table, netdevs, luci.i18n.translate("SIP devices discovered for") .. " " .. curnet)
117          else
118             local interfacestring = ""
119             if ( interface ~= "" ) then
120                interfacestring = ", " .. interface
121             end
122             s = devmap:section(luci.cbi.Table, netdevs, luci.i18n.translate("SIP devices discovered for") .. " " .. curnet .. " (" .. subnet .. ":" .. ports .. interfacestring .. ")")
123          end
124          s.template = "diag/smapsection"
125       else
126          if (mini) then
127             s = devmap:section(luci.cbi.Table, netdevs, luci.i18n.translate("Devices discovered for") .. " " .. curnet)
128          else
129             local interfacestring = ""
130             if ( interface ~= "" ) then
131                interfacestring = ", " .. interface
132             end
133             s = devmap:section(luci.cbi.Table, netdevs, luci.i18n.translate("Devices discovered for") .. " " .. curnet .. " (" .. subnet .. interfacestring .. ")")
134          end
135       end
136       s:option(DummyValue, "ip", translate("IP Address"))
137       if (not mini) then
138          s:option(DummyValue, "mac", translate("MAC Address"))
139       end
140       s:option(DummyValue, "vendor", translate("Vendor"))
141       s:option(DummyValue, "type", translate("Device Type"))
142       if (not mini) then
143          s:option(DummyValue, "model", translate("Model"))
144       end
145       if (haslink) then
146          s:option(DummyValue, "config_page", translate("Link to Device"))
147       end
148       if (debug) then
149          s:option(DummyValue, "raw", translate("Raw"))
150       end
151       curnet = next(outnets, curnet)
152    end
153 end
154
155 function get_network_device(interface)
156    local state = luci.model.uci.cursor_state()
157    state:load("network")
158    local dev
159    
160    return state:get("network", interface, "ifname")
161 end
162
163
164 function cbi_add_networks(field)
165         uci.cursor():foreach("network", "interface",
166                 function (section)
167                         if section[".name"] ~= "loopback" then
168                                 field:value(section[".name"])
169                         end
170                 end
171         )
172         field.titleref = luci.dispatcher.build_url("admin", "network", "network")
173 end
174
175 function config_devinfo_scan(map, scannet)
176    local o
177    o = scannet:option(luci.cbi.Flag, "enable", translate("Enable"))
178    o.optional = false
179    o.rmempty = false
180
181    o = scannet:option(luci.cbi.Value, "interface", translate("Interface"))
182    o.optional = false
183    luci.controller.luci_diag.devinfo_common.cbi_add_networks(o)
184    
185    local scansubnet
186    scansubnet = scannet:option(luci.cbi.Value, "subnet", translate("Subnet"))
187    scansubnet.optional = false
188    
189    o = scannet:option(luci.cbi.Value, "timeout", translate("Timeout"), translate("Time to wait for responses in seconds (default 10)"))
190    o.optional = true
191    
192    o = scannet:option(luci.cbi.Value, "repeat_count", translate("Repeat Count"), translate("Number of times to send requests (default 1)"))
193    o.optional = true
194    
195    o = scannet:option(luci.cbi.Value, "sleepreq", translate("Sleep Between Requests"), translate("Milliseconds to sleep between requests (default 100)"))
196    o.optional = true
197 end