1 -- Copyright 2008 Freifunk Leipzig / Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
4 module("luci.statistics.datatree", package.seeall)
6 local util = require("luci.util")
7 local sys = require("luci.sys")
8 local fs = require("nixio.fs")
9 local uci = require("luci.model.uci").cursor()
10 local sections = uci:get_all("luci_statistics")
13 Instance = util.class()
15 function Instance.__init__( self, host )
16 self._host = host or sys.hostname()
17 self._libdir = "/usr/lib/collectd"
18 self._rrddir = "/tmp/rrd"
20 if sections and sections.collectd then
21 self._host = host or sections.collectd.Hostname or sys.hostname()
22 self._libdir = sections.collectd.PluginDir or "/usr/lib/collectd"
24 if sections and sections.collectd_rrdtool then
25 self._rrddir = sections.collectd_rrdtool.DataDir or "/tmp/rrd"
28 self._libdir = self._libdir:gsub("/$","")
29 self._rrddir = self._rrddir:gsub("/$","")
35 function Instance._mkpath( self, plugin, pinstance )
36 local dir = self._rrddir .. "/" .. self._host
38 if type(plugin) == "string" and plugin:len() > 0 then
39 dir = dir .. "/" .. plugin
41 if type(pinstance) == "string" and pinstance:len() > 0 then
42 dir = dir .. "-" .. pinstance
49 function Instance._ls( self, ... )
50 local ditr = fs.dir(self:_mkpath(...))
55 if not d then break end
62 function Instance._notzero( self, table )
63 for k in pairs(table) do
70 function Instance._scan( self )
71 local dirs = self:_ls()
76 -- for i, plugin in ipairs( dirs ) do
77 -- if plugin:match("%w+.so") then
78 -- self._plugins[ plugin:gsub("%.so$", "") ] = { }
82 for _, dir in ipairs(dirs) do
83 if dir ~= "." and dir ~= ".." and
84 fs.stat(self:_mkpath(dir)).type == "dir"
86 local plugin = dir:gsub("%-.+$", "")
87 if not self._plugins[plugin] then
88 self._plugins[plugin] = { }
93 for plugin, instances in pairs( self._plugins ) do
95 local dirs = self:_ls()
97 if type(dirs) == "table" then
98 for i, dir in ipairs(dirs) do
99 if dir:find( plugin .. "%-" ) or dir == plugin then
102 if dir ~= plugin then
103 instance = dir:gsub( plugin .. "%-", "", 1 )
106 instances[instance] = { }
111 for instance, data_instances in pairs( instances ) do
113 dirs = self:_ls(plugin, instance)
115 if type(dirs) == "table" then
116 for i, file in ipairs(dirs) do
117 if file:find("%.rrd") then
118 file = file:gsub("%.rrd","")
123 if file:find("%-") then
124 data_type = file:gsub( "%-.+","" )
125 data_instance = file:gsub( "[^%-]-%-", "", 1 )
131 if not data_instances[data_type] then
132 data_instances[data_type] = { data_instance }
134 table.insert( data_instances[data_type], data_instance )
144 function Instance.plugins( self )
147 for plugin, val in pairs( self._plugins ) do
148 if self:_notzero( val ) then
149 table.insert( rv, plugin )
156 function Instance.plugin_instances( self, plugin )
159 for instance, val in pairs( self._plugins[plugin] ) do
160 table.insert( rv, instance )
166 function Instance.data_types( self, plugin, instance )
168 local p = self._plugins[plugin]
170 if type(p) == "table" and type(p[instance]) == "table" then
171 for type, val in pairs(p[instance]) do
172 table.insert( rv, type )
179 function Instance.data_instances( self, plugin, instance, dtype )
181 local p = self._plugins[plugin]
183 if type(p) == "table" and type(p[instance]) == "table" and type(p[instance][dtype]) == "table" then
184 for i, instance in ipairs(p[instance][dtype]) do
185 table.insert( rv, instance )
192 function Instance.host_instances( self )
193 local hosts_path = fs.glob(self._rrddir..'/*')
198 for path in hosts_path do
199 hosts[#hosts+1] = fs.basename(path)