Class: GRI::Graph

Inherits:
Object show all
Includes:
Utils
Defined in:
lib/gri/graph.rb

Constant Summary collapse

SIZE =
{'ss'=>[145, 80], 's'=>[400, 80], 'l'=>[640, 300],
'll'=>[860, 500], 'xl'=>[1200,700], 'gf'=>[387, 110]}
SCOLORS =
['3366ff', 'ff6666', '00cc00', 'ffccff', 'ff0000', 'ffff00',
'00ff00', '009900', '33ccff', '990000', 'cc99ff', '330099', 'ff33ff',
'33ffff', 'ffcc33', '33cc99', 'cccccc', '9999ff', '000000', 'eeee66',
'3f3fff', '3faf3f', 'ff3f3f', 'ffaf3f', '990033', '003399', '3fff3f',
'ffff3f', '3f667f', '00cc00', 'ff9966', '3366cc', '666666', '99ff99']

Instance Method Summary collapse

Methods included from Utils

get_prop, key_encode, load_records, parse_host_key, parse_key, search_records, update_ltsv_file, url_encode

Constructor Details

#initialize(options = {}) ⇒ Graph

Returns a new instance of Graph.



20
21
22
23
24
# File 'lib/gri/graph.rb', line 20

def initialize options={}
  @options = options
  @hprops = {}
  @hosthash = {}
end

Instance Method Details

#call(env) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/gri/graph.rb', line 166

def call env
  req = GRI::Request.new env
  params = req.gparams
  ENV['TZ'] = Config['tz'] if Config['tz']
  ENV['TZ'] = params['tz'].to_s unless params['tz'].blank?
  stime, etime = req.params['stime'].to_i, req.params['etime'].to_i
  etime = (Time.now + etime).to_i if etime <= 0
  stime = (etime + stime).to_i if stime <= 0

  if req.params['fmt'] == 'json'
    str = xport stime, etime, params
    [200, {'Content-type'=>'text/plain'}, [str]]
  else
    img = graph stime, etime, params
    [200, {'Content-type'=>'image/png'}, [img]]
  end
rescue
  Log.error "#{$!}: #{$@.first}"
  [500, {}, []]
end

#get_rrdpaths_and_ub(rnames) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gri/graph.rb', line 26

def get_rrdpaths_and_ub rnames
  total_ub = 0
  rrdpaths = []
  dirs = @options[:dirs]
  for rname in rnames
    host, key = rname.split('_', 2)
    @hosthash[host] = true
    dir, h = search_records dirs, host
    if h
      @hprops[host] ||= h
      if h[key]
        prop = get_prop h[key]
        total_ub += prop[:ub].to_i
      end
      path = "#{dir}/#{host}/#{Rack::Utils.escape rname}.rrd"
      rrdpaths.push path if File.exist? path
    end
  end
  return rrdpaths, total_ub
end

#graph(stime, etime, params) ⇒ Object



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
# File 'lib/gri/graph.rb', line 209

def graph stime, etime, params
  rnames = params.getvar 'r'
  r = rnames.first
  host, data_name, index = parse_host_key r
  rrdpaths, total_ub = get_rrdpaths_and_ub rnames

  args = []
  if total_ub > 0 and (params['p'] != 'v')
    args.push "-u #{total_ub} --alt-autoscale-max" if params['y'] == 'u'
    args.push "HRULE:#{total_ub}#ff0000"
  end
  if params['title']
    args.push "--title \"#{params['title']}\""
  elsif (title = mk_graph_title rnames, params)
    args.push "--title #{title}"
  end

  specs = DEFS.get_specs data_name
  args += mk_graph_args specs, rrdpaths, params

  rrd = RRD.new Config['rrdcached-address']
  # size
  rrd.width, rrd.height = SIZE[params['z']]
  # font
  if Config['font']
    Config.getvar('font').each {|fstr| rrd.fonts.push fstr}
  end

  img = rrd.graphgen stime, etime, args
end

#mk_comp_defs(rrdpaths, comp_t, gattr, &block) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/gri/graph.rb', line 126

def mk_comp_defs rrdpaths, comp_t, gattr, &block
  case comp_t
  when 's'; mk_stack_defs rrdpaths, gattr, &block
  when 'v'; mk_ov_defs rrdpaths, gattr, &block
  else mk_sum_defs rrdpaths, gattr, &block
  end
end

#mk_defstr(ds_specs, rrdpaths, params, limit) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/gri/graph.rb', line 98

