aboutsummaryrefslogtreecommitdiff
path: root/dio.lua
blob: ea3aa9de07bd805de7792721838b966495792c00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
---------------------------------------------------
-- Licensed under the GNU General Public License v2
--  * (c) 2009, Adrian C. <anrxc@sysphere.org>
---------------------------------------------------

-- {{{ Grab environment
local ipairs = ipairs
local io = { open = io.open }
local setmetatable = setmetatable
local math = { floor = math.floor }
local table = { insert = table.insert }
local string = {
    match = string.match,
    gmatch = string.gmatch
}
-- }}}


-- Disk I/O: provides I/O statistics for requested storage devices
module("vicious.dio")


-- Initialise function tables
local disk_usage = {}
local disk_total = {}

-- {{{ Disk I/O widget type
local function worker(format, disk)
    -- Get /proc/diskstats
    local f = io.open("/proc/diskstats")
    local disk_lines = {}

    for line in f:lines() do
        if string.match(line, "("..disk..")%s") then
            -- Todo: find a way to do this
            --for stat in string.gmatch(line, "%s([%d]+)") do
            --    table.insert(disk_lines, stat)
            --end
            --
            -- Skip first two matches
            local stat = string.gmatch(line, "%s([%d]+)")
            stat()
            stat()
            -- Store the rest
            for i = 1, 11 do
                table.insert(disk_lines, stat())
            end
        end
    end
    f:close()

    -- Ensure tables are initialized correctly
    while #disk_total < #disk_lines do
        table.insert(disk_total, 0)
    end

    local diff_total  = {}

    for i, v in ipairs(disk_lines) do
        -- Diskstats are absolute, substract our last reading
        diff_total[i] = v - disk_total[i]

        -- Store totals
        disk_total[i] = v
    end

    -- Calculate I/O
    disk_usage["{raw}"] = diff_total[7] + diff_total[3]
    -- Divide "sectors read" by 2 and 1024 to get KB and MB
    disk_usage["{kb}"] = math.floor(diff_total[7] + diff_total[3])/2
    disk_usage["{mb}"] = math.floor(diff_total[7] + diff_total[3])/1024

    return disk_usage
end
-- }}}

setmetatable(_M, { __call = function(_, ...) return worker(...) end })