From 3ec3d82da0592576fb48318e62df25f62b542ae1 Mon Sep 17 00:00:00 2001 From: Conor Branagan Date: Fri, 14 Nov 2014 15:31:37 -0800 Subject: [PATCH] Add CouchDB service check. Pretty much the same as how it works in Couchbase with a test. --- checks.d/couch.py | 17 ++++++++++++++++- tests/test_couch.py | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/checks.d/couch.py b/checks.d/couch.py index 1750dc3f5e..c6f627da0b 100644 --- a/checks.d/couch.py +++ b/checks.d/couch.py @@ -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', {}) @@ -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: diff --git a/tests/test_couch.py b/tests/test_couch.py index f0b7941c56..9aa9490b50 100644 --- a/tests/test_couch.py +++ b/tests/test_couch.py @@ -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', @@ -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) +