| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
local io = io |
| 7 |
local setmetatable = setmetatable |
| 8 |
local tonumber = tonumber |
| 9 |
local pairs = pairs |
| 10 |
local lib = { |
| 11 |
widget = require("obvious.lib.widget") |
| 12 |
} |
| 13 |
|
| 14 |
local function info(dev) |
| 15 |
local f = io.open("/proc/diskstats") |
| 16 |
local line |
| 17 |
|
| 18 |
if f == nil then |
| 19 |
return ret |
| 20 |
end |
| 21 |
|
| 22 |
line = f:read() |
| 23 |
|
| 24 |
while line and not line:match(dev) do |
| 25 |
line = f:read() |
| 26 |
end |
| 27 |
|
| 28 |
f:close() |
| 29 |
|
| 30 |
if not line then |
| 31 |
return nil |
| 32 |
end |
| 33 |
|
| 34 |
local ret = { } |
| 35 |
|
| 36 |
local matches = line:gmatch("%d+") |
| 37 |
|
| 38 |
|
| 39 |
matches() |
| 40 |
matches() |
| 41 |
|
| 42 |
ret.reads_completed = tonumber(matches()) |
| 43 |
ret.reads_merged = tonumber(matches()) |
| 44 |
ret.reads_sectors = tonumber(matches()) |
| 45 |
ret.reads_time_ms = tonumber(matches()) |
| 46 |
ret.writes_completed = tonumber(matches()) |
| 47 |
ret.writes_merged = tonumber(matches()) |
| 48 |
ret.writes_sectors = tonumber(matches()) |
| 49 |
ret.writes_time_ms = tonumber(matches()) |
| 50 |
ret.in_progress = tonumber(matches()) |
| 51 |
ret.time_ms = tonumber(matches()) |
| 52 |
ret.time_ms_weight = tonumber(matches()) |
| 53 |
|
| 54 |
return ret |
| 55 |
end |
| 56 |
|
| 57 |
local function get_increase(data) |
| 58 |
local last = data.last |
| 59 |
local cur = info(data.device) |
| 60 |
|
| 61 |
if not cur then |
| 62 |
return nil |
| 63 |
end |
| 64 |
|
| 65 |
data.last = cur |
| 66 |
|
| 67 |
|
| 68 |
if last == nil then |
| 69 |
last = cur |
| 70 |
end |
| 71 |
|
| 72 |
local ret = { } |
| 73 |
for k, v in pairs(cur) do |
| 74 |
ret[k] = cur[k] - last[k] |
| 75 |
end |
| 76 |
|
| 77 |
return ret |
| 78 |
end |
| 79 |
|
| 80 |
local function get(data) |
| 81 |
local val = get_increase(data) |
| 82 |
if not val then |
| 83 |
return |
| 84 |
end |
| 85 |
return val.writes_sectors + val.reads_sectors |
| 86 |
end |
| 87 |
|
| 88 |
local function get_data_source(device) |
| 89 |
local device = device or "sda" |
| 90 |
local ret = {} |
| 91 |
|
| 92 |
ret.get = get |
| 93 |
ret.device = device |
| 94 |
|
| 95 |
return lib.widget.from_data_source(ret) |
| 96 |
end |
| 97 |
|
| 98 |
return setmetatable({}, { __call = function (_, ...) return get_data_source(...) end }) |
| 99 |
|
| 100 |
|