summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-vnstat2/luasrc
diff options
context:
space:
mode:
authorJan Hoffmann <jan@3e8.eu>2020-01-02 23:20:37 +0100
committerJan Hoffmann <jan@3e8.eu>2020-01-23 12:44:22 +0100
commit1750433bc162bf923557a16f6ed7dfd1d3b90be4 (patch)
tree668c2fd83bbed07b13c2a59729d50730daabeb85 /applications/luci-app-vnstat2/luasrc
parentd844caf13787a66a5deac438d9f28746b79136ff (diff)
luci-app-vnstat2: add application
This adds an application for vnStat version 2. Signed-off-by: Jan Hoffmann <jan@3e8.eu>
Diffstat (limited to 'applications/luci-app-vnstat2/luasrc')
-rw-r--r--applications/luci-app-vnstat2/luasrc/controller/vnstat2.lua50
-rw-r--r--applications/luci-app-vnstat2/luasrc/view/vnstat2/graphs.htm55
2 files changed, 105 insertions, 0 deletions
diff --git a/applications/luci-app-vnstat2/luasrc/controller/vnstat2.lua b/applications/luci-app-vnstat2/luasrc/controller/vnstat2.lua
new file mode 100644
index 0000000000..139c1f4991
--- /dev/null
+++ b/applications/luci-app-vnstat2/luasrc/controller/vnstat2.lua
@@ -0,0 +1,50 @@
+module("luci.controller.vnstat2", package.seeall)
+
+function index()
+ if not nixio.fs.access("/etc/config/vnstat") then
+ return
+ end
+
+ entry({"admin", "status", "vnstat2"}, alias("admin", "status", "vnstat2", "graphs"), _("vnStat Traffic Monitor"), 90)
+ entry({"admin", "status", "vnstat2", "graphs"}, template("vnstat2/graphs"), _("Graphs"), 1)
+ entry({"admin", "status", "vnstat2", "config"}, view("vnstat2/config"), _("Configuration"), 2)
+ entry({"admin", "status", "vnstat2", "graph"}, call("action_graph"), nil, 3)
+end
+
+function action_graph()
+ local util = require "luci.util"
+
+ local param = luci.http.formvalue
+
+ local iface = param("iface")
+ local style = param("style")
+
+ if not iface or not style then
+ luci.http.status(404, "Not Found")
+ return
+ end
+
+ local style_valid = false
+ for _, v in ipairs({"s", "t", "5", "h", "d", "m", "y"}) do
+ if v == style then
+ style_valid = true
+ break
+ end
+ end
+
+ if not style_valid then
+ luci.http.status(404, "Not Found")
+ return
+ end
+
+ luci.http.prepare_content("image/png")
+
+ local cmd = "vnstati -i %s -%s -o -" % {
+ util.shellquote(iface),
+ util.shellquote(style)
+ }
+
+ local image = io.popen(cmd)
+ luci.http.write(image:read("*a"))
+ image:close()
+end
diff --git a/applications/luci-app-vnstat2/luasrc/view/vnstat2/graphs.htm b/applications/luci-app-vnstat2/luasrc/view/vnstat2/graphs.htm
new file mode 100644
index 0000000000..318611a9d6
--- /dev/null
+++ b/applications/luci-app-vnstat2/luasrc/view/vnstat2/graphs.htm
@@ -0,0 +1,55 @@
+<%#
+ This is free software, licensed under the Apache License, Version 2.0
+-%>
+
+<%-
+
+local util = require "luci.util"
+local json = require "luci.jsonc"
+
+
+local ifaces = {}
+
+local data = util.exec("vnstat --json f 1 2>/dev/null")
+local content = json.parse(data)
+if type(content) == "table" then
+ for _, iface in pairs(content["interfaces"]) do
+ table.insert(ifaces, iface["name"])
+ end
+end
+
+
+local function render_section(style, title)
+ %><div class="cbi-section" data-tab="<%=style%>" data-tab-title="<%=title%>"><%
+
+ for _, iface in ipairs(ifaces) do
+ %><p><img src="<%=url("admin/status/vnstat2/graph")%>?iface=<%=iface%>&amp;style=<%=style%>" alt="" style="max-width:100%" /></p><%
+ end
+
+ %></div><%
+end
+
+
+-%>
+
+<%+header%>
+
+<h2 name="content"><%:vnStat Graphs%></h2>
+
+<div>
+ <%
+ if #ifaces == 0 then
+ %><p><em><%:No monitored interfaces have been found. Go to the configuration to enable monitoring for one or more interfaces.%></em></p><%
+ else
+ render_section("s", translate("Summary"))
+ render_section("t", translate("Top"))
+ render_section("5", translate("5 Minute"))
+ render_section("h", translate("Hourly"))
+ render_section("d", translate("Daily"))
+ render_section("m", translate("Monthly"))
+ render_section("y", translate("Yearly"))
+ end
+ %>
+</div>
+
+<%+footer%>