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)
32 if not buffer or #buffer == 0 then
43 nixio.dup(fdo, nixio.stdout)
46 nixio.exece(cmd, args, nil)
52 function action_data()
53 local http = require "luci.http"
57 json = "application/json"
61 local mtype = http.formvalue("type") or "json"
62 local delim = http.formvalue("delim") or ";"
63 local period = http.formvalue("period")
64 local group_by = http.formvalue("group_by")
65 local order_by = http.formvalue("order_by")
71 http.status(400, "Unsupported type")
75 if delim and #delim > 0 then
76 args[#args+1] = "-s%s" % delim
79 if period and #period > 0 then
81 args[#args+1] = period
84 if group_by and #group_by > 0 then
86 args[#args+1] = group_by
89 if order_by and #order_by > 0 then
91 args[#args+1] = order_by
94 http.prepare_content(types[mtype])
95 exec("/usr/sbin/nlbw", args, http.write)
98 function action_list()
99 local http = require "luci.http"
101 local fd = io.popen("/usr/sbin/nlbw -c list")
106 local period = fd:read("*l")
112 periods[#periods+1] = period
118 http.prepare_content("application/json")
119 http.write_json(periods)
122 function action_ptr(...)
123 local http = require "luci.http"
124 local util = require "luci.util"
126 http.prepare_content("application/json")
127 http.write_json(util.ubus("network.rrdns", "lookup", {
128 addrs = {...}, timeout = 3000
132 function action_download()
133 local nixio = require "nixio"
134 local http = require "luci.http"
135 local sys = require "luci.sys"
136 local uci = require "luci.model.uci".cursor()
138 local dir = uci:get_first("nlbwmon", "nlbwmon", "database_directory")
139 or "/var/lib/nlbwmon"
141 if dir and nixio.fs.stat(dir, "type") == "dir" then
142 local n = "nlbwmon-backup-%s-%s.tar.gz"
143 %{ sys.hostname(), os.date("%Y-%m-%d") }
145 http.prepare_content("application/octet-stream")
146 http.header("Content-Disposition", "attachment; filename=\"%s\"" % n)
147 exec("/bin/tar", { "-C", dir, "-c", "-z", ".", "-f", "-" }, http.write)
149 http.status(500, "Unable to find database directory")
153 function action_restore()
154 local nixio = require "nixio"
155 local http = require "luci.http"
156 local i18n = require "luci.i18n"
157 local tpl = require "luci.template"
158 local uci = require "luci.model.uci".cursor()
160 local tmp = "/tmp/nlbw-restore.tar.gz"
161 local dir = uci:get_first("nlbwmon", "nlbwmon", "database_directory")
162 or "/var/lib/nlbwmon"
166 function(meta, chunk, eof)
167 if not fp and meta and meta.name == "archive" then
168 fp = io.open(tmp, "w")
179 local tar = io.popen("/bin/tar -tzf %s" % tmp, "r")
182 local file = tar:read("*l")
185 elseif file:match("^%d%d%d%d%d%d%d%d%.db%.gz$") or
186 file:match("^%./%d%d%d%d%d%d%d%d%.db%.gz$") then
187 files[#files+1] = file
194 http.status(500, "Internal Server Error")
195 tpl.render("nlbw/backup", {
196 message = i18n.translate("Invalid or empty backup archive")
204 exec("/etc/init.d/nlbwmon", { "stop" })
205 exec("/bin/mkdir", { "-p", dir })
207 exec("/bin/tar", { "-C", dir, "-vxzf", tmp, unpack(files) },
208 function(chunk) output[#output+1] = chunk:match("%S+") end)
210 exec("/bin/rm", { "-f", tmp })
211 exec("/etc/init.d/nlbwmon", { "start" })
213 tpl.render("nlbw/backup", {
214 message = i18n.translatef(
215 "The following database files have been restored: %s",
216 table.concat(output, ", "))
220 function action_commit()
221 local http = require "luci.http"
222 local disp = require "luci.dispatcher"
224 http.redirect(disp.build_url("admin/nlbw/display"))
225 exec("/usr/sbin/nlbw", { "-c", "commit" })