Class: GraphiteGraph

Inherits:
Object
  • Object
show all
Defined in:
lib/graphite_graph.rb,
lib/graphite_graph/version.rb

Overview

for full details

Constant Summary collapse

VERSION =
"0.0.7"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, overrides = {}, info = {}) ⇒ GraphiteGraph

Returns a new instance of GraphiteGraph.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/graphite_graph.rb', line 10

def initialize(file, overrides={}, info={})
  @info = info
  @file = file
  @munin_mode = false
  @overrides = overrides
  @linecount = 0

  @critical_threshold = []
  @warning_threshold = []

  load_graph
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/graphite_graph.rb', line 63

def method_missing(meth, *args)
  if properties.include?(meth)
    properties[meth] = args.first unless @overrides.include?(meth)
  else
    super
  end
end

Instance Attribute Details

#critical_thresholdObject (readonly)

Returns the value of attribute critical_threshold.



8
9
10
# File 'lib/graphite_graph.rb', line 8

def critical_threshold
  @critical_threshold
end

#infoObject (readonly)

Returns the value of attribute info.



8
9
10
# File 'lib/graphite_graph.rb', line 8

def info
  @info
end

#propertiesObject (readonly)

Returns the value of attribute properties.



8
9
10
# File 'lib/graphite_graph.rb', line 8

def properties
  @properties
end

#target_orderObject (readonly)

Returns the value of attribute target_order.



8
9
10
# File 'lib/graphite_graph.rb', line 8

def target_order
  @target_order
end

#targetsObject (readonly)

Returns the value of attribute targets.



8
9
10
# File 'lib/graphite_graph.rb', line 8

def targets
  @targets
end

#warning_thresholdObject (readonly)

Returns the value of attribute warning_threshold.



8
9
10
# File 'lib/graphite_graph.rb', line 8

def warning_threshold
  @warning_threshold
end

Instance Method Details

#[](key) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/graphite_graph.rb', line 55

def [](key)
  if key == :url
    url
  else
    @properties[key]
  end
end

#critical(options) ⇒ Object

draws a single dashed line with predictable names, defaults to red line

data can be a single item or a 2 item array, it doesn’t break if you supply more but # more than 2 items just doesn’t make sense generally

critical :value => [700, -700], :color => “red”

You can prevent the line from being drawn but just store the ranges for monitoring purposes by adding :hide => true to the arguments



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/graphite_graph.rb', line 189

def critical(options)
  raise "critical lines need a value" unless options[:value]

  @critical_threshold = [options[:value]].flatten

  options[:color] ||= "red"

  unless options[:hide]
    @critical_threshold.each_with_index do |crit, index|
      line :caption => "crit_#{index}", :value => crit, :color => options[:color], :dashed => true
    end
  end
end

#defaultsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/graphite_graph.rb', line 23

def defaults
  @properties = {:title => nil,
                 :vtitle => nil,
                 :vtitle_right => nil,
                 :width => 500,
                 :height => 250,
                 :from => "-1hour",
                 :until => "now",
                 :surpress => false,
                 :description => nil,
                 :hide_legend => nil,
                 :hide_grid => nil,
                 :ymin => nil,
                 :ymax => nil,
                 :yunit_system => nil,
                 :linewidth => nil,
                 :linemode => nil,
                 :fontsize => nil,
                 :fontbold => false,
                 :fontname => nil,
                 :timezone => nil,
                 :xformat => nil,
                 :background_color => nil,
                 :foreground_color => nil,
                 :draw_null_as_zero => false,
                 :major_grid_line_color => nil,
                 :minor_grid_line_color => nil,
                 :area => :none,
                 :logbase => nil,
                 :placeholders => nil}.merge(@overrides)
end

#field(name, args) ⇒ Object

adds a field to the graph, each field needs a unique name



247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/graphite_graph.rb', line 247

def field(name, args)
  raise "A field called #{name} already exist for this graph" if targets.include?(name)

  default = {}

  if @service_mode
    default[:data] = [info[:hostname], @service_mode[:service], @service_mode[:data], name].join(".")
  end

  targets[name] = default.merge(args)
  target_order << name
end

#group(name, args) ⇒ Object

takes a series of metrics in a wildcard query and aggregates the values by a subgroup

data must contain a wildcard query, a subgroup position, and an optional aggregate function. if the aggregate function is omitted, sumSeries will be used.

group :data => “metric.*.value”, :subgroup => “2”, :aggregator => “sumSeries”



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/graphite_graph.rb', line 168

def group(name, args)
  raise ":data is needed as an argument to group metrics" unless args[:data]
  raise ":subgroup is needed as an argument to group metrics" unless args.include?(:subgroup)

  args[:aggregator] = "sumSeries" unless args[:aggregator]

  group_args = args.clone
  group_args[:data] = "groupByNode(#{group_args[:data]},#{group_args[:subgroup]},\"#{group_args[:aggregator]}\")"
  field "#{name}_group", group_args

