rrdtool.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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.rrdtool", package.seeall)
  4. require("luci.statistics.datatree")
  5. require("luci.statistics.rrdtool.colors")
  6. require("luci.statistics.i18n")
  7. require("luci.model.uci")
  8. require("luci.util")
  9. require("luci.sys")
  10. local fs = require "nixio.fs"
  11. Graph = luci.util.class()
  12. function Graph.__init__( self, timespan, opts )
  13. opts = opts or { }
  14. local uci = luci.model.uci.cursor()
  15. local sections = uci:get_all( "luci_statistics" )
  16. -- options
  17. opts.timespan = timespan or sections.rrdtool.default_timespan or 900
  18. opts.rrasingle = opts.rrasingle or ( sections.collectd_rrdtool.RRASingle == "1" )
  19. opts.host = opts.host or sections.collectd.Hostname or luci.sys.hostname()
  20. opts.width = opts.width or sections.rrdtool.image_width or 400
  21. opts.rrdpath = opts.rrdpath or sections.collectd_rrdtool.DataDir or "/tmp/rrd"
  22. opts.imgpath = opts.imgpath or sections.rrdtool.image_path or "/tmp/rrdimg"
  23. opts.rrdpath = opts.rrdpath:gsub("/$","")
  24. opts.imgpath = opts.imgpath:gsub("/$","")
  25. -- helper classes
  26. self.colors = luci.statistics.rrdtool.colors.Instance()
  27. self.tree = luci.statistics.datatree.Instance(opts.host)
  28. self.i18n = luci.statistics.i18n.Instance( self )
  29. -- rrdtool default args
  30. self.args = {
  31. "-a", "PNG",
  32. "-s", "NOW-" .. opts.timespan,
  33. "-w", opts.width
  34. }
  35. -- store options
  36. self.opts = opts
  37. end
  38. function Graph._mkpath( self, plugin, plugin_instance, dtype, dtype_instance )
  39. local t = self.opts.host .. "/" .. plugin
  40. if type(plugin_instance) == "string" and plugin_instance:len() > 0 then
  41. t = t .. "-" .. plugin_instance
  42. end
  43. t = t .. "/" .. dtype
  44. if type(dtype_instance) == "string" and dtype_instance:len() > 0 then
  45. t = t .. "-" .. dtype_instance
  46. end
  47. return t
  48. end
  49. function Graph.mkrrdpath( self, ... )
  50. return string.format( "%s/%s.rrd", self.opts.rrdpath, self:_mkpath( ... ) )
  51. end
  52. function Graph.mkpngpath( self, ... )
  53. return string.format( "%s/%s.%i.png", self.opts.imgpath, self:_mkpath( ... ), self.opts.timespan )
  54. end
  55. function Graph.strippngpath( self, path )
  56. return path:sub( self.opts.imgpath:len() + 2 )
  57. end
  58. function Graph._forcelol( self, list )
  59. if type(list[1]) ~= "table" then
  60. return( { list } )
  61. end
  62. return( list )
  63. end
  64. function Graph._rrdtool( self, def, rrd )
  65. -- prepare directory
  66. local dir = def[1]:gsub("/[^/]+$","")
  67. fs.mkdirr( dir )
  68. -- construct commandline
  69. local cmdline = "rrdtool graph"
  70. -- copy default arguments to def stack
  71. for i, opt in ipairs(self.args) do
  72. table.insert( def, 1 + i, opt )
  73. end
  74. -- construct commandline from def stack
  75. for i, opt in ipairs(def) do
  76. opt = opt .. "" -- force string
  77. if rrd then
  78. opt = opt:gsub( "{file}", rrd )
  79. end
  80. if opt:match("[^%w]") then
  81. cmdline = cmdline .. " '" .. opt .. "'"
  82. else
  83. cmdline = cmdline .. " " .. opt
  84. end
  85. end
  86. -- execute rrdtool
  87. local rrdtool = io.popen( cmdline )
  88. rrdtool:close()
  89. end
  90. function Graph._generic( self, opts, plugin, plugin_instance, dtype, index )
  91. -- generated graph defs
  92. local defs = { }
  93. -- internal state variables
  94. local _args = { }
  95. local _sources = { }
  96. local _stack_neg = { }
  97. local _stack_pos = { }
  98. local _longest_name = 0
  99. local _has_totals = false
  100. -- some convenient aliases
  101. local _ti = table.insert
  102. local _sf = string.format
  103. -- local helper: append a string.format() formatted string to given table
  104. function _tif( list, fmt, ... )
  105. table.insert( list, string.format( fmt, ... ) )
  106. end
  107. -- local helper: create definitions for min, max, avg and create *_nnl (not null) variable from avg
  108. function __def(source)
  109. local inst = source.sname
  110. local rrd = source.rrd
  111. local ds = source.ds
  112. if not ds or ds:len() == 0 then ds = "value" end
  113. _tif( _args, "DEF:%s_avg_raw=%s:%s:AVERAGE", inst, rrd, ds )
  114. _tif( _args, "CDEF:%s_avg=%s_avg_raw,%s", inst, inst, source.transform_rpn )
  115. if not self.opts.rrasingle then
  116. _tif( _args, "DEF:%s_min_raw=%s:%s:MIN", inst, rrd, ds )
  117. _tif( _args, "CDEF:%s_min=%s_min_raw,%s", inst, inst, source.transform_rpn )
  118. _tif( _args, "DEF:%s_max_raw=%s:%s:MAX", inst, rrd, ds )
  119. _tif( _args, "CDEF:%s_max=%s_max_raw,%s", inst, inst, source.transform_rpn )
  120. end
  121. _tif( _args, "CDEF:%s_nnl=%s_avg,UN,0,%s_avg,IF", inst, inst, inst )
  122. end
  123. -- local helper: create cdefs depending on source options like flip and overlay
  124. function __cdef(source)
  125. local prev
  126. -- find previous source, choose stack depending on flip state
  127. if source.flip then
  128. prev = _stack_neg[#_stack_neg]
  129. else
  130. prev = _stack_pos[#_stack_pos]
  131. end
  132. -- is first source in stack or overlay source: source_stk = source_nnl
  133. if not prev or source.overlay then
  134. -- create cdef statement for cumulative stack (no NaNs) and also
  135. -- for display (preserving NaN where no points should be displayed)
  136. _tif( _args, "CDEF:%s_stk=%s_nnl", source.sname, source.sname )
  137. _tif( _args, "CDEF:%s_plot=%s_avg", source.sname, source.sname )
  138. -- is subsequent source without overlay: source_stk = source_nnl + previous_stk
  139. else
  140. -- create cdef statement
  141. _tif( _args, "CDEF:%s_stk=%s_nnl,%s_stk,+", source.sname, source.sname, prev )
  142. _tif( _args, "CDEF:%s_plot=%s_avg,%s_stk,+", source.sname, source.sname, prev )
  143. end
  144. -- create multiply by minus one cdef if flip is enabled
  145. if source.flip then
  146. -- create cdef statement: source_stk = source_stk * -1
  147. _tif( _args, "CDEF:%s_neg=%s_plot,-1,*", source.sname, source.sname )
  148. -- push to negative stack if overlay is disabled
  149. if not source.overlay then
  150. _ti( _stack_neg, source.sname )
  151. end
  152. -- no flipping, push to positive stack if overlay is disabled
  153. elseif not source.overlay then
  154. -- push to positive stack
  155. _ti( _stack_pos, source.sname )
  156. end
  157. -- calculate total amount of data if requested
  158. if source.total then
  159. _tif( _args,
  160. "CDEF:%s_avg_sample=%s_avg,UN,0,%s_avg,IF,sample_len,*",
  161. source.sname, source.sname, source.sname
  162. )
  163. _tif( _args,
  164. "CDEF:%s_avg_sum=PREV,UN,0,PREV,IF,%s_avg_sample,+",
  165. source.sname, source.sname, source.sname
  166. )
  167. end
  168. end
  169. -- local helper: create cdefs required for calculating total values
  170. function __cdef_totals()
  171. if _has_totals then
  172. _tif( _args, "CDEF:mytime=%s_avg,TIME,TIME,IF", _sources[1].sname )
  173. _ti( _args, "CDEF:sample_len_raw=mytime,PREV(mytime),-" )
  174. _ti( _args, "CDEF:sample_len=sample_len_raw,UN,0,sample_len_raw,IF" )
  175. end
  176. end
  177. -- local helper: create line and area statements
  178. function __line(source)
  179. local line_color
  180. local area_color
  181. local legend
  182. local var
  183. -- find colors: try source, then opts.colors; fall back to random color
  184. if type(source.color) == "string" then
  185. line_color = source.color
  186. area_color = self.colors:from_string( line_color )
  187. elseif type(opts.colors[source.name:gsub("[^%w]","_")]) == "string" then
  188. line_color = opts.colors[source.name:gsub("[^%w]","_")]
  189. area_color = self.colors:from_string( line_color )
  190. else
  191. area_color = self.colors:random()
  192. line_color = self.colors:to_string( area_color )
  193. end
  194. -- derive area background color from line color
  195. area_color = self.colors:to_string( self.colors:faded( area_color ) )
  196. -- choose source_plot or source_neg variable depending on flip state
  197. if source.flip then
  198. var = "neg"
  199. else
  200. var = "plot"
  201. end
  202. -- create legend
  203. legend = _sf( "%-" .. _longest_name .. "s", source.title )
  204. -- create area if not disabled
  205. if not source.noarea then
  206. _tif( _args, "AREA:%s_%s#%s", source.sname, var, area_color )
  207. end
  208. -- create line1 statement
  209. _tif( _args, "LINE%d:%s_%s#%s:%s",
  210. source.noarea and 2 or 1,
  211. source.sname, var, line_color, legend )
  212. end
  213. -- local helper: create gprint statements
  214. function __gprint(source)
  215. local numfmt = opts.number_format or "%6.1lf"
  216. local totfmt = opts.totals_format or "%5.1lf%s"
  217. -- don't include MIN if rrasingle is enabled
  218. if not self.opts.rrasingle then
  219. _tif( _args, "GPRINT:%s_min:MIN:\tMin\\: %s", source.sname, numfmt )
  220. end
  221. -- always include AVERAGE
  222. _tif( _args, "GPRINT:%s_avg:AVERAGE:\tAvg\\: %s", source.sname, numfmt )
  223. -- don't include MAX if rrasingle is enabled
  224. if not self.opts.rrasingle then
  225. _tif( _args, "GPRINT:%s_max:MAX:\tMax\\: %s", source.sname, numfmt )
  226. end
  227. -- include total count if requested else include LAST
  228. if source.total then
  229. _tif( _args, "GPRINT:%s_avg_sum:LAST:(ca. %s Total)\\l", source.sname, totfmt )
  230. else
  231. _tif( _args, "GPRINT:%s_avg:LAST:\tLast\\: %s\\l", source.sname, numfmt )
  232. end
  233. end
  234. --
  235. -- find all data sources
  236. --
  237. -- find data types
  238. local data_types
  239. if dtype then
  240. data_types = { dtype }
  241. else
  242. data_types = opts.data.types or { }
  243. end
  244. if not ( dtype or opts.data.types ) then
  245. if opts.data.instances then
  246. for k, v in pairs(opts.data.instances) do
  247. _ti( data_types, k )
  248. end
  249. elseif opts.data.sources then
  250. for k, v in pairs(opts.data.sources) do
  251. _ti( data_types, k )
  252. end
  253. end
  254. end
  255. -- iterate over data types
  256. for i, dtype in ipairs(data_types) do
  257. -- find instances
  258. local data_instances
  259. if not opts.per_instance then
  260. if type(opts.data.instances) == "table" and type(opts.data.instances[dtype]) == "table" then
  261. data_instances = opts.data.instances[dtype]
  262. else
  263. data_instances = self.tree:data_instances( plugin, plugin_instance, dtype )
  264. end
  265. end
  266. if type(data_instances) ~= "table" or #data_instances == 0 then data_instances = { "" } end
  267. -- iterate over data instances
  268. for i, dinst in ipairs(data_instances) do
  269. -- construct combined data type / instance name
  270. local dname = dtype
  271. if dinst:len() > 0 then
  272. dname = dname .. "_" .. dinst
  273. end
  274. -- find sources
  275. local data_sources = { "value" }
  276. if type(opts.data.sources) == "table" then
  277. if type(opts.data.sources[dname]) == "table" then
  278. data_sources = opts.data.sources[dname]
  279. elseif type(opts.data.sources[dtype]) == "table" then
  280. data_sources = opts.data.sources[dtype]
  281. end
  282. end
  283. -- iterate over data sources
  284. for i, dsource in ipairs(data_sources) do
  285. local dsname = dtype .. "_" .. dinst:gsub("[^%w]","_") .. "_" .. dsource
  286. local altname = dtype .. "__" .. dsource
  287. --assert(dtype ~= "ping", dsname .. " or " .. altname)
  288. -- find datasource options
  289. local dopts = { }
  290. if type(opts.data.options) == "table" then
  291. if type(opts.data.options[dsname]) == "table" then
  292. dopts = opts.data.options[dsname]
  293. elseif type(opts.data.options[altname]) == "table" then
  294. dopts = opts.data.options[altname]
  295. elseif type(opts.data.options[dname]) == "table" then
  296. dopts = opts.data.options[dname]
  297. elseif type(opts.data.options[dtype]) == "table" then
  298. dopts = opts.data.options[dtype]
  299. end
  300. end
  301. -- store values
  302. _ti( _sources, {
  303. rrd = dopts.rrd or self:mkrrdpath( plugin, plugin_instance, dtype, dinst ),
  304. color = dopts.color or self.colors:to_string( self.colors:random() ),
  305. flip = dopts.flip or false,
  306. total = dopts.total or false,
  307. overlay = dopts.overlay or false,
  308. transform_rpn = dopts.transform_rpn or "0,+",
  309. noarea = dopts.noarea or false,
  310. title = dopts.title or nil,
  311. weight = dopts.weight or nil,
  312. ds = dsource,
  313. type = dtype,
  314. instance = dinst,
  315. index = #_sources + 1,
  316. sname = ( #_sources + 1 ) .. dtype
  317. } )
  318. -- generate datasource title
  319. _sources[#_sources].title = self.i18n:ds( _sources[#_sources] )
  320. -- find longest name ...
  321. if _sources[#_sources].title:len() > _longest_name then
  322. _longest_name = _sources[#_sources].title:len()
  323. end
  324. -- has totals?
  325. if _sources[#_sources].total then
  326. _has_totals = true
  327. end
  328. end
  329. end
  330. end
  331. --
  332. -- construct diagrams
  333. --
  334. -- if per_instance is enabled then find all instances from the first datasource in diagram
  335. -- if per_instance is disabled then use an empty pseudo instance and use model provided values
  336. local instances = { "" }
  337. if opts.per_instance then
  338. instances = self.tree:data_instances( plugin, plugin_instance, _sources[1].type )
  339. end
  340. -- iterate over instances
  341. for i, instance in ipairs(instances) do
  342. -- store title and vlabel
  343. _ti( _args, "-t" )
  344. _ti( _args, self.i18n:title( plugin, plugin_instance, _sources[1].type, instance, opts.title ) )
  345. _ti( _args, "-v" )
  346. _ti( _args, self.i18n:label( plugin, plugin_instance, _sources[1].type, instance, opts.vlabel ) )
  347. if opts.y_max then
  348. _ti ( _args, "-u" )
  349. _ti ( _args, opts.y_max )
  350. end
  351. if opts.y_min then
  352. _ti ( _args, "-l" )
  353. _ti ( _args, opts.y_min )
  354. end
  355. if opts.units_exponent then
  356. _ti ( _args, "-X" )
  357. _ti ( _args, opts.units_exponent )
  358. end
  359. if opts.alt_autoscale then
  360. _ti ( _args, "-A" )
  361. end
  362. if opts.alt_autoscale_max then
  363. _ti ( _args, "-M" )
  364. end
  365. -- store additional rrd options
  366. if opts.rrdopts then
  367. for i, o in ipairs(opts.rrdopts) do _ti( _args, o ) end
  368. end
  369. -- sort sources
  370. table.sort(_sources, function(a, b)
  371. local x = a.weight or a.index or 0
  372. local y = b.weight or b.index or 0
  373. return x < y
  374. end)
  375. -- create DEF statements for each instance
  376. for i, source in ipairs(_sources) do
  377. -- fixup properties for per instance mode...
  378. if opts.per_instance then
  379. source.instance = instance
  380. source.rrd = self:mkrrdpath( plugin, plugin_instance, source.type, instance )
  381. end
  382. __def( source )
  383. end
  384. -- create CDEF required for calculating totals
  385. __cdef_totals()
  386. -- create CDEF statements for each instance in reversed order
  387. for i, source in ipairs(_sources) do
  388. __cdef( _sources[1 + #_sources - i] )
  389. end
  390. -- create LINE1, AREA and GPRINT statements for each instance
  391. for i, source in ipairs(_sources) do
  392. __line( source )
  393. __gprint( source )
  394. end
  395. -- prepend image path to arg stack
  396. _ti( _args, 1, self:mkpngpath( plugin, plugin_instance, index .. instance ) )
  397. -- push arg stack to definition list
  398. _ti( defs, _args )
  399. -- reset stacks
  400. _args = { }
  401. _stack_pos = { }
  402. _stack_neg = { }
  403. end
  404. return defs
  405. end
  406. function Graph.render( self, plugin, plugin_instance, is_index )
  407. dtype_instances = dtype_instances or { "" }
  408. local pngs = { }
  409. -- check for a whole graph handler
  410. local plugin_def = "luci.statistics.rrdtool.definitions." .. plugin
  411. local stat, def = pcall( require, plugin_def )
  412. if stat and def and type(def.rrdargs) == "function" then
  413. -- temporary image matrix
  414. local _images = { }
  415. -- get diagram definitions
  416. for i, opts in ipairs( self:_forcelol( def.rrdargs( self, plugin, plugin_instance, nil, is_index ) ) ) do
  417. if not is_index or not opts.detail then
  418. _images[i] = { }
  419. -- get diagram definition instances
  420. local diagrams = self:_generic( opts, plugin, plugin_instance, nil, i )
  421. -- render all diagrams
  422. for j, def in ipairs( diagrams ) do
  423. -- remember image
  424. _images[i][j] = def[1]
  425. -- exec
  426. self:_rrdtool( def )
  427. end
  428. end
  429. end
  430. -- remember images - XXX: fixme (will cause probs with asymmetric data)
  431. for y = 1, #_images[1] do
  432. for x = 1, #_images do
  433. table.insert( pngs, _images[x][y] )
  434. end
  435. end
  436. end
  437. return pngs
  438. end