Example #1
0
def discovery_redis(*args):
    """
    find redis instance
    @return: [(ip, prot, passwd)]
    """

    redises = []
    redis_conf_path_root = '/data'
    redis_conf_file_name = 'redis.conf'
    for redis_process in [x
                          for x in psutil.process_iter()
                          if len(x.cmdline()) > 0 and (
                os.path.basename(x.exe()) == BINNAME or os.path.basename(x.exe()) == CODISBINNAME)]:
        try:
            redis_ip, redis_port = sorted([laddr.laddr
                                           for laddr in redis_process.connections()
                                           if laddr.status == 'LISTEN'])[0]
        except:
            continue

        redis_passwd = ''
        config_files = []

        if os.path.isdir(redis_process.cwd()):
            cwd_try = os.path.join(redis_process.cwd(), redis_conf_file_name)
            if os.path.exists(cwd_try):
                config_files.append(cwd_try)
            else:
                for root_dir, dirs, files in os.walk(os.path.dirname(redis_process.cwd())):
                    if 'redis.conf' in files:
                        config_files.append(os.path.join(root_dir, redis_conf_file_name))
        elif len(redis_process.cmdline()) > 1 and os.path.isfile(redis_process.cmdline()[1]):
            config_files.append(redis_process.cmdline()[1])
        else:
            for root_dir, dirs, files in os.walk(redis_conf_path_root):
                if 'redis.conf' in files:
                    config_files.append(os.path.join(root_dir, redis_conf_file_name))
        for config_file in config_files:
            try:
                with open(config_file, 'r') as f:
                    passwd = None
                    port = None
                    for line in f.readlines():
                        if re.search('^requirepass', line):
                            passwd = line.split()[1]
                        if re.search('^port', line):
                            port = line.split()[1]
                if passwd and port and str(redis_port) == port:
                    redis_passwd = Monitor.encode_password(passwd)
                    break
            except:
                pass
        if redis_ip == '0.0.0.0' or redis_ip == '::' or redis_ip == '127.0.0.1' or redis_ip == '':
            redis_ip = Monitor.get_local_ip()
        if [redis_ip, redis_port, redis_passwd] not in redises:
            redises.append([redis_ip, redis_port, redis_passwd])
    return redises
Example #2
0
def discovery_redis():
    """
    find redis instance
    @return: [(ip, prot, passwd)]
    """

    redises = []
    redis_conf_path_root = "/data"
    redis_conf_file_name = "redis.conf"
    for redis_process in [
        x for x in psutil.process_iter() if len(x.cmdline()) > 0 and os.path.basename(x.exe()) == BINNAME
    ]:
        try:
            redis_ip, redis_port = sorted(
                [laddr.laddr for laddr in redis_process.get_connections() if laddr.status == "LISTEN"]
            )[0]
        except:
            continue

        redis_passwd = ""
        config_files = []

        if os.path.isdir(redis_process.getcwd()):
            cwd_try = os.path.join(redis_process.getcwd(), redis_conf_file_name)
            if os.path.exists(cwd_try):
                config_files.append(cwd_try)
            else:
                for root_dir, dirs, files in os.walk(os.path.dirname(redis_process.getcwd())):
                    if "redis.conf" in files:
                        config_files.append(os.path.join(root_dir, redis_conf_file_name))
        elif len(redis_process.cmdline()) > 1 and os.path.isfile(redis_process.cmdline()[1]):
            config_files.append(redis_process.cmdline()[1])
        else:
            for root_dir, dirs, files in os.walk(redis_conf_path_root):
                if "redis.conf" in files:
                    config_files.append(os.path.join(root_dir, redis_conf_file_name))
        for config_file in config_files:
            try:
                with open(config_file, "r") as f:
                    passwd = None
                    port = None
                    for line in f.readlines():
                        if re.search("^requirepass", line):
                            passwd = line.split()[1]
                        if re.search("^port", line):
                            port = line.split()[1]
                if passwd and port and str(redis_port) == port:
                    redis_passwd = Monitor.encode_password(passwd)
                    break
            except:
                pass
        if redis_ip == "0.0.0.0":
            redis_ip = Monitor.get_local_ip()
        redises.append([redis_ip, redis_port, redis_passwd])
    return redises