Skip to content

Commit

Permalink
Added basic auth support
Browse files Browse the repository at this point in the history
  • Loading branch information
itsderek23 committed Apr 7, 2010
1 parent 0704d34 commit 4414e94
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 13 deletions.
11 changes: 9 additions & 2 deletions haproxy_stats/haproxy_stats.rb
Expand Up @@ -15,23 +15,30 @@ class HaproxyStats < Scout::Plugin
name: URI
notes: URI of the haproxy CSV stats url. See the 'CSV Export' link on your haproxy stats page
default: http://yourdomain.com/;csv
user:
notes: If protected under basic authentication provide the user name
password:
notes: If protected under basic authentication provide the password.
EOS

def build_report

if option(:uri) == ''
if option(:uri).nil?
return error('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

begin
FasterCSV.parse(open(option(:uri)), :headers => true) do |row|
FasterCSV.parse(open(option(:uri),:http_basic_authentication => [option(:user),option(:password)]), :headers => true) do |row|
if row["svname"] == 'FRONTEND' || row["svname"] == 'BACKEND'
name = row["# pxname"] + ' ' + row["svname"].downcase
report "#{name} Current Sessions" => row["scur"]
report "#{name} Current Queue" => row["qcur"] unless row["qcur"].nil?
report "#{name} Requests / second" => row["rate"] unless row["rate"].nil?
end
end
rescue OpenURI::HTTPError
raise if $!.message != '404 Not Found'
return error("Unable to find the stats page", "The stats page could not be found at: #{option(:uri)}. If you are providing a user and password please ensure those are correct.")
rescue FasterCSV::MalformedCSVError
return error('Error accessing stats page', "The plugin encountered an error attempting to access the stats page (in CSV format) at: #{option(:uri)}. The exception: #{$!.message}\n#{$!.backtrace}")
end
Expand Down
60 changes: 49 additions & 11 deletions haproxy_stats/test.rb
Expand Up @@ -15,17 +15,7 @@ def test_normal_run
FakeWeb.register_uri(:get, uri, :body => FIXTURES[:valid])
@plugin=HaproxyStats.new(nil,{},{:uri=>uri})
res = @plugin.run()
assert_equal [{"http-in frontend Current Sessions"=>"1"},
{"http-in frontend Requests / second"=>"1"},
{"www backend Current Sessions"=>"0"},
{"www backend Current Queue"=>"0"},
{"www backend Requests / second"=>"0"},
{"git backend Current Sessions"=>"0"},
{"git backend Current Queue"=>"0"},
{"git backend Requests / second"=>"0"},
{"demo backend Current Sessions"=>"1"},
{"demo backend Current Queue"=>"0"},
{"demo backend Requests / second"=>"1"}], res[:reports]
assert_valid_report(res)
end

def test_invalid_csv
Expand All @@ -38,8 +28,56 @@ def test_invalid_csv
assert_equal 1, res[:errors].size
assert_equal "Error accessing stats page", res[:errors].first[:subject]
end

def test_blank_uri
@plugin=HaproxyStats.new(nil,{},{:uri=>nil})

res = @plugin.run()
assert_equal 0, res[:reports].size
assert_equal 1, res[:errors].size
assert_equal "URI to HAProxy Stats Required", res[:errors].first[:subject]
end

def test_invalid_basic_auth
uri_no_auth = "http://example.com/secret"
uri = "http://user:pass@example.com/secret"
FakeWeb.register_uri(:get, uri_no_auth, :body => "Unauthorized", :status => ["401", "Unauthorized"])
FakeWeb.register_uri(:get, uri, :body => FIXTURES[:valid])

@plugin=HaproxyStats.new(nil,{},{:uri=>uri_no_auth, :user => 'user', :password => 'invalid'})
res = @plugin.run()
assert_equal 0, res[:reports].size
assert_equal 1, res[:errors].size
assert_equal "Unable to find the stats page", res[:errors].first[:subject]
end

def test_valid_basic_auth
uri_no_auth = "http://example.com/secret"
uri = "http://user:pass@example.com/secret"
FakeWeb.register_uri(:get, uri_no_auth, :body => "Unauthorized", :status => ["401", "Unauthorized"])
FakeWeb.register_uri(:get, uri, :body => FIXTURES[:valid])

@plugin=HaproxyStats.new(nil,{},{:uri=>uri_no_auth, :user => 'user', :password => 'pass'})
res = @plugin.run()
assert_valid_report(res)
end

### helper methods

def assert_valid_report(res)
assert_equal [{"http-in frontend Current Sessions"=>"1"},
{"http-in frontend Requests / second"=>"1"},
{"www backend Current Sessions"=>"0"},
{"www backend Current Queue"=>"0"},
{"www backend Requests / second"=>"0"},
{"git backend Current Sessions"=>"0"},
{"git backend Current Queue"=>"0"},
{"git backend Requests / second"=>"0"},
{"demo backend Current Sessions"=>"1"},
{"demo backend Current Queue"=>"0"},
{"demo backend Requests / second"=>"1"}], res[:reports]
end

FIXTURES=YAML.load(<<-EOS)
:valid: |
# pxname,svname,qcur,qmax,scur,smax,slim,stot,bin,bout,dreq,dresp,ereq,econ,eresp,wretr,wredis,status,weight,act,bck,chkfail,chkdown,lastchg,downtime,qlimit,pid,iid,sid,throttle,lbtot,tracked,type,rate,rate_lim,rate_max,check_status,check_code,check_duration,hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,req_rate,req_rate_max,req_tot,cli_abrt,srv_abrt,
Expand Down

0 comments on commit 4414e94

Please sign in to comment.