calculating swap space using swapinfo (more reliable)

This commit is contained in:
mutlusun 2018-06-06 21:55:36 +02:00
parent 18d9d3aa35
commit 6e185bc89e
1 changed files with 26 additions and 6 deletions

View File

@ -47,14 +47,34 @@ local function worker(format)
local _swp = { buf = {}, total = nil } local _swp = { buf = {}, total = nil }
if vm_swap_enabled == 1 and vm_swap_total > 0 then if vm_swap_enabled == 1 and vm_swap_total > 0 then
-- Get swap space in bytes -- Initialise variables
_swp.total = vm_swap_total _swp.usep = 0
_swp.buf.free = _swp.total - tonumber(vm_stats.v_swapin) _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 -- Rework into megabytes
_swp.total = math.floor(_swp.total/1048576) _swp.total = math.floor(_swp.total/1024)
_swp.buf.free = math.floor(_swp.buf.free/1048576) _swp.buf.free = math.floor(_swp.buf.free/1024)
-- Calculate percentage -- Calculate percentage
_swp.inuse = _swp.total - _swp.buf.free
_swp.usep = math.floor(_swp.inuse / _swp.total * 100) _swp.usep = math.floor(_swp.inuse / _swp.total * 100)
else else
_swp.usep = -1 _swp.usep = -1