Skip to content

Commit

Permalink
Explicitly support unix socket in redis check configuration
Browse files Browse the repository at this point in the history
Fix #730
  • Loading branch information
remh committed Jan 23, 2014
1 parent 6d95c64 commit 6a641b5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
25 changes: 19 additions & 6 deletions checks.d/redisdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,15 @@ def _parse_dict_string(self, string, key, default):
self.log.exception("Cannot parse dictionary string: %s" % string)
return default

def _generate_instance_key(self, instance):
if 'unix_socket_path' in instance:
return (instance.get('unix_socket_path'), instance.get('db'))
else:
return (instance.get('host'), instance.get('port'), instance.get('db'))

def _get_conn(self, instance):
import redis
key = (instance.get('host'), instance.get('port'), instance.get('db'))
key = self._generate_instance_key(instance)
if key not in self.connections:
try:

Expand All @@ -117,12 +123,17 @@ def _get_conn(self, instance):
def _check_db(self, instance, custom_tags=None):
conn = self._get_conn(instance)
tags = set(custom_tags or [])
tags = sorted(tags.union(["redis_host:%s" % instance.get('host'),
"redis_port:%s" % instance.get('port'),
]))

if 'unix_socket_path' in instance:
tags_to_add = ["unix_socket_path:%s" % instance.get("unix_socket_path")]
else:
tags_to_add = ["redis_host:%s" % instance.get('host'), "redis_port:%s" % instance.get('port')]

if instance.get('db') is not None:
tags.append("db:%s" % instance.get('db'))

tags_to_add.append("db:%s" % instance.get('db'))

tags = sorted(tags.union(tags_to_add))

# Ping the database for info, and track the latency.
start = time.time()
try:
Expand Down Expand Up @@ -169,6 +180,8 @@ def check(self, instance):
except ImportError:
raise Exception('Python Redis Module can not be imported. Please check the installation instruction on the Datadog Website')

if (not "host" in instance or not "port" in instance) and not "unix_socket_path" in instance:
raise Exception("You must specify a host/port couple or a unix_socket_path")
custom_tags = instance.get('tags', [])
self._check_db(instance,custom_tags)

Expand Down
1 change: 1 addition & 0 deletions conf.d/redisdb.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ init_config:
instances:
# - host: localhost
# port: 6379
# unix_socket_path: /var/run/redis/redis.sock # optional, can be used in lieu of host/port
# password: mypassword
# tags:
# - optional_tag1
Expand Down

0 comments on commit 6a641b5

Please sign in to comment.