-- acf model for displaying logfiles recusivly module (..., package.seeall) require("fs") require("date") require("format") -- ############################################################### -- Private functions local function querycmd ( cmdline ) local cmd = io.popen( format.escapespecialcharacters(cmdline) ) local cmd_result = cmd:read("*a") or "unknown" cmd:close() return cmd_result end local function diskfree ( media ) if not (media) then media = "" end local cmd = io.popen( "df -h " .. format.escapespecialcharacters(media) ) local cmd_result = cmd:read("*a") or "unknown" cmd:close() return cmd_result end local function getpercentage ( data,grep,field1,field2 ) local debugoutput = "" local grepline = format.string_to_table(string.match(data,grep .. ".*"),"%s+") local val1percentage = math.floor(((grepline[field1] / (grepline[field1]+grepline[field2]))*100)+0.5) return val1percentage end -- ############################################################### -- Public functions get_system = function (self) local system = {} system.uptime = cfe({ value=querycmd("/usr/bin/uptime"), label="Uptime" }) system.date = cfe({ value=querycmd("/bin/date"), label="Date" }) system.version = cfe({ value=fs.read_file("/etc/alpine-release") or "Unknown", label="Version" }) system.timezone = cfe({ value=date.what_tz(), label="Time Zone" }) system.uname = cfe({ value=querycmd("/bin/uname -a"), label="UName" }) system.memory = cfe({ value=querycmd("/usr/bin/free"), label="Memory usage" }) system.memory.used = getpercentage(querycmd("/usr/bin/free"), "Total:", 3, 4) return cfe({ type="group", value=system }) end get_storage = function (self) local storage = {} local disk = diskfree() .. "\n" local other = {} for line in string.gmatch(disk, "([^\n]*)\n") do if string.match(line, "^/dev/fd%d+") then if not storage.floppy then storage.floppy = cfe({ type="group", value={}, label="Floppy drives" }) end local name = string.match(line, "^(/dev/fd%d+)") storage.floppy.value[name] = cfe({ value=string.match(disk, "[^\n]*\n")..line, label="Floppy Capacity" }) storage.floppy.value[name].used = string.match(line, name.."%s*%S*%s*%S*%s*%S*%s*(%S*)%%") elseif string.match(line, "^/dev/none") then if not storage.ramdisk then storage.ramdisk = cfe({ type="group", value={}, label="RAM disks" }) end local name = "/dev/none" storage.ramdisk.value[name] = cfe({ value=string.match(disk, "[^\n]*\n")..line, label="RAM Disk Capacity" }) storage.ramdisk.value[name].used = string.match(line, name.."%s*%S*%s*%S*%s*%S*%s*(%S*)%%") elseif string.match(line, "^/dev/[hs]d%a%d+") then if not storage.hd then storage.hd = cfe({ type="group", value={}, label="Hard drives" }) end local name = string.match(line, "^(/dev/[hs]d%a%d+)") storage.hd.value[name] = cfe({ value=string.match(disk, "[^\n]*\n")..line, label="Hard Drive Capacity" }) storage.hd.value[name].used = string.match(line, name.."%s*%S*%s*%S*%s*%S*%s*(%S*)%%") end end storage.partitions = cfe({ value=fs.read_file("/proc/partitions") or "", label="Partitions" }) return cfe({ type="group", value=storage }) end get_network = function (self) local network = {} network.interfaces = cfe({ value=querycmd("/bin/ip addr"), label="Interfaces" }) network.routes = cfe({ value=querycmd("/bin/ip route"), label="Routes" }) return cfe({ type="group", value=network }) end get_modules = function (self) local modules = {} modules.list = cfe({ value=querycmd("/sbin/lsmod"), label="Modules List" }) return cfe({ type="group", value=modules }) end get_proc = function (self) local proc = {} proc.processor = cfe({ value=fs.read_file("/proc/cpuinfo") or "", label="Processor" }) proc.memory = cfe({ value=fs.read_file("/proc/meminfo") or "", label="Memory" }) return cfe({ type="group", value=proc }) end get_networkstats = function () local stats = cfe({ type="structure", value={}, label="Network Stats", timestamp=os.time() }) local result = fs.read_file("/proc/net/dev") or "" -- parse the result local i=0 for line in string.gmatch(result, "[^\n]+\n?") do if i>1 then local intf = string.match(line, "([^%s:]+):") line = string.match(line, ":(.*)$") local words = {} for word in string.gmatch(line, "%S+") do words[#words+1] = word end stats.value[intf] = {} stats.value[intf].RX = {bytes=words[1], packets=words[2]} stats.value[intf].TX = {bytes=words[9], packets=words[10]} end i=i+1 end return stats end