1 -- Copyright 2017 Jo-Philipp Wich <jo@mein.io>
2 -- Licensed to the public under the Apache License 2.0.
4 module("luci.controller.nlbw", package.seeall)
7 entry({"admin", "nlbw"}, firstchild(), _("Bandwidth Monitor"), 80)
8 entry({"admin", "nlbw", "display"}, template("nlbw/display"), _("Display"), 1)
9 entry({"admin", "nlbw", "config"}, cbi("nlbw/config"), _("Configuration"), 2)
10 entry({"admin", "nlbw", "backup"}, template("nlbw/backup"), _("Backup"), 3)
11 entry({"admin", "nlbw", "data"}, call("action_data"), nil, 4)
12 entry({"admin", "nlbw", "list"}, call("action_list"), nil, 5)
13 entry({"admin", "nlbw", "ptr"}, call("action_ptr"), nil, 6).leaf = true
14 entry({"admin", "nlbw", "download"}, call("action_download"), nil, 7)
15 entry({"admin", "nlbw", "restore"}, post("action_restore"), nil, 8)
16 entry({"admin", "nlbw", "commit"}, call("action_commit"), nil, 9)
19 local function exec(cmd, args, writer)
20 local os = require "os"
21 local nixio = require "nixio"
23 local fdi, fdo = nixio.pipe()
24 local pid = nixio.fork()
30 local buffer = fdi:read(2048)
31 local wpid, stat, code = nixio.waitpid(pid, "nohang")
33 if writer and buffer and #buffer > 0 then
37 if wpid and stat == "exited" then
42 nixio.dup(fdo, nixio.stdout)
45 nixio.exece(cmd, args, nil)
51 function action_data()
52 local http = require "luci.http"
56 json = "application/json"
60 local mtype = http.formvalue("type") or "json"
61 local delim = http.formvalue("delim") or ";"
62 local period = http.formvalue("period")
63 local group_by = http.formvalue("group_by")
64 local order_by = http.formvalue("order_by")
70 http.status(400, "Unsupported type")
74 if delim and #delim > 0 then
75 args[#args+1] = "-s%s" % delim
78 if period and #period > 0 then
80 args[#args+1] = period
83 if group_by and #group_by > 0 then
85 args[#args+1] = group_by
88 if order_by and #order_by > 0 then
90 args[#args+1] = order_by
93 http.prepare_content(types[mtype])
94 exec("/usr/sbin/nlbw", args, http.write)
97 function action_list()
98 local http = require "luci.http"
100 local fd = io.popen("/usr/sbin/nlbw -c list")
105 local period = fd:read("*l")
111 periods[#periods+1] = period
117 http.prepare_content("application/json")
118 http.write_json(periods)
121 function action_ptr(...)
122 local http = require "luci.http"
123 local util = require "luci.util"
125 http.prepare_content("application/json")
126 http.write_json(util.ubus("network.rrdns", "lookup", {
127 addrs = {...}, timeout = 3000
131 function action_download()
132 local nixio = require "nixio"
133 local http = require "luci.http"
134 local sys = require "luci.sys"
135 local uci = require "luci.model.uci".cursor()
137 local dir = uci:get_first("nlbwmon", "nlbwmon", "database_directory")
138 or "/var/lib/nlbwmon"
140 if dir and nixio.fs.stat(dir, "type") == "dir" then
141 local n = "nlbwmon-backup-%s-%s.tar.gz"
142 %{ sys.hostname(), os.date("%Y-%m-%d") }
144 http.prepare_content("application/octet-stream")
145 http.header("Content-Disposition", "attachment; filename=\"%s\"" % n)
146 exec("/bin/tar", { "-C", dir, "-c", "-z", ".", "-f", "-" }, http.write)
148 http.status(500, "Unable to find database directory")
152 function action_restore()
153 local nixio = require "nixio"
154 local http = require "luci.http"
155 local i18n = require "luci.i18n"
156 local tpl = require "luci.template"
157 local uci = require "luci.model.uci".cursor()
159 local tmp = "/tmp/nlbw-restore.tar.gz"
160 local dir = uci:get_first("nlbwmon", "nlbwmon", "database_directory")
161 or "/var/lib/nlbwmon"
165 function(meta, chunk, eof)
166 if not fp and meta and meta.name == "archive" then
167 fp = io.open(tmp, "w")
178 local tar = io.popen("/bin/tar -tzf %s" % tmp, "r")
181 local file = tar:read("*l")
184 elseif file:match("^%d%d%d%d%d%d%d%d%.db%.gz$") or
185 file:match("^%./%d%d%d%d%d%d%d%d%.db%.gz$") then
186 files[#files+1] = file
193 http.status(500, "Internal Server Error")
194 tpl.render("nlbw/backup", {
195 message = i18n.translate("Invalid or empty backup archive")
203 exec("/etc/init.d/nlbwmon", { "stop" })
204 exec("/bin/mkdir", { "-p", dir })
206 exec("/bin/tar", { "-C", dir, "-vxzf", tmp, unpack(files) },
207 function(chunk) output[#output+1] = chunk:match("%S+") end)
209 exec("/bin/rm", { "-f", tmp })
210 exec("/etc/init.d/nlbwmon", { "start" })
212 tpl.render("nlbw/backup", {
213 message = i18n.translatef(
214 "The following database files have been restored: %s",
215 table.concat(output, ", "))
219 function action_commit()
220 local http = require "luci.http"
221 local disp = require "luci.dispatcher"
223 http.redirect(disp.build_url("admin/nlbw/display"))
224 exec("/usr/sbin/nlbw", { "-c", "commit" })