1
0

datatree.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. -- Copyright 2008 Freifunk Leipzig / Jo-Philipp Wich <jow@openwrt.org>
  2. -- Licensed to the public under the Apache License 2.0.
  3. module("luci.statistics.datatree", package.seeall)
  4. local util = require("luci.util")
  5. local sys = require("luci.sys")
  6. local fs = require("nixio.fs")
  7. local uci = require("luci.model.uci").cursor()
  8. local sections = uci:get_all("luci_statistics")
  9. Instance = util.class()
  10. function Instance.__init__( self, host )
  11. self._host = host or sys.hostname()
  12. self._libdir = "/usr/lib/collectd"
  13. self._rrddir = "/tmp/rrd"
  14. if sections and sections.collectd then
  15. self._host = host or sections.collectd.Hostname or sys.hostname()
  16. self._libdir = sections.collectd.PluginDir or "/usr/lib/collectd"
  17. end
  18. if sections and sections.collectd_rrdtool then
  19. self._rrddir = sections.collectd_rrdtool.DataDir or "/tmp/rrd"
  20. end
  21. self._libdir = self._libdir:gsub("/$","")
  22. self._rrddir = self._rrddir:gsub("/$","")
  23. self._plugins = { }
  24. self:_scan()
  25. end
  26. function Instance._mkpath( self, plugin, pinstance )
  27. local dir = self._rrddir .. "/" .. self._host
  28. if type(plugin) == "string" and plugin:len() > 0 then
  29. dir = dir .. "/" .. plugin
  30. if type(pinstance) == "string" and pinstance:len() > 0 then
  31. dir = dir .. "-" .. pinstance
  32. end
  33. end
  34. return dir
  35. end
  36. function Instance._ls( self, ... )
  37. local ditr = fs.dir(self:_mkpath(...))
  38. if ditr then
  39. local dirs = { }
  40. while true do
  41. local d = ditr()
  42. if not d then break end
  43. dirs[#dirs+1] = d
  44. end
  45. return dirs
  46. end
  47. end
  48. function Instance._notzero( self, table )
  49. for k in pairs(table) do
  50. return true
  51. end
  52. return false
  53. end
  54. function Instance._scan( self )
  55. local dirs = self:_ls()
  56. if not dirs then
  57. return
  58. end
  59. -- for i, plugin in ipairs( dirs ) do
  60. -- if plugin:match("%w+.so") then
  61. -- self._plugins[ plugin:gsub("%.so$", "") ] = { }
  62. -- end
  63. -- end
  64. for _, dir in ipairs(dirs) do
  65. if dir ~= "." and dir ~= ".." and
  66. fs.stat(self:_mkpath(dir)).type == "dir"
  67. then
  68. local plugin = dir:gsub("%-.+$", "")
  69. if not self._plugins[plugin] then
  70. self._plugins[plugin] = { }
  71. end
  72. end
  73. end
  74. for plugin, instances in pairs( self._plugins ) do
  75. local dirs = self:_ls()
  76. if type(dirs) == "table" then
  77. for i, dir in ipairs(dirs) do
  78. if dir:find( plugin .. "%-" ) or dir == plugin then
  79. local instance = ""
  80. if dir ~= plugin then
  81. instance = dir:gsub( plugin .. "%-", "", 1 )
  82. end
  83. instances[instance] = { }
  84. end
  85. end
  86. end
  87. for instance, data_instances in pairs( instances ) do
  88. dirs = self:_ls(plugin, instance)
  89. if type(dirs) == "table" then
  90. for i, file in ipairs(dirs) do
  91. if file:find("%.rrd") then
  92. file = file:gsub("%.rrd","")
  93. local data_type
  94. local data_instance
  95. if file:find("%-") then
  96. data_type = file:gsub( "%-.+","" )
  97. data_instance = file:gsub( "[^%-]-%-", "", 1 )
  98. else
  99. data_type = file
  100. data_instance = ""
  101. end
  102. if not data_instances[data_type] then
  103. data_instances[data_type] = { data_instance }
  104. else
  105. table.insert( data_instances[data_type], data_instance )
  106. end
  107. end
  108. end
  109. end
  110. end
  111. end
  112. end
  113. function Instance.plugins( self )
  114. local rv = { }
  115. for plugin, val in pairs( self._plugins ) do
  116. if self:_notzero( val ) then
  117. table.insert( rv, plugin )
  118. end
  119. end
  120. return rv
  121. end
  122. function Instance.plugin_instances( self, plugin )
  123. local rv = { }
  124. for instance, val in pairs( self._plugins[plugin] ) do
  125. table.insert( rv, instance )
  126. end
  127. return rv
  128. end
  129. function Instance.data_types( self, plugin, instance )
  130. local rv = { }
  131. local p = self._plugins[plugin]
  132. if type(p) == "table" and type(p[instance]) == "table" then
  133. for type, val in pairs(p[instance]) do
  134. table.insert( rv, type )
  135. end
  136. end
  137. return rv
  138. end
  139. function Instance.data_instances( self, plugin, instance, dtype )
  140. local rv = { }
  141. local p = self._plugins[plugin]
  142. if type(p) == "table" and type(p[instance]) == "table" and type(p[instance][dtype]) == "table" then
  143. for i, instance in ipairs(p[instance][dtype]) do
  144. table.insert( rv, instance )
  145. end
  146. end
  147. return rv
  148. end
  149. function Instance.host_instances( self )
  150. local hosts_path = fs.glob(self._rrddir..'/*')
  151. local hosts = { }
  152. if hosts_path then
  153. local path
  154. for path in hosts_path do
  155. hosts[#hosts+1] = fs.basename(path)
  156. end
  157. end
  158. return hosts
  159. end