Merge branch 'slim-down-lua-scripts' of https://github.com/FreifunkBremen/gluon

This commit is contained in:
Matthias Schiffer 2015-09-03 15:29:27 +02:00
commit 3599d8912b
19 changed files with 76 additions and 64 deletions

View File

@ -2,7 +2,6 @@
local announce = require 'gluon.announce' local announce = require 'gluon.announce'
local json = require 'luci.jsonc' local json = require 'luci.jsonc'
local ltn12 = require 'luci.ltn12'
local announce_dir = '/lib/gluon/announce/' .. arg[1] .. '.d' local announce_dir = '/lib/gluon/announce/' .. arg[1] .. '.d'

View File

@ -1,8 +1,8 @@
local n = 0 local n = 0
local cpus = util.trim(fs.readfile('/sys/devices/system/cpu/online')) local cpus = util.readline(io.open('/sys/devices/system/cpu/online'))
for _, entry in ipairs(cpus:split(',')) do for entry in cpus:gmatch('([^,]+)') do
local x, y = entry:match('(%d+)-(%d+)') local x, y = entry:match('(%d+)-(%d+)')
if x then if x then
n = n + tonumber(y) - tonumber(x) + 1 n = n + tonumber(y) - tonumber(x) + 1

View File

@ -1,4 +1,4 @@
return { return {
base = 'gluon-' .. util.trim(fs.readfile('/lib/gluon/gluon-version')), base = 'gluon-' .. util.readline(io.open('/lib/gluon/gluon-version')),
release = util.trim(fs.readfile('/lib/gluon/release')), release = util.readline(io.open('/lib/gluon/release')),
} }

View File

@ -1,3 +1 @@
local site = require 'gluon.site_config' return require('gluon.site_config').site_code
return site.site_code

View File

@ -1 +1 @@
return tonumber(fs.readfile('/proc/uptime'):match('^[^ ]+ ([^ ]+)')) return tonumber(util.readline(io.open('/proc/uptime')):match('^[^ ]+ ([^ ]+)'))

View File

@ -1 +1 @@
return tonumber(fs.readfile('/proc/loadavg'):match('^([^ ]+) ')) return tonumber(util.readline(io.open('/proc/loadavg')):match('^([^ ]+) '))

View File

@ -1,4 +1,4 @@
local data = fs.readfile('/proc/meminfo') local data = io.open('/proc/meminfo'):read('*a')
local fields = {} local fields = {}
for k, v in data:gmatch('([^\n:]+):%s*(%d+) kB') do for k, v in data:gmatch('([^\n:]+):%s*(%d+) kB') do

View File

@ -1,3 +1,3 @@
local running, total = fs.readfile('/proc/loadavg'):match('^[^ ]+ [^ ]+ [^ ]+ (%d+)/(%d+)') local running, total = util.readline(io.open('/proc/loadavg')):match('^[^ ]+ [^ ]+ [^ ]+ (%d+)/(%d+)')
return { running = tonumber(running), total = tonumber(total) } return { running = tonumber(running), total = tonumber(total) }

View File

@ -1 +1 @@
return tonumber(fs.readfile('/proc/uptime'):match('^([^ ]+) ')) return tonumber(util.readline(io.open('/proc/uptime')):match('^([^ ]+) '))

View File

@ -4,7 +4,7 @@ module('gluon.announce', package.seeall)
fs = require 'nixio.fs' fs = require 'nixio.fs'
uci = require('luci.model.uci').cursor() uci = require('luci.model.uci').cursor()
util = require 'luci.util' util = require 'gluon.util'
local function collect_entry(entry) local function collect_entry(entry)
if fs.stat(entry, 'type') == 'dir' then if fs.stat(entry, 'type') == 'dir' then

View File

@ -38,7 +38,7 @@ local uci = require('luci.model.uci').cursor()
module 'gluon.util' module 'gluon.util'
function exec(...) function exec(...)
return os.execute(escape_args('', ...)) return os.execute(escape_args('', 'exec', ...))
end end
-- Removes all lines starting with a prefix from a file, optionally adding a new one -- Removes all lines starting with a prefix from a file, optionally adding a new one
@ -52,6 +52,12 @@ function replace_prefix(file, prefix, add)
os.rename(tmp, file) os.rename(tmp, file)
end end
function readline(fd)
local line = fd:read('*l')
fd:close()
return line
end
function lock(file) function lock(file)
exec('lock', file) exec('lock', file)
end end

View File

@ -5,7 +5,7 @@ function ifname2address(ifname)
if ifname_address_cache[ifname] ~= nil then if ifname_address_cache[ifname] ~= nil then
ifaddress = ifname_address_cache[ifname] ifaddress = ifname_address_cache[ifname]
else else
ifaddress = util.trim(fs.readfile("/sys/class/net/" .. ifname .. "/address")) ifaddress = util.readline(io.open("/sys/class/net/" .. ifname .. "/address"))
ifname_address_cache[ifname] = ifaddress ifname_address_cache[ifname] = ifaddress
end end

View File

