calculating swap space using swapinfo (more reliable)
This commit is contained in:
parent
18d9d3aa35
commit
6e185bc89e
|
@ -47,14 +47,34 @@ local function worker(format)
|
|||
local _swp = { buf = {}, total = nil }
|
||||
|
||||
if vm_swap_enabled == 1 and vm_swap_total > 0 then
|
||||
-- Get swap space in bytes
|
||||
_swp.total = vm_swap_total
|
||||
_swp.buf.free = _swp.total - tonumber(vm_stats.v_swapin)
|
||||
-- Initialise variables
|
||||
_swp.usep = 0
|
||||
_swp.inuse = 0
|
||||
_swp.total = 0
|
||||
_swp.buf.free = 0
|
||||
|
||||
-- Read output of swapinfo
|
||||
local f = io.popen("swapinfo")
|
||||
-- Skip first line (table header)
|
||||
f:read("*line")
|
||||
-- Read content and sum up
|
||||
for line in f:lines() do
|
||||
local ltotal, lused, lfree = string.match(line, "%s+([%d]+)%s+([%d]+)%s+([%d]+)")
|
||||
print(ltotal)
|
||||
print(lused)
|
||||
print(lfree)
|
||||
|
||||
-- Add swap space in kbytes
|
||||
_swp.total = _swp.total + tonumber(ltotal)
|
||||
_swp.inuse = _swp.inuse + tonumber(lused)
|
||||
_swp.buf.free = _swp.buf.free + tonumber(lfree)
|
||||
end
|
||||
f:close()
|
||||
|
||||
-- Rework into megabytes
|
||||
_swp.total = math.floor(_swp.total/1048576)
|
||||
_swp.buf.free = math.floor(_swp.buf.free/1048576)
|
||||
_swp.total = math.floor(_swp.total/1024)
|
||||
_swp.buf.free = math.floor(_swp.buf.free/1024)
|
||||
-- Calculate percentage
|
||||
_swp.inuse = _swp.total - _swp.buf.free
|
||||
_swp.usep = math.floor(_swp.inuse / _swp.total * 100)
|
||||
else
|
||||
_swp.usep = -1
|
||||
|
|
Loading…
Reference in New Issue