| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
> |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 35 |
|
| 36 |
local net_linux = {} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
local nets = {} |
| 41 |
|
| 42 |
local unit = { ["b"] = 1, ["kb"] = 1024, |
| 43 |
["mb"] = 1024^2, ["gb"] = 1024^3 |
| 44 |
} |
| 45 |
|
| 46 |
|
| 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 |
|
| 62 |
for line in io.lines("/proc/net/dev") do |
| 63 |
|
| 64 |
local name = string.match(line, "^[%s]?[%s]?[%s]?[%s]?([%w]+):") |
| 65 |
if name ~= nil then |
| 66 |
|
| 67 |
local recv = tonumber(string.match(line, ":[%s]*([%d]+)")) |
| 68 |
|
| 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 |
|
| 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 |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
| 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 |
|
| 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 }) |