From 701c64499129f7e1285c00d682aa155b6c8956e5 Mon Sep 17 00:00:00 2001 From: srgshames Date: Thu, 27 Sep 2012 18:38:50 +0200 Subject: [PATCH 1/8] Added support for graph files generation from config --- lib/graphite_graph_gen.rb | 76 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 lib/graphite_graph_gen.rb diff --git a/lib/graphite_graph_gen.rb b/lib/graphite_graph_gen.rb new file mode 100644 index 0000000..522d64d --- /dev/null +++ b/lib/graphite_graph_gen.rb @@ -0,0 +1,76 @@ +require 'uri' +require 'cgi' +# A small DSL to assist in the creation of Graphite graphs +# see https://github.com/ripienaar/graphite-graph-dsl/wiki +# for full details +class GraphiteGraphGenerator + + def initialize(graphite_base, directory, graph_gen_file_name, info={}) + @graphite_metrics_url = [graphite_base, "/metrics/index.json"].join + @graphs_dir = directory + @graph_gen_file = File.join(directory, graph_gen_file_name) + @info = info + + @stats = [] + @properties = {} + @general_text = "" + + load_graph_gen + generate_graph_definitions + end + + def load_graph_gen + self.instance_eval(File.read(@graph_gen_file)) unless @graph_gen_file == :none + end + + def generator(options) + %w(context stats naming_pattern).each do |option| + raise "field_gen needs a #{option}" unless options.include?(option.to_sym) + end + + options.each do |key, value| + if key.to_s.start_with? 'field_prop' + @properties[key.to_s.match(/^field_prop:(.*)$/)[1].to_sym] = value + else + self.instance_variable_set "@#{key}", value + end + end + end + + def method_missing(method, *args) + @general_text << "#{method} :#{args[0]}" + if args.length > 1 + @general_text << ",\n#{args[1].to_s[1..-2]}\n" + end + @general_text << "\n\n" + end + + def generate_graph_definitions + metric_branches.each do |branch| + graph_name = branch.scan(@naming_pattern)[0][0] + graph_file_name = "gen_#{graph_name}.graph" + File.open(File.join(@graphs_dir, graph_file_name), 'w') {|f| f.write(graph_file_content(graph_name, branch)) } + end + end + + def metric_branches + response = Typhoeus::Request.get(@graphite_metrics_url) + raise "Error fetching #{@graphite_metrics_url}. #{response.inspect}" unless response.success? + json = Yajl::Parser.parse(response.body) + branches = json.join.scan(@context).uniq.map { |branch| branch[0] } + end + + def graph_file_content(graph_name, branch) + graph_title = graph_name.gsub('_', ' ').gsub(' - ', ' ').gsub('.', ' - ').gsub(' ', ' ') + graph_file_content = "title \"#{graph_title}\"\n\n" << @general_text + @stats.uniq.each do |stat| + full_metric = [branch, stat].join('.') + graph_file_content << "field :#{stat},\n" << ":data => \"#{full_metric}\"" + if @properties.length > 0 + graph_file_content << ",\n#{@properties.to_s[1..-2]}\n" + end + graph_file_content << "\n\n" + end + graph_file_content + end +end From a1f5e25f02f49c192fb772bf41776508e8b8ec3f Mon Sep 17 00:00:00 2001 From: srgshames Date: Thu, 27 Sep 2012 18:42:03 +0200 Subject: [PATCH 2/8] Added missing dependencies to gem file and bumped version --- Gemfile | 2 ++ lib/graphite_graph/version.rb | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 1aa98e4..69cd686 100644 --- a/Gemfile +++ b/Gemfile @@ -1,2 +1,4 @@ source "http://rubygems.org" +gem 'typhoeus' +gem 'yajl-ruby' gemspec diff --git a/lib/graphite_graph/version.rb b/lib/graphite_graph/version.rb index ea58af3..34b2a1a 100644 --- a/lib/graphite_graph/version.rb +++ b/lib/graphite_graph/version.rb @@ -1,3 +1,3 @@ class GraphiteGraph - VERSION = "0.0.7" + VERSION = "0.0.8" end From 35193019847672a2b8bf37e66a1813b838d2d0fe Mon Sep 17 00:00:00 2001 From: srgshames Date: Fri, 28 Sep 2012 12:26:48 +0200 Subject: [PATCH 3/8] Set dependencies the right way (in gemspec and not Gemfile) --- Gemfile | 2 -- graphite_graph.gemspec | 3 +++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index 69cd686..1aa98e4 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,2 @@ source "http://rubygems.org" -gem 'typhoeus' -gem 'yajl-ruby' gemspec diff --git a/graphite_graph.gemspec b/graphite_graph.gemspec index a36d590..d3ae6cf 100644 --- a/graphite_graph.gemspec +++ b/graphite_graph.gemspec @@ -16,4 +16,7 @@ Gem::Specification.new do |s| s.files = `git ls-files`.split("\n") s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.require_paths = ["lib"] + + s.add_dependency 'typhoeus' + s.add_dependency 'yajl-ruby' end From e57af884742541035106bffbcbae4b49340f97c9 Mon Sep 17 00:00:00 2001 From: srgshames Date: Sun, 30 Sep 2012 17:02:35 +0200 Subject: [PATCH 4/8] Fixed require statements --- lib/graphite_graph_gen.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/graphite_graph_gen.rb b/lib/graphite_graph_gen.rb index 522d64d..f32dd2b 100644 --- a/lib/graphite_graph_gen.rb +++ b/lib/graphite_graph_gen.rb @@ -1,5 +1,5 @@ -require 'uri' -require 'cgi' +require 'typhoeus' +require 'yajl' # A small DSL to assist in the creation of Graphite graphs # see https://github.com/ripienaar/graphite-graph-dsl/wiki # for full details From 7ea6da43cfa695a541a93fca8a70edee3314bf42 Mon Sep 17 00:00:00 2001 From: srgshames Date: Sun, 30 Sep 2012 17:22:38 +0200 Subject: [PATCH 5/8] Feature - removing old generated graphs before generating new ones (to avoid 'garbage' graphs) --- lib/graphite_graph_gen.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/graphite_graph_gen.rb b/lib/graphite_graph_gen.rb index f32dd2b..b4da6bd 100644 --- a/lib/graphite_graph_gen.rb +++ b/lib/graphite_graph_gen.rb @@ -15,6 +15,7 @@ def initialize(graphite_base, directory, graph_gen_file_name, info={}) @properties = {} @general_text = "" + clean_old_generated_graphs load_graph_gen generate_graph_definitions end @@ -45,6 +46,10 @@ def method_missing(method, *args) @general_text << "\n\n" end + def clean_old_generated_graphs + Dir.entries(@graphs_dir).select{|f| fn = File.join(@graphs_dir, f); File.delete(fn) if f.match(/gen.*\.graph$/)} + end + def generate_graph_definitions metric_branches.each do |branch| graph_name = branch.scan(@naming_pattern)[0][0] From 22d016b9b0ea1c060f492a659603d661bdb42bc5 Mon Sep 17 00:00:00 2001 From: srgshames Date: Sun, 30 Sep 2012 19:08:12 +0200 Subject: [PATCH 6/8] Handling case when there's no match for graph_name; Added :target_wrapper - ability to wrap the :data field with graphite functions --- lib/graphite_graph_gen.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/graphite_graph_gen.rb b/lib/graphite_graph_gen.rb index b4da6bd..4539e4a 100644 --- a/lib/graphite_graph_gen.rb +++ b/lib/graphite_graph_gen.rb @@ -52,12 +52,17 @@ def clean_old_generated_graphs def generate_graph_definitions metric_branches.each do |branch| - graph_name = branch.scan(@naming_pattern)[0][0] + graph_name = extract_graph_name(branch) graph_file_name = "gen_#{graph_name}.graph" File.open(File.join(@graphs_dir, graph_file_name), 'w') {|f| f.write(graph_file_content(graph_name, branch)) } end end + def extract_graph_name(branch) + name_match = branch.match(@naming_pattern) + name_match ? name_match[1] : branch + end + def metric_branches response = Typhoeus::Request.get(@graphite_metrics_url) raise "Error fetching #{@graphite_metrics_url}. #{response.inspect}" unless response.success? @@ -69,13 +74,18 @@ def graph_file_content(graph_name, branch) graph_title = graph_name.gsub('_', ' ').gsub(' - ', ' ').gsub('.', ' - ').gsub(' ', ' ') graph_file_content = "title \"#{graph_title}\"\n\n" << @general_text @stats.uniq.each do |stat| - full_metric = [branch, stat].join('.') - graph_file_content << "field :#{stat},\n" << ":data => \"#{full_metric}\"" + graph_file_content << "field :#{stat},\n" << ":data => \"#{full_metric(branch, stat)}\"" if @properties.length > 0 graph_file_content << ",\n#{@properties.to_s[1..-2]}\n" end graph_file_content << "\n\n" end graph_file_content - end + end + + def full_metric(branch, stat) + full_metric = [branch, stat].join('.') + full_metric = @target_wrapper % full_metric if @target_wrapper + full_metric + end end From 80a4c3fa4951d304ee088becec75ab634b6ec51a Mon Sep 17 00:00:00 2001 From: srgshames Date: Mon, 1 Oct 2012 16:38:10 +0200 Subject: [PATCH 7/8] Handling cases when first argument of a general param is string and not symbol --- lib/graphite_graph_gen.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/graphite_graph_gen.rb b/lib/graphite_graph_gen.rb index 4539e4a..9772f36 100644 --- a/lib/graphite_graph_gen.rb +++ b/lib/graphite_graph_gen.rb @@ -39,7 +39,8 @@ def generator(options) end def method_missing(method, *args) - @general_text << "#{method} :#{args[0]}" + @general_text << "#{method} " + @general_text << ( args[0].is_a?(Symbol) ? ":#{args[0]}" : "\"#{args[0]}\"" ) if args.length > 1 @general_text << ",\n#{args[1].to_s[1..-2]}\n" end From 1d1cdefc248f283e6ea977815e6c2eb8b0dc5b4c Mon Sep 17 00:00:00 2001 From: Sergei Shames Date: Thu, 6 Jun 2013 05:56:08 +0000 Subject: [PATCH 8/8] Fixed bug in metrics matching that caused every second graph not to display --- bin/check_graph.rb | 0 lib/graphite_graph_gen.rb | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) mode change 100644 => 100755 bin/check_graph.rb diff --git a/bin/check_graph.rb b/bin/check_graph.rb old mode 100644 new mode 100755 diff --git a/lib/graphite_graph_gen.rb b/lib/graphite_graph_gen.rb index 9772f36..f59035e 100644 --- a/lib/graphite_graph_gen.rb +++ b/lib/graphite_graph_gen.rb @@ -14,7 +14,7 @@ def initialize(graphite_base, directory, graph_gen_file_name, info={}) @stats = [] @properties = {} @general_text = "" - + clean_old_generated_graphs load_graph_gen generate_graph_definitions @@ -68,7 +68,7 @@ def metric_branches response = Typhoeus::Request.get(@graphite_metrics_url) raise "Error fetching #{@graphite_metrics_url}. #{response.inspect}" unless response.success? json = Yajl::Parser.parse(response.body) - branches = json.join.scan(@context).uniq.map { |branch| branch[0] } + branches = json.join('|').scan(@context).uniq.map { |branch| branch[0] } end def graph_file_content(graph_name, branch)