File: init.lua

package info (click to toggle)
awesome-extra 2010071402
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 488 kB
  • ctags: 319
  • sloc: makefile: 40
file content (80 lines) | stat: -rw-r--r-- 1,811 bytes parent folder | download | duplicates (2)
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
-----------------------------------
-- Author: Uli Schlachter        --
-- Copyright 2009 Uli Schlachter --
-----------------------------------

local setmetatable = setmetatable
local io = io
local tonumber = tonumber
local lib = {
    widget = require("obvious.lib.widget")
}

module("obvious.net")

-- Returns the total traffic send/received on some interface
local function netinfo(interface)
    local net = io.open("/proc/net/dev")
    local ret = { }

    -- Init in case we don't find any matches
    ret.recv = 0
    ret.send = 0

    for line in net:lines() do
        if line:match("^%s+" .. interface) then
            ret.recv = tonumber(line:match(":%s*(%d+)"))
            ret.send = tonumber(line:match("(%d+)%s+%d+%s+%d+%s+%d+%s+%d+%s+%d+%s+%d+$"))
        end
    end
    net:close()
    return ret
end

local function get_data(object)
    local last = object.last
    local cur = netinfo(object.device)
    object.last = cur

    local ret = { }
    if last then
        ret.recv = cur.recv - last.recv
        ret.send = cur.send - last.send
    else
        ret.recv = 0
        ret.send = 0
    end

    -- This can happen e.g. when an interface is brought down and up again
    -- or when some counter overflows
    if ret.recv < 0 then
        ret.recv = 0
    end
    if ret.send < 0 then
        ret.send = 0
    end

    return ret
end

local function data(device, key)
    local device = device or "eth0"
    local ret = {}

    ret.device = device
    ret.get = function(obj)
        return get_data(obj)[key]
    end

    return lib.widget.from_data_source(ret)
end

function recv(device)
    return data(device, "recv")
end

function send(device)
    return data(device, "send")
end

-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=4:softtabstop=4:encoding=utf-8:textwidth=80