def mk_defstr ds_specs, rrdpaths, params, limit
  mdefs = []
  ds_param = params['ds'].blank? ? nil : params['ds']
  if limit and limit.size == 2
    lower, upper = limit
  end
  x_p = (params['fmt'] == 'json')
  for ds_spec in ds_specs
    mname, dsname, dst, cf, gline, gcolor, legend, mag = ds_spec.split(',')
    next if gline == '-' or (ds_param and ds_param != dsname)
    cf = 'MAX' if cf.blank?
    gline = 'LINE1' if gline.blank?
    gcolor = '#0000ff' if gcolor.blank?
    legend = dsname if legend.blank?
    mag ||= 1
    gattr = [gline, gcolor, legend, params['cl'], params['hw']]
    sdefs = mk_comp_defs(rrdpaths, params['p'], gattr) {
      |paths, xcf, ind, gattr|
      xgline, xgcolor, xlegend = gattr
      xgline = 'XPORT' if x_p
      RRD.defstr paths, dsname, cf, "#{dsname}#{ind}",
        xgline, xgcolor, xlegend, mag, lower, upper
    }
    mdefs.push sdefs
  end
  mdefs
end

#mk_graph_args(specs, rrdpaths, params) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/gri/graph.rb', line 79

def mk_graph_args specs, rrdpaths, params
  defs = []

  gidx = params['g'].to_i
  gr_spec = (specs[:graph] || [])[gidx]
  vlabel, base, limit, pickup_re, opts = gr_spec
  defs.push %Q{-v "#{vlabel.gsub(/\"/, '')}"} if vlabel
  if Numeric === base
    defs.push(base.zero? ? '--units-exponent 0 --alt-y-grid' :
               "--base #{base}")
  end
  defs.push "--lower-limit 0"

  ds_specs = specs[:ds] || []
  ds_specs = ds_specs.grep pickup_re if pickup_re
  defs += mk_defstr ds_specs, rrdpaths, params, limit
  defs.flatten
end

#mk_graph_title(rs, params) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gri/graph.rb', line 47

def mk_graph_title rs, params
  if params['p'] == 't'
    r, = rs
    host, key = r.split('_', 2)
    dir, h = search_records @options[:dirs], host
    if h
      @hprops[host] ||= h
      if h[key]
        prop = get_prop h[key]
        t = %Q{"#{host} #{prop[:name]} #{prop[:description]}"}
      end
    end
  elsif (params['p'] == 's' or params['p'] == 'v') and
      (params['ds'] and params['ds'] != '')
    t = params['ds']
  end
  t
end

#mk_label(path) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/gri/graph.rb', line 66

def mk_label path
  dir = File.dirname path
  label = File.basename path, '.rrd'
  host, key = label.split('_', 2)
  h = (@hprops[host] ||= (load_records "#{dir}/#{host}"))
  if h and h[key]
    prop = get_prop h[key]
    name = prop[:legend] || prop[:name] || key
    label = (@hosthash.size > 1) ? "#{host} #{name}" : name
  end
  label
end

#mk_ov_defs(rrdpaths, gattr, &block) ⇒ Object



155
156
157
158
159
160
161
162
163
164
# File 'lib/gri/graph.rb', line 155

def mk_ov_defs rrdpaths, gattr, &block
  defs = []
  rrdpaths.each_with_index {|path, ind|
    gcolor = '#' + SCOLORS[ind % SCOLORS.size]
    legend = mk_label path
    gattr = ['LINE1', gcolor, legend]
    defs += [block.call([path], 'MAX', ind, gattr)]
  }
  defs
end

#mk_stack_defs(rrdpaths, gattr, &block) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/gri/graph.rb', line 142

def mk_stack_defs rrdpaths, gattr, &block
  defs = []
  gline = 'AREA'
  rrdpaths.each_with_index {|path, ind|
    gcolor = '#' + SCOLORS[ind % SCOLORS.size]
    legend = mk_label path
    gattr = [gline, gcolor, legend]
    defs += [block.call([path], 'MAX', ind, gattr)]
    gline = 'STACK'
  }
  defs
end

#mk_sum_defs(rrdpaths, gattr, &block) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/gri/graph.rb', line 134

def mk_sum_defs rrdpaths, gattr, &block
  if (legend = gattr[2]) == ':index'
    path, = rrdpaths
    host, dn, gattr[2] = File.basename(path, '.rrd').split('_', 3)
  end
  block.call(rrdpaths, 'MAX', 0, gattr)
end

#xport(stime, etime, params) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/gri/graph.rb', line 187

def xport stime, etime, params
  rnames = params.getvar 'r'
  r = rnames.first
  host, data_name, index = parse_host_key r
  rrdpaths, total_ub = get_rrdpaths_and_ub rnames

  specs = DEFS.get_specs data_name
  gidx = params['g'].to_i
  gr_spec = (specs[:graph] || [])[gidx]
  vlabel, base, limit, pickup_re, opts = gr_spec

  ds_specs = specs[:ds] || []
  ds_specs = ds_specs.grep pickup_re if pickup_re
  args = []
  if params['maxrows'].present?
    args.push "--maxrows #{params['maxrows'].to_i}"
  end
  args += mk_defstr(ds_specs, rrdpaths, params, limit).flatten
  rrd = RRD.new Config['rrdcached-address']
  str = rrd.xport stime, etime, *args
end