* luci/app/statistics:
[project/luci.git] / applications / luci-statistics / luasrc / statistics / datatree.lua
1 --[[
2
3 Luci statistics - rrd data tree builder
4 (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
5
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
9
10                 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13
14 ]]--
15
16 module("luci.statistics.datatree", package.seeall)
17
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")
23
24
25 Instance = util.class()
26
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"
31
32         self._libdir  = self._libdir:gsub("/$","")
33         self._rrddir  = self._rrddir:gsub("/$","")
34         self._plugins = { }
35
36         self:_scan()
37 end
38
39 function Instance._mkpath( self, plugin, pinstance )
40         local dir = self._rrddir .. "/" .. self._host
41
42         if type(plugin) == "string" and plugin:len() > 0 then
43                 dir = dir .. "/" .. plugin
44
45                 if type(pinstance) == "string" and pinstance:len() > 0 then
46                         dir = dir .. "-" .. pinstance
47                 end
48         end
49
50         return dir
51 end
52
53 function Instance._notzero( self, table )
54         for k in pairs(table) do
55                 return true
56         end
57
58         return false
59 end
60
61 function Instance._scan( self )
62         local dirs = fs.dir( self:_mkpath() )
63         if not dirs then
64                 return
65         end
66
67 --      for i, plugin in ipairs( dirs ) do
68 --              if plugin:match("%w+.so") then
69 --                      self._plugins[ plugin:gsub("%.so$", "") ] = { }
70 --              end
71 --      end
72
73         for _, dir in ipairs(dirs) do
74                 if dir ~= "." and dir ~= ".." and
75                    fs.stat(self:_mkpath(dir)).type == "directory"
76                 then
77                         local plugin = dir:gsub("%-.+$", "")
78                         if not self._plugins[plugin] then
79                                 self._plugins[plugin] = { }
80                         end
81                 end
82         end
83
84         for plugin, instances in pairs( self._plugins ) do
85
86                 local dirs = fs.dir( self:_mkpath() )
87
88                 if type(dirs) == "table" then
89                         for i, dir in ipairs(dirs) do
90                                 if dir:find( plugin .. "%-" ) or dir == plugin then
91                                         local instance = ""
92
93                                         if dir ~= plugin then
94                                                 instance = dir:gsub( plugin .. "%-", "", 1 )
95                                         end
96
97                                         instances[instance] = { }
98                                 end
99                         end
100                 end
101
102                 for instance, data_instances in pairs( instances ) do
103
104                         dirs = fs.dir( self:_mkpath( plugin, instance ) )
105
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","")
110
111                                                 local data_type
112                                                 local data_instance
113
114                                                 if file:find("%-") then
115                                                         data_type     = file:gsub( "%-.+","" )
116                                                         data_instance = file:gsub( "[^%-]-%-", "", 1 )
117                                                 else
118                                                         data_type     = file
119                                                         data_instance = ""
120                                                 end
121
122                                                 if not data_instances[data_type] then
123                                                         data_instances[data_type] = { data_instance }
124                                                 else
125                                                         table.insert( data_instances[data_type], data_instance )
126                                                 end
127                                         end
128                                 end
129                         end
130                 end
131         end
132 end
133
134
135 function Instance.plugins( self )
136         local rv = { }
137
138         for plugin, val in pairs( self._plugins ) do
139                 if self:_notzero( val ) then
140                         table.insert( rv, plugin )
141                 end
142         end
143
144         return rv
145 end
146
147 function Instance.plugin_instances( self, plugin )
148         local rv = { }
149
150         for instance, val in pairs( self._plugins[plugin] ) do
151                 table.insert( rv, instance )
152         end
153
154         return rv
155 end
156
157 function Instance.data_types( self, plugin, instance )
158         local rv = { }
159         local p  = self._plugins[plugin]
160
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 )
164                 end
165         end
166
167         return rv
168 end
169
170 function Instance.data_instances( self, plugin, instance, dtype )
171         local rv = { }
172         local p  = self._plugins[plugin]
173
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 )
177                 end
178         end
179
180         return rv
181 end