]> 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:

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