applications/luci-statistics: make host selectable if collectd-mod-network server...
[project/luci.git] / applications / luci-statistics / luasrc / controller / luci_statistics / luci_statistics.lua
index a49c404..2e25154 100644 (file)
@@ -21,9 +21,6 @@ function index()
        require("luci.util")
        require("luci.statistics.datatree")
 
-       -- get rrd data tree
-       local tree = luci.statistics.datatree.Instance()
-
        -- override entry(): check for existance <plugin>.so where <plugin> is derived from the called path
        function _entry( path, ... )
                local file = path[5] or path[4]
@@ -69,7 +66,7 @@ function index()
        }
 
        -- create toplevel menu nodes
-       local st = entry({"admin", "statistics"}, call("statistics_index"), _("Statistics"), 80)
+       local st = entry({"admin", "statistics"}, template("admin_statistics/index"), _("Statistics"), 80)
        st.i18n = "statistics"
        st.index = true
        
@@ -99,13 +96,17 @@ function index()
        end
 
        -- output views
-       local page = entry( { "admin", "statistics", "graph" }, call("statistics_index"), _("Graphs"), 80)
+       local page = entry( { "admin", "statistics", "graph" }, template("admin_statistics/index"), _("Graphs"), 80)
              page.i18n     = "statistics"
              page.setuser  = "nobody"
              page.setgroup = "nogroup"
 
        local vars = luci.http.formvalue(nil, true)
        local span = vars.timespan or nil
+       local host = vars.host or nil
+
+       -- get rrd data tree
+       local tree = luci.statistics.datatree.Instance(host)
 
        for i, plugin in luci.util.vspairs( tree:plugins() ) do
 
@@ -115,8 +116,8 @@ function index()
                -- plugin menu entry
                entry(
                        { "admin", "statistics", "graph", plugin },
-                       template("admin_statistics/index"), labels[plugin], i
-               ).query = { timespan = span }
+                       call("statistics_render"), labels[plugin], i
+               ).query = { timespan = span , host = host }
 
                -- if more then one instance is found then generate submenu
                if #instances > 1 then
@@ -125,7 +126,7 @@ function index()
                                entry(
                                        { "admin", "statistics", "graph", plugin, inst },
                                        call("statistics_render"), inst, j
-                               ).query = { timespan = span }
+                               ).query = { timespan = span , host = host }
                        end
                end
        end
@@ -143,7 +144,10 @@ function statistics_render()
        local uci   = luci.model.uci.cursor()
        local spans = luci.util.split( uci:get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
        local span  = vars.timespan or uci:get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
-       local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ) )
+       local host  = vars.host     or uci:get( "luci_statistics", "collectd", "Hostname" ) or luci.sys.hostname()
+       local opts = { host = vars.host }
+       local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ), opts )
+       local hosts = graph.tree:host_instances()
 
        local is_index = false
 
@@ -196,6 +200,8 @@ function statistics_render()
                plugin           = plugin,
                timespans        = spans,
                current_timespan = span,
+               hosts            = hosts,
+               current_host     = host,
                is_index         = is_index
        } )
 end