summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-statistics/luasrc/statistics/datatree.lua
diff options
context:
space:
mode:
authorSteven Barth <steven@midlink.org>2008-05-27 12:23:39 +0000
committerSteven Barth <steven@midlink.org>2008-05-27 12:23:39 +0000
commitd35a620e9f5665a94967f4bd02c93581a1dd7e00 (patch)
treecbe7a9b9fe71e40d850c649361c87608d56ea48b /applications/luci-statistics/luasrc/statistics/datatree.lua
parent5df565faba5eb0c26a7ce0e2d6e9092bf71cb1e8 (diff)
commit 4f6198094cf4134179d1f9c9fa8f79759a27c87e
Author: Felix Fietkau <nbd@openwrt.org> Date: Tue May 27 13:56:12 2008 +0200 rename src/ to luasrc/
Diffstat (limited to 'applications/luci-statistics/luasrc/statistics/datatree.lua')
-rw-r--r--applications/luci-statistics/luasrc/statistics/datatree.lua134
1 files changed, 134 insertions, 0 deletions
diff --git a/applications/luci-statistics/luasrc/statistics/datatree.lua b/applications/luci-statistics/luasrc/statistics/datatree.lua
new file mode 100644
index 000000000..957b230a9
--- /dev/null
+++ b/applications/luci-statistics/luasrc/statistics/datatree.lua
@@ -0,0 +1,134 @@
+module("luci.statistics.datatree", package.seeall)
+
+local util = require("luci.util")
+local sys = require("luci.sys")
+local fs = require("luci.fs")
+local uci = require("luci.model.uci").Session()
+local sections, names = uci:sections( "luci_statistics" )
+
+
+Instance = util.class()
+
+function Instance.__init__( self, host )
+ self._host = host or sections.collectd.Hostname or sys.hostname()
+ self._libdir = sections.collectd.PluginDir or "/usr/lib/collectd"
+ self._rrddir = sections.collectd_rrdtool.DataDir or "/tmp"
+
+ self._libdir = self._libdir:gsub("/$","")
+ self._rrddir = self._rrddir:gsub("/$","")
+ self._plugins = { }
+
+ self:_scan()
+end
+
+function Instance._mkpath( self, plugin, pinstance )
+ local dir = self._rrddir .. "/" .. self._host
+
+ if type(plugin) == "string" and plugin:len() > 0 then
+ dir = dir .. "/" .. plugin
+
+ if type(pinstance) == "string" and pinstance:len() > 0 then
+ dir = dir .. "-" .. pinstance
+ end
+ end
+
+ return dir
+end
+
+function Instance._notzero( self, table )
+ for k in pairs(table) do
+ return true
+ end
+
+ return false
+end
+
+function Instance._scan( self )
+ for i, plugin in ipairs( fs.dir( self._libdir ) ) do
+ if plugin:match("%w+.so") then
+ self._plugins[ plugin:gsub(".so", "") ] = { }
+ end
+ end
+
+ for plugin, instances in pairs( self._plugins ) do
+ for i, dir in ipairs( fs.dir( self:_mkpath() ) ) do
+ if dir:find( plugin .. "%-" ) or dir == plugin then
+ local instance = ""
+
+ if dir ~= plugin then
+ instance = dir:gsub( plugin .. "%-", "", 1 )
+ end
+
+ instances[instance] = { }
+ end
+ end
+
+ for instance, data_instances in pairs( instances ) do
+ for i, file in ipairs( fs.dir( self:_mkpath( plugin, instance ) ) ) do
+ if file:find("%.rrd") then
+ file = file:gsub("%.rrd","")
+
+ local data_type
+ local data_instance
+
+ if file:find("%-") then
+ data_type = file:gsub( "%-.+","" )
+ data_instance = file:gsub( "[^%-]-%-", "", 1 )
+ else
+ data_type = file
+ data_instance = ""
+ end
+
+ if not data_instances[data_type] then
+ data_instances[data_type] = { data_instance }
+ else
+ table.insert( data_instances[data_type], data_instance )
+ end
+ end
+ end
+ end
+ end
+end
+
+
+function Instance.plugins( self )
+ local rv = { }
+
+ for plugin, val in pairs( self._plugins ) do
+ if self:_notzero( val ) then
+ table.insert( rv, plugin )
+ end
+ end
+
+ return rv
+end
+
+function Instance.plugin_instances( self, plugin )
+ local rv = { }
+
+ for instance, val in pairs( self._plugins[plugin] ) do
+ table.insert( rv, instance )
+ end
+
+ return rv
+end
+
+function Instance.data_types( self, plugin, instance )
+ local rv = { }
+
+ for type, val in pairs( self._plugins[plugin][instance] ) do
+ table.insert( rv, type )
+ end
+
+ return rv
+end
+
+function Instance.data_instances( self, plugin, instance, type )
+ local rv = { }
+
+ for i, instance in ipairs( self._plugins[plugin][instance][type] ) do
+ table.insert( rv, instance )
+ end
+
+ return rv
+end