[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: lua to read Linux, Solaris, FreeBSD kernel statistics
- From: David Favro <lua@...>
- Date: Sat, 21 Feb 2015 09:56:34 -0500
On 02/21/2015 05:59 AM, Stefan Parvu wrote:
Anyone, any ideas if there is anything done in Lua which can read /proc from Linux or
sysctl interface from *BSD systems. Im interested to use Lua to fetch system statistics:
* cpu, memory utilization
* disk, nic utilization
I've always just coded up a quick parse of Linux /proc/foo when necessary,
which is typically very easy using Lua string pattern matching. If you want
an API to return various utilization statistics that is platform-independent
across Linux & BSD, I'm not aware of one but I think you could code it
yourself very easily.
A Linux-only example below, works on 2.6 and 3.X kernels. I'm not a VM
expert, so your interpretation of what constitutes "free memory" may be
different.
local function get_meminfo()
local rv = {};
local f = assert( io.open("/proc/meminfo","r") );
for l in f:lines() do
local key, n, units = l:match( "^([^:%s]+):%s+(%d+)%s?(%a?%a?)$" );
if not key then error("Can't parse line: "..l); end
n = tonumber(n);
if units ~= "" then
if units ~= "kB" then error("Unknown units: "..units); end
else
if n ~= 0 then n=n/1024; end
end
rv[key] = n;
end
f:close();
return rv;
end;
local mi = get_meminfo();
local free = mi.Buffers + mi.Cached + mi.SwapCached;
print( "Free memory percent: ", 100*free/mi.MemTotal );