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 json = require 'luci.jsonc'
local ltn12 = require 'luci.ltn12'
local announce_dir = '/lib/gluon/announce/' .. arg[1] .. '.d'

View File

@ -1,8 +1,8 @@
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+)')
if x then
n = n + tonumber(y) - tonumber(x) + 1

View File

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

View File

@ -1,3 +1 @@
local site = require 'gluon.site_config'
return site.site_code
return require('gluon.site_config').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 = {}
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) }

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'
uci = require('luci.model.uci').cursor()
util = require 'luci.util'
util = require 'gluon.util'
local function collect_entry(entry)
if fs.stat(entry, 'type') == 'dir' then

View File

@ -38,7 +38,7 @@ local uci = require('luci.model.uci').cursor()
module 'gluon.util'
function exec(...)
return os.execute(escape_args('', ...))
return os.execute(escape_args('', 'exec', ...))
end
-- 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)
end
function readline(fd)
local line = fd:read('*l')
fd:close()
return line
end
function lock(file)
exec('lock', file)
end

View File

@ -5,7 +5,7 @@ function ifname2address(ifname)
if ifname_address_cache[ifname] ~= nil then
ifaddress = ifname_address_cache[ifname]
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
end

View File

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

View File

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

View File

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

View File

@ -1,4 +1,5 @@
return {
enabled = uci:get_bool('fastd', 'mesh_vpn', 'enabled'),
version = util.exec('fastd -v'):match('^[^%s]+%s+([^\n]+)'),
local ret = {
enabled = uci:get('fastd', 'mesh_vpn', 'enabled') ~= 0,
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 nixio = require 'nixio'
local site = require 'gluon.site_config'
local uci = require('luci.model.uci').cursor()
local fastd_sock = nixio.socket('unix', 'stream')
local socket_path = uci:get('fastd', 'mesh_vpn', 'status_socket')