datatree.lua 4.1 KB

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