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 sections.collectd.Hostname or sys.hostname()
17 self._libdir = sections.collectd.PluginDir or "/usr/lib/collectd"
18 self._rrddir = sections.collectd_rrdtool.DataDir or "/tmp/rrd"
20 self._libdir = self._libdir:gsub("/$","")
21 self._rrddir = self._rrddir:gsub("/$","")
27 function Instance._mkpath( self, plugin, pinstance )
28 local dir = self._rrddir .. "/" .. self._host
30 if type(plugin) == "string" and plugin:len() > 0 then
31 dir = dir .. "/" .. plugin
33 if type(pinstance) == "string" and pinstance:len() > 0 then
34 dir = dir .. "-" .. pinstance
41 function Instance._ls( self, ... )
42 local ditr = fs.dir(self:_mkpath(...))
47 if not d then break end
54 function Instance._notzero( self, table )
55 for k in pairs(table) do
62 function Instance._scan( self )
63 local dirs = self:_ls()
68 -- for i, plugin in ipairs( dirs ) do
69 -- if plugin:match("%w+.so") then
70 -- self._plugins[ plugin:gsub("%.so$", "") ] = { }
74 for _, dir in ipairs(dirs) do
75 if dir ~= "." and dir ~= ".." and
76 fs.stat(self:_mkpath(dir)).type == "dir"
78 local plugin = dir:gsub("%-.+$", "")
79 if not self._plugins[plugin] then
80 self._plugins[plugin] = { }
85 for plugin, instances in pairs( self._plugins ) do
87 local dirs = self:_ls()
89 if type(dirs) == "table" then
90 for i, dir in ipairs(dirs) do
91 if dir:find( plugin .. "%-" ) or dir == plugin then
95 instance = dir:gsub( plugin .. "%-", "", 1 )
98 instances[instance] = { }
103 for instance, data_instances in pairs( instances ) do
105 dirs = self:_ls(plugin, instance)
107 if type(dirs) == "table" then
108 for i, file in ipairs(dirs) do
109 if file:find("%.rrd") then
110 file = file:gsub("%.rrd","")
115 if file:find("%-") then
116 data_type = file:gsub( "%-.+","" )
117 data_instance = file:gsub( "[^%-]-%-", "", 1 )
123 if not data_instances[data_type] then
124 data_instances[data_type] = { data_instance }
126 table.insert( data_instances[data_type], data_instance )
136 function Instance.plugins( self )
139 for plugin, val in pairs( self._plugins ) do
140 if self:_notzero( val ) then
141 table.insert( rv, plugin )
148 function Instance.plugin_instances( self, plugin )
151 for instance, val in pairs( self._plugins[plugin] ) do
152 table.insert( rv, instance )
158 function Instance.data_types( self, plugin, instance )
160 local p = self._plugins[plugin]
162 if type(p) == "table" and type(p[instance]) == "table" then
163 for type, val in pairs(p[instance]) do
164 table.insert( rv, type )
171 function Instance.data_instances( self, plugin, instance, dtype )
173 local p = self._plugins[plugin]
175 if type(p) == "table" and type(p[instance]) == "table" and type(p[instance][dtype]) == "table" then
176 for i, instance in ipairs(p[instance][dtype]) do
177 table.insert( rv, instance )
184 function Instance.host_instances( self )
185 local hosts_path = fs.glob(self._rrddir..'/*')
190 for path in hosts_path do
191 hosts[#hosts+1] = fs.basename(path)