Method: Focuslight::RRD#graph

Defined in:
lib/focuslight/rrd.rb

#graph(datas, args) ⇒ Object



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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/focuslight/rrd.rb', line 153

def graph(datas, args)
  datas = [datas] unless datas.is_a?(Array)
  span = args.fetch(:t, :d)
  from = args[:from]
  to = args[:to]
  width = args.fetch(:width, 390)
  height = args.fetch(:height, 110)

  period_title, period, period_end, xgrid = calc_period(span, from, to)

  tmpfile = Tempfile.new(["", ".png"]) # [basename_prefix, suffix]
  rrdoptions = [
    tmpfile.path,
    '-w', width,
    '-h', height,
    '-a', 'PNG',
    '-l', 0, #minimum
    '-u', 2, #maximum
    '-x', (args[:xgrid].empty? ? xgrid : args[:xgrid]),
    '-s', period,
    '-e', period_end,
    '--slope-mode',
    '--disable-rrdtool-tag',
    '--color', 'BACK#' + args[:background_color].to_s.upcase,
    '--color', 'CANVAS#' + args[:canvas_color].to_s.upcase,
    '--color', 'FONT#' + args[:font_color].to_s.upcase,
    '--color', 'FRAME#' + args[:frame_color].to_s.upcase,
    '--color', 'AXIS#' + args[:axis_color].to_s.upcase,
    '--color', 'SHADEA#' + args[:shadea_color].to_s.upcase,
    '--color', 'SHADEB#' + args[:shadeb_color].to_s.upcase,
    '--border', args[:border].to_s.upcase
  ]
  rrdoptions.push('-y', args[:ygrid]) unless args[:ygrid].empty?
  rrdoptions.push('-t', period_title.to_s.dup) unless args[:notitle]
  rrdoptions.push('-v', args[:vertical_label]) unless args[:vertical_label].empty?
  rrdoptions.push('--no-legend') unless args[:legend]
  rrdoptions.push('--only-graph') if args[:graphonly]
  rrdoptions.push('--logarithmic') if args[:logarithmic]

  rrdoptions.push('--font', "AXIS:8:")
  rrdoptions.push('--font', "LEGEND:8:")

  rrdoptions.push('-u', args[:upper_limit]) unless args[:upper_limit].empty?
  rrdoptions.push('-l', args[:lower_limit]) unless args[:lower_limit].empty?
  rrdoptions.push('-r') if args[:rigid]
  rrdoptions.push('--units-exponent', args[:units_exponent]) if args[:units_exponent]

  defs = []
  datas.each_with_index do |data, i|
    type  = data.c_type ? data.c_type : data.type
    gdata =  'num'
    llimit = data.llimit
    ulimit = data.ulimit
    stack = (data.stack && i > 0 ? ':STACK' : '')
    file = (span =~ /^s/ ? path(data, :short) : path(data, :long))
    unit = (data.unit || '').gsub('%', '%%')

    rrdoptions.push(
      'DEF:%s%dt=%s:%s:AVERAGE' % [gdata, i, file, gdata],
      'CDEF:%s%d=%s%dt,%s,%s,LIMIT,%d,%s' % [gdata, i, gdata, i, llimit, ulimit, data.adjustval, data.adjust],
      '%s:%s%d%s:%s %s' % [type, gdata, i, data.color, _escape(data.graph), stack],
      'GPRINT:%s%d:LAST:Cur\: %%4.1lf%%s%s' % [gdata, i, unit],
      'GPRINT:%s%d:AVERAGE:Avg\: %%4.1lf%%s%s' % [gdata, i, unit],
      'GPRINT:%s%d:MAX:Max\: %%4.1lf%%s%s' % [gdata, i, unit],
      'GPRINT:%s%d:MIN:Min\: %%4.1lf%%s%s\l' % [gdata, i, unit],
      'VDEF:%s%dcur=%s%d,LAST' % [gdata, i, gdata, i],
      'PRINT:%s%dcur:%%.8lf' % [gdata, i],
      'VDEF:%s%davg=%s%d,AVERAGE' % [gdata, i, gdata, i],
      'PRINT:%s%davg:%%.8lf' % [gdata, i],
      'VDEF:%s%dmax=%s%d,MAXIMUM' % [gdata, i, gdata, i],
      'PRINT:%s%dmax:%%.8lf' % [gdata, i],
      'VDEF:%s%dmin=%s%d,MINIMUM' % [gdata, i, gdata, i],
      'PRINT:%s%dmin:%%.8lf' % [gdata, i],
    )
    defs << ('%s%d' % [gdata, i])
  end

  if args[:sumup]
    sumup = [ defs.shift ]
    unit = datas.first.unit.gsub('%', '%%')
    defs.each do |d|
      sumup.push(d, '+')
    end
    rrdoptions.push(
      'CDEF:sumup=%s' % [ sumup.join(',') ],
      'LINE0:sumup#cccccc:total',
      'GPRINT:sumup:LAST:Cur\: %%4.1lf%%s%s' % [unit],
      'GPRINT:sumup:AVERAGE:Avg\: %%4.1lf%%s%s' % [unit],
      'GPRINT:sumup:MAX:Max\: %%4.1lf%%s%s' % [unit],
      'GPRINT:sumup:MIN:Min\: %%4.1lf%%s%s\l' % [unit],
      'VDEF:sumupcur=sumup,LAST',
      'PRINT:sumupcur:%.8lf',
      'VDEF:sumupavg=sumup,AVERAGE',
      'PRINT:sumupavg:%.8lf',
      'VDEF:sumupmax=sumup,MAXIMUM',
      'PRINT:sumupmax:%.8lf',
      'VDEF:sumupmin=sumup,MINIMUM',
      'PRINT:sumupmin:%.8lf',
    )
  end

  ret = RRD::Wrapper.graph(*rrdoptions.map(&:to_s))
  unless ret
    tmpfile.close!
    raise "RRDtool returns error to draw graph, error: #{RRD::Wrapper.error}"
  end

  # Cannot get last PRINT return value, set of [current,avg,max,min] of each data source
  # This makes 'summary' API not supported

  graph_img = IO.binread(tmpfile.path); # read as binary
  tmpfile.delete

  [
    "/var/folders/tl/xtb7dnc132nggd6hs83y58h40000gq/T/20140117-86285-1igjvvh.png",
    "-w", 390,
    "-h", 110,
    "-a", "PNG",
    "-l", 0,
    "-u", 2,
    "-x", "HOUR:1:HOUR:2:HOUR:2:0:%H",
    "-s", -118800,
    "-e", "now",
    "--slope-mode",
    "--disable-rrdtool-tag",
    "--color", "BACK#F3F3F3", "--color", "CANVAS#FFFFFF", "--color", "FONT#000000",
    "--color", "FRAME#000000", "--color", "AXIS#000000", "--color", "SHADEA#CFCFCF",
    "--color", "SHADEB#9E9E9E",
    "--border", "3",
    "-t", "Day (1min avg)",
    "--no-legend",
    "--font", "AXIS:8:",
    "--font", "LEGEND:8:",
    "DEF:num0t=./data/c4ca4238a0b923820dcc509a6f75849b.rrd:num:AVERAGE",
    "CDEF:num0=num0t,-1000000000.0,1.0e+15,LIMIT,1,*",
    "AREA:num0:one",
    "GPRINT:num0:LAST:Cur\\: %4.1lf%s",
    "GPRINT:num0:AVERAGE:Avg\\: %4.1lf%s",
    "GPRINT:num0:MAX:Max\\: %4.1lf%s",
    "GPRINT:num0:MIN:Min\\: %4.1lf%s\\l",
    "VDEF:num0cur=num0,LAST",
    "PRINT:num0cur:%.8lf",
    "VDEF:num0avg=num0,AVERAGE",
    "PRINT:num0avg:%.8lf",
    "VDEF:num0max=num0,MAXIMUM",
    "PRINT:num0max:%.8lf",
    "VDEF:num0min=num0,MINIMUM",
    "PRINT:num0min:%.8lf"
  ]

  graph_img
end