Skip to content

Commit

Permalink
[fix] Ensure pipes are always closed #15 #32
Browse files Browse the repository at this point in the history
Fixes #15
Fixes #32
  • Loading branch information
devkapilbansal committed Aug 15, 2021
1 parent 01cd0f3 commit 3740362
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
8 changes: 6 additions & 2 deletions openwrt-openwisp-monitoring/files/lib/openwisp/interfaces.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ local specialized_interfaces={
modemmanager=function(_, interface)
local modem=uci_cursor.get('network', interface['interface'], 'device')
local info={}
local general=io.popen('mmcli --output-json -m '..modem):read("*a")
local general_file=io.popen('mmcli --output-json -m '..modem)
local general=general_file:read("*a")
general_file:close()
if general and pcall(cjson.decode, general) then
general=cjson.decode(general)
general=general.modem
Expand All @@ -41,7 +43,9 @@ local specialized_interfaces={
end
end

local signal=io.popen('mmcli --output-json -m '..modem..' --signal-get'):read("*a")
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
Expand Down
18 changes: 14 additions & 4 deletions openwrt-openwisp-monitoring/files/lib/openwisp/neighbors.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
-- retrieve neighbors information
local cjson=require('cjson')
local io=require('io')
local utils=require('openwisp.monitoring_utils')

local neighbors={}

-- parse /proc/net/arp
function neighbors.parse_arp()
local arp_info={}
for line in io.popen('cat /proc/net/arp 2> /dev/null'):lines() do
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()
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+)")
table.insert(arp_info, {
Expand All @@ -21,7 +27,9 @@ end

function neighbors.get_ip_neigh_json()
local arp_info={}
local output=io.popen('ip -json neigh 2> /dev/null'):read()
local output_file=io.popen('ip -json neigh 2> /dev/null')
local output=output_file:read('*a')
output_file:close()
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 @@ -38,8 +46,10 @@ end

function neighbors.get_ip_neigh()
local arp_info={}
local output=io.popen('ip neigh 2> /dev/null')
for line in output:lines() do
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 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
@@ -1,12 +1,15 @@
-- retrieve resources usage
local io=require('io')
local utils=require('openwisp.monitoring_utils')

local resources={}

function resources.parse_disk_usage()
local file=io.popen('df')
local disk_usage_info={}
for line in file:lines() do
local disk_usage_file=io.popen('df')
local disk_usage = disk_usage_file:read("*a")
disk_usage_file:close()
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+)')
Expand All @@ -24,14 +27,14 @@ function resources.parse_disk_usage()
end
end
end
file:close()
return disk_usage_info
end

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ local monitoring=require('openwisp.monitoring')
-- collect system info
local system_info=ubus:call('system', 'info', {})
local board=ubus:call('system', 'board', {})
local loadavg_output=io.popen('cat /proc/loadavg'):read()
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])}

Expand Down

0 comments on commit 3740362

Please sign in to comment.