end

#hw_predict(name, args) ⇒ Object Also known as: forecast

add forecast, bands, aberrations and actual fields using the Holt-Winters Confidence Band prediction model

hw_predict :foo, :data => "some.data.item", :alias => "Some Item"

You can tweak the colors by setting:

:forecast_color => "blue"
:bands_color => "grey"
:aberration_color => "red"

You can add an aberration line:

:aberration_line => true,
:aberration_second_y => true

You can disable the forecast line by setting:

:forecast_line => false

You can disable the confidence lines by settings:

:bands_lines => false

You can disable the display of the actual data:

:actual_line => false


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/graphite_graph.rb', line 115

def hw_predict(name, args)
  raise ":data is needed as an argument to a Holt-Winters Confidence forecast" unless args[:data]

  unless args[:forecast_line] == false
    forecast_args = args.clone
    forecast_args[:data] = "holtWintersForecast(#{forecast_args[:data]})"
    forecast_args[:alias] = "#{args[:alias]} Forecast"
    forecast_args[:color] = args[:forecast_color] || "blue"
    field "#{name}_forecast", forecast_args
  end

  unless args[:bands_lines] == false
    bands_args = args.clone
    bands_args[:data] = "holtWintersConfidenceBands(#{bands_args[:data]})"
    bands_args[:color] = args[:bands_color] || "grey"
    bands_args[:dashed] = true
    bands_args[:alias] = "#{args[:alias]} Confidence"
    field "#{name}_bands", bands_args
  end

  if args[:aberration_line]
    aberration_args = args.clone
    aberration_args[:data] = "holtWintersAberration(keepLastValue(#{aberration_args[:data]}))"
    aberration_args[:color] = args[:aberration_color] || "orange"
    aberration_args[:alias] = "#{args[:alias]} Aberration"
    aberration_args[:second_y_axis] = true if aberration_args[:aberration_second_y]
    field "#{name}_aberration", aberration_args
  end

  if args[:critical]
    color = args[:critical_color] || "red"
    critical :value => args[:critical], :color => color, :name => name
  end

  if args[:warning]
    color = args[:warning_color] || "orange"
    warning :value => args[:warning], :color => color, :name => name
  end

  args[:color] ||= "yellow"

  field name, args unless args[:actual_line] == false
end

#line(options) ⇒ Object

draws a simple line on the graph with a caption, value and color.

line :caption => “warning”, :value => 50, :color => “orange”



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/graphite_graph.rb', line 229

def line(options)
  raise "lines need a caption" unless options.include?(:caption)
  raise "lines need a value" unless options.include?(:value)
  raise "lines need a color" unless options.include?(:color)

  options[:alias] = options[:caption] unless options[:alias]

  args = {:data => "threshold(#{options[:value]})", :color => options[:color], :alias => options[:alias]}

  args[:dashed] = true if options[:dashed]
  args[:second_y_axis] = true if options[:second_y_axis]

  field "line_#{@linecount}", args

  @linecount += 1
end

#load_graphObject



71
72
73
74
75
76
77
# File 'lib/graphite_graph.rb', line 71

def load_graph
  @properties = defaults
  @targets = {}
  @target_order = []

  self.instance_eval(File.read(@file)) unless @file == :none
end

#service(service, data, &blk) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/graphite_graph.rb', line 79

def service(service, data, &blk)
  raise "No hostname given for this instance" unless info[:hostname]

  @service_mode = {:service => service, :data => data}

  blk.call

  @service_mode = false
end

#url(format = nil, url = true) ⇒ Object



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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/graphite_graph.rb', line 260

