diff --git a/checks.d/gearmand.py b/checks.d/gearmand.py index cae486a7c8..2d9586991e 100644 --- a/checks.d/gearmand.py +++ b/checks.d/gearmand.py @@ -5,6 +5,7 @@ import gearman class Gearman(AgentCheck): + SERVICE_CHECK_NAME = 'gearman.can_connect' def get_library_versions(self): return {"gearman": gearman.__version__} @@ -57,4 +58,12 @@ def check(self, instance): host, port, tags = self._get_conf(instance) client = self._get_client(host, port) self.log.debug("Connected to gearman") - self._get_metrics(client, tags) + + try: + self._get_metrics(client, tags) + self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.OK, + message="Connection to %s:%s succeeded." % (host, port)) + except Exception as e: + self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL, + message=str(e)) + raise diff --git a/tests/test_gearman.py b/tests/test_gearman.py index 2f5ad3be69..5aebfa1ba8 100644 --- a/tests/test_gearman.py +++ b/tests/test_gearman.py @@ -1,12 +1,12 @@ import unittest from nose.plugins.attrib import attr from tests.common import load_check +from checks import AgentCheck @attr(requires='gearman') class GearmanTestCase(unittest.TestCase): - def testMetrics(self): - + def test_metrics(self): config = { 'instances': [{ 'tags': ['first_tag', 'second_tag'] @@ -18,10 +18,35 @@ def testMetrics(self): } self.check = load_check('gearmand', config, agentConfig) - self.check.check(config['instances'][0]) metrics = self.check.get_metrics() self.assertTrue(type(metrics) == type([]), metrics) self.assertTrue(len(metrics) == 4) self.assertTrue(len([k for k in metrics if "second_tag" in k[3]['tags']]) == 4) + + def test_service_checks(self): + config = { + 'instances': [ + {'host': '127.0.0.1', 'port': 4730}, + {'host': '127.0.0.1', 'port': 4731}] + } + agentConfig = { + 'version': '0.1', + 'api_key': 'toto' + } + + self.check = load_check('gearmand', config, agentConfig) + self.check.check(config['instances'][0]) + self.assertRaises(self.check.check, Exception, 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)