Skip to content

Commit

Permalink
Merge pull request #1201 from DataDog/conor/couchdb-service-check
Browse files Browse the repository at this point in the history
Add CouchDB service check.
  • Loading branch information
LeoCavaille committed Dec 10, 2014
2 parents 28d3119 + 6a6942c commit 700cf22
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
19 changes: 18 additions & 1 deletion checks.d/couch.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class CouchDb(AgentCheck):
"""

SOURCE_TYPE_NAME = 'couchdb'
SERVICE_CHECK_NAME = 'couchdb.can_connect'

def _create_metric(self, data, tags=None):
overall_stats = data.get('stats', {})
Expand Down Expand Up @@ -61,7 +62,23 @@ def get_data(self, server, instance):
endpoint = '/_stats/'

url = '%s%s' % (server, endpoint)
overall_stats = self._get_stats(url, instance)

# Fetch initial stats and capture a service check based on response.
service_check_tags = ['instance:%s' % server]
try:
overall_stats = self._get_stats(url, instance)
except urllib2.URLError as e:
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL,
tags=service_check_tags, message=e.reason)
raise
except Exception as e:
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL,
tags=service_check_tags, message=str(e))
raise
else:
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.OK,
tags=service_check_tags,
message='Connection to %s was successful' % url)

# No overall stats? bail out now
if overall_stats is None:
Expand Down
31 changes: 29 additions & 2 deletions tests/test_couch.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import unittest
from tests.common import load_check
from nose.plugins.attrib import attr
from checks import AgentCheck

@attr(requires='couchdb')
class CouchDBTestCase(unittest.TestCase):

def testMetrics(self):

def test_metrics(self):
config = {
'instances': [{
'server': 'http://localhost:5984',
Expand All @@ -25,3 +25,30 @@ def testMetrics(self):
self.assertTrue(type(metrics) == type([]), metrics)
self.assertTrue(len(metrics) > 3)
self.assertTrue(len([k for k in metrics if "instance:http://localhost:5984" in k[3]['tags']]) > 3)

def test_service_checks(self):
config = {
'instances': [
{'server': 'http://localhost:5984'},
{'server': 'http://localhost:5985'}]
}
agentConfig = {
'version': '0.1',
'api_key': 'toto'
}

self.check = load_check('couch', config, agentConfig)
self.check.check(config['instances'][0])
self.assertRaises(Exception, self.check.check, config['instances'][1])

service_checks = self.check.get_service_checks()
self.assertEqual(len(service_checks), 2)

ok_svc_check = service_checks[0]
self.assertEqual(ok_svc_check['check'], self.check.SERVICE_CHECK_NAME)
self.assertEqual(ok_svc_check['status'], AgentCheck.OK)

cr_svc_check = service_checks[1]
self.assertEqual(cr_svc_check['check'], self.check.SERVICE_CHECK_NAME)
self.assertEqual(cr_svc_check['status'], AgentCheck.CRITICAL)

0 comments on commit 700cf22

Please sign in to comment.