Skip to content

Commit

Permalink
added support for http basic auth
Browse files Browse the repository at this point in the history
  • Loading branch information
tinomen committed Oct 31, 2011
1 parent 976489f commit 7d6c6b2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
21 changes: 18 additions & 3 deletions couchdb_overall_monitoring/couchdb_overall_monitoring.rb
Expand Up @@ -7,6 +7,12 @@ class CouchDBOverallMonitoring< Scout::Plugin
couchdb_host:
notes: The host that CouchDB is running on
default: http://127.0.0.1
couchdb_user:
notes: The CouchDB http basic authentication user
default: admin
couchdb_pwd:
notes: The CouchDB http basic authentication password
default: secret
EOS

needs 'open-uri', 'json'
Expand All @@ -29,13 +35,22 @@ def build_report
if option(:couchdb_host).nil? or option(:couchdb_port).nil?
return error("Please provide the host & port", "The Couch DB Host and Port is required.\n\nCouch DB Host: #{option(:couchdb_host)}\n\nCouch DB Port: #{option(:couchdb_port)}")
end
options = {}
@base_url = "#{option(:couchdb_host)}:#{option(:couchdb_port)}/"
@response = JSON.parse(open(@base_url + "_stats").read)
options[:http_basic_authentication] = [option(:couchdb_user), option(:couchdb_pwd)] if option(:couchdb_user)
@response = JSON.parse(open(@base_url + "_stats", options).read)

report_metrics
report_httpd_status_codes
rescue OpenURI::HTTPError
error("Stats URL not found","Please ensure the base url for Couch DB Stats is correct. Current URL: \n\n#{@base_url}")
rescue OpenURI::HTTPError => e
if e.message.include? "401 Unauthorized"
status = "Stats URL access denied"
msg = "Please ensure the http basic auth user and password is correct. Current URL: \n\n#{@base_url}"
else
status = "Stats URL not found"
msg = "Please ensure the base url for Couch DB Stats is correct. Current URL: \n\n#{@base_url}"
end
error(status,msg)
rescue SocketError
error("Hostname is invalid","Please ensure the Couch DB Host is correct - the host could not be found. Current URL: \n\n#{@base_url}")
end
Expand Down
18 changes: 18 additions & 0 deletions couchdb_overall_monitoring/test.rb
Expand Up @@ -48,6 +48,24 @@ def test_normal_run
end # Timecop.travel
end

def test_basic_auth
FakeWeb.clean_registry
FakeWeb.register_uri(:get, "http://testuser:password@127.0.0.1:5984/_stats", :body => FIXTURES[:initial])

@plugin=CouchDBOverallMonitoring.new(nil,{},{:couchdb_host=>'http://127.0.0.1',:couchdb_port=>'5984', :couchdb_user => 'testuser', :couchdb_pwd => 'password'})
res = @plugin.run()
assert res[:errors].empty?
assert_equal res[:memory]["_counter_database_reads"][:value], 100
end

def test_not_authenticated
FakeWeb.clean_registry
FakeWeb.register_uri(:get, "http://127.0.0.1:5984/_stats", :body => "Unauthorized", :status => ["401", "Unauthorized"])
@plugin=CouchDBOverallMonitoring.new(nil,{},{:couchdb_host=>'http://127.0.0.1',:couchdb_port=>'5984'})
res = @plugin.run()
assert res[:errors][0][:subject] == "Stats URL access denied"
end

def test_not_found
CouchDBOverallMonitoring::METRICS.each do |metric|
uri="http://127.0.0.1:5984/_stats"
Expand Down

0 comments on commit 7d6c6b2

Please sign in to comment.