2 Copyright (C) 2011 Pau Escrich <pau@dabax.net>
3 Contributors Jo-Philipp Wich <xm@subsignal.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
23 local ltn12 = require("luci.ltn12")
24 local json = require("luci.json")
25 local util = require("luci.util")
26 local uci = require("luci.model.uci")
27 local sys = require("luci.sys")
28 local template = require("luci.template")
29 local http = require("luci.http")
30 local string = require("string")
31 local table = require("table")
32 local nixio = require("nixio")
33 local nixiofs = require("nixio.fs")
36 module "luci.model.bmx6json"
38 -- Returns a LUA object from bmx6 JSON daemon
40 function get(field, host)
43 if host:match(":") then
44 url = 'http://[%s]/cgi-bin/bmx6-info?' % host
46 url = 'http://%s/cgi-bin/bmx6-info?' % host
49 url = uci.cursor():get("luci-bmx6","luci","json")
53 print_error("bmx6 json url not configured, cannot fetch bmx6 daemon data",true)
57 local json_url = util.split(url,":")
60 if json_url[1] == "http" then
61 raw,err = wget(url..field,1000)
64 if json_url[1] == "exec" then
65 raw = sys.exec(json_url[2]..' '..field)
67 print_error("bmx6 json url not recognized, cannot fetch bmx6 daemon data. Use http: or exec:",true)
75 if raw and raw:len() > 10 then
76 local decoder = json.Decoder()
77 ltn12.pump.all(ltn12.source.string(raw), decoder:sink())
80 -- print_error("Cannot get data from bmx6 daemon",true)
87 function print_error(txt,popup)
89 sys.call("logger -t bmx6json " .. txt)
92 http.write('<script type="text/javascript">alert("Some error detected, please check it: '..txt..'");</script>')
94 http.write("<h1>Dammit! some error detected</h1>")
95 http.write("bmx6-luci: " .. txt)
96 http.write('<p><FORM><INPUT TYPE="BUTTON" VALUE="Go Back" ONCLICK="history.go(-1)"></FORM></p>')
101 function text2html(txt)
102 txt = string.gsub(txt,"<","{")
103 txt = string.gsub(txt,">","}")
104 txt = util.striptags(txt)
109 function wget(url, timeout)
110 local rfd, wfd = nixio.pipe()
111 local pid = nixio.fork()
114 nixio.dup(wfd, nixio.stdout)
116 local candidates = { "/usr/bin/wget", "/bin/wget" }
118 for _, bin in ipairs(candidates) do
119 if nixiofs.access(bin, "x") then
120 nixio.exec(bin, "-q", "-O", "-", url)
126 rfd:setblocking(false)
132 local ready = nixio.poll({{ fd = rfd, events = nixio.poll_flags("in") }}, timeout)
134 nixio.kill(pid, nixio.const.SIGKILL)
139 local rv = rfd:read(4096)
146 buffer[#buffer+1] = rv
149 if nixio.errno() ~= nixio.const.EAGAIN and
150 nixio.errno() ~= nixio.const.EWOULDBLOCK then
157 nixio.waitpid(pid, "nohang")
159 return table.concat(buffer)
161 return nil, err1, err2
166 function getOptions(name)
167 -- Getting json and Checking if bmx6-json is avaiable
168 local options = get("options")
169 if options == nil or options.OPTIONS == nil then
170 m.message = "bmx6-json plugin is not running or some mistake in luci-bmx6 configuration, check /etc/config/luci-bmx6"
173 options = options.OPTIONS
176 -- Filtering by the option name
180 for _,i in ipairs(options) do
181 if i.name == name and i.CHILD_OPTIONS ~= nil then
182 namedopt = i.CHILD_OPTIONS
191 -- Rturns a help string formated to be used in HTML scope
192 function getHtmlHelp(opt)
193 if opt == nil then return nil end
196 if opt.help ~= nil then
197 help = text2html(opt.help)
199 if opt.syntax ~= nil then
200 help = help .. "<br/><b>Syntax: </b>" .. text2html(opt.syntax)
206 function testandreload()
207 local test = sys.call('bmx6 -c --test > /tmp/bmx6-luci.err.tmp')
209 return sys.exec("cat /tmp/bmx6-luci.err.tmp")
212 local err = sys.call('bmx6 -c --configReload > /tmp/bmx6-luci.err.tmp')
214 return sys.exec("cat /tmp/bmx6-luci.err.tmp")