|
2 | 2 | local tonumber = tonumber |
3 | 3 | local setmetatable = setmetatable |
4 | 4 | local math = { floor = math.floor } |
| 5 | +local io = { popen = io.popen } |
5 | 6 | local helpers = require("vicious.helpers") |
6 | 7 | -- }}} |
7 | 8 |
|
@@ -47,14 +48,27 @@ local function worker(format) |
47 | 48 | local _swp = { buf = {}, total = nil } |
48 | 49 |
|
49 | 50 | if vm_swap_enabled == 1 and vm_swap_total > 0 then |
50 | | - -- Get swap space in bytes |
51 | | - _swp.total = vm_swap_total |
52 | | - _swp.buf.free = _swp.total - tonumber(vm_stats.v_swapin) |
53 | | - -- Rework into megabytes |
54 | | - _swp.total = math.floor(_swp.total/1048576) |
55 | | - _swp.buf.free = math.floor(_swp.buf.free/1048576) |
| 51 | + -- Initialise variables |
| 52 | + _swp.usep = 0 |
| 53 | + _swp.inuse = 0 |
| 54 | + _swp.total = 0 |
| 55 | + _swp.buf.free = 0 |
| 56 | + |
| 57 | + -- Read output of swapinfo in Mbytes |
| 58 | + local f = io.popen("swapinfo -m") |
| 59 | + -- Skip first line (table header) |
| 60 | + f:read("*line") |
| 61 | + -- Read content and sum up |
| 62 | + for line in f:lines() do |
| 63 | + local ltotal, lused, lfree = string.match(line, "%s+([%d]+)%s+([%d]+)%s+([%d]+)") |
| 64 | + -- Add swap space in Mbytes |
| 65 | + _swp.total = _swp.total + tonumber(ltotal) |
| 66 | + _swp.inuse = _swp.inuse + tonumber(lused) |
| 67 | + _swp.buf.free = _swp.buf.free + tonumber(lfree) |
| 68 | + end |
| 69 | + f:close() |
| 70 | + |
56 | 71 | -- Calculate percentage |
57 | | - _swp.inuse = _swp.total - _swp.buf.free |
58 | 72 | _swp.usep = math.floor(_swp.inuse / _swp.total * 100) |
59 | 73 | else |
60 | 74 | _swp.usep = -1 |
|
0 commit comments