Class: NewRelicHaproxyAgent::Agent

Inherits:
NewRelic::Plugin::Agent::Base
  • Object
show all
Defined in:
lib/newrelic_haproxy_agent.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#identObject (readonly)

Returns the value of attribute ident.



27
28
29
# File 'lib/newrelic_haproxy_agent.rb', line 27

def ident
  @ident
end

Instance Method Details

#poll_cycleObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
83
84
85
86
87
88
89
90
91
# File 'lib/newrelic_haproxy_agent.rb', line 38

def poll_cycle
  if uri.nil?
    raise("URI to HAProxy Stats Required It looks like the URI to the HAProxy stats page (in csv format) hasn't been provided. Please enter this URI in the plugin settings.")
  end
  if proxy_type
    if proxy_type =~ /frontend|backend/i
     @proxy_type = proxy_type.upcase
    end
  end
  found_proxies = []
  possible_proxies = []
  begin
    FasterCSV.parse(open(uri, :http_basic_authentication => [user, password]), :headers => true) do |row|
      next if proxy_type and proxy_type != row["svname"] # ensure the proxy type (if provided) matches
      possible_proxies << row["# pxname"] # used in error message
      next unless proxy.to_s.strip.downcase == row["# pxname"].downcase # ensure the proxy name matches
      # if multiple proxies have the same name, we don't know which to report on.
      if found_proxies.include?(row["# pxname"])
        raise("Multiple proxies have the name '#{proxy}'. Please specify the proxy type (ex: BACKEND or FRONTEND) in the plugin's settings.")
      end
      found_proxies << row["# pxname"]
      report_metric "Requests", "Requests/Minute",             (@requests.process(row['stot'].to_i) || 0) * 60
      report_metric "Errors/Request", "Errors/Minute",         (@errors_req.process(row['ereq'].to_i) || 0) * 60
      report_metric "Errors/Connection", "Errors/Minute",      (@errors_conn.process(row['econ'].to_i) || 0) * 60
      report_metric "Errors/Response", "Errors/Minute",        (@errors_resp.process(row['eresp'].to_i) || 0) * 60

      report_metric "Bytes/Received", "Bytes/Seconds",          @bytes_in.process(row['bin'].to_i)
      report_metric "Bytes/Sent", "Bytes/Seconds",              @bytes_out.process(row['bout'].to_i)

      report_metric "Sessions/Active", "Sessions",              row['scur']
      report_metric "Sessions/Queued", "Sessions",              row['qcur']
      report_metric "Servers/Active", "Servers",                row['act']
      report_metric "Servers/Backup", "Servers",                row['bck']
      report_metric "ProxyUp", "Status",                        %w(UP OPEN).find {|s| s == row['status']} ? 1 : 0

    end # FasterCSV.parse
  rescue OpenURI::HTTPError
    if $!.message == '401 Unauthorized'
      raise("Authentication Failed. Unable to access the stats page at #{uri} with the username '#{user}' and provided password. Please ensure the username, password, and URI are correct.")
    elsif $!.message != '404 Not Found'
      raise("Unable to find the stats page. The stats page could not be found at: #{uri}.")
    else
      raise
    end
  rescue FasterCSV::MalformedCSVError
    raise("Unable to access stats page. The plugin encountered an error attempting to access the stats page (in CSV format) at: #{uri}. The exception: #{$!.message}\n#{$!.backtrace}")
  end
  if proxy.nil?
    raise("Proxy name required. The name of the proxy to monitor must be provided in the plugin settings. The possible proxies to monitor: #{possible_proxies.join(', ')}")
  elsif found_proxies.empty?
    raise("Proxy not found. The proxy '#{proxy}' #{proxy_type ? "w/proxy type [#{proxy_type}]" : nil} was not found. The possible proxies #{proxy_type ? "for this proxy type" : nil} to monitor: #{possible_proxies.join(', ')}")
  end

end

#setup_metricsObject



29
30
31
32
33
34
35
36
# File 'lib/newrelic_haproxy_agent.rb', line 29

def setup_metrics
  @requests=NewRelic::Processor::EpochCounter.new
  @errors_req=NewRelic::Processor::EpochCounter.new
  @errors_conn=NewRelic::Processor::EpochCounter.new
  @errors_resp=NewRelic::Processor::EpochCounter.new
  @bytes_in=NewRelic::Processor::EpochCounter.new
  @bytes_out=NewRelic::Processor::EpochCounter.new
end