Ejemplo n.º 1
0
def discovery_tomcat(*args):
    '''
    discovery tomcat instance's host and port
    '''
    result = []
    port = ''
    cmdline = []
    host = Monitor.get_local_ip()
    for i in psutil.process_iter():
        if i.name() == BINNAME:
            pid = int(i.pid)
            cmdline = i.cmdline()

            for opt in cmdline:
                if opt.find('-Dcom.sun.management.jmxremote.port') == 0:
                    monitor_port = opt.strip().split('=')[1]
                if opt.find('-Dcatalina.base') == 0:
                    config_file = opt.strip().split(
                        '=')[1] + '/conf/server.xml'
                    tree = ET.ElementTree(file=config_file)
                    root = tree.getroot()
                    for root_child in root:
                        if root_child.tag == 'Service':
                            for service_child in root_child:
                                if service_child.tag == 'Connector':
                                    listen_port = service_child.attrib['port']

            if monitor_port.isdigit() and listen_port.isdigit():
                result.append([host, listen_port, monitor_port])
    return result
Ejemplo n.º 2
0
def discovery_kingshard(*args):
    '''
    discovery kingshard instance's host and port
    '''
    result = []
    port = ''
    address = ''
    cmdline = []
    host = Monitor.get_local_ip()
    for i in psutil.process_iter():
        if i.name() == BINNAME:
            pid = int(i.pid)
            cmdline = i.cmdline()

            for opt in cmdline:
                if opt.find('-config=') == 0:
                    config_file = opt.strip().split('=')[1]
            if os.path.exists(config_file):
                f = open(config_file, 'r')
                cfg_content = f.read()
                f.close()
            for line in cfg_content.split("\n"):
                if line.find('addr : ') == 0:
                    address = line.split(':')[1].strip()
                    port = line.split(':')[2].strip()
                    if address == '0.0.0.0':
                        address = host
                    result.append([address, port])
    return result
Ejemplo n.º 3
0
def discovery_tomcat(*args):
    '''
    discovery tomcat instance's host and port
    '''
    result = []
    port = ''
    cmdline = []
    host = Monitor.get_local_ip()
    for i in psutil.process_iter():
        if i.name() == BINNAME:
            pid = int(i.pid)
            cmdline = i.cmdline()

            for opt in cmdline:
                if opt.find('-Dcom.sun.management.jmxremote.port') == 0:
                    monitor_port = opt.strip().split('=')[1]
                if opt.find('-Dcatalina.base') == 0:
                    config_file = opt.strip().split('=')[1] + '/conf/server.xml'
                    tree = ET.ElementTree(file=config_file)
                    root = tree.getroot()
                    for root_child in root:
                        if root_child.tag == 'Service':
                            for service_child in root_child:
                                if service_child.tag == 'Connector':
                                    listen_port = service_child.attrib['port']

            if monitor_port.isdigit() and listen_port.isdigit():
                result.append([host, listen_port, monitor_port])
    return result
Ejemplo n.º 4
0
def discovery_etcd(*args):
    '''
    discovery etcd instance's host and port
    '''
    result = []
    port = ''
    address = ''
    cmdline = []
    host = Monitor.get_local_ip()
    for i in psutil.process_iter():
        if i.name() == BINNAME:
            pid = int(i.pid)
            cmdline = i.cmdline()

            for opt in cmdline:
                if opt.find('--name=') == 0:
                    name = opt.strip().split('=')[1].strip()
                    config_file = CFG_DIR + 'etcd-' + name + '.conf'
            if os.path.exists(config_file):
                f = open(config_file, 'r')
                cfg_content = f.read()
                f.close()
            for line in cfg_content.split("\n"):
                if line.find('ETCD_LISTEN_CLIENT_URLS=') == 0:
                    address_port = line.split('//')[1].strip('"')
                    address = address_port.split(':')[0]
                    port = address_port.split(':')[1]
                    if address == '0.0.0.0':
                        address = host
                    result.append([address, port, name])
    return result
Ejemplo n.º 5
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
Ejemplo n.º 6
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
Ejemplo n.º 7
0
def discovery_nginx(status_path=False, *args):
    '''
    discovery nginx instance's host and port
    '''
    nginx_pid = ''
    instance_list = []
    for i in psutil.process_iter():
        if i.name() == BINNAME and i.username() == 'root':
            nginx_pid = i.pid

    nginx_proc_file = '/proc/{0}/cmdline'.format(nginx_pid)

    if os.path.exists(nginx_proc_file):
        f = open(nginx_proc_file, 'r')
        nginx_cmd = f.read()
        f.close()
        nginx_cfg_file = nginx_cmd.strip().split()[-1]

        f = open(nginx_cfg_file, 'r')
        cfg_content = f.read()
        f.close()

        #        print cfg_content
        p_status_on = re.compile('stub_status\s+on;', re.I)
        m_status_on = p_status_on.search(cfg_content)
        if m_status_on:
            p_path = re.compile('location.*', re.I)
            m_path = p_path.search(cfg_content)
            if m_path:
                path = m_path.group(0).strip().split()[1]
            p_port = re.compile('listen\s+.*', re.I)
            m_port = p_port.search(cfg_content)
            if m_port:
                l_port = m_port.group(0).strip().strip(';').split()[-1].split(
                    ':')
                if (len(l_port) == 2):
                    port = l_port[1]
                else:
                    port = l_port[0]
            ip = Monitor.get_local_ip()

    result = []
    if not status_path:
        result.append([str(ip), str(port)])
        return result
    else:
        return path
