]> git.madduck.net Git - etc/awesome.git/blob - widget/net.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

removed whitespaced signatures; wiki updated
[etc/awesome.git] / widget / net.lua
1 --[[
2
3      Licensed under GNU General Public License v2
4       * (c) 2013,      Luke Bonham
5       * (c) 2010-2012, Peter Hofmann
6
7 --]]
8
9 local helpers = require("lain.helpers")
10 local naughty = require("naughty")
11 local wibox   = require("wibox")
12 local string  = { format = string.format, match = string.match }
13
14 -- Network infos
15 -- lain.widget.net
16
17 local function factory(args)
18     local net      = { widget = wibox.widget.textbox(), devices = {} }
19     local args     = args or {}
20     local timeout  = args.timeout or 2
21     local units    = args.units or 1024 -- KB
22     local notify   = args.notify or "on"
23     local screen   = args.screen or 1
24     local settings = args.settings or function() end
25
26     -- Compatibility with old API where iface was a string corresponding to 1 interface
27     net.iface = (args.iface and (type(args.iface) == "string" and {args.iface}) or
28                 (type(args.iface) == "table" and args.iface)) or {}
29
30     function net.get_device()
31         helpers.async(string.format("ip link show", device_cmd), function(ws)
32             ws = ws:match("(%w+): <BROADCAST,MULTICAST,.-UP,LOWER_UP>")
33             net.iface = ws and { ws } or {}
34         end)
35     end
36
37     if #net.iface == 0 then net.get_device() end
38
39     function net.update()
40         -- These are the totals over all specified interfaces
41         net_now = {
42             devices  = {},
43             -- Bytes since last iteration
44             sent     = 0,
45             received = 0
46         }
47
48         for i, dev in ipairs(net.iface) do
49             local dev_now    = {}
50             local dev_before = net.devices[dev] or { last_t = 0, last_r = 0 }
51             local now_t      = tonumber(helpers.first_line(string.format("/sys/class/net/%s/statistics/tx_bytes", dev)) or 0)
52             local now_r      = tonumber(helpers.first_line(string.format("/sys/class/net/%s/statistics/rx_bytes", dev)) or 0)
53
54             dev_now.carrier  = helpers.first_line(string.format("/sys/class/net/%s/carrier", dev)) or "0"
55             dev_now.state    = helpers.first_line(string.format("/sys/class/net/%s/operstate", dev)) or "down"
56
57             dev_now.sent     = (now_t - dev_before.last_t) / timeout / units
58             dev_now.received = (now_r - dev_before.last_r) / timeout / units
59
60             net_now.sent     = net_now.sent + dev_now.sent
61             net_now.received = net_now.received + dev_now.received
62
63             dev_now.sent     = string.format("%.1f", dev_now.sent)
64             dev_now.received = string.format("%.1f", dev_now.received)
65
66             dev_now.last_t   = now_t
67             dev_now.last_r   = now_r
68
69             net.devices[dev] = dev_now
70
71             -- Notify only once when connection is loss
72             if string.match(dev_now.carrier, "0") and notify == "on" and helpers.get_map(dev) then
73                 naughty.notify {
74                     title    = dev,
75                     text     = "No carrier",
76                     icon     = helpers.icons_dir .. "no_net.png",
77                     screen   = screen
78                 }
79                 helpers.set_map(dev, false)
80             elseif string.match(dev_now.carrier, "1") then
81                 helpers.set_map(dev, true)
82             end
83
84             net_now.carrier = dev_now.carrier
85             net_now.state = dev_now.state
86             net_now.devices[dev] = dev_now
87             -- new_now.sent and net_now.received will be
88             -- the totals across all specified devices
89         end
90
91         net_now.sent = string.format("%.1f", net_now.sent)
92         net_now.received = string.format("%.1f", net_now.received)
93
94         widget = net.widget
95         settings()
96     end
97
98     helpers.newtimer("network", timeout, net.update)
99
100     return net
101 end
102
103 return factory