aboutsummaryrefslogtreecommitdiff
path: root/net.lua
blob: 83e79245e0037f6fe4dc61c9fd9d7f7f7fa517ff (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
78
79
80
81
82
83
84
---------------------------------------------------
-- Licensed under the GNU General Public License v2
--  * (c) 2009, Adrian C. <anrxc@sysphere.org>
--  * (c) 2009, Henning Glawe <glaweh@debian.org>
--  * (c) 2008, Lucas de Vries <lucas@glacicle.com>
---------------------------------------------------

-- {{{ Grab environment
local tonumber = tonumber
local os = { time = os.time }
local io = { open = io.open }
local setmetatable = setmetatable
local string = {
    match = string.match,
    format = string.format
}
-- }}}


-- Net: provides usage statistics for all network interfaces
module("vicious.net")


-- Initialise function tables
local nets = {}

-- {{{ Net widget type
local function worker(format)
    -- Get /proc/net/dev
    local f = io.open("/proc/net/dev")
    local args = {}

    local function uformat(array, key, value)
        array["{"..key.."_b}"]  = string.format("%.1f", value)
        array["{"..key.."_kb}"] = string.format("%.1f", value/1024)
        array["{"..key.."_mb}"] = string.format("%.1f", value/1024/1024)
        array["{"..key.."_gb}"] = string.format("%.1f", value/1024/1024/1024)

        return array
    end

    for line in f:lines() do
        -- Match wmaster0 as well as rt0 (multiple leading spaces)
        if string.match(line, "^[%s]?[%s]?[%s]?[%s]?[%w]+:") then
            local name = string.match(line, "^[%s]?[%s]?[%s]?[%s]?([%w]+):")
            -- Received bytes, first value after the name
            local recv = tonumber(string.match(line, ":[%s]*([%d]+)"))
            local send = -- Transmited bytes, 7 fields from end of the line
             tonumber(string.match(line, "([%d]+)%s+%d+%s+%d+%s+%d+%s+%d+%s+%d+%s+%d+%s+%d$"))

            uformat(args, name .. " rx", recv)
            uformat(args, name .. " tx", send)

            if nets[name] == nil then 
                -- Default values on the first run
                nets[name] = {}
                uformat(args, name .. " down", 0)
                uformat(args, name .. " up", 0)

                nets[name].time = os.time()
            else
                -- Net stats are absolute, substract our last reading
                local interval = os.time() - nets[name].time
                nets[name].time = os.time()

                local down = (recv - nets[name][1])/interval
                local up   = (send - nets[name][2])/interval

                uformat(args, name .. " down", down)
                uformat(args, name .. " up", up)
            end

            -- Store totals
            nets[name][1] = recv
            nets[name][2] = send
        end
    end
    f:close()

    return args
end
-- }}}

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