Skip to content

Commit

Permalink
[refactor] Created separate peopen functions in files
Browse files Browse the repository at this point in the history
  • Loading branch information
devkapilbansal committed Aug 12, 2021
1 parent 9833405 commit ba0c7a6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
15 changes: 9 additions & 6 deletions openwrt-openwisp-monitoring/files/lib/openwisp/interfaces.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ local interface_data=ubus:call('network.interface', 'dump', {})

local interfaces={}

function popen(command)
local command_file = io.popen(command)
local output = command_file:read("*a")
command_file:close()
return output
end

local specialized_interfaces={
modemmanager=function(_, interface)
local modem=uci_cursor.get('network', interface['interface'], 'device')
local info={}
local general_file=io.popen('mmcli --output-json -m '..modem)
local general=general_file:read("*a")
general_file:close()
local general=popen('mmcli --output-json -m '..modem)
if general and pcall(cjson.decode, general) then
general=cjson.decode(general)
general=general.modem
Expand All @@ -43,9 +48,7 @@ local specialized_interfaces={
end
end

local signal_file=io.popen('mmcli --output-json -m '..modem..' --signal-get')
local signal = signal_file:read("*a")
signal_file:close()
local signal=popen('mmcli --output-json -m '..modem..' --signal-get')
if signal and pcall(cjson.decode, signal) then
signal=cjson.decode(signal)
-- only send data if not empty to avoid generating too much traffic
Expand Down
21 changes: 11 additions & 10 deletions openwrt-openwisp-monitoring/files/lib/openwisp/neighbors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ local utils=require('openwisp.monitoring_utils')

local neighbors={}

function popen(command)
local command_file = io.popen(command)
local output = command_file:read("*a")
command_file:close()
return output
end

-- parse /proc/net/arp
function neighbors.parse_arp()
local arp_info={}
local arp_data_file=io.popen('cat /proc/net/arp 2> /dev/null')
local arp_data=arp_data_file:read("*a")
arp_data_file:close()
local arp_data=popen('cat /proc/net/arp 2> /dev/null')
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+)")
Expand All @@ -27,9 +32,7 @@ end

function neighbors.get_ip_neigh_json()
local arp_info={}
local output_file=io.popen('ip -json neigh 2> /dev/null')
local output=output_file:read('*a')
output_file:close()
local output=popen('ip -json neigh 2> /dev/null')
if output ~=nil and pcall(cjson.decode, output) then
local json_output=cjson.decode(output)
for _, arp_entry in pairs(json_output) do
Expand All @@ -46,10 +49,8 @@ end

function neighbors.get_ip_neigh()
local arp_info={}
local neigh_data_file=io.popen('ip neigh 2> /dev/null')
local neigh_data=neigh_data_file:read("*a")
neigh_data_file:close()
for _,line in ipairs(utils.split(neigh_data, "\n")) do
local output=popen('ip neigh 2> /dev/null')
for _,line in ipairs(utils.split(output, "\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, {
Expand Down
15 changes: 9 additions & 6 deletions openwrt-openwisp-monitoring/files/lib/openwisp/resources.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ local utils=require('openwisp.monitoring_utils')

local resources={}

local function popen(command)
local command_file = io.popen(command)
local output = command_file:read("*a")
command_file:close()
return output
end

function resources.parse_disk_usage()
local disk_usage_info={}
local disk_usage_file=io.popen('df')
local disk_usage = disk_usage_file:read("*a")
disk_usage_file:close()
local disk_usage=popen('df')
for _,line in ipairs(utils.split(disk_usage, "\n")) do
if line:sub(1, 10) ~='Filesystem' then
local filesystem, size, used, available, percent, location=
Expand All @@ -31,9 +36,7 @@ function resources.parse_disk_usage()
end

function resources.get_cpus()
local processors_file=io.popen('cat /proc/cpuinfo | grep -c processor')
local processors=processors_file:read('*a')
processors_file:close()
local processors=popen('cat /proc/cpuinfo | grep -c processor')
local cpus=tonumber(processors)
return cpus
end
Expand Down
11 changes: 8 additions & 3 deletions openwrt-openwisp-monitoring/files/sbin/netjson-monitoring.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ end

local monitoring=require('openwisp.monitoring')

local function popen(command)
local command_file = io.popen(command)
local output = command_file:read("*a")
command_file:close()
return output
end

-- collect system info
local system_info=ubus:call('system', 'info', {})
local board=ubus:call('system', 'board', {})
local loadavg_file=io.popen('cat /proc/loadavg')
local loadavg_output=loadavg_file:read()
loadavg_file:close()
local loadavg_output=popen('cat /proc/loadavg')
loadavg_output=monitoring.utils.split(loadavg_output, ' ')
local load_average={tonumber(loadavg_output[1]), tonumber(loadavg_output[2]), tonumber(loadavg_output[3])}

Expand Down

0 comments on commit ba0c7a6

Please sign in to comment.