Merge pull request #1818 from dibdot/lxc_fix
[project/luci.git] / applications / luci-app-nlbwmon / luasrc / controller / nlbw.lua
1 -- Copyright 2017 Jo-Philipp Wich <jo@mein.io>
2 -- Licensed to the public under the Apache License 2.0.
3
4 module("luci.controller.nlbw", package.seeall)
5
6 function index()
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)
17 end
18
19 local function exec(cmd, args, writer)
20         local os = require "os"
21         local nixio = require "nixio"
22
23         local fdi, fdo = nixio.pipe()
24         local pid = nixio.fork()
25
26         if pid > 0 then
27                 fdo:close()
28
29                 while true do
30                         local buffer = fdi:read(2048)
31
32                         if not buffer or #buffer == 0 then
33                                 break
34                         end
35
36                         if writer then
37                                 writer(buffer)
38                         end
39                 end
40
41                 nixio.waitpid(pid)
42         elseif pid == 0 then
43                 nixio.dup(fdo, nixio.stdout)
44                 fdi:close()
45                 fdo:close()
46                 nixio.exece(cmd, args, nil)
47                 nixio.stdout:close()
48                 os.exit(1)
49         end
50 end
51
52 function action_data()
53         local http = require "luci.http"
54
55         local types = {
56                 csv = "text/csv",
57                 json = "application/json"
58         }
59
60         local args = { }
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")
66
67         if types[mtype] then
68                 args[#args+1] = "-c"
69                 args[#args+1] = mtype
70         else
71                 http.status(400, "Unsupported type")
72                 return
73         end
74
75         if delim and #delim > 0 then
76                 args[#args+1] = "-s%s" % delim
77         end
78
79         if period and #period > 0 then
80                 args[#args+1] = "-t"
81                 args[#args+1] = period
82         end
83
84         if group_by and #group_by > 0 then
85                 args[#args+1] = "-g"
86                 args[#args+1] = group_by
87         end
88
89         if order_by and #order_by > 0 then
90                 args[#args+1] = "-o"
91                 args[#args+1] = order_by
92         end
93
94         http.prepare_content(types[mtype])
95         exec("/usr/sbin/nlbw", args, http.write)
96 end
97
98 function action_list()
99         local http = require "luci.http"
100
101         local fd = io.popen("/usr/sbin/nlbw -c list")
102         local periods = { }
103
104         if fd then
105                 while true do
106                         local period = fd:read("*l")
107
108                         if not period then
109                                 break
110                         end
111
112                         periods[#periods+1] = period
113                 end
114
115                 fd:close()
116         end
117
118         http.prepare_content("application/json")
119         http.write_json(periods)
120 end
121
122 function action_ptr(...)
123         local http = require "luci.http"
124         local util = require "luci.util"
125
126         http.prepare_content("application/json")
127         http.write_json(util.ubus("network.rrdns", "lookup", {
128                 addrs = {...}, timeout = 3000
129         }))
130 end
131
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()
137
138         local dir = uci:get_first("nlbwmon", "nlbwmon", "database_directory")
139                 or "/var/lib/nlbwmon"
140
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") }
144
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)
148         else
149                 http.status(500, "Unable to find database directory")
150         end
151 end
152
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()
159
160         local tmp = "/tmp/nlbw-restore.tar.gz"
161         local dir = uci:get_first("nlbwmon", "nlbwmon", "database_directory")
162                 or "/var/lib/nlbwmon"
163
164         local fp
165         http.setfilehandler(
166                 function(meta, chunk, eof)
167                         if not fp and meta and meta.name == "archive" then
168                                 fp = io.open(tmp, "w")
169                         end
170                         if fp and chunk then
171                                 fp:write(chunk)
172                         end
173                         if fp and eof then
174                                 fp:close()
175                         end
176                 end)
177
178         local files = { }
179         local tar = io.popen("/bin/tar -tzf %s" % tmp, "r")
180         if tar then
181                 while true do
182                         local file = tar:read("*l")
183                         if not file then
184                                 break
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
188                         end
189                 end
190                 tar:close()
191         end
192
193         if #files == 0 then
194                 http.status(500, "Internal Server Error")
195                 tpl.render("nlbw/backup", {
196                         message = i18n.translate("Invalid or empty backup archive")
197                 })
198                 return
199         end
200
201
202         local output = { }
203
204         exec("/etc/init.d/nlbwmon", { "stop" })
205         exec("/bin/mkdir", { "-p", dir })
206
207         exec("/bin/tar", { "-C", dir, "-vxzf", tmp, unpack(files) },
208                 function(chunk) output[#output+1] = chunk:match("%S+") end)
209
210         exec("/bin/rm", { "-f", tmp })
211         exec("/etc/init.d/nlbwmon", { "start" })
212
213         tpl.render("nlbw/backup", {
214                 message = i18n.translatef(
215                         "The following database files have been restored: %s",
216                         table.concat(output, ", "))
217         })
218 end
219
220 function action_commit()
221         local http = require "luci.http"
222         local disp = require "luci.dispatcher"
223
224         http.redirect(disp.build_url("admin/nlbw/display"))
225         exec("/usr/sbin/nlbw", { "-c", "commit" })
226 end