import re

from checks import *

class Redis(Check):
    db_key_pattern = re.compile(r'^db\d+')
    subkeys = ['keys', 'expires']
    
    def __init__(self, logger):
        Check.__init__(self, logger)
        try:
            import redis
        except ImportError, e:
            self.client = None
        else:
            self.client = redis.Redis()
        
        self.prev_total_commands = None
        
    def check(self, agentConfig):
        if self.client is not None:
            try:
                info = self.client.info()
                output = {
                    'redis.net.clients':  info['connected_clients'],
                    'redis.net.slaves':   info['connected_slaves'],
                    'redis.net.blocked':  info['blocked_clients'],
                    'redis.mem.used':     info['used_memory'],
                }
            
                # Subtract 1 to correct for the agent's INFO command to 
                totall_commands = info['total_commands_processed'] - 1 
            
                if self.prev_total_commands is not None:
                    output['redis.net.commands'] = info['total_commands_processed'] - self.prev_total_commands
            
                self.prev_total_commands = info['total_commands_processed']
            
                for key in info.keys():
                    if self.db_key_pattern.match(key):
                        for subkey in self.subkeys:
                            output['.'.join([key, subkey])] = info[key].get(subkey, -1)
                return output
            except:
                self.logger.exception("Cannot get Redis stats")
                return False
        else:
            return False

@@ -405,24 +405,568 @@
 ne%0A        %0A
+    def _parse_dict_string(self, string, key, default):%0A        %22%22%22Take from a more recent redis.py, parse_info%22%22%22%0A        try:%0A            for item in string.split(','):%0A                k, v = item.rsplit('=', 1)%0A                if k == key:%0A                    try:%0A                        return int(v)%0A                    except ValueError:%0A                        return v%0A            return default%0A        except Exception, e:%0A            self.logger.exception(%22Cannot parse dictionnary string: %25s%22 %25 string)%0A            return default%0A%0A
     def chec
@@ -987,16 +987,16 @@
 onfig):%0A
-
         
@@ -2014,67 +2014,579 @@
     
-output%5B'.'.join(%5Bkey, subkey%5D)%5D = info%5Bkey%5D.get(subkey, -1)
+# Old redis module on ubuntu 10.04 (python-redis 0.6.1) does not%0A                            # returns a dict for those key but a string: keys=3,expires=0%0A                            # Try to parse it (see lighthouse #46)            %0A                            val = - 1%0A                            try:%0A                                val = info%5Bkey%5D.get(subkey, -1)%0A                            except AttributeError:%0A                                val = self._parse_dict_string(info%5Bkey%5D, subkey, -1)%0A%0A                            output%5B'.'.join(%5Bkey, subkey%5D)%5D = val
 %0A   

Handle old python-redis module (ie, ubuntu 10.04 with python-redis 0.6.1)