Class: NagiosHerald::Helpers::GraphiteGraph

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/nagios-herald/helpers/graphite_graph.rb

Instance Method Summary collapse

Methods included from Logging

#configure_logger_for, #logger, #logger_for

Constructor Details

#initializeGraphiteGraph

Public: Initialize a GraphiteGraph helper object.

Returns a GraphiteGraph helper object.



12
13
14
15
16
17
# File 'lib/nagios-herald/helpers/graphite_graph.rb', line 12

def initialize
  # Currently hard-codes the value for optional graphs showing historical
  # data.
  @graphite_historical_lookup = '-24h'
  @image_paths = []
end

Instance Method Details

#download_image(url, download_path) ⇒ Object

Public: Download a Graphite image.

url - The Graphite url we’ll download as an image. download_path - The path to where the image will be downloaded.

Returns nothing. Appends the downloaded image path to @image_paths.



25
26
27
28
29
30
31
32
# File 'lib/nagios-herald/helpers/graphite_graph.rb', line 25

def download_image(url, download_path)
  success = NagiosHerald::Helpers::UrlImage.download_image(url, download_path)
  if success
    @image_paths.push(download_path)
  else
    logger.warn("Could not download Graphite graph for '#{url}'")
  end
end

#get_graph(url, path, show_historical = nil) ⇒ Object

Public: Retrieve a Graphite graph

url - A string containing the full URL to get from Graphite. path - The local path on the host running nagios-herald under which image

files will be temporarily generated.

show_historical - A boolean that allows one to optionally download a

showing historical data for comparison.
Defaults to false.

Because this will probably be fed URLs used in Nagios checks, we’ll strip out ‘&format’ and ‘&rawData’ query parameters to ensure we get an image instead of text/json/csv/etc.

In cases where the method is called requesting an historical image we’ll strip ‘&until’ and replace the value of ‘&from’ with that of Example

get_graph("http://graphite.example.com/render/?target=foo.bar.baz?from=-15min", "/tmp/img1234", true)

Returns the local path of the downloaded image to be attached/inlined with a message.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/nagios-herald/helpers/graphite_graph.rb', line 56

def get_graph(url, path, show_historical=nil)
  uri = URI(url)
  # Strip &rawData parameter.
  uri.query.gsub!(/&rawData([^&]*)/, '')
  # Strip the &format parameter.
  uri.query.gsub!(/&format([^&])*/, '')
  # Strip the trailing slash from the path.
  path = path.sub(/\/$/, "")
  # Generate a random UUID to be used in the image filename.
  image_uuid = SecureRandom.uuid
  image_path = "#{path}/#{image_uuid}.png"
  image_url = uri.to_s
  download_image(image_url, image_path)
  if show_historical
    historical_image_path = "#{path}/#{image_uuid}#{@graphite_historical_lookup}.png"
    if uri.query =~ /&from/
      # Replace the &from value.
      uri.query.gsub!(/from=([^&]*)/, "from=#{@graphite_historical_lookup}")
    else
      # Set the &from value.
      uri.query = "#{uri.query}&from=#{@graphite_historical_lookup}"
    end
    historical_url = uri.to_s
    download_image(historical_url, historical_image_path)
  end
  return @image_paths
end