summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-statistics/luasrc/statistics/datatree.lua
blob: d6de6fa2178b08a15d526d41db6f81f31a2bfcb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
--[[

Luci statistics - rrd data tree builder
(c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

		http://www.apache.org/licenses/LICENSE-2.0

$Id$

]]--

module("luci.statistics.datatree", package.seeall)

local util = require("luci.util")
local sys  = require("luci.sys")
local fs   = require("nixio.fs")
local uci  = require("luci.model.uci").cursor()
local sections = uci:get_all("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/rrd"

	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._ls( self, ... )
	local ditr = fs.dir(self:_mkpath(...))
	if ditr then
		local dirs = { }
		while true do
			local d = ditr()
			if not d then break end
			dirs[#dirs+1] = d
		end
		return dirs
	end
end

function Instance._notzero( self, table )
	for k in pairs(table) do
		return true
	end

	return false
end

function Instance._scan( self )
	local dirs = self:_ls()
	if not dirs then
		return
	end

--	for i, plugin in ipairs( dirs ) do
--		if plugin:match("%w+.so") then
--			self._plugins[ plugin:gsub("%.so$", "") ] = { }
--		end
--	end

	for _, dir in ipairs(dirs) do
		if dir ~= "." and dir ~= ".." and
		   fs.stat(self:_mkpath(dir)).type == "dir"
		then
			local plugin = dir:gsub("%-.+$", "")
			if not self._plugins[plugin] then
				self._plugins[plugin] = { }
			end
		end
	end

	for plugin, instances in pairs( self._plugins ) do

		local dirs = self:_ls()

		if type(dirs) == "table" then
			for i, dir in ipairs(dirs) 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
		end

		for instance, data_instances in pairs( instances ) do

			dirs = self:_ls(plugin, instance)

			if type(dirs) == "table" then
				for i, file in ipairs(dirs) 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
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 = { }
	local p  = self._plugins[plugin]

	if type(p) == "table" and type(p[instance]) == "table" then
		for type, val in pairs(p[instance]) do
			table.insert( rv, type )
		end
	end

	return rv
end

function Instance.data_instances( self, plugin, instance, dtype )
	local rv = { }
	local p  = self._plugins[plugin]

	if type(p) == "table" and type(p[instance]) == "table" and type(p[instance][dtype]) == "table" then
		for i, instance in ipairs(p[instance][dtype]) do
			table.insert( rv, instance )
		end
	end

	return rv
end

function Instance.host_instances( self )
	local hosts_path = fs.glob(self._rrddir..'/*')
	local hosts = { }
	k = 1
	for v in hosts_path do
		hosts[k] = fs.basename(v)
		k=k+1
	end

	return hosts
end