Ejemplo n.º 1
0
def do_docker_show():
    d = get_kunai_json('/docker/stats')
    scontainers = d.get('containers')
    simages     = d.get('images')

    print_info_title('Docker Stats')
    if not scontainers:
        cprint("No running containers", color='grey')
    for (cid, stats) in scontainers.iteritems():
        print_info_title('Container:%s' % cid)
        keys = stats.keys()
        keys.sort()
        e = []
        for k in keys:
            sd = stats[k]
            e.append( (k, sd['value']) )
            
        # Normal agent information
        print_2tab(e, capitalize=False, col_size=30)

    for (cid, stats) in simages.iteritems():
        print_info_title('Image:%s (sum)' % cid)
        keys = stats.keys()
        keys.sort()
        e = []
        for k in keys:
            sd = stats[k]
            e.append( (k, sd['value']) )
            
        # Normal agent information
        print_2tab(e, capitalize=False, col_size=30)
Ejemplo n.º 2
0
def do_start(daemon):
    cprint("Starting kunai daemon", color="green")
    lock_path = CONFIG.get("lock", "/var/run/kunai.pid")
    l = Launcher(lock_path=lock_path)
    l.do_daemon_init_and_start(is_daemon=daemon)
    # Here only the last son reach this
    l.main()
Ejemplo n.º 3
0
def do_start(daemon):
    cprint('Starting kunai daemon', color='green')
    lock_path = CONFIG.get('lock', '/var/run/kunai.pid')
    l = Launcher(lock_path=lock_path)
    l.do_daemon_init_and_start(is_daemon=daemon)
    # Here only the last son reach this
    l.main()
Ejemplo n.º 4
0
def do_keygen():
    k = uuid.uuid1().hex[:16]
    cprint('UDP Encryption key: (aka encryption_key)', end='')
    cprint(base64.b64encode(k), color='green')
    print ''
    try:
        from Crypto.PublicKey import RSA
    except ImportError:
        logger.error('Missing python-crypto module for RSA keys generation, please install it')
        return
    key = RSA.generate(2048)
    privkey = key.exportKey()
    pub_key = key.publickey()
    pubkey = pub_key.exportKey()
    print "Private RSA key (2048). (aka master_key_priv for for file mfkey.priv)"
    cprint(privkey, color='green')
    print ''
    print "Public RSA key (2048). (aka master_key_pub for file mfkey.pub)"
    cprint(pubkey, color='green')
    print ''
Ejemplo n.º 5
0
def do_keygen():
    k = uuid.uuid1().hex[:16]
    cprint("UDP Encryption key: (aka encryption_key)", end="")
    cprint(base64.b64encode(k), color="green")
    print ""
    try:
        import rsa
    except ImportError:
        logger.error("Missing python-rsa module for RSA keys generation, please install it")
        return
    pubkey, privkey = rsa.newkeys(2048)

    print "Private RSA key (2048). (aka master_key_priv for for file mfkey.priv)"
    s_privkey = privkey.save_pkcs1()
    cprint(s_privkey, color="green")
    print ""
    print "Public RSA key (2048). (aka master_key_pub for file mfkey.pub)"
    s_pubkey = pubkey.save_pkcs1()
    cprint(s_pubkey, color="green")
    print ""
Ejemplo n.º 6
0
    def main(self):
        c = Cluster()
        cprint('Linking services and checks', color='green')    
        c.link_services()
        c.link_checks()
        cprint('Launching listeners', color='green')    
        c.launch_listeners()
        cprint('Joining seeds nodes', color='green')    
        c.join()
        cprint('Starting check and generator threads', color='green')    
        c.launch_check_thread()
        c.launch_generator_thread()
        
        if 'kv' in c.tags:
            c.launch_replication_backlog_thread()
            c.launch_replication_first_sync_thread()
        if 'ts' in c.tags:
            c.start_ts_listener()

        # Blocking function here
        c.main()
Ejemplo n.º 7
0
def print_2tab(e, capitalize=True, col_size=20):
    for (k, v) in e:
        label = k
        if capitalize:
            label = label.capitalize()
        s = '%s: ' % label
        s = s.ljust(col_size)
        cprint(s, end='', color='blue')
        # If it's a dict, we got additiionnal data like color or type
        if isinstance(v, dict):
            color = v.get('color', 'green')
            _type = v.get('type', 'std')
            value = v.get('value')
            cprint(value, color=color)
        else:
            cprint(v, color='green')
Ejemplo n.º 8
0
def do_version():
    cprint(VERSION)
Ejemplo n.º 9
0
    except rq.exceptions.ConnectionError, exp:
        logger.error(exp)
        return
    try:
        members = json.loads(r.text).values()
    except ValueError, exp:# bad json
        logger.error('Bad return from the server %s' % exp)
        return
    members = sorted(members, key=lambda e:e['name'])
    for m in members:
        name = m['name']
        tags = m['tags']
        port = m['port']
        addr = m['addr']
        state = m['state']
        cprint('%s ' % name, end='')
        c = {'alive':'green', 'dead':'red', 'suspect':'yellow', 'leave':'cyan'}.get(state, 'cyan')
        cprint(state, color=c, end='')
        cprint(' %s:%s' % (addr, port), end='')
        cprint(' %s ' % ','.join(tags))        



def do_leave(name=''):
    # Lookup at the localhost name first
    if not name:
        try:
            r = rq.get('http://%s:6768/agent/name' % HOSTNAME)
        except rq.exceptions.ConnectionError, exp:
            logger.error(exp)
            return
