Skip to content

Commit

Permalink
[refactor] Set max_line_length to 88 in LuaCheck and 86 in LuaFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
devkapilbansal committed Aug 16, 2021
1 parent db7823f commit 706182a
Show file tree
Hide file tree
Showing 20 changed files with 283 additions and 89 deletions.
2 changes: 2 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
files["openwrt-openwisp-monitoring/tests"]={
ignore={"Test.*"}
}

max_line_length=88
11 changes: 9 additions & 2 deletions openwrt-openwisp-monitoring/files/lib/openwisp/dhcp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ function dhcp.parse_dhcp_lease_file(path, leases)
local f = io.open(path, 'r')
if not f then return leases end
for line in f:lines() do
local expiry, mac, ip, name, id = line:match('(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+(%S+)')
table.insert(leases, {expiry = tonumber(expiry), mac = mac, ip = ip, client_name = name, client_id = id})
local expiry, mac, ip, name, id = line:match(
'(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+(%S+)')
table.insert(leases, {
expiry = tonumber(expiry),
mac = mac,
ip = ip,
client_name = name,
client_id = id
})
end

return leases
Expand Down
37 changes: 28 additions & 9 deletions openwrt-openwisp-monitoring/files/lib/openwisp/interfaces.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,25 @@ local specialized_interfaces = {
end
end

local signal_file = io.popen('mmcli --output-json -m ' .. modem .. ' --signal-get')
local signal_file =
io.popen('mmcli --output-json -m ' .. modem .. ' --signal-get')
local signal = signal_file:read("*a")
signal_file:close()
if signal and pcall(cjson.decode, signal) then
signal = cjson.decode(signal)
-- only send data if not empty to avoid generating too much traffic
if not utils.is_table_empty(signal.modem) and not utils.is_table_empty(signal.modem.signal) then
if not utils.is_table_empty(signal.modem) and
not utils.is_table_empty(signal.modem.signal) then
-- omit refresh rate
signal.modem.signal.refresh = nil
info.signal = {}
-- collect section and values only if not empty
for section_key, section_values in pairs(signal.modem.signal) do
for key, value in pairs(section_values) do
if value ~= '--' then
if utils.is_table_empty(info.signal[section_key]) then info.signal[section_key] = {} end
if utils.is_table_empty(info.signal[section_key]) then
info.signal[section_key] = {}
end
info.signal[section_key][key] = tonumber(value)
end
end
Expand All @@ -68,7 +72,9 @@ local specialized_interfaces = {
}

function interfaces.find_default_gateway(routes)
for i = 1, #routes do if routes[i].target == '0.0.0.0' then return routes[i].nexthop end end
for i = 1, #routes do
if routes[i].target == '0.0.0.0' then return routes[i].nexthop end
end
return nil
end

Expand Down Expand Up @@ -133,7 +139,12 @@ function interfaces.get_addresses(name)
end
if family == 'ipv4' or family == 'ipv6' then
if not utils.has_value(addresses_list, addr) then
table.insert(addresses, {address = addr, mask = nixio_data[i].prefix, proto = proto, family = family})
table.insert(addresses, {
address = addr,
mask = nixio_data[i].prefix,
proto = proto,
family = family
})
end
end
end
Expand All @@ -146,14 +157,20 @@ function interfaces.get_interface_info(name, netjson_interface)
local info = {dns_search = nil, dns_servers = nil}
for _, interface in pairs(interface_data['interface']) do
if interface['l3_device'] == name then
if next(interface['dns-search']) then info.dns_search = interface['dns-search'] end
if next(interface['dns-server']) then info.dns_servers = interface['dns-server'] end
if next(interface['dns-search']) then
info.dns_search = interface['dns-search']
end
if next(interface['dns-server']) then
info.dns_servers = interface['dns-server']
end
if netjson_interface.type == 'bridge' then
info.stp = uci_cursor.get('network', interface['interface'], 'stp') == '1'
end
-- collect specialized info if available
local specialized_info = specialized_interfaces[interface.proto]
if specialized_info then info.specialized = specialized_info(name, interface) end
if specialized_info then
info.specialized = specialized_info(name, interface)
end
end
end
return info
Expand All @@ -166,7 +183,9 @@ function interfaces.get_vpn_interfaces()

if utils.is_table_empty(items) then return {} end

for _, config in pairs(items) do if config and config.dev then vpn_interfaces[config.dev] = true end end
for _, config in pairs(items) do
if config and config.dev then vpn_interfaces[config.dev] = true end
end
return vpn_interfaces
end

Expand Down
10 changes: 7 additions & 3 deletions openwrt-openwisp-monitoring/files/lib/openwisp/neighbors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ function neighbors.parse_arp()
arp_data_file:close()
for _, line in ipairs(utils.split(arp_data, "\n")) do
if line:sub(1, 10) ~= 'IP address' then
local ip, _, _, mac, _, dev = line:match("(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+(%S+)")
local ip, _, _, mac, _, dev = line:match(
"(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+(%S+)")
table.insert(arp_info, {ip = ip, mac = mac, interface = dev, state = ''})
end
end
Expand Down Expand Up @@ -45,8 +46,11 @@ function neighbors.get_ip_neigh()
local neigh_data = neigh_data_file:read("*a")
neigh_data_file:close()
for _, line in ipairs(utils.split(neigh_data, "\n")) do
local ip, dev, mac, state = line:match("(%S+)%s+dev%s+(%S+)%s+lladdr%s+(%S+).*%s(%S+)")
if mac ~= nil then table.insert(arp_info, {ip = ip, mac = mac, interface = dev, state = state}) end
local ip, dev, mac, state = line:match(
"(%S+)%s+dev%s+(%S+)%s+lladdr%s+(%S+).*%s(%S+)")
if mac ~= nil then
table.insert(arp_info, {ip = ip, mac = mac, interface = dev, state = state})
end
end
return arp_info
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function resources.parse_disk_usage()
for _, line in ipairs(utils.split(disk_usage, "\n")) do
if line:sub(1, 10) ~= 'Filesystem' then
local filesystem, size, used, available, percent, location = line:match(
'(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+(%S+)')
'(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+(%S+)')
if filesystem ~= 'tmpfs' and not string.match(filesystem, 'overlayfs') then
percent = percent:gsub('%W', '')
-- available, size and used are in KiB
Expand Down
3 changes: 2 additions & 1 deletion openwrt-openwisp-monitoring/files/lib/openwisp/wifi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ end

