-
Notifications
You must be signed in to change notification settings - Fork 12
/
system.lua
146 lines (119 loc) · 3.67 KB
/
system.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
local utility = require('utility')
local asyncshell = require('asyncshell')
local system = { }
-- Network subsystem --
system.network = { hosts = {},
interfaces = {},
options = { ping_count = 4,
ping_timeout = 5,
interval = 60 } }
local initialized = false
local hosts_metrics = {}
local callbacks = { latency = {}, connection = {} }
local complete = {}
local interface_connected = {}
local last_iface_connected = nil
local iproute_command = "ip addr show %s 2> /dev/null"
local ping_command = "ping -c %d -w %d -q %s"
function system.network.add_latency_callback (fn)
table.insert(callbacks.latency, fn)
end
function system.network.add_connection_callback (fn)
table.insert(callbacks.connection, fn)
end
local function check_complete()
for _, v in pairs(complete) do
if not v then return end
end
for k, _ in pairs(complete) do
complete[k] = false
end
for _, callback in ipairs(callbacks.latency) do
callback(hosts_metrics)
end
end
local function ping_callback(f, host)
local l = f:read()
if l == nil then
hosts_metrics[host].loss = 100
hosts_metrics[host].time = -1
complete[host] = true
check_complete()
return
end
-- Skip two lines
f:read()
f:read()
_, _, loss = string.find(f:read(), ", (%d+)%% packet loss")
if loss ~= "100" then
_, _, time = string.find(f:read(), "min/avg/max/mdev = [%d%.]+/([%d%.]+)/.*")
else
time = -1
end
hosts_metrics[host].loss = tonumber(loss)
hosts_metrics[host].time = tonumber(time)
complete[host] = true
check_complete()
end
local function get_iface_type (iface)
if not iface then
return "none"
elseif iface:match("wl.+") then
return "wireless"
else
return "wired"
end
end
local function reping_network()
for _, host in ipairs(system.network.hosts) do
asyncshell.request(string.format(ping_command, system.network.options.ping_count,
system.network.options.ping_timeout, host),
function(f) ping_callback(f, host) end)
end
end
local function check_connected ()
local count = 0
for _ in pairs(interface_connected) do count = count + 1 end
if count < #system.network.interfaces then
return
end
local connected_iface = nil
for _, iface in ipairs(system.network.interfaces) do
if interface_connected[iface] then
connected_iface = iface
break
end
end
local type = get_iface_type(connected_iface)
for _, callback in ipairs(callbacks.connection) do
callback(connected_iface, type)
end
if last_iface_connected ~= connected_iface then
reping_network()
last_iface_connected = connected_iface
end
end
local function iproute_callback (f, iface)
local t = f:read("*all")
interface_connected[iface] = t:match("inet %d+%.%d+%.%d+%.%d+") and true or false
check_connected()
end
local function requery_network()
interface_connected = {}
for _, iface in ipairs(system.network.interfaces) do
asyncshell.request(string.format(iproute_command, iface),
function(f) iproute_callback(f, iface) end)
end
end
function system.network.init()
if initialized then return end
for _, host in ipairs(system.network.hosts) do
hosts_metrics[host] = {}
complete[host] = false
end
scheduler.register_recurring("system.network.connection", 10, requery_network)
scheduler.register_recurring("system.network.latency", system.network.options.interval,
reping_network)
initialized = true
end
return system