class SensuPluginsGraphite::GraphiteProxy::Proxy
Attributes
config[RW]
Public Class Methods
new(config)
click to toggle source
# File lib/sensu-plugins-graphite/graphite_proxy/proxy.rb, line 17 def initialize(config) self.config = config end
Public Instance Methods
derive_password(given_opts)
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# 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)
click to toggle source
# 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) if !raw['datapoints'].first.nil? && !raw['datapoints'].last.nil? && raw['datapoints'].size.to_f > 0 start = raw['datapoints'].first.last dend = raw['datapoints'].last.last rdpsize = raw['datapoints'].size.to_f step = ((dend - start) / rdpsize).ceil # moved it here to avoid division by 0. else # Filling up some dummy variables start = 0 dend = 0 step = 0 end { 'target' => target, 'data' => data, 'start' => start, 'end' => dend, 'step' => step } end
request_auth_options(given_opts)
click to toggle source
# File lib/sensu-plugins-graphite/graphite_proxy/proxy.rb, line 31 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!()
click to toggle source
grab data from graphite
# File lib/sensu-plugins-graphite/graphite_proxy/proxy.rb, line 85 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 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