@ -16,18 +16,17 @@ end
function interfaces() function interfaces()
local interfaces = {} local interfaces = {}
for _, line in ipairs(util.split(util.exec('batctl if'))) do local popen = io.popen('exec batctl if')
ifname = line:match('^(.-): active') for ifname in popen:read('*a'):gmatch('([^%s]+): active') do
if ifname ~= nil then pcall(function()
pcall(function() local address = util.readline(io.open('/sys/class/net/' .. ifname .. '/address'))
local address = util.trim(fs.readfile('/sys/class/net/' .. ifname .. '/address')) local wifitype = iwinfo.type(ifname)
local wifitype = iwinfo.type(ifname) if wifitype ~= nil then
if wifitype ~= nil then interfaces[address] = { ifname = ifname, iw = iwinfo[wifitype] }
interfaces[address] = { ifname = ifname, iw = iwinfo[wifitype] } end
end end)
end)
end
end end
popen:close()
return interfaces return interfaces
end end

View File

@ -1,5 +1,3 @@
local list = util.exec('batctl if')
local wireless = {} local wireless = {}
local tunnel = {} local tunnel = {}
local other = {} local other = {}
@ -7,21 +5,27 @@ local other = {}
local function get_address(t, ifname) local function get_address(t, ifname)
pcall( pcall(
function() function()
table.insert(t, util.trim(fs.readfile('/sys/class/net/' .. ifname .. '/address'))) table.insert(t, util.readline(io.open('/sys/class/net/' .. ifname .. '/address')))
end end
) )
end end
local function is_wireless(ifname) local function file_exists(filename)
local type = fs.stat('/sys/class/net/' .. ifname .. '/wireless', 'type') local f = io.open(filename)
if f == nil then
return false
else
f:close()
return true
end
end
return type == 'directory' local function is_wireless(ifname)
return file_exists('/sys/class/net/' .. ifname .. '/wireless')
end end
local function is_tuntap(ifname) local function is_tuntap(ifname)
local type = fs.stat('/sys/class/net/' .. ifname .. '/tun_flags', 'type') return file_exists('/sys/class/net/' .. ifname .. '/tun_flags')
return type == 'regular'
end end
local function nil_table(t) local function nil_table(t)
@ -32,18 +36,17 @@ local function nil_table(t)
end end
end end
for _, line in ipairs(util.split(list)) do local popen = io.popen('exec batctl if')
local ifname = line:match('^(.-):') for ifname in popen:read('*a'):gmatch('([^%s]+): active') do
if ifname ~= nil then if is_wireless(ifname) then
if is_wireless(ifname) then get_address(wireless, ifname)
get_address(wireless, ifname) elseif is_tuntap(ifname) then
elseif is_tuntap(ifname) then get_address(tunnel, ifname)
get_address(tunnel, ifname) else
else get_address(other, ifname)
get_address(other, ifname)
end
end end
end end
popen:close()
return { return {
wireless = nil_table(wireless), wireless = nil_table(wireless),

View File

@ -1,15 +1,13 @@
local list = util.exec('batctl if')
local interfaces = {} local interfaces = {}
for _, line in ipairs(util.split(list)) do
local ifname = line:match('^(.-):') local popen = io.popen('exec batctl if')
if ifname ~= nil then for ifname in popen:read('*a'):gmatch('([^%s]+): active') do
pcall( pcall(
function() function()
table.insert(interfaces, util.trim(fs.readfile('/sys/class/net/' .. ifname .. '/address'))) table.insert(interfaces, util.readline(io.open('/sys/class/net/' .. ifname .. '/address')))
end end
) )
end
end end
popen:close()
return interfaces return interfaces

View File

@ -1 +1 @@
return util.trim(fs.readfile('/sys/module/batman_adv/version')) return util.readline(io.open('/sys/module/batman_adv/version'))

View File

@ -1,5 +1,14 @@
local gateway = util.trim(util.exec("batctl -m bat0 gateways | awk '/^=>/ { print $2 }'")) local gateway = ''
local popen = io.popen("exec batctl -m bat0 gateways")
for line in popen:lines() do
if line:sub(1, 3) == '=> ' then
gateway = line:sub(4, 20)
break
end
end
popen:close()
if gateway ~= '' then if gateway ~= '' then
return gateway return gateway
end end

View File

@ -1,4 +1,5 @@
return { local ret = {
enabled = uci:get_bool('fastd', 'mesh_vpn', 'enabled'), enabled = uci:get('fastd', 'mesh_vpn', 'enabled') ~= 0,
version = util.exec('fastd -v'):match('^[^%s]+%s+([^\n]+)'), version = util.readline(io.popen('exec fastd -v')):match('^[^%s]+%s+(.+)'),
} }
return ret

View File

@ -2,7 +2,6 @@ local json = require 'luci.jsonc'
local ltn12 = require 'luci.ltn12' local ltn12 = require 'luci.ltn12'
local nixio = require 'nixio' local nixio = require 'nixio'
local site = require 'gluon.site_config' local site = require 'gluon.site_config'
local uci = require('luci.model.uci').cursor()
local fastd_sock = nixio.socket('unix', 'stream') local fastd_sock = nixio.socket('unix', 'stream')
local socket_path = uci:get('fastd', 'mesh_vpn', 'status_socket') local socket_path = uci:get('fastd', 'mesh_vpn', 'status_socket')