diff --git a/couchdb_overall_monitoring/couchdb_overall_monitoring.rb b/couchdb_overall_monitoring/couchdb_overall_monitoring.rb index 8f5a0b6c..9717ab80 100644 --- a/couchdb_overall_monitoring/couchdb_overall_monitoring.rb +++ b/couchdb_overall_monitoring/couchdb_overall_monitoring.rb @@ -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' @@ -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 diff --git a/couchdb_overall_monitoring/test.rb b/couchdb_overall_monitoring/test.rb index ce39258d..68f0b38b 100644 --- a/couchdb_overall_monitoring/test.rb +++ b/couchdb_overall_monitoring/test.rb @@ -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"