Ejemplo n.º 8
0
def discovery_nginx(status_path=False, *args):
    '''
    discovery nginx instance's host and port
    '''
    nginx_pid = ''
    instance_list = []
    for i in psutil.process_iter():
        if i.name() == BINNAME and i.username() == 'root':
            nginx_pid = i.pid

    nginx_proc_file = '/proc/{0}/cmdline'.format(nginx_pid)

    if os.path.exists(nginx_proc_file):
        f = open(nginx_proc_file, 'r')
        nginx_cmd = f.read()
        f.close()
        nginx_cfg_file = nginx_cmd.strip().split()[-1]

        f = open(nginx_cfg_file, 'r')
        cfg_content = f.read()
        f.close()

        #        print cfg_content
        p_status_on = re.compile('stub_status\s+on;', re.I)
        m_status_on = p_status_on.search(cfg_content)
        if m_status_on:
            p_path = re.compile('location.*', re.I)
            m_path = p_path.search(cfg_content)
            if m_path:
                path = m_path.group(0).strip().split()[1]
            p_port = re.compile('listen\s+.*', re.I)
            m_port = p_port.search(cfg_content)
            if m_port:
                l_port = m_port.group(0).strip().strip(';').split()[-1].split(':')
                if (len(l_port) == 2):
                    port = l_port[1]
                else:
                    port = l_port[0]
            ip = Monitor.get_local_ip()

    result = []
    if not status_path:
        result.append([str(ip), str(port)])
        return result
    else:
        return path
Ejemplo n.º 9
0
def discovery_codisHa(*args):
    '''
    discovery codis-ha instance's host and port
    '''
    result = []
    port = ''
    address = ''
    cmdline = []
    host = Monitor.get_local_ip()
    for i in psutil.process_iter():
        if i.name() == BINNAME:
            pid = int(i.pid)
            cmdline = i.cmdline()

            for opt in cmdline:
                if opt.find('--dashboard=') == 0:
                    address = opt.strip().split('=')[1].split(':')[0].strip()
                    port = opt.strip().split('=')[1].split(':')[1].strip()
                    result.append([address, port])
    return result
Ejemplo n.º 10
0
def discovery_ipmi(arg='', *args):
    '''
    discovery ipmi instance
    '''
    result = []
    host = Monitor.get_local_ip()
    if not arg:
        result.append([host])
    elif arg == 'FAN':
        (status, output) = commands.getstatusoutput(
            "/usr/bin/ipmitool sdr type fan | grep -v 'Fully Redundant' | cut -d '|' -f 1 "
        )
        if status == 0:
            for fan in output.split('\n'):
                fan = fan.lower()
                fan = fan.strip()
                fan = fan.replace(' ', '_')
                result.append([host, fan])
    # cpu memory system io
    elif arg == 'CMSI':
        (status, output) = commands.getstatusoutput(
            "/usr/bin/ipmitool sdr elist full | grep -E '(CPU|IO|MEM|SYS)'| cut -d '|' -f 1"
        )
        if status == 0:
            for cmsi in output.split('\n'):
                cmsi = cmsi.lower()
                cmsi = cmsi.strip()
                cmsi = cmsi.replace(' ', '_')
                result.append([host, cmsi])
    # current voltage temp
    elif arg == 'VCT':
        (status, output) = commands.getstatusoutput(
            "/usr/bin/ipmitool sdr elist full | grep -E '(Inlet Temp|Exhaust Temp|Current|Voltage)'| cut -d '|' -f 1"
        )
        if status == 0:
            for vct in output.split('\n'):
                vct = vct.lower()
                vct = vct.strip()
                vct = vct.replace(' ', '_')
                result.append([host, vct])
    elif arg == 'PW':
        (status, output) = commands.getstatusoutput(
            "/usr/bin/ipmitool chassis status | grep -E '(System Power|Power Overload)' | cut -d ':' -f 1"
        )
        if status == 0:
            for pw in output.split('\n'):
                pw = pw.lower()
                pw = pw.strip()
                pw = pw.replace(' ', '_')
                result.append([host, pw])
    elif arg == 'SEL':
        (status, output) = commands.getstatusoutput(
            "/usr/bin/ipmitool sel info | grep -v Information | cut -d ':' -f 1"
        )
        if status == 0:
            for sel in output.split('\n'):
                sel = sel.lower()
                sel = sel.strip()
                sel = sel.replace(' ', '_')
                result.append([host, sel])

    return result