aboutsummaryrefslogtreecommitdiff
path: root/widgets/net.lua
diff options
context:
space:
mode:
authorAdrian C. (anrxc) <anrxc@sysphere.org>2010-03-14 01:55:33 +0100
committerAdrian C. (anrxc) <anrxc@sysphere.org>2010-03-14 01:55:33 +0100
commit237470c8f45190b213e3a173ce6ae1a74b3e11fe (patch)
tree7f53c8144761947d4bde20715bcad34f4be0d6c0 /widgets/net.lua
parent9a82d4113a8271b7dfc7506f2b07379e3ede89a8 (diff)
downloadvicious-legacy-237470c8f45190b213e3a173ce6ae1a74b3e11fe.tar.xz
API: transform widgets namespace table to a directory
Diffstat (limited to 'widgets/net.lua')
-rw-r--r--widgets/net.lua75
1 files changed, 75 insertions, 0 deletions
diff --git a/widgets/net.lua b/widgets/net.lua
new file mode 100644
index 0000000..9fd221f
--- /dev/null
+++ b/widgets/net.lua
@@ -0,0 +1,75 @@
+---------------------------------------------------
+-- Licensed under the GNU General Public License v2
+-- * (c) 2010, Adrian C. <anrxc@sysphere.org>
+-- * (c) 2009, Lucas de Vries <lucas@glacicle.com>
+---------------------------------------------------
+
+-- {{{ Grab environment
+local tonumber = tonumber
+local os = { time = os.time }
+local io = { lines = io.lines }
+local setmetatable = setmetatable
+local string = { match = string.match }
+local helpers = require("vicious.helpers")
+-- }}}
+
+
+-- Net: provides usage statistics for all network interfaces
+module("vicious.widgets.net")
+
+
+-- Initialise function tables
+local nets = {}
+-- Variable definitions
+local unit = { ["b"] = 1, ["kb"] = 1024,
+ ["mb"] = 1024^2, ["gb"] = 1024^3
+}
+
+-- {{{ Net widget type
+local function worker(format)
+ local args = {}
+
+ -- Get NET stats
+ for line in io.lines("/proc/net/dev") do
+ -- Match wmaster0 as well as rt0 (multiple leading spaces)
+ local name = string.match(line, "^[%s]?[%s]?[%s]?[%s]?([%w]+):")
+ if name ~= nil then
+ -- 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$"))
+
+ helpers.uformat(args, name .. " rx", recv, unit)
+ helpers.uformat(args, name .. " tx", send, unit)
+
+ if nets[name] == nil then
+ -- Default values on the first run
+ nets[name] = {}
+ helpers.uformat(args, name .. " down", 0, unit)
+ helpers.uformat(args, name .. " up", 0, unit)
+
+ nets[name].time = os.time()
+ else -- Net stats are absolute, substract our last reading
+ local interval = os.time() - nets[name].time > 0 and
+ os.time() - nets[name].time or 1
+ nets[name].time = os.time()
+
+ local down = (recv - nets[name][1]) / interval
+ local up = (send - nets[name][2]) / interval
+
+ helpers.uformat(args, name .. " down", down, unit)
+ helpers.uformat(args, name .. " up", up, unit)
+ end
+
+ -- Store totals
+ nets[name][1] = recv
+ nets[name][2] = send
+ end
+ end
+
+ return args
+end
+-- }}}
+
+setmetatable(_M, { __call = function(_, ...) return worker(...) end })