Skip to content

Commit

Permalink
Add service check for Gearman
Browse files Browse the repository at this point in the history
  • Loading branch information
conorbranagan committed Nov 17, 2014
1 parent a9a26b2 commit 759ea9b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
11 changes: 10 additions & 1 deletion checks.d/gearmand.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import gearman

class Gearman(AgentCheck):
SERVICE_CHECK_NAME = 'gearman.can_connect'

def get_library_versions(self):
return {"gearman": gearman.__version__}
Expand Down Expand Up @@ -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
31 changes: 28 additions & 3 deletions tests/test_gearman.py
Original file line number Diff line number Diff line change
@@ -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']
Expand All @@ -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)

0 comments on commit 759ea9b

Please sign in to comment.