Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Gearman] Improve integration #1476

Merged
merged 2 commits into from
Mar 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions checks.d/gearmand.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,20 @@ def check(self, instance):
self.log.debug("Gearman check start")

host, port, tags = self._get_conf(instance)
service_check_tags = ["server:{0}".format(host),
"port:{0}".format(port)]

client = self._get_client(host, port)
self.log.debug("Connected to gearman")

tags += service_check_tags

try:
self._get_metrics(client, tags)
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.OK,
message="Connection to %s:%s succeeded." % (host, port))
message="Connection to %s:%s succeeded." % (host, port),
tags=service_check_tags)
except Exception as e:
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL,
message=str(e))
message=str(e), tags=service_check_tags)
raise
75 changes: 39 additions & 36 deletions tests/test_gearman.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,55 @@
import unittest
from nose.plugins.attrib import attr
from tests.common import load_check
# Agent
from checks import AgentCheck
from tests.common import AgentCheckTest

# 3rd party
from nose.plugins.attrib import attr


@attr(requires='gearman')
class GearmanTestCase(unittest.TestCase):
class GearmanTestCase(AgentCheckTest):
CHECK_NAME = "gearmand"

def test_metrics(self):
tags = ['first_tag', 'second_tag']
service_checks_tags = ['server:127.0.0.1', 'port:4730']
config = {
'instances': [{
'tags': ['first_tag', 'second_tag']
'tags': tags
}]
}
agentConfig = {
'version': '0.1',
'api_key': 'toto'
}

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)
tags += service_checks_tags
self.run_check(config)
self.assertMetric('gearman.unique_tasks', value=0.0, tags=tags, count=1)
self.assertMetric('gearman.running', value=0.0, tags=tags, count=1)
self.assertMetric('gearman.queued', value=0.0, tags=tags, count=1)
self.assertMetric('gearman.workers', value=0.0, tags=tags, count=1)

self.assertServiceCheck("gearman.can_connect", status=AgentCheck.OK,
tags=service_checks_tags, count=1)
self.coverage_report()


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(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)

self.assertRaises(Exception, self.run_check, config)
service_checks_tags_ok = ['server:127.0.0.1', 'port:4730']
service_checks_tags_not_ok = ['server:127.0.0.1', 'port:4731']

tags = service_checks_tags_ok

self.assertMetric('gearman.unique_tasks', value=0.0, tags=tags, count=1)
self.assertMetric('gearman.running', value=0.0, tags=tags, count=1)
self.assertMetric('gearman.queued', value=0.0, tags=tags, count=1)
self.assertMetric('gearman.workers', value=0.0, tags=tags, count=1)
self.assertServiceCheck("gearman.can_connect", status=AgentCheck.OK,
tags=service_checks_tags_ok, count=1)
self.assertServiceCheck("gearman.can_connect", status=AgentCheck.CRITICAL,
tags=service_checks_tags_not_ok, count=1)

self.coverage_report()