Module: MetricsDownloader

Defined in:
lib/metrics_downloader.rb

Class Method Summary collapse

Class Method Details

.create_urls_from_host_params(host_list, port, route = "metricz/") ⇒ Object

Parameters:

  • host_list

    A list of host strings, without the port, optionally with http:// prefix



29
30
31
32
33
34
# File 'lib/metrics_downloader.rb', line 29

def self.create_urls_from_host_params(host_list, port, route = "metricz/")
  host_list.map do |host|
    host = host[7..-1] if host.start_with?("http:")
    "http://#{host}:#{port}/#{route}"
  end
end

.download_and_parse_json_from_urls(urls, timeout = 5) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/metrics_downloader.rb', line 7

def self.download_and_parse_json_from_urls(urls, timeout = 5)
  threads = urls.map do |url|
    Thread.new do
      Thread.current[:output] = begin
        body = RestClient::Request.execute(:method => :get, :url => url,
                                           :timeout => timeout,
                                           :open_timeout => timeout)
        JSON.parse(body)
      rescue
        # TODO: log the exception?
        STDERR.puts "WARNING: Unable to parse JSON from #{url}"
        {}
      end
    end
  end

  threads.map { |t| t.join; t[:output] }
end

.expand_host_expr(host_expr) ⇒ Object

Parameters:

  • host_expr

    A host string with an embedded range [1..18] or individual numbers,

    1..5,7,9

    > 1,2,3,4,5,7,9 will be substituted



39
40
41
42
43
44
45
46
47
# File 'lib/metrics_downloader.rb', line 39

def self.expand_host_expr(host_expr)
  if host_expr =~ /(\[([0-9.,]+)\])/
    embedded_expr = $1
    numbers = $2.split(",").map { |item| item.include?("..") ? eval(item).to_a : [item.to_i] }.flatten
    numbers.map { |n| host_expr.gsub(embedded_expr, n.to_s) }
  else
    [host_expr]
  end
end

.parse_json_from(hosts, port, route = "metricz/", timeout = 5) ⇒ Object

Parses JSON from a list of host expressions, as passed to expand_host_expr. Basically a combination of expand_host_expr, create_urls_from_host_params, and download_and_parse_json_from_urls.

Parameters:

  • hosts

    A list of host expressions, as passed to expand_host_expr

  • port

    The integer port to query

  • route (defaults to: "metricz/")

    The string after / for getting metrics, defaults to “metricz/”

  • timeout (defaults to: 5)

    The timeout in seconds for fetching data



79
80
81
82
83
84
# File 'lib/metrics_downloader.rb', line 79

def self.parse_json_from(hosts, port, route = "metricz/", timeout = 5)
  expanded_hosts = hosts.map { |expr| MetricsDownloader.expand_host_expr(expr) }.flatten
  urls = MetricsDownloader.create_urls_from_host_params(expanded_hosts, port, route)
  json_trees = MetricsDownloader.download_and_parse_json_from_urls(urls, timeout)
  Hash[expanded_hosts.zip(json_trees)]
end

.split_host_exprs(host_exprs_str) ⇒ Object

Splits a comma-delimited string of host exprs. This is tricky since there may be commas within brackets

Parameters:

  • host_exprs

    A single string with multiple host expressions separated by commas



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/metrics_downloader.rb', line 51

def self.split_host_exprs(host_exprs_str)
  host_exprs = []
  expr = ""
  in_brackets = false
  host_exprs_str.each_char do |c|
    if c == "," && !in_brackets
      host_exprs << expr
      expr = ""
      next
    elsif c == "["
      in_brackets = true
    elsif c == "]"
      in_brackets = false
    end
    expr << c
  end
  host_exprs << expr if expr != ""
  host_exprs
end