forked from vicious-widgets/vicious
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_freebsd.lua
108 lines (88 loc) · 3.25 KB
/
net_freebsd.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
-- network status and usage widget type for FreeBSD
-- Copyright (C) 2017,2019 mutlusun <[email protected]>
-- Copyright (C) 2019 Nguyễn Gia Phong <[email protected]>
--
-- This file is part of Vicious.
--
-- Vicious is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as
-- published by the Free Software Foundation, either version 2 of the
-- License, or (at your option) any later version.
--
-- Vicious is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
-- {{{ Grab environment
local tonumber = tonumber
local os = { time = os.time }
local string = {
match = string.match,
gmatch = string.gmatch
}
local helpers = require("vicious.helpers")
local spawn = require("vicious.spawn")
-- }}}
-- Net: provides state and usage statistics of all network interfaces
-- vicious.widgets.net
local net_freebsd = {}
-- Initialize function tables
local nets = {}
-- Variable definitions
local unit = { ["b"] = 1, ["kb"] = 1024,
["mb"] = 1024^2, ["gb"] = 1024^3
}
-- {{{ Net widget type
local function parse(stdout, stderr, exitreason, exitcode)
local args = {}
local buffer = nil
local now = os.time()
for line in string.gmatch(stdout, "[^\n]+") do
if not (line:find("<Link") or line:find("Name")) then -- skipping missleading lines
local split = { line:match(("([^%s]*)%s*"):rep(12)) }
if buffer == nil then
buffer = { tonumber(split[8]), tonumber(split[11]) } -- recv (field 8) and send (field 11)
else
buffer = { buffer[1] + tonumber(split[8]),
buffer[2] + tonumber(split[11]) }
end
end
end
if buffer == nil then
args["{carrier}"] = 0
helpers.uformat(args, "rx", 0, unit)
helpers.uformat(args, "tx", 0, unit)
helpers.uformat(args, "down", 0, unit)
helpers.uformat(args, "up", 0, unit)
else
args["{carrier}"] = 1
helpers.uformat(args, "rx", buffer[1], unit)
helpers.uformat(args, "tx", buffer[2], unit)
if next(nets) == nil then
helpers.uformat(args, "down", 0, unit)
helpers.uformat(args, "up", 0, unit)
else
local interval = now - nets["time"]
if interval <= 0 then interval = 1 end
local down = (buffer[1] - nets[1]) / interval
local up = (buffer[2] - nets[2]) / interval
helpers.uformat(args, "down", down, unit)
helpers.uformat(args, "up", up, unit)
end
nets["time"] = now
-- Store totals
nets[1] = buffer[1]
nets[2] = buffer[2]
end
return args
end
function net_freebsd.async(format, warg, callback)
if not warg then return callback{} end
spawn.easy_async("netstat -n -b -I " .. helpers.shellquote(warg),
function (...) callback(parse(...)) end)
end
-- }}}
return helpers.setasyncall(net_freebsd)