-
Notifications
You must be signed in to change notification settings - Fork 0
/
lime_daemon.lua
146 lines (120 loc) · 4 KB
/
lime_daemon.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
#!/usr/bin/env lua
#!/usr/bin/lua
--[[
lime-ubus
Copyright 2017 Nicolas Echaniz <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-3.0
]]--
-- deprecated calls
--
-- get_cloud_nodes, replaced by standard ubus call to uci:
-- ubus call uci get '{"config": "system", "section": "@system[0]", "option": "hostname"}'
require "ubus"
require "uloop"
function lines(str)
print(str)
local t = {}
local function helper(line)
table.insert(t, line)
return ""
end
helper((str:gsub("(.-)\r?\n", helper)))
return t
end
local function shell(command)
-- TODO(nicoechaniz): sanitize or evaluate if this is a security risk
local handle = io.popen(command)
local result = handle:read("*a")
handle:close()
return result
end
local function _get_loss(host, ip_version)
local ping_cmd = "ping"
if ip_version then
if ip_version == 6 then
ping_cmd = "ping6"
end
end
local shell_output = shell(ping_cmd.." -q -i 0.1 -c4 -w2 "..host)
local loss = "100"
if shell_output ~= "" then
loss = shell_output:match("(%d*)%% packet loss")
end
return loss
end
uloop.init()
local conn = ubus.connect()
if not conn then
error("Failed to connect to ubus")
end
local function get_cloud_nodes(req, msg)
local local_net = conn:call("uci", "get", {config="network", section="lm_net_anygw_route4", option="target" }).value
local nodes = lines(shell("bmx6 -cd8 | grep ".. local_net .." | awk '{ print $10 }'"))
local result = {}
result.nodes = {}
for _, line in ipairs(nodes) do --nodes:gmatch("[^\n]*") do
if line ~= "" then
table.insert(result.nodes, line)
end
end
conn:reply(req, result);
end
local function get_location(req, msg)
local result = {}
local lat = conn:call("uci", "get", {config="libremap", section="location", option="latitude" }).value
local lon = conn:call("uci", "get", {config="libremap", section="location", option="longitude" }).value
if (type(tonumber(lat)) == "number" and type(tonumber(lon)) == "number") then
result.lat = lat
result.lon = lon
else
result.lat = conn:call("uci", "get", {config="libremap", section="@libremap[0]",
option="community_lat" }).value
result.lon = conn:call("uci", "get", {config="libremap", section="@libremap[0]",
option="community_lon" }).value
end
conn:reply(req, result);
end
local function get_metrics(req, msg)
conn:reply(req, {status="processing"})
local def_req = conn:defer_request(req)
uloop.timer(function()
local result = {}
local node = msg.target
local loss = _get_loss(node..".mesh", 6)
local bw = 0
if loss ~= "100" then
local command = "netperf -l 6 -H "..node..".mesh -t tcp_maerts| tail -n1| awk '{ print $5 }'"
local shell_output = shell(command)
if shell_output ~= "" then
bw = shell_output:match("[%d.]+")
end
end
result.loss = loss
result.bandwidth = bw
conn:reply(def_req, result)
conn:complete_deferred_request(def_req, 0)
print("Deferred request complete")
end, 500)
print("Call to function 'deferred'")
end
local lime_api = {
lime = {
get_cloud_nodes = { get_cloud_nodes, {} },
get_location = { get_location, {} },
get_metrics = { get_metrics, { msg=ubus.STRING } },
}
}
conn:add(lime_api)
local my_event = {
test = function(msg)
print("Call to test event")
for k, v in pairs(msg) do
print("key=" .. k .. " value=" .. tostring(v))
end
end,
}
conn:listen(my_event)
uloop.run()