Skip to content

Commit

Permalink
Add CouchDB service check.
Browse files Browse the repository at this point in the history
Pretty much the same as how it works in Couchbase with a test.
  • Loading branch information
conorbranagan committed Nov 14, 2014
1 parent a9a26b2 commit 3ec3d82
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
17 changes: 16 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,21 @@ 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)

# No overall stats? bail out now
if overall_stats is None:
Expand Down
36 changes: 34 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 tetst_metrics(self):
config = {
'instances': [{
'server': 'http://localhost:5984',
Expand All @@ -25,3 +25,35 @@ 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])
try:
self.check.check(config['instances'][1])
except Exception:
pass
else:
raise Exception('CouchDB instance was expected to fail.')

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 3ec3d82

Please sign in to comment.