Developing lightweight computation at the DSG edge

qmpinfo.lua 5.9 KB
Newer Older
1
#!/usr/bin/lua
2
--[[
3
    Copyright (C) 2011 Fundacio Privada per a la Xarxa Oberta, Lliure i Neutral guifi.net
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

    This program 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.

    This program 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 this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

    The full GNU General Public License is included in this distribution in
    the file called "COPYING".
21
22
23
24

    Contributors:
        Simó Albert i Beltran

25
--]]
26
27

iwinfo = require "iwinfo"
28
29
util = require "luci.util"
sys = require "luci.sys"
30
31

qmpinfo = {}
p4u's avatar
p4u committed
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
local i,d

function qmpinfo.get_devices()

	ethernet_interfaces = { 'eth' }
	wireless_interfaces = { 'ath', 'wlan' }

	local eth_int = {}
	for i,d in ipairs(sys.net.devices()) do
		for i,r in ipairs(ethernet_interfaces) do
			if string.find(d,r) ~= nil then
				if string.find(d,"%.") == nil  then
					table.insert(eth_int,d)
				end
			end
		end
	end

	local wl_int = {}
	for i,d in ipairs(luci.sys.net.devices()) do
		for i,r in ipairs(wireless_interfaces) do
			if string.find(d,r) ~= nil then
				if string.find(d,"%.") == nil  then
					table.insert(wl_int,d)
				end
			end
		end
	end

	return {eth_int,wl_int}

end
64
65

function qmpinfo.get_modes(dev)
66
	local modes = {}
67
	local iw = iwinfo[iwinfo.type(dev)]
68
69
	if iw ~= nil then modes = iw.hwmodelist(dev) end
	return modes
70

71
72
end

73
74
75
76

function qmpinfo.get_txpower(dev)
	local iw = iwinfo[iwinfo.type(dev)]
	local txpower_supported = {}
77
	if iw ~= nil then
78
		local txp = iw.txpwrlist(dev) or {}
79
80
81
		for _,v in ipairs(txp) do
			table.insert(txpower_supported,v.dbm)
		end
82
	end
83

84
	return txpower_supported
85

86
87
end

88
function qmpinfo.get_channels(dev)
89
90
91
92
93
94
95
96
97
98
99
100
101
102
	local clist = {} -- output channel list
	local iw = iwinfo[iwinfo.type(dev)]
	local ch = {}

	-- if there are not wireless cards, returning a dummy value
	if iw == nil then
		ch.channel=0
		ch.adhoc=false
		ch.ht40p=false
		ch.ht40m=false
		table.insert(clist,ch)
		return clist
	end

103
104
105
106
107
	local freqs = iw.freqlist(dev) --freqs list
	local c -- current channel
	local nc = 0 -- next channel
	local pc = 0 -- previous channel
	local adhoc
108
	local ht40_support = qmpinfo.get_modes(dev).n
109

110
111
112
113
114
115
116

	for i,f in ipairs(freqs) do
		c = f.channel
		ch = {}
		ch.channel = c
		ch.ht40p = false
		ch.ht40m = false
117
118
119
120
121
122

		if not f.restricted then
			ch.adhoc = true
		else
			ch.adhoc = false
		end
123
124
125

		-- 2.4Ghz band
		if c < 15 then
126
			if c < 4 then
127
				ch.ht40p = true
128
129
130

			elseif c < 10 then
				ch.ht40m = true
131
				ch.ht40p = true
132
			else
133
134
				ch.ht40m = true
			end
135

136
137
138
139
140
141
142
143
144
145
		-- 5Ghz band
		elseif c > 14 then
			if #freqs == i then nc = nil
			else nc = freqs[i+1].channel
			end

			if i == 1 then pc = nil
			else pc = freqs[i-1].channel
			end

146
147
			if nc ~= nil and nc-c == 4 then
				ch.ht40p = true
148
			end
149

150
151
152
153
154
			if pc ~= nil and c-pc == 4 then
				ch.ht40m = true
			end

		end
155
156
157
158
159
160
161

		-- If the device does not support ht40, both vars (+/-) are false
		if not ht40_support then
			ch.ht40p = false
			ch.ht40m = false
		end

162
		table.insert(clist,ch)
163

164
	end
165
166
167
	return clist
end

168
169
170

function qmpinfo.get_ipv4()
	local ipv4 = {}
171
	local ipv4_raw = util.exec("ip -4 a | awk '/inet/{print $2}'")
172
	for _,v in ipairs(util.split(ipv4_raw)) do
173
174
175
176
177
178
179
		local match = false
		local i = 1
		while i <= #ipv4 and not match do
			match = string.match(util.trim(v),util.trim(ipv4[i]))
			i = i + 1
		end
		if not match and #util.trim(v) > 1 then
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
			table.insert(ipv4,util.trim(v))
		end
	end
	return ipv4
end

function qmpinfo.get_hostname()
	local hostname = util.exec("cat /proc/sys/kernel/hostname")
	return hostname
end

function qmpinfo.get_uname()
	local uname = util.exec("uname -a")
	return uname
end

196
function qmpinfo.bandwidth_test(ip)
197
        local bwtest = util.trim(util.exec("netperf -6 -p 12865 -H "..ip.." -fm -v0 -P0"))
198
199
200
201
202
203
204
        local result = nil
        if #bwtest < 10 then
                result = bwtest
        end

        return result
end
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219

function qmpinfo.nodes()
	local nodes = util.split(util.exec('bmx6 -c --originators | awk \'{print $1 "|" $3}\' | grep -e ".*:.*:"'))
	local ni
	result = {}
	for _,n in ipairs(nodes) do
		if n ~= "" then
		 ni = util.split(n,"|")
		 ni[1] = util.split(ni[1],".")[1]
		 table.insert(result,ni)
		end
	end
	return result
end

p4u's avatar
p4u committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
function qmpinfo.links()
	local nodes = util.split(util.exec('bmx6 -c --links | awk \'{print $1 "|" $2}\' |  grep -e ".*:.*:"'))
	local ni
	result = {}
	for _,n in ipairs(nodes) do
		if n ~= "" then
		ni = util.split(n,"|")
		ni[1] = util.split(ni[1],".")[1]
		table.insert(result,ni)
		end
	end
	return result
end

234
235
236
237
238
239
240
241
242
243
244
245
function qmpinfo.get_version(option)                    
	local version = nil
	if option == nil or option == "full" then version = util.exec("cat /etc/qmp/qmp.release | grep DESCRIPTION | cut -d= -f2")
	elseif option == "build" then version = util.exec("cat /etc/qmp/qmp.release | grep BUILDDATE | cut -d= -f2")
	elseif option == "branch" then version = util.exec("cat /etc/qmp/qmp.release | grep BRANCH | cut -d= -f2")
	elseif option == "codename" then version = util.exec("cat /etc/qmp/qmp.release | grep CODENAME | cut -d= -f2")
	elseif option == "release" then version = util.exec("cat /etc/qmp/qmp.release | grep RELEASE | cut -d= -f2")
	elseif option == "revision" then version = util.exec("cat /etc/qmp/qmp.release | grep REVISION | cut -d= -f2")
	else version = nil
	end
	return version
end  
p4u's avatar
p4u committed
246

247
248
249
250
251
252
253
254
255
function qmpinfo.get_key()
	local keyf = util.exec("uci get qmp.node.key")
	if #keyf < 2 then
		keyf = "/tmp/qmp_key"
	end
	local key = util.split(util.exec("cat "..keyf))[1]
	return key
end

256
return qmpinfo
p4u's avatar
p4u committed
257