def url(format = nil, url=true)
  return nil if properties[:surpress]

  url_parts = []
  colors = []

  [:title, :vtitle, :from, :width, :height, :until].each do |item|
    url_parts << "#{item}=#{properties[item]}" if properties[item]
  end

  url_parts << "areaMode=#{properties[:area]}" if properties[:area]
  url_parts << "hideLegend=#{properties[:hide_legend]}" unless properties[:hide_legend].nil?
  url_parts << "hideGrid=#{properties[:hide_grid]}" if properties[:hide_grid]
  url_parts << "yMin=#{properties[:ymin]}" if properties[:ymin]
  url_parts << "yMax=#{properties[:ymax]}" if properties[:ymax]
  url_parts << "yUnitSystem=#{properties[:yunit_system]}" if properties[:yunit_system]
  url_parts << "lineWidth=#{properties[:linewidth]}" if properties[:linewidth]
  url_parts << "lineMode=#{properties[:linemode]}" if properties[:linemode]
  url_parts << "fontSize=#{properties[:fontsize]}" if properties[:fontsize]
  url_parts << "fontBold=#{properties[:fontbold]}" if properties[:fontbold]
  url_parts << "fontName=#{properties[:fontname]}" if properties[:fontname]
  url_parts << "drawNullAsZero=#{properties[:draw_null_as_zero]}" if properties[:draw_null_as_zero]
  url_parts << "tz=#{properties[:timezone]}" if properties[:timezone]
  url_parts << "xFormat=#{properties[:xformat]}" if properties[:xformat]
  url_parts << "majorGridLineColor=#{properties[:major_grid_line_color]}" if properties[:major_grid_line_color]
  url_parts << "minorGridLineColor=#{properties[:minor_grid_line_color]}" if properties[:minor_grid_line_color]
  url_parts << "bgcolor=#{properties[:background_color]}" if properties[:background_color]
  url_parts << "fgcolor=#{properties[:foreground_color]}" if properties[:foreground_color]
  url_parts << "vtitleRight=#{properties[:vtitle_right]}" if properties[:vtitle_right]
  url_parts << "logBase=#{properties[:logbase]}" if properties[:logbase]

  target_order.each do |name|
    target = targets[name]

    if target[:target]
      url_parts << "target=#{target[:target]}"
    else
      raise "field #{name} does not have any data associated with it" unless target[:data]

      graphite_target = target[:data]

      graphite_target = "keepLastValue(#{graphite_target})" if target[:keep_last_value]
      graphite_target = "sum(#{graphite_target})" if target[:sum]
      if target[:derivative]
        graphite_target = "derivative(#{graphite_target})"
      elsif target[:non_negative_derivative]
        graphite_target = "nonNegativeDerivative(#{graphite_target})"
      end
      graphite_target = "highestAverage(#{graphite_target},#{target[:highest_average]})" if target[:highest_average]
      if target[:scale]
        graphite_target = "scale(#{graphite_target},#{target[:scale]})"
      elsif target[:scale_to_seconds]
        graphite_target = "scaleToSeconds(#{graphite_target},#{target[:scale_to_seconds]})"
      end
      if target[:as_percent] == true
        graphite_target = "asPercent(#{graphite_target})"
      elsif target[:as_percent]
        graphite_target = "asPercent(#{graphite_target},#{target[:as_percent]})"
      end
      graphite_target = "drawAsInfinite(#{graphite_target})" if target[:line]
      graphite_target = "movingAverage(#{graphite_target},#{target[:smoothing]})" if target[:smoothing]

      graphite_target = "color(#{graphite_target},\"#{target[:color]}\")" if target[:color]
      graphite_target = "dashed(#{graphite_target})" if target[:dashed]
      graphite_target = "secondYAxis(#{graphite_target})" if target[:second_y_axis]

      unless target.include?(:subgroup)
        if target[:alias_by_node]
          graphite_target = "aliasByNode(#{graphite_target},#{target[:alias_by_node]})"
        elsif target[:alias_sub_search]
          graphite_target = "aliasSub(#{graphite_target},\"#{target[:alias_sub_search]}\",\"#{target[:alias_sub_replace]}\")"
        elsif target[:alias]
          graphite_target = "alias(#{graphite_target},\"#{target[:alias]}\")"
        elsif target[:no_alias]
          graphite_target = graphite_target # no-op
        else
          graphite_target = "alias(#{graphite_target},\"#{name.to_s.capitalize}\")"
        end

        if target[:cacti_style]
          graphite_target = "cactiStyle(#{graphite_target})"
        elsif target[:legend_value]
          graphite_target = "legendValue(#{graphite_target},\"#{target[:legend_value]}\")"
        end
      end

      url_parts << "target=#{graphite_target}"
    end
  end

  url_parts << "format=#{format}" if format

  if url
    url_str = url_parts.map { |pair| k,v = pair.split('='); "#{k}=#{CGI.escape(v)}" }.join("&")
    properties[:placeholders].each { |k,v| url_str.gsub!("%{#{k}}", v.to_s) } if properties[:placeholders].is_a?(Hash)

    url_str
  else
    url_parts
  end
end

#warning(options) ⇒ Object

draws a single dashed line with predictable names, defaults to orange line

data can be a single item or a 2 item array, it doesn’t break if you supply more but # more than 2 items just doesn’t make sense generally

warning :value => [700, -700], :color => “orange”

You can prevent the line from being drawn but just store the ranges for monitoring purposes by adding :hide => true to the arguments



212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/graphite_graph.rb', line 212

def warning(options)
  raise "warning lines need a value" unless options[:value]

  @warning_threshold = [options[:value]].flatten

  options[:color] ||= "orange"

  unless options[:hide]
    @warning_threshold.flatten.each_with_index do |warn, index|
      line :caption => "warn_#{index}", :value => warn, :color => options[:color], :dashed => true
    end
  end
end