# HG changeset patch # User Matthew Wild # Date 1267326672 0 # Node ID 813b739a632b58020f7b8d67dfdc8cfe9c01ae6b Initial commit diff -r 000000000000 -r 813b739a632b demo.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demo.lua Sun Feb 28 03:11:12 2010 +0000 @@ -0,0 +1,26 @@ +local stats = require "init"; + +print("## Memory"); +print("Total:", stats.memory_total().." KB"); +print("Used:", stats.memory_used().." KB"); +print("Free:", stats.memory_free().." KB"); + +print(""); + +local disks = stats.disks(); +print("## Storage"); +for _, info in pairs(disks) do + print(info.mounted_on); + print(" Device:", info.device); + print(" Size: ", info.total.." KB"); + print(" Used: ", info.used.." KB"); + print(" Free: ", info.free.." KB"); +end + +print(""); + +local five, ten, fifteen = stats.load_average(); +print("## Load"); +print("Past 5 minutes: ", five); +print("Past 10 minutes:", ten); +print("Past 15 minutes:", fifteen); diff -r 000000000000 -r 813b739a632b init.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/init.lua Sun Feb 28 03:11:12 2010 +0000 @@ -0,0 +1,9 @@ +local ok, lfs = pcall(require,"lfs"); + +local backend; + +if (ok and lfs.attributes("/proc")) or (package.config:sub(1,1) == "/") then + backend = require "proc"; +end + +return backend or { backend = function () return nil; end }; diff -r 000000000000 -r 813b739a632b proc.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/proc.lua Sun Feb 28 03:11:12 2010 +0000 @@ -0,0 +1,58 @@ +module("systats", package.seeall); + +local cache_time = 5; + +local memory_info = { __fetched = 0 }; +local function get_memory_info() + if (os.time() - memory_info.__fetched) > cache_time then + memory_info.__fetched = os.time(); + for line in io.lines("/proc/meminfo") do + local k, v = line:match("^(%w+):%s*(%d+) kB"); + if k and v then + memory_info[k] = tonumber(v); + end + end + end + return memory_info; +end + +function memory_total() + return get_memory_info().MemTotal; +end + +function memory_free() + return get_memory_info().MemFree; +end + +function memory_used() + return memory_total() - memory_free(); +end + +function load_average() + local l = io.open("/proc/loadavg"); + local five, ten, fifteen = l:read("*l"):match("^(%S+) (%S+) (%S+)"); + l:close(); + return tonumber(five), tonumber(ten), tonumber(fifteen); +end + +function disks() + local df = io.popen("df -k"); + if not df then return nil; end + local disks = {}; + for line in df:lines() do + local device, total, used, free, percent, mounted = + line:match("^(/%S+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%%%s+(.+)$"); + if device then + disks[#disks+1] = { + device = device, + total = tonumber(total), used = tonumber(used), + free = tonumber(free), percent_used = tonumber(percent), + mounted_on = mounted, + }; + end + end + df:close(); + return disks; +end + +return _M;