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