Debian Codegraph @

vicious/contrib/net_linux.lua

raw symbols SCIP
1 -- contrib/net_linux.lua
2 -- Copyright (C) 2009 Henning Glawe <[email protected]>
3 -- Copyright (C) 2009 Lucas de Vries <[email protected]>
4 -- Copyright (C) 2010 Adrian C. <[email protected]>
5 -- Copyright (C) 2017 Jörg Thalheim <[email protected]>
6 -- Copyright (C) 2017 Roberto <[email protected]>
7 --
8 -- This file is part of Vicious.
9 --
10 -- Vicious is free software: you can redistribute it and/or modify
11 -- it under the terms of the GNU General Public License as
12 -- published by the Free Software Foundation, either version 2 of the
13 -- License, or (at your option) any later version.
14 --
15 -- Vicious is distributed in the hope that it will be useful,
16 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
17 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 -- GNU General Public License for more details.
19 --
20 -- You should have received a copy of the GNU General Public License
21 -- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
22
23 -- {{{ Grab environment
24 local pairs = pairs
25 local tonumber = tonumber
26 local os = { time = os.time }
27 local io = { lines = io.lines }
28 local setmetatable = setmetatable
29 local string = { match = string.match }
30 local helpers = require("vicious.helpers")
31 -- }}}
32
33
34 -- Net: provides usage statistics for all network interfaces
35 -- vicious.contrib.net
36 local net_linux = {}
37
38
39 -- Initialise function tables
40 local nets = {}
41 -- Variable definitions
42 local unit = { ["b"] = 1, ["kb"] = 1024,
43 ["mb"] = 1024^2, ["gb"] = 1024^3
44 }
45
46 -- {{{ Net widget type
47 local function worker(format, tignorelist)
48 local args = {}
49 local tignore = {}
50 local total_rx = 0
51 local total_tx = 0
52 local any_up = 0
53
54 if not tignorelist then
55 tignorelist = {"lo", "wmaster0"}
56 end
57 for k, i in pairs(tignorelist) do
58 tignore[i] = true
59 end
60
61 -- Get NET stats
62 for line in io.lines("/proc/net/dev") do
63 -- Match wmaster0 as well as rt0 (multiple leading spaces)
64 local name = string.match(line, "^[%s]?[%s]?[%s]?[%s]?([%w]+):")
65 if name ~= nil then
66 -- Received bytes, first value after the name
67 local recv = tonumber(string.match(line, ":[%s]*([%d]+)"))
68 -- Transmited bytes, 7 fields from end of the line
69 local send = tonumber(string.match(line,
70 "([%d]+)%s+%d+%s+%d+%s+%d+%s+%d+%s+%d+%s+%d+%s+%d$"))
71
72 if not tignore[name] then
73 total_rx = total_rx + recv
74 total_tx = total_tx + send
75 end
76
77 helpers.uformat(args, name .. " rx", recv, unit)
78 helpers.uformat(args, name .. " tx", send, unit)
79
80 if nets[name] == nil then
81 -- Default values on the first run
82 nets[name] = {}
83
84 helpers.uformat(args, name .. " down", 0, unit)
85 helpers.uformat(args, name .. " up", 0, unit)
86 args["{"..name.." carrier}"] = 0
87
88 nets[name].time = os.time()
89 else -- Net stats are absolute, substract our last reading
90 local interval = os.time() - nets[name].time > 0 and
91 os.time() - nets[name].time or 1
92 nets[name].time = os.time()
93
94 local down = (recv - nets[name][1]) / interval
95 local up = (send - nets[name][2]) / interval
96
97 helpers.uformat(args, name .. " down", down, unit)
98 helpers.uformat(args, name .. " up", up, unit)
99
100 -- Carrier detection
101 sysnet = helpers.pathtotable("/sys/class/net/" .. name)
102
103 if sysnet.carrier then
104 ccarrier = tonumber(sysnet.carrier)
105
106 args["{"..name.." carrier}"] = ccarrier
107 if ccarrier == 1 and not tignore[name] then
108 any_up = 1
109 end
110 else
111 args["{"..name.." carrier}"] = 0
112 end
113 end
114
115 -- Store totals
116 nets[name][1] = recv
117 nets[name][2] = send
118 end
119 end
120
121 helpers.uformat(args, "total rx", total_rx, unit)
122 helpers.uformat(args, "total tx", total_tx, unit)
123
124 if nets["total"] == nil then
125 -- Default values on the first run
126 nets["total"] = {}
127
128 helpers.uformat(args, "total down", 0, unit)
129 helpers.uformat(args, "total up", 0, unit)
130 args["{total carrier}"] = 0
131
132 nets["total"].time = os.time()
133 else -- Net stats are absolute, substract our last reading
134 local interval = os.time() - nets["total"].time > 0 and
135 os.time() - nets["total"].time or 1
136 nets["total"].time = os.time()
137
138 local down = (total_rx - nets["total"][1]) / interval
139 local up = (total_tx - nets["total"][2]) / interval
140
141 helpers.uformat(args, "total down", down, unit)
142 helpers.uformat(args, "total up", up, unit)
143 args["{total carrier}"] = any_up
144 end
145
146 -- Store totals
147 nets["total"][1] = total_rx
148 nets["total"][2] = total_tx
149
150 return args
151 end
152 -- }}}
153
154 return setmetatable(net_linux, { __call = function(_, ...) return worker(...) end })