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

0
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 module("systats", package.seeall);
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local cache_time = 5;
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local memory_info = { __fetched = 0 };
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local function get_memory_info()
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 if (os.time() - memory_info.__fetched) > cache_time then
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 memory_info.__fetched = os.time();
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 for line in io.lines("/proc/meminfo") do
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local k, v = line:match("^(%w+):%s*(%d+) kB");
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 if k and v then
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 memory_info[k] = tonumber(v);
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 end
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 end
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 end
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 return memory_info;
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 end
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 function memory_total()
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 return get_memory_info().MemTotal;
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 end
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 function memory_free()
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 return get_memory_info().MemFree;
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 function memory_used()
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 return memory_total() - memory_free();
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 end
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 function load_average()
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 local l = io.open("/proc/loadavg");
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 local five, ten, fifteen = l:read("*l"):match("^(%S+) (%S+) (%S+)");
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 l:close();
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 return tonumber(five), tonumber(ten), tonumber(fifteen);
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 function disks()
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 local df = io.popen("df -k");
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 if not df then return nil; end
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local disks = {};
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 for line in df:lines() do
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 local device, total, used, free, percent, mounted =
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 line:match("^(/%S+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%%%s+(.+)$");
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 if device then
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 disks[#disks+1] = {
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 device = device,
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 total = tonumber(total), used = tonumber(used),
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 free = tonumber(free), percent_used = tonumber(percent),
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 mounted_on = mounted,
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 };
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 end
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 df:close();
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 return disks;
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 end
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57
813b739a632b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 return _M;

mercurial