-- takes ubus wireless.status clients output and converts it to NetJSON
function wifi.netjson_clients(clients, is_mesh)
return (is_mesh and wifi.parse_iwinfo_clients(clients) or wifi.parse_hostapd_clients(clients))
return (is_mesh and wifi.parse_iwinfo_clients(clients) or
wifi.parse_hostapd_clients(clients))
end

return wifi
34 changes: 26 additions & 8 deletions openwrt-openwisp-monitoring/files/sbin/netjson-monitoring.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@ local loadavg_file = io.popen('cat /proc/loadavg')
local loadavg_output = loadavg_file:read()
loadavg_file:close()
loadavg_output = monitoring.utils.split(loadavg_output, ' ')
local load_average = {tonumber(loadavg_output[1]), tonumber(loadavg_output[2]), tonumber(loadavg_output[3])}
local load_average = {
tonumber(loadavg_output[1]), tonumber(loadavg_output[2]),
tonumber(loadavg_output[3])
}

-- init netjson data structure
local netjson = {
type = 'DeviceMonitoring',
general = {hostname = board.hostname, local_time = system_info.localtime, uptime = system_info.uptime},
general = {
hostname = board.hostname,
local_time = system_info.localtime,
uptime = system_info.uptime
},
resources = {
load = load_average,
memory = system_info.memory,
Expand All @@ -36,10 +43,14 @@ local netjson = {
}

local dhcp_leases = monitoring.dhcp.get_dhcp_leases()
if not monitoring.utils.is_table_empty(dhcp_leases) then netjson.dhcp_leases = dhcp_leases end
if not monitoring.utils.is_table_empty(dhcp_leases) then
netjson.dhcp_leases = dhcp_leases
end

local host_neighbors = monitoring.neighbors.get_neighbors()
if not monitoring.utils.is_table_empty(host_neighbors) then netjson.neighbors = host_neighbors end
if not monitoring.utils.is_table_empty(host_neighbors) then
netjson.neighbors = host_neighbors
end

-- determine the interfaces to monitor
local arg = {...}
Expand Down Expand Up @@ -90,7 +101,8 @@ for _, radio in pairs(wireless_status) do
if hostapd_output then clients = hostapd_output.clients end
end
if not monitoring.utils.is_table_empty(clients) then
netjson_interface.wireless.clients = monitoring.wifi.netjson_clients(clients, is_mesh)
netjson_interface.wireless.clients =
monitoring.wifi.netjson_clients(clients, is_mesh)
end
wireless_interfaces[name] = netjson_interface
end
Expand Down Expand Up @@ -154,11 +166,17 @@ for name, interface in pairs(network_status) do
if next(addresses) then netjson_interface.addresses = addresses end
local info = monitoring.interfaces.get_interface_info(name, netjson_interface)
if info.stp ~= nil then netjson_interface.stp = info.stp end
if info.specialized then for key, value in pairs(info.specialized) do netjson_interface[key] = value end end
if info.specialized then
for key, value in pairs(info.specialized) do netjson_interface[key] = value end
end
table.insert(host_interfaces, netjson_interface)
-- DNS info is independent from interface
if info.dns_servers then monitoring.utils.array_concat(info.dns_servers, dns_servers) end
if info.dns_search then monitoring.utils.array_concat(info.dns_search, dns_search) end
if info.dns_servers then
monitoring.utils.array_concat(info.dns_servers, dns_servers)
end
if info.dns_search then
monitoring.utils.array_concat(info.dns_search, dns_search)
end
end
end

Expand Down
6 changes: 5 additions & 1 deletion openwrt-openwisp-monitoring/luaformat.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
column_limit: 120
column_limit: 86
indent_width: 2
tab_width: 2
continuation_indent_width: 2
column_table_limit: 86
line_breaks_after_function_body: 1
align_args: false
align_parameter: false
9 changes: 8 additions & 1 deletion openwrt-openwisp-monitoring/tests/basic_env.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ env.ubus = {
end
}

env.uci = {cursor = function() return {get_all = function(...) return nil end, get = function(...) return nil end} end}
env.uci = {
cursor = function()
return {
get_all = function(...) return nil end,
get = function(...) return nil end
}
end
}

return env
6 changes: 4 additions & 2 deletions openwrt-openwisp-monitoring/tests/main_env.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ env.uci = {
elseif arg[1] == 'network' and arg[3] == 'stp' then
return '1'
elseif arg[1] == 'network' and arg[3] == 'device' then
return '/sys/devices/platform/soc/8af8800.usb3/8a00000.dwc3/xhci-hcd.0.auto/usb2/2-1'
return '/sys/devices/platform/soc/8af8800.usb3/8a00000.dwc3/' ..
'xhci-hcd.0.auto/usb2/2-1'
end
end
}
Expand All @@ -45,7 +46,8 @@ env.io = {
elseif arg == 'ip neigh 2> /dev/null' then
return io.open(test_file_dir .. 'ip_neigh.txt')
else
local modem = '/sys/devices/platform/soc/8af8800.usb3/8a00000.dwc3/xhci-hcd.0.auto/usb2/2-1'
local modem =
'/sys/devices/platform/soc/8af8800.usb3/8a00000.dwc3/xhci-hcd.0.auto/usb2/2-1'
if arg == 'mmcli --output-json -m ' .. modem then
return io.open(test_file_dir .. 'modem_data.txt')
elseif arg == 'mmcli --output-json -m ' .. modem .. ' --signal-get' then
Expand Down
6 changes: 4 additions & 2 deletions openwrt-openwisp-monitoring/tests/test_dhcp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ function TestDhcp.test_dhcp_leases()
local dhcp_functions = require('dhcp')

luaunit.assertEquals(dhcp_functions.get_dhcp_leases(), dhcp_data.leases)
luaunit.assertEquals(dhcp_functions.parse_dhcp_lease_file('/tmp/dhcp.leases', {}), dhcp_data.leases)
luaunit.assertEquals(dhcp_functions.parse_dhcp_lease_file('/tmp/no_dhcp.leases', {}), {})
luaunit.assertEquals(dhcp_functions.parse_dhcp_lease_file('/tmp/dhcp.leases', {}),
dhcp_data.leases)
luaunit.assertEquals(
dhcp_functions.parse_dhcp_lease_file('/tmp/no_dhcp.leases', {}), {})
end

function TestNetJSON.test_netjson_monitoring_dhcp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ test_data.eth2_interface = {
neighbors = {},
pending = false,
proto = "dhcp",
route = {{mask = 0, nexthop = "192.168.0.1", source = "192.168.0.144/32", target = "0.0.0.0"}},
route = {
{
mask = 0,
nexthop = "192.168.0.1",
source = "192.168.0.144/32",
target = "0.0.0.0"
}
},
up = true,
updated = {"addresses", "routes", "data"},
uptime = 1973
Expand Down
9 changes: 8 additions & 1 deletion openwrt-openwisp-monitoring/tests/test_files/dhcp_data.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ dhcp.config = {
loglevel = "4",
maindhcp = "0"
},
wan = {[".anonymous"] = false, [".index"] = 2, [".name"] = "wan", [".type"] = "dhcp", ignore = "1", interface = "wan"}
wan = {
[".anonymous"] = false,
[".index"] = 2,
[".name"] = "wan",
[".type"] = "dhcp",
ignore = "1",
interface = "wan"
}
}

dhcp.leases = {
Expand Down
Loading

0 comments on commit 706182a

Please sign in to comment.