3 Luci statistics - rrd data tree builder
4 (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
16 module("luci.statistics.datatree", package.seeall)
18 local util = require("luci.util")
19 local sys = require("luci.sys")
20 local fs = require("nixio.fs")
21 local uci = require("luci.model.uci").cursor()
22 local sections = uci:get_all("luci_statistics")
25 Instance = util.class()
27 function Instance.__init__( self, host )
28 self._host = host or sections.collectd.Hostname or sys.hostname()
29 self._libdir = sections.collectd.PluginDir or "/usr/lib/collectd"
30 self._rrddir = sections.collectd_rrdtool.DataDir or "/tmp/rrd"
32 self._libdir = self._libdir:gsub("/$","")
33 self._rrddir = self._rrddir:gsub("/$","")
39 function Instance._mkpath( self, plugin, pinstance )
40 local dir = self._rrddir .. "/" .. self._host
42 if type(plugin) == "string" and plugin:len() > 0 then
43 dir = dir .. "/" .. plugin
45 if type(pinstance) == "string" and pinstance:len() > 0 then
46 dir = dir .. "-" .. pinstance
53 function Instance._ls( self, ... )
54 local ditr = fs.dir(self:_mkpath(...))
59 if not d then break end
66 function Instance._notzero( self, table )
67 for k in pairs(table) do
74 function Instance._scan( self )
75 local dirs = self:_ls()
80 -- for i, plugin in ipairs( dirs ) do
81 -- if plugin:match("%w+.so") then
82 -- self._plugins[ plugin:gsub("%.so$", "") ] = { }
86 for _, dir in ipairs(dirs) do
87 if dir ~= "." and dir ~= ".." and
88 fs.stat(self:_mkpath(dir)).type == "dir"
90 local plugin = dir:gsub("%-.+$", "")
91 if not self._plugins[plugin] then
92 self._plugins[plugin] = { }
97 for plugin, instances in pairs( self._plugins ) do
99 local dirs = self:_ls()
101 if type(dirs) == "table" then
102 for i, dir in ipairs(dirs) do
103 if dir:find( plugin .. "%-" ) or dir == plugin then
106 if dir ~= plugin then
107 instance = dir:gsub( plugin .. "%-", "", 1 )
110 instances[instance] = { }
115 for instance, data_instances in pairs( instances ) do
117 dirs = self:_ls(plugin, instance)
119 if type(dirs) == "table" then
120 for i, file in ipairs(dirs) do
121 if file:find("%.rrd") then
122 file = file:gsub("%.rrd","")
127 if file:find("%-") then
128 data_type = file:gsub( "%-.+","" )
129 data_instance = file:gsub( "[^%-]-%-", "", 1 )
135 if not data_instances[data_type] then
136 data_instances[data_type] = { data_instance }
138 table.insert( data_instances[data_type], data_instance )
148 function Instance.plugins( self )
151 for plugin, val in pairs( self._plugins ) do
152 if self:_notzero( val ) then
153 table.insert( rv, plugin )
160 function Instance.plugin_instances( self, plugin )
163 for instance, val in pairs( self._plugins[plugin] ) do
164 table.insert( rv, instance )
170 function Instance.data_types( self, plugin, instance )
172 local p = self._plugins[plugin]
174 if type(p) == "table" and type(p[instance]) == "table" then
175 for type, val in pairs(p[instance]) do
176 table.insert( rv, type )
183 function Instance.data_instances( self, plugin, instance, dtype )
185 local p = self._plugins[plugin]
187 if type(p) == "table" and type(p[instance]) == "table" and type(p[instance][dtype]) == "table" then
188 for i, instance in ipairs(p[instance][dtype]) do
189 table.insert( rv, instance )