proc.lua

Sun, 28 Feb 2010 03:11:12 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Sun, 28 Feb 2010 03:11:12 +0000
changeset 0
813b739a632b
permissions
-rw-r--r--

Initial commit

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;

mercurial