Class: SensuPluginsGraphite::GraphiteProxy::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/sensu-plugins-graphite/graphite_proxy/proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Proxy

Returns a new instance of Proxy.


14
15
16
# File 'lib/sensu-plugins-graphite/graphite_proxy/proxy.rb', line 14

def initialize(config)
  self.config = config
end

Instance Attribute Details

#configObject

Returns the value of attribute config


12
13
14
# File 'lib/sensu-plugins-graphite/graphite_proxy/proxy.rb', line 12

def config
  @config
end

Instance Method Details

#derive_password(given_opts) ⇒ Object


43
44
45
46
47
48
49
# File 'lib/sensu-plugins-graphite/graphite_proxy/proxy.rb', line 43

def derive_password(given_opts)
  if given_opts[:passfile]
    File.open(given_opts[:passfile]).readline
  elsif given_opts[:password]
    given_opts[:password]
  end
end

#format_output(data) ⇒ Object


51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sensu-plugins-graphite/graphite_proxy/proxy.rb', line 51

def format_output(data)
  output = {}

  data.each do |raw|
    unless raw['datapoints'].empty?
      line = output_line(raw)
      output[line['target']] = line
    end
  end
  output
end

#formatted_targetObject


18
19
20
21
22
23
24
25
26
# File 'lib/sensu-plugins-graphite/graphite_proxy/proxy.rb', line 18

def formatted_target
  if config[:target].include?('$')
    require 'socket'
    formatted = Socket.gethostbyname(Socket.gethostname).first.gsub('.', config[:hostname_sub] || '_')
    config[:target].gsub('$', formatted)
  else
    URI.escape config[:target]
  end
end

#output_line(raw) ⇒ Object


63
64
65
66
67
68
69
70
71
72
# File 'lib/sensu-plugins-graphite/graphite_proxy/proxy.rb', line 63

def output_line(raw)
  raw['datapoints'].delete_if { |v| v.first.nil? }
  target = raw['target']
  data = raw['datapoints'].map(&:first)
  start = raw['datapoints'].first.last
  dend = raw['datapoints'].last.last
  step = ((dend - start) / raw['datapoints'].size.to_f).ceil

  { 'target' => target, 'data' => data, 'start' => start, 'end' => dend, 'step' => step }
end

#request_auth_options(given_opts) ⇒ Object


28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sensu-plugins-graphite/graphite_proxy/proxy.rb', line 28

def request_auth_options(given_opts)
  url_opts = {}

  url_opts[:ssl_verify_mode] = OpenSSL::SSL::VERIFY_NONE if given_opts[:no_ssl_verify]

  if given_opts[:username]
    pass = derive_password(given_opts)
    url_opts[:http_basic_authentication] = [given_opts[:username], pass.chomp]
  end # we don't have both username and password trying without

  url_opts['Authorization'] = "Bearer #{given_opts[:auth]}" if given_opts[:auth]

  url_opts
end

#retrieve_data!Object

grab data from graphite


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sensu-plugins-graphite/graphite_proxy/proxy.rb', line 75

def retrieve_data!
  unless @raw_data
    begin
      unless config[:server].start_with?('https://', 'http://')
        config[:server].prepend('http://')
      end

      url = "#{config[:server]}/render?format=json&target=#{formatted_target}&from=#{config[:from]}"

      handle = open(url, request_auth_options(config))

      @raw_data = handle.gets
      json_data = JSON.parse(@raw_data)
      format_output(json_data)
    rescue OpenURI::HTTPError
      raise ProxyError, 'Failed to connect to Graphite server'
    rescue NoMethodError, JSON::ParserError
      raise ProxyError, 'No data for time period and/or target'
    rescue Errno::ECONNREFUSED
      raise ProxyError, 'Connection refused when connecting to Graphite server'
    rescue Errno::ECONNRESET
      raise ProxyError, 'Connection reset by peer when connecting to Graphite server'
    rescue EOFError
      raise ProxyError, 'End of file error when reading from Graphite server'
    rescue => e
      raise ProxyError, "An unknown error occurred: #{e.inspect}"
    end
  end
end