Class: SensuPluginsGraphite::GraphiteProxy::Proxy
- Inherits:
-
Object
- Object
- SensuPluginsGraphite::GraphiteProxy::Proxy
- Defined in:
- lib/sensu-plugins-graphite/graphite_proxy/proxy.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
Instance Method Summary collapse
- #derive_password(given_opts) ⇒ Object
- #format_output(data) ⇒ Object
- #formatted_target ⇒ Object
-
#initialize(config) ⇒ Proxy
constructor
A new instance of Proxy.
- #output_line(raw) ⇒ Object
- #request_auth_options(given_opts) ⇒ Object
-
#retrieve_data! ⇒ Object
grab data from graphite.
Constructor Details
#initialize(config) ⇒ Proxy
Returns a new instance of Proxy.
17 18 19 |
# File 'lib/sensu-plugins-graphite/graphite_proxy/proxy.rb', line 17 def initialize(config) self.config = config end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
15 16 17 |
# File 'lib/sensu-plugins-graphite/graphite_proxy/proxy.rb', line 15 def config @config end |
Instance Method Details
#derive_password(given_opts) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/sensu-plugins-graphite/graphite_proxy/proxy.rb', line 46 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
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/sensu-plugins-graphite/graphite_proxy/proxy.rb', line 54 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_target ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/sensu-plugins-graphite/graphite_proxy/proxy.rb', line 21 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
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/sensu-plugins-graphite/graphite_proxy/proxy.rb', line 66 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
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/sensu-plugins-graphite/graphite_proxy/proxy.rb', line 31 def (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
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 104 105 106 107 108 109 110 |
# File 'lib/sensu-plugins-graphite/graphite_proxy/proxy.rb', line 78 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, (config)) @raw_data = handle.gets if @raw_data == '[]' unknown 'Empty data received from Graphite - metric probably doesn\'t exists' else json_data = JSON.parse(@raw_data) format_output(json_data) end rescue OpenURI::HTTPError => e raise ProxyError.new('Failed to connect to graphite server', exception: e) rescue NoMethodError => e raise ProxyError.new('No data for time period and/or target', exception: e) rescue Errno::ECONNREFUSED => e raise ProxyError.new('Connection refused when connecting to graphite server', exception: e) rescue Errno::ECONNRESET => e raise ProxyError.new('Connection reset by peer when connecting to graphite server', exception: e) rescue EOFError => e raise ProxyError.new('End of file error when reading from graphite server', exception: e) rescue => e raise ProxyError.new("An unknown error occured: #{e.inspect}", exception: e) end end end |