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").Session()
22 local sections = uci:sections( "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 dir = fs.dir( self._libdir )
66 for i, plugin in ipairs( dir ) do
67 if plugin:match("%w+.so") then
68 self._plugins[ plugin:gsub(".so", "") ] = { }
72 for plugin, instances in pairs( self._plugins ) do
74 local dirs = fs.dir( self:_mkpath() )
76 if type(dirs) == "table" then
77 for i, dir in ipairs(dirs) do
78 if dir:find( plugin .. "%-" ) or dir == plugin then
82 instance = dir:gsub( plugin .. "%-", "", 1 )
85 instances[instance] = { }
90 for instance, data_instances in pairs( instances ) do
92 dirs = fs.dir( self:_mkpath( plugin, instance ) )
94 if type(dirs) == "table" then
95 for i, file in ipairs(dirs) do
96 if file:find("%.rrd") then
97 file = file:gsub("%.rrd","")
102 if file:find("%-") then
103 data_type = file:gsub( "%-.+","" )
104 data_instance = file:gsub( "[^%-]-%-", "", 1 )
110 if not data_instances[data_type] then
111 data_instances[data_type] = { data_instance }
113 table.insert( data_instances[data_type], data_instance )
123 function Instance.plugins( self )
126 for plugin, val in pairs( self._plugins ) do
127 if self:_notzero( val ) then
128 table.insert( rv, plugin )
135 function Instance.plugin_instances( self, plugin )
138 for instance, val in pairs( self._plugins[plugin] ) do
139 table.insert( rv, instance )
145 function Instance.data_types( self, plugin, instance )
147 local p = self._plugins[plugin]
149 if type(p) == "table" and type(p[instance]) == "table" then
150 for type, val in pairs(p[instance]) do
151 table.insert( rv, type )
158 function Instance.data_instances( self, plugin, instance, dtype )
160 local p = self._plugins[plugin]
162 if type(p) == "table" and type(p[instance]) == "table" and type(p[instance][dtype]) == "table" then
163 for i, instance in ipairs(p[instance][dtype]) do
164 table.insert( rv, instance )