luci-app-statistics: convert plugin definitions to JSON
[project/luci.git] / applications / luci-app-statistics / luasrc / controller / luci_statistics / luci_statistics.lua
index 443f20b0f4bbaf6de1a7d765a2823fe3d8b5eb66..529cc2335ac9ca1c3e53e0eb6604093de99aae67 100644 (file)
@@ -9,8 +9,9 @@ function index()
        require("nixio.fs")
        require("luci.util")
        require("luci.statistics.datatree")
+       require("luci.jsonc")
 
-       -- override entry(): check for existance <plugin>.so where <plugin> is derived from the called path
+       -- override entry(): check for existence <plugin>.so where <plugin> is derived from the called path
        function _entry( path, ... )
                local file = path[5] or path[4]
                if nixio.fs.access( "/usr/lib/collectd/" .. file .. ".so" ) then
@@ -22,51 +23,25 @@ function index()
                s_output        = _("Output plugins"),
                s_general       = _("General plugins"),
                s_network       = _("Network plugins"),
-
-               conntrack       = _("Conntrack"),
-               contextswitch   = _("Context Switches"),
-               cpu                     = _("Processor"),
-               cpufreq         = _("CPU Frequency"),
-               csv                     = _("CSV Output"),
-               df                      = _("Disk Space Usage"),
-               disk            = _("Disk Usage"),
-               dns                     = _("DNS"),
-               email           = _("Email"),
-               entropy         = _("Entropy"),
-               exec            = _("Exec"),
-               interface       = _("Interfaces"),
-               iptables        = _("Firewall"),
-               irq                     = _("Interrupts"),
-               iwinfo          = _("Wireless"),
-               load            = _("System Load"),
-               memory          = _("Memory"),
-               netlink         = _("Netlink"),
-               network         = _("Network"),
-               nut                     = _("UPS"),
-               olsrd           = _("OLSRd"),
-               openvpn         = _("OpenVPN"),
-               ping            = _("Ping"),
-               processes       = _("Processes"),
-               rrdtool         = _("RRDTool"),
-               sensors     = _("Sensors"),
-               splash_leases = _("Splash Leases"),
-               tcpconns        = _("TCP Connections"),
-               thermal =       _("Thermal"),
-               unixsock        = _("UnixSock"),
-               uptime          = _("Uptime")
        }
 
        -- our collectd menu
        local collectd_menu = {
-               output  = { "csv", "network", "rrdtool", "unixsock" },
-               general = { "contextswitch", "cpu", "cpufreq", "df", "disk", "email",
-                       "entropy", "exec", "irq", "load", "memory",
-                       "nut", "processes", "sensors", "thermal", "uptime" },
-               network = { "conntrack", "dns", "interface", "iptables",
-                       "netlink", "olsrd", "openvpn", "ping",
-                       "splash_leases", "tcpconns", "iwinfo" }
+               output  = { },
+               general = { },
+               network = { }
        }
 
+       local plugin_dir = "/usr/share/luci/statistics/plugins/"
+       for filename in nixio.fs.dir(plugin_dir) do
+               local plugin_def = luci.jsonc.parse(nixio.fs.readfile(plugin_dir .. filename))
+               if type(plugin_def) == "table" then
+                       local name = filename:gsub("%.json", "")
+                       table.insert(collectd_menu[plugin_def.category], name)
+                       labels[name] = plugin_def.title
+               end
+       end
+
        -- create toplevel menu nodes
        local st = entry({"admin", "statistics"}, template("admin_statistics/index"), _("Statistics"), 80)
        st.index = true
@@ -113,11 +88,15 @@ function index()
                -- get plugin instances
                local instances = tree:plugin_instances( plugin )
 
-               -- plugin menu entry
-               entry(
-                       { "admin", "statistics", "graph", plugin },
-                       call("statistics_render"), labels[plugin], idx
-               ).query = { timespan = span , host = host }
+               -- load plugin menu entry from the description
+               local plugin_name = "luci.statistics.rrdtool.definitions." .. plugin
+               local stat, def = pcall( require, plugin_name )
+               if stat and def and type(def.item) == "function" then
+                       entry(
+                               { "admin", "statistics", "graph", plugin },
+                               call("statistics_render"), def.item(), idx
+                       ).query = { timespan = span , host = host }
+               end
 
                -- if more then one instance is found then generate submenu
                if #instances > 1 then
@@ -168,12 +147,12 @@ function statistics_render()
        local images = { }
 
        -- find requested plugin and instance
-    for i, p in ipairs( luci.dispatcher.context.path ) do
-        if luci.dispatcher.context.path[i] == "graph" then
-            plugin    = luci.dispatcher.context.path[i+1]
-            instances = { luci.dispatcher.context.path[i+2] }
-        end
-    end
+       for i, p in ipairs( luci.dispatcher.context.path ) do
+               if luci.dispatcher.context.path[i] == "graph" then
+                       plugin    = luci.dispatcher.context.path[i+1]
+                       instances = { luci.dispatcher.context.path[i+2] }
+               end
+       end
 
        -- no instance requested, find all instances
        if #instances == 0 then
@@ -187,7 +166,6 @@ function statistics_render()
                is_index = true
        end
 
-
        -- render graphs
        for i, inst in luci.util.vspairs( instances ) do
                for i, img in luci.util.vspairs( graph:render( plugin, inst, is_index ) ) do