2013-09-07 12:06:42 +02:00
|
|
|
|
|
|
|
--[[
|
|
|
|
|
|
|
|
Licensed under GNU General Public License v2
|
|
|
|
* (c) 2013, Luke Bonham
|
|
|
|
* (c) 2010-2012, Peter Hofmann
|
|
|
|
|
|
|
|
--]]
|
|
|
|
|
|
|
|
local helpers = require("lain.helpers")
|
|
|
|
|
2013-09-10 23:02:11 +02:00
|
|
|
local notify_fg = require("beautiful").fg_focus
|
|
|
|
local naughty = require("naughty")
|
2013-09-07 12:06:42 +02:00
|
|
|
local wibox = require("wibox")
|
|
|
|
|
2013-09-10 23:02:11 +02:00
|
|
|
local string = { format = string.format,
|
2014-11-15 13:38:07 +01:00
|
|
|
gsub = string.gsub,
|
|
|
|
match = string.match }
|
2013-09-07 12:06:42 +02:00
|
|
|
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
|
|
|
|
-- Network infos
|
|
|
|
-- lain.widgets.net
|
2015-10-12 12:09:44 +02:00
|
|
|
local net = {}
|
2013-09-07 12:06:42 +02:00
|
|
|
|
|
|
|
function net.get_device()
|
2015-08-13 01:56:37 +02:00
|
|
|
local ws = helpers.read_pipe("ip link show | cut -d' ' -f2,9")
|
2014-10-20 15:56:44 +02:00
|
|
|
ws = ws:match("%w+: UP") or ws:match("ppp%w+: UNKNOWN")
|
2013-09-07 12:06:42 +02:00
|
|
|
if ws ~= nil then
|
2014-10-20 15:56:44 +02:00
|
|
|
return ws:match("(%w+):")
|
2013-09-07 12:06:42 +02:00
|
|
|
else
|
2014-02-07 12:39:18 +01:00
|
|
|
return "network off"
|
2013-09-07 12:06:42 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-11 19:39:14 +02:00
|
|
|
local function worker(args)
|
2013-09-07 12:06:42 +02:00
|
|
|
local args = args or {}
|
2015-10-12 12:09:44 +02:00
|
|
|
local timeout = args.timeout or 1
|
2013-09-10 23:02:11 +02:00
|
|
|
local units = args.units or 1024 --kb
|
2014-04-13 12:19:02 +02:00
|
|
|
local notify = args.notify or "on"
|
2014-10-11 14:45:14 +02:00
|
|
|
local screen = args.screen or 1
|
2013-09-10 23:02:11 +02:00
|
|
|
local settings = args.settings or function() end
|
2013-09-07 12:06:42 +02:00
|
|
|
|
2014-11-15 13:21:22 +01:00
|
|
|
iface = args.iface or net.get_device()
|
|
|
|
|
2013-09-11 19:39:14 +02:00
|
|
|
net.widget = wibox.widget.textbox('')
|
2013-09-07 12:06:42 +02:00
|
|
|
|
2013-09-10 23:02:11 +02:00
|
|
|
helpers.set_map(iface, true)
|
2015-10-12 12:09:44 +02:00
|
|
|
helpers.set_map("net_t", 0)
|
|
|
|
helpers.set_map("net_r", 0)
|
2013-09-07 12:06:42 +02:00
|
|
|
|
2013-09-13 18:15:25 +02:00
|
|
|
function update()
|
2015-10-12 12:09:44 +02:00
|
|
|
net_now = {
|
|
|
|
sent = "0.0",
|
|
|
|
received = "0.0"
|
|
|
|
}
|
2013-09-13 00:07:44 +02:00
|
|
|
|
2014-11-15 13:38:07 +01:00
|
|
|
if iface == "" or string.match(iface, "network off")
|
|
|
|
then
|
|
|
|
iface = net.get_device()
|
|
|
|
end
|
2013-09-07 12:06:42 +02:00
|
|
|
|
|
|
|
local now_t = helpers.first_line('/sys/class/net/' .. iface ..
|
2013-09-10 23:02:11 +02:00
|
|
|
'/statistics/tx_bytes') or 0
|
2013-09-07 12:06:42 +02:00
|
|
|
local now_r = helpers.first_line('/sys/class/net/' .. iface ..
|
2013-09-10 23:02:11 +02:00
|
|
|
'/statistics/rx_bytes') or 0
|
|
|
|
|
2015-10-12 12:09:44 +02:00
|
|
|
if now_t ~= helpers.get_map("net_t")
|
|
|
|
or now_r ~= helpers.get_map("net_r") then
|
|
|
|
net_now.carrier = helpers.first_line('/sys/class/net/' .. iface ..
|
|
|
|
'/carrier') or "0"
|
|
|
|
net_now.state = helpers.first_line('/sys/class/net/' .. iface ..
|
|
|
|
'/operstate') or "down"
|
2013-09-10 23:02:11 +02:00
|
|
|
|
2015-10-12 12:09:44 +02:00
|
|
|
net_now.sent = (now_t - net.last_t) / timeout / units
|
|
|
|
net_now.sent = string.gsub(string.format('%.1f', net_now.sent), ",", ".")
|
2013-09-10 23:02:11 +02:00
|
|
|
|
2015-10-12 12:09:44 +02:00
|
|
|
net_now.received = (now_r - net.last_r) / timeout / units
|
|
|
|
net_now.received = string.gsub(string.format('%.1f', net_now.received), ",", ".")
|
2013-09-10 23:02:11 +02:00
|
|
|
|
2015-10-12 12:09:44 +02:00
|
|
|
widget = net.widget
|
|
|
|
settings()
|
|
|
|
|
|
|
|
helpers.set_map("net_t", now_t)
|
|
|
|
helpers.set_map("net_r", now_r)
|
|
|
|
end
|
2013-09-07 12:06:42 +02:00
|
|
|
|
2014-04-13 12:19:02 +02:00
|
|
|
if net_now.carrier ~= "1" and notify == "on"
|
2013-09-07 12:06:42 +02:00
|
|
|
then
|
|
|
|
if helpers.get_map(iface)
|
|
|
|
then
|
2013-09-10 23:02:11 +02:00
|
|
|
naughty.notify({
|
2014-02-07 12:39:18 +01:00
|
|
|
title = iface,
|
2013-09-10 23:02:11 +02:00
|
|
|
text = "no carrier",
|
|
|
|
timeout = 7,
|
|
|
|
position = "top_left",
|
|
|
|
icon = helpers.icons_dir .. "no_net.png",
|
2014-07-19 16:36:15 +02:00
|
|
|
fg = notify_fg or "#FFFFFF",
|
2014-10-11 14:45:14 +02:00
|
|
|
screen = screen
|
2013-09-10 23:02:11 +02:00
|
|
|
})
|
2013-09-07 12:06:42 +02:00
|
|
|
helpers.set_map(iface, false)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
helpers.set_map(iface, true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-12 12:09:44 +02:00
|
|
|
helpers.newtimer(iface, timeout, update, false)
|
|
|
|
|
2013-09-11 19:39:14 +02:00
|
|
|
return net.widget
|
2013-09-07 12:06:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(net, { __call = function(_, ...) return worker(...) end })
|