# Path: collectors/lib/utils.py
# PY3 = sys.version_info[0] > 2
# USER = "nobody"
# def drop_privileges(user=USER):
# def is_sockfile(path):
# def err(msg):
#     def is_numeric(value):
#     def is_numeric(value):
# 
# Path: collectors/etc/redis_stats_conf.py
# def get_config():


import re
import subprocess
import sys
import time
    import redis
from collectors.lib import utils
from collectors.etc import redis_stats_conf

def scan_for_instances():
    """Use netstat to find instances of Redis listening on the local machine, then
    figure out what configuration file they're using to name the cluster."""

    out = {}
    tcre = re.compile(r"^\s*#\s*tcollector.(\w+)\s*=\s*(.+)$")

    ns_proc = subprocess.Popen(["netstat", "-tnlp"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, _ = ns_proc.communicate()
    if ns_proc.returncode != 0:
        print("failed to find instances %r" % ns_proc.returncode, file=sys.stderr)
        return {}

    for line in stdout.split("\n"):
        if not (line and 'redis-server' in line):
            continue
        pid = int(line.split()[6].split("/")[0])
        port = int(line.split()[3].split(":")[-1])

        # now we have to get the command line.  we look in the redis config file for
        # a special line that tells us what cluster this is.  else we default to using
        # the port number which should work.
        cluster = "port-%d" % port
        try:
            f = open("/proc/%d/cmdline" % pid)
            cfg = f.readline().split("\0")[-2]
            f.close()

            f = open(cfg)

# Path: collectors/lib/utils.py
# PY3 = sys.version_info[0] > 2
# USER = "nobody"
# def drop_privileges(user=USER):
# def is_sockfile(path):
# def err(msg):
#     def is_numeric(value):
#     def is_numeric(value):
# 
# Path: collectors/etc/redis_stats_conf.py
# def get_config():


# Path: collectors/0/redis_stats.py
import re
import subprocess
import sys
import time
    import redis
from collectors.lib import utils
from collectors.etc import redis_stats_conf


def scan_for_instances():
    """Use netstat to find instances of Redis listening on the local machine, then
    figure out what configuration file they're using to name the cluster."""

    out = {}
    tcre = re.compile(r"^\s*#\s*tcollector.(\w+)\s*=\s*(.+)$")

    ns_proc = subprocess.Popen(["netstat", "-tnlp"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, _ = ns_proc.communicate()
    if ns_proc.returncode != 0:
        print("failed to find instances %r" % ns_proc.returncode, file=sys.stderr)
        return {}

    for line in stdout.split("\n"):
        if not (line and 'redis-server' in line):
            continue
        pid = int(line.split()[6].split("/")[0])
        port = int(line.split()[3].split(":")[-1])

        # now we have to get the command line.  we look in the redis config file for
        # a special line that tells us what cluster this is.  else we default to using
        # the port number which should work.
        cluster = "port-%d" % port
        try:
            f = open("/proc/%d/cmdline" % pid)
            cfg = f.readline().split("\0")[-2]
            f.close()

            f = open(cfg)

            for cfgline in f: