]> git.madduck.net Git - etc/awesome.git/blob - widgets/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:

{alsa,pulse}bar: remove reset_timeout due to (timer already started) issue
[etc/awesome.git] / widgets / 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,
14                        match  = string.match }
15 local setmetatable = setmetatable
16
17 -- Network infos
18 -- lain.widgets.net
19
20 local function worker(args)
21     local net = helpers.make_widget_textbox()
22     net.last_t = 0
23     net.last_r = 0
24     net.devices = {}
25
26     local args       = args or {}
27     local timeout    = args.timeout or 2
28     local units      = args.units or 1024 --kb
29     local notify     = args.notify or "on"
30     local screen     = args.screen or 1
31     local settings   = args.settings or function() end
32
33     -- Compatibility with old API where iface was a string corresponding to 1 interface
34     net.iface = (args.iface and (type(args.iface) == "string" and {args.iface}) or
35                 (type(args.iface) == "table" and args.iface)) or {}
36
37     function net.get_device()
38         helpers.async(string.format("ip link show", device_cmd), function(ws)
39             ws = ws:match("(%w+): <BROADCAST,MULTICAST,.-UP,LOWER_UP>")
40             net.iface = ws and { ws } or {}
41         end)
42     end
43
44     if #net.iface == 0 then net.get_device() end
45
46     function update()
47         -- These are the totals over all specified interfaces
48         net_now = {
49             -- New api - Current state of requested devices
50             devices  = {},
51             -- Bytes since last iteration
52             sent     = 0,
53             received = 0
54         }
55
56         -- Total bytes transfered
57         local total_t = 0
58         local total_r = 0
59
60         for i, dev in ipairs(net.iface) do
61             local dev_now    = {}
62             local dev_before = net.devices[dev] or { last_t = 0, last_r = 0 }
63             local now_t      = tonumber(helpers.first_line(string.format("/sys/class/net/%s/statistics/tx_bytes", dev)) or 0)
64             local now_r      = tonumber(helpers.first_line(string.format("/sys/class/net/%s/statistics/rx_bytes", dev)) or 0)
65
66             dev_now.carrier  = helpers.first_line(string.format("/sys/class/net/%s/carrier", dev)) or "0"
67             dev_now.state    = helpers.first_line(string.format("/sys/class/net/%s/operstate", dev)) or "down"
68
69             dev_now.sent     = (now_t - dev_before.last_t) / timeout / units
70             dev_now.received = (now_r - dev_before.last_r) / timeout / units
71
72             net_now.sent     = net_now.sent     + dev_now.sent
73             net_now.received = net_now.received + dev_now.received
74
75             dev_now.sent     = string.format('%.1f', dev_now.sent)
76             dev_now.received = string.format('%.1f', dev_now.received)
77
78             dev_now.last_t   = now_t
79             dev_now.last_r   = now_r
80
81             -- This will become dev_before in the next update/iteration
82             net.devices[dev] = dev_now
83
84             total_t  = total_t + now_t
85             total_r  = total_r + now_r
86
87             -- Notify only once when connection is loss
88             if string.match(dev_now.carrier, "0") and notify == "on" and helpers.get_map(dev) then
89                 naughty.notify({
90                     title    = dev,
91                     text     = "no carrier",
92                     icon     = helpers.icons_dir .. "no_net.png",
93                     screen   = screen
94                 })
95                 helpers.set_map(dev, false)
96             elseif string.match(dev_now.carrier, "1") then
97                 helpers.set_map(dev, true)
98             end
99
100             -- Old api compatibility
101             net_now.carrier      = dev_now.carrier
102             net_now.state        = dev_now.state
103             -- And new api
104             net_now.devices[dev] = dev_now
105             -- With the new api new_now.sent and net_now.received will be the
106             -- totals across all specified devices
107         end
108
109         if total_t ~= net.last_t or total_r ~= net.last_r then
110             net_now.sent     = string.format('%.1f', net_now.sent)
111             net_now.received = string.format('%.1f', net_now.received)
112             net.last_t       = total_t
113             net.last_r       = total_r
114         end
115
116         widget = net.widget
117         settings()
118     end
119
120     helpers.newtimer("network", timeout, update)
121
122     return net
123 end
124
125 return setmetatable({}, { __call = function(_, ...) return worker(...) end })