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("luci.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._notzero( self, table )
54 for k in pairs(table) do
61 function Instance._scan( self )
62 local dirs = fs.dir( self:_mkpath() )
67 -- for i, plugin in ipairs( dirs ) do
68 -- if plugin:match("%w+.so") then
69 -- self._plugins[ plugin:gsub("%.so$", "") ] = { }
73 for _, dir in ipairs(dirs) do
74 if dir ~= "." and dir ~= ".." and
75 fs.stat(self:_mkpath(dir)).type == "directory"
77 local plugin = dir:gsub("%-.+$", "")
78 if not self._plugins[plugin] then
79 self._plugins[plugin] = { }
84 for plugin, instances in pairs( self._plugins ) do
86 local dirs = fs.dir( self:_mkpath() )
88 if type(dirs) == "table" then
89 for i, dir in ipairs(dirs) do
90 if dir:find( plugin .. "%-" ) or dir == plugin then
94 instance = dir:gsub( plugin .. "%-", "", 1 )
97 instances[instance] = { }
102 for instance, data_instances in pairs( instances ) do
104 dirs = fs.dir( self:_mkpath( plugin, instance ) )
106 if type(dirs) == "table" then
107 for i, file in ipairs(dirs) do
108 if file:find("%.rrd") then
109 file = file:gsub("%.rrd","")
114 if file:find("%-") then
115 data_type = file:gsub( "%-.+","" )
116 data_instance = file:gsub( "[^%-]-%-", "", 1 )
122 if not data_instances[data_type] then
123 data_instances[data_type] = { data_instance }
125 table.insert( data_instances[data_type], data_instance )
135 function Instance.plugins( self )
138 for plugin, val in pairs( self._plugins ) do
139 if self:_notzero( val ) then
140 table.insert( rv, plugin )
147 function Instance.plugin_instances( self, plugin )
150 for instance, val in pairs( self._plugins[plugin] ) do
151 table.insert( rv, instance )
157 function Instance.data_types( self, plugin, instance )
159 local p = self._plugins[plugin]
161 if type(p) == "table" and type(p[instance]) == "table" then
162 for type, val in pairs(p[instance]) do
163 table.insert( rv, type )
170 function Instance.data_instances( self, plugin, instance, dtype )
172 local p = self._plugins[plugin]
174 if type(p) == "table" and type(p[instance]) == "table" and type(p[instance][dtype]) == "table" then
175 for i, instance in ipairs(p[instance][dtype]) do
176 table.insert( rv, instance )