Skip to content

Commit 3740362

Browse files
[fix] Ensure pipes are always closed #15 #32
Fixes #15 Fixes #32
1 parent 01cd0f3 commit 3740362

4 files changed

Lines changed: 32 additions & 13 deletions

File tree

openwrt-openwisp-monitoring/files/lib/openwisp/interfaces.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ local specialized_interfaces={
2222
modemmanager=function(_, interface)
2323
local modem=uci_cursor.get('network', interface['interface'], 'device')
2424
local info={}
25-
local general=io.popen('mmcli --output-json -m '..modem):read("*a")
25+
local general_file=io.popen('mmcli --output-json -m '..modem)
26+
local general=general_file:read("*a")
27+
general_file:close()
2628
if general and pcall(cjson.decode, general) then
2729
general=cjson.decode(general)
2830
general=general.modem
@@ -41,7 +43,9 @@ local specialized_interfaces={
4143
end
4244
end
4345

44-
local signal=io.popen('mmcli --output-json -m '..modem..' --signal-get'):read("*a")
46+
local signal_file=io.popen('mmcli --output-json -m '..modem..' --signal-get')
47+
local signal = signal_file:read("*a")
48+
signal_file:close()
4549
if signal and pcall(cjson.decode, signal) then
4650
signal=cjson.decode(signal)
4751
-- only send data if not empty to avoid generating too much traffic

openwrt-openwisp-monitoring/files/lib/openwisp/neighbors.lua

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
-- retrieve neighbors information
22
local cjson=require('cjson')
33
local io=require('io')
4+
local utils=require('openwisp.monitoring_utils')
5+
46
local neighbors={}
7+
58
-- parse /proc/net/arp
69
function neighbors.parse_arp()
710
local arp_info={}
8-
for line in io.popen('cat /proc/net/arp 2> /dev/null'):lines() do
11+
local arp_data_file=io.popen('cat /proc/net/arp 2> /dev/null')
12+
local arp_data=arp_data_file:read("*a")
13+
arp_data_file:close()
14+
for _,line in ipairs(utils.split(arp_data, "\n")) do
915
if line:sub(1, 10) ~='IP address' then
1016
local ip, _, _, mac, _, dev=line:match("(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+(%S+)")
1117
table.insert(arp_info, {
@@ -21,7 +27,9 @@ end
2127

2228
function neighbors.get_ip_neigh_json()
2329
local arp_info={}
24-
local output=io.popen('ip -json neigh 2> /dev/null'):read()
30+
local output_file=io.popen('ip -json neigh 2> /dev/null')
31+
local output=output_file:read('*a')
32+
output_file:close()
2533
if output ~=nil and pcall(cjson.decode, output) then
2634
local json_output=cjson.decode(output)
2735
for _, arp_entry in pairs(json_output) do
@@ -38,8 +46,10 @@ end
3846

3947
function neighbors.get_ip_neigh()
4048
local arp_info={}
41-
local output=io.popen('ip neigh 2> /dev/null')
42-
for line in output:lines() do
49+
local neigh_data_file=io.popen('ip neigh 2> /dev/null')
50+
local neigh_data=neigh_data_file:read("*a")
51+
neigh_data_file:close()
52+
for _,line in ipairs(utils.split(neigh_data, "\n")) do
4353
local ip, dev, mac, state=line:match("(%S+)%s+dev%s+(%S+)%s+lladdr%s+(%S+).*%s(%S+)")
4454
if mac ~=nil then
4555
table.insert(arp_info, {

openwrt-openwisp-monitoring/files/lib/openwisp/resources.lua

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
-- retrieve resources usage
22
local io=require('io')
3+
local utils=require('openwisp.monitoring_utils')
34

45
local resources={}
56

67
function resources.parse_disk_usage()
7-
local file=io.popen('df')
88
local disk_usage_info={}
9-
for line in file:lines() do
9+
local disk_usage_file=io.popen('df')
10+
local disk_usage = disk_usage_file:read("*a")
11+
disk_usage_file:close()
12+
for _,line in ipairs(utils.split(disk_usage, "\n")) do
1013
if line:sub(1, 10) ~='Filesystem' then
1114
local filesystem, size, used, available, percent, location=
1215
line:match('(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+(%S+)')
@@ -24,14 +27,14 @@ function resources.parse_disk_usage()
2427
end
2528
end
2629
end
27-
file:close()
2830
return disk_usage_info
2931
end
3032

3133
function resources.get_cpus()
32-
local processors=io.popen('cat /proc/cpuinfo | grep -c processor')
33-
local cpus=tonumber(processors:read('*a'))
34-
processors:close()
34+
local processors_file=io.popen('cat /proc/cpuinfo | grep -c processor')
35+
local processors=processors_file:read('*a')
36+
processors_file:close()
37+
local cpus=tonumber(processors)
3538
return cpus
3639
end
3740

openwrt-openwisp-monitoring/files/sbin/netjson-monitoring.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ local monitoring=require('openwisp.monitoring')
1717
-- collect system info
1818
local system_info=ubus:call('system', 'info', {})
1919
local board=ubus:call('system', 'board', {})
20-
local loadavg_output=io.popen('cat /proc/loadavg'):read()
20+
local loadavg_file=io.popen('cat /proc/loadavg')
21+
local loadavg_output=loadavg_file:read()
22+
loadavg_file:close()
2123
loadavg_output=monitoring.utils.split(loadavg_output, ' ')
2224
local load_average={tonumber(loadavg_output[1]), tonumber(loadavg_output[2]), tonumber(loadavg_output[3])}
2325

0 commit comments

Comments
 (0)