Optimise luci.sys

This commit is contained in:
Steven Barth 2009-07-10 13:33:48 +00:00
parent 6ea0b5eb45
commit 223963e419

View file

@ -219,26 +219,14 @@ end
-- @param bytes Number of bytes for the unique id -- @param bytes Number of bytes for the unique id
-- @return String containing hex encoded id -- @return String containing hex encoded id
function uniqueid(bytes) function uniqueid(bytes)
local fp = io.open("/dev/urandom") local rand = luci.fs.readfile("/dev/urandom", bytes)
local chunk = { fp:read(bytes):byte(1, bytes) } return rand and nixio.bin.hexlify(rand)
fp:close()
local hex = ""
local pattern = "%02X"
for i, byte in ipairs(chunk) do
hex = hex .. pattern:format(byte)
end
return hex
end end
--- Returns the current system uptime stats. --- Returns the current system uptime stats.
-- @return String containing total uptime in seconds -- @return String containing total uptime in seconds
-- @return String containing idle time in seconds
function uptime() function uptime()
local loadavg = io.lines("/proc/uptime")() return nixio.sysinfo().uptime
return loadavg:match("^(.-) (.-)$")
end end
@ -331,25 +319,43 @@ end
--- Determine the names of available network interfaces. --- Determine the names of available network interfaces.
-- @return Table containing all current interface names -- @return Table containing all current interface names
function net.devices() function net.devices()
local devices = {} local devs = {}
for line in io.lines("/proc/net/dev") do for k, v in ipairs(nixio.getifaddrs()) do
table.insert(devices, line:match(" *(.-):")) if v.family == "packet" then
devs[#devs+1] = v.name
end
end end
return devices return devs
end end
--- Return information about available network interfaces. --- Return information about available network interfaces.
-- @return Table containing all current interface names and their information -- @return Table containing all current interface names and their information
function net.deviceinfo() function net.deviceinfo()
local devices = {} local devs = {}
for line in io.lines("/proc/net/dev") do for k, v in ipairs(nixio.getifaddrs()) do
local name, data = line:match("^ *(.-): *(.*)$") if v.family == "packet" then
if name and data then local d = v.data
devices[name] = luci.util.split(data, " +", nil, true) d[1] = d.rx_bytes
d[2] = d.rx_packets
d[3] = d.rx_errors
d[4] = d.rx_dropped
d[5] = 0
d[6] = 0
d[7] = 0
d[8] = d.multicast
d[9] = d.tx_bytes
d[10] = d.tx_packets
d[11] = d.tx_errors
d[12] = d.tx_dropped
d[13] = 0
d[14] = d.collisions
d[15] = 0
d[16] = 0
devs[v.name] = d
end end
end end
return devices return devs
end end