Ejemplo n.º 10
0
def do_members():
    try:
        members = get_kunai_json("/agent/members").values()
    except request_errors, exp:
        logger.error("Cannot join kunai agent: %s" % exp)
        sys.exit(1)
    members = sorted(members, key=lambda e: e["name"])
    max_name_size = max([len(m["name"]) for m in members])
    max_addr_size = max([len(m["addr"]) + len(str(m["port"])) + 1 for m in members])
    for m in members:
        name = m["name"]
        tags = m["tags"]
        port = m["port"]
        addr = m["addr"]
        state = m["state"]
        cprint("%s  " % name.ljust(max_name_size), end="")
        c = {"alive": "green", "dead": "red", "suspect": "yellow", "leave": "cyan"}.get(state, "cyan")
        cprint(state, color=c, end="")
        s = " %s:%s " % (addr, port)
        s = s.ljust(max_addr_size + 2)  # +2 for the spaces
        cprint(s, end="")
        cprint(" %s " % ",".join(tags))


def do_leave(name=""):
    # Lookup at the localhost name first
    if not name:
        try:
            (code, r) = get_kunai_local("/agent/name")
        except request_errors, exp:
            logger.error(exp)
Ejemplo n.º 11
0
def print_info_title(title):
    #t = title.ljust(15)
    #s = '=================== %s ' % t
    #s += '='*(50 - len(s))
    #cprint(s)
    cprint('========== [%s]:' % title)
Ejemplo n.º 12
0
def do_info(show_logs):
    d = get_kunai_json('/agent/info')
    
    logs = d.get('logs')
    version = d.get('version')
    pid = d.get('pid')
    name = d.get('name')
    port = d.get('port')
    addr = d.get('addr')
    nb_threads = d.get('threads')['nb_threads']
    httpservers = d.get('httpservers', {'internal':None, 'external':None})
    socket_path = d.get('socket')
    _uuid = d.get('uuid')
    graphite = d.get('graphite')
    statsd = d.get('statsd')
    websocket = d.get('websocket')
    dns = d.get('dns')
    _docker = d.get('docker')

    e = [('name', name), ('uuid',_uuid), ('version', version), ('pid', pid), ('port',port), ('addr',addr), ('socket',socket_path), ('threads', nb_threads)]

    # Normal agent information
    print_info_title('Kunai Daemon')
    print_2tab(e)
    
    # Normal agent information
    int_server = httpservers['external']
    if int_server:
        e = (('threads', int_server['nb_threads']), ('idle_threads', int_server['idle_threads']), ('queue', int_server['queue']) )
        print_info_title('HTTP (LAN)')
        print_2tab(e)

    # Unix socket http daemon
    int_server = httpservers['internal']
    if int_server:
        e = (('threads', int_server['nb_threads']), ('idle_threads', int_server['idle_threads']), ('queue', int_server['queue']) )
        print_info_title('HTTP (Unix Socket)')
        print_2tab(e)
        
    # Now DNS part
    print_info_title('DNS')
    if dns is None:
        cprint('No dns configured')
    else:
        w = dns
        e = [('enabled', w['enabled']), ('port', w['port']), ('domain',w['domain']) ]
        print_2tab(e)
    
    # Now websocket part
    print_info_title('Websocket')
    if websocket is None:
        cprint('No websocket configured')
    else:
        w = websocket
        st = d.get('websocket_info', None)
        e = [('enabled', w['enabled']), ('port', w['port']) ]
        if st:
            e.append( ('Nb connexions', st.get('nb_connexions')) )
        print_2tab(e)

    # Now graphite part
    print_info_title('Graphite')
    if graphite is None:
        cprint('No graphite configured')
    else:
        g = graphite
        e = [('enabled', g['enabled']), ('port', g['port']), ('udp', g['udp']), ('tcp', g['tcp']) ]
        print_2tab(e)

    # Now statsd part
    print_info_title('Statsd')
    if statsd is None:
        cprint('No statsd configured')
    else:
        s = statsd
        e = [('enabled', s['enabled']), ('port', s['port']), ('interval', s['interval'])]
        print_2tab(e)


    # Now statsd part
    print_info_title('Docker')
    _d = _docker
    if _d['connected']:
        e = [('enabled', _d['enabled']), ('connected', _d['connected']),
             ('version',_d['version']), ('api', _d['api']),
              ('containers', len(_d['containers'])),
             ('images', len(_d['images'])),
        ]
    else:
        e = [
            ('enabled', {'value':_d['enabled'], 'color':'grey'}),
            ('connected', {'value':_d['connected'], 'color':'grey'}),
            ]
            
    print_2tab(e)
    
    # Show errors logs if any
    print_info_title('Logs')
    errors  = logs.get('ERROR')
    warnings = logs.get('WARNING')
 
    # Put warning and errors in red/yellow if need only
    e = []
    if len(errors) > 0:
        e.append( ('error', {'value':len(errors), 'color':'red'}) )
    else:
        e.append( ('error', len(errors)) )
    if len(warnings) > 0:
        e.append( ('warning', {'value':len(warnings), 'color':'yellow'}) )
    else:
        e.append( ('warning', len(warnings)) )

    print_2tab(e)

    if show_logs:
        if len(errors) > 0:
            print_info_title('Error logs')
            for s in errors:
                cprint(s, color='red')
    
        if len(warnings) > 0:
            print_info_title('Warning logs')
            for s in warnings:
                cprint(s, color='yellow')
        
    logger.debug('Raw information: %s' % d)