1
0

rrdtool.lua 16 KB

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