--------------------------------------------------- -- Licensed under the GNU General Public License v2 -- * (c) 2012, Axel Arnold -- * (c) 2010, Adrian C. -- * (c) 2010, Henning Glawe -- * (c) 2009, Lucas de Vries --------------------------------------------------- --------------------------------------------------- -- looks for the given wireless devices (watch_devices), -- and displays the up and downstream traffic. --------------------------------------------------- local watch_devices = { "ppp0", "wlan0", "eth0" } -- {{{ Grab environment local pairs = pairs local tonumber = tonumber local os = { time = os.time } local io = { open = io.open } local setmetatable = setmetatable local string = { match = string.match, format = string.format } -- }}} -- Net: provides usage statistics for all network interfaces module("mynet") -- Initialise function tables local nets = {} -- {{{ Net widget type local function worker(format) -- Get /proc/net/dev local f = io.open("/proc/net/dev") for device,val in pairs(nets) do nets[device].up = nil nets[device].down = nil end for line in f:lines() do -- Match wmaster0 as well as rt0 (multiple leading spaces) if string.match(line, "^[%s]?[%s]?[%s]?[%s]?[%w]+:") then local name = string.match(line, "^[%s]?[%s]?[%s]?[%s]?([%w]+):") -- Received bytes, first value after the name local recv = tonumber(string.match(line, ":[%s]*([%d]+)")) -- Transmited bytes, 7 fields from end of the line local send = tonumber(string.match(line, "([%d]+)%s+%d+%s+%d+%s+%d+%s+%d+%s+%d+%s+%d+%s+%d$")) local up = 0 local down = 0 if nets[name] ~= nil then local interval = os.time() - nets[name].time > 0 and os.time() - nets[name].time or 1 down = (recv - nets[name].recv) / interval up = (send - nets[name].send) / interval end -- Store totals nets[name] = { time = os.time(), recv = recv, send = send, up = up, down = down } end end f:close() local args = { ["{device}"]="none", ["{send}"]="0", ["{recv}"]="0" } for idx,name in pairs(watch_devices) do if nets[name] ~= nil and nets[name].up ~= nil then args["{send}"] = string.format("%.1f", nets[name].up/1024) args["{recv}"] = string.format("%.1f", nets[name].down/1024) args["{device}"] = name break end end return args end -- }}} setmetatable(_M, { __call = function(_, ...) return worker(...) end })