Skip to content

Commit

Permalink
[refactor] Split functions in multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
devkapilbansal committed May 9, 2021
1 parent 277e109 commit f1de4ff
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 223 deletions.
78 changes: 78 additions & 0 deletions address.lua → interface_functions.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env lua

utils = require('utils')
nixio = require('nixio')
ubus_lib = require('ubus')
Expand All @@ -10,6 +12,56 @@ ubus = ubus_lib.connect()
interface_data = ubus:call('network.interface', 'dump', {})
nixio_data = nixio.getifaddrs()

specialized_interfaces = {
modemmanager = function(name, interface)
local modem = uci_cursor.get('network', interface['interface'], 'device')
local info = {}

local general = io.popen('mmcli --output-json -m '..modem):read("*a")
if general and pcall(function () general = cjson.decode(general) end) then
general = general.modem

if not utils.is_table_empty(general['3gpp']) then
info.imei = general['3gpp'].imei
info.operator_name = general['3gpp']['operator-name']
info.operator_code = general['3gpp']['operator-code']
end

if not utils.is_table_empty(general.generic) then
info.manufacturer = general.generic.manufacturer
info.model = general.generic.model
info.connection_status = general.generic.state
info.power_status = general.generic['power-state']
end
end

local signal = io.popen('mmcli --output-json -m '..modem..' --signal-get'):read()
if signal and pcall(function () signal = cjson.decode(signal) end) then
-- 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
-- omit refresh rate
signal.modem.signal.refresh = nil
info.signal = {}
-- collect section 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
-- convert to number
section_values[key] = tonumber(value)
-- store in info
if utils.is_table_empty(info[section_key]) then
info.signal[section_key] = section_values
end
end
end
end
end
end

return {type='modem-manager', mobile=info}
end
}

function find_default_gateway(routes)
for i = 1, #routes do
if routes[i].target == '0.0.0.0' then
Expand Down Expand Up @@ -99,4 +151,30 @@ function get_addresses(name)
return addresses
end

function get_interface_info(name, netjson_interface)
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 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
end
end
return info
end

print(get_addresses('eth2'))
82 changes: 82 additions & 0 deletions miscellaneous_functions.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env lua

utils = require('utils')
uci = require('uci')
uci_cursor = uci.cursor()


function parse_hostapd_clients(clients)
local data = {}
for mac, properties in pairs(clients) do
properties.mac = mac
table.insert(data, properties)
end
return data
end

function parse_iwinfo_clients(clients)
local data = {}
for i, p in pairs(clients) do
client = {}
client.ht = p.rx.ht
client.mac = p.mac
client.authorized = p.authorized
client.vht = p.rx.vht
client.wmm = p.wme
client.mfp = p.mfp
client.auth = p.authenticated
client.signal = p.signal
client.noise = p.noise
table.insert(data, client)
end
return data
end

function parse_disk_usage()
file = io.popen('df')
disk_usage_info = {}
for line in file:lines() do
if line:sub(1, 10) ~= 'Filesystem' then
filesystem, size, used, available, percent, location =
line:match('(%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
table.insert(disk_usage_info, {
filesystem = filesystem,
available_bytes = tonumber(available) * 1024,
size_bytes = tonumber(size) * 1024,
used_bytes = tonumber(used) * 1024,
used_percent = tonumber(percent),
mount_point = location
})
end
end
end
file:close()
return disk_usage_info
end

function get_cpus()
processors = io.popen('cat /proc/cpuinfo | grep -c processor')
cpus = tonumber(processors:read('*a'))
processors:close()
return cpus
end

function get_vpn_interfaces()
-- only openvpn supported for now
local items = uci_cursor:get_all('openvpn')
local vpn_interfaces = {}

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

for name, config in pairs(items) do
if config and config.dev then
vpn_interfaces[config.dev] = true
end
end
return vpn_interfaces
end
2 changes: 2 additions & 0 deletions neighbors_functions.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env lua

local cjson = require('cjson')

netjson = {}
Expand Down
Loading

0 comments on commit f1de4ff

Please sign in to comment.