Beispiel #1
0
def do_detect_list():
    try:
        (code, r) = get_opsbro_local('/agent/detectors')
    except get_request_errors() as exp:
        logger.error(exp)
        return
    
    try:
        d = jsoner.loads(r)
    except ValueError as exp:  # bad json
        logger.error('Bad return from the server %s' % exp)
        return
    print_info_title('Detectors')
    logger.debug(str(d))
    e = []
    d = sorted(d, key=lambda i: i['name'])
    for i in d:
        # aligne name too
        name = '%-20s' % i['name'].split('/')[-1]
        groups = i['add_groups']
        # align pack level
        pack_level = '%-6s' % i['pack_level']
        # Aligne pack name
        pack_name = '%-10s' % i['pack_name']
        
        print_element_breadcumb(pack_name, pack_level, 'detector', name, set_pack_color=True)
        cprint('')
        cprint('   if:         ', color='grey', end='')
        cprint(i['apply_if'], color='green')
        cprint('   add groups: ', color='grey', end='')
        cprint(','.join(groups), color='magenta')
Beispiel #2
0
def do_kv_store_list():
    expr_64 = base64.b64encode(expr)
    try:
        r = post_opsbro_json('/agent/evaluator/eval', {'expr': expr_64},
                             timeout=30)
    except get_request_errors() as exp:
        logger.error(exp)
        return

    print_info_title('Result')
    cprint(r)
Beispiel #3
0
def do_evaluator_eval(expr):
    expr = string_encode(expr)
    expr_64 = base64.b64encode(expr)
    try:
        r = post_opsbro_json('/agent/evaluator/eval', {'expr': expr_64}, timeout=30)
    except Exception as exp:
        logger.error(exp)
        sys.exit(2)
    
    print_info_title('Result')
    cprint(r)
Beispiel #4
0
def do_zone_change(name=''):
    if not name:
        cprint("Need a zone name")
        return

    cprint("Switching to zone", name)
    try:
        r = put_opsbro_json('/agent/zone', name)
    except get_request_errors() as exp:
        logger.error(exp)
        return
    print_info_title('Result')
    print(r)
Beispiel #5
0
def do_detect_state():
    try:
        (code, r) = get_opsbro_local('/agent/detectors/state')
    except get_request_errors() as exp:
        logger.error(exp)
        return
    
    try:
        groups = jsoner.loads(r)
    except ValueError as exp:  # bad json
        logger.error('Bad return from the server %s' % exp)
        return
    
    print_info_title('Current detected groups')
    groups.sort()
    for group in groups:
        cprint(' * ', end='')
        cprint('%s' % group, color='magenta')
Beispiel #6
0
def do_collectors_show(name='', all=False):
    try:
        collectors = get_opsbro_json('/collectors')
    except get_request_errors() as exp:
        logger.error(exp)
        return

    disabled = []
    for (cname, d) in collectors.items():
        if name and not name == cname:
            continue
        if not name and not d['active'] and not all:
            disabled.append(d)
            continue
        print_info_title('Collector %s' % cname)
        pretty_print(d)
    if len(disabled) > 0:
        print_info_title('Disabled collectors')
        cprint(','.join([d['name'] for d in disabled]), color='grey')
Beispiel #7
0
def do_zone_change(name=''):
    if not name:
        cprint("Need a zone name")
        return

    cprint("Switching to zone %s" % name)
    try:
        r = put_opsbro_json('/agent/zone', name)
    except get_request_errors() as exp:
        logger.error(exp)
        return
    print_info_title('Result')
    success = r['success']
    text = r['text']
    if success:
        cprint(text, color='green')
        sys.exit(0)
    else:
        cprint(text, color='red')
        sys.exit(2)
Beispiel #8
0
def do_modules_state():
    try:
        d = get_opsbro_json('/agent/info')
    except get_request_errors() as exp:
        logger.error('Cannot join opsbro agent for module state: %s' % exp)
        sys.exit(1)
    modules = d.get('modules', {})
    print_info_title('Modules')
    modules_types = {}
    for (module_name, module) in modules.items():
        module_type = module['module_type']
        if module_type not in modules_types:
            modules_types[module_type] = {}
        modules_types[module_type][module_name] = module

    modules_type_names = list(modules_types.keys())
    modules_type_names.sort()

    for (module_type, _modules) in modules_types.items():
        cprint(' - [', end='')
        cprint(' %-10s ' % module_type.capitalize(), color='magenta', end='')
        cprint(' ]: ', end='')
        cprint(TYPES_DESCRIPTIONS.get(module_type, 'unknown module type'),
               color='grey')

        module_names = list(_modules.keys())
        module_names.sort()
        for module_name in module_names:
            module = _modules[module_name]
            state = module['state']
            state_color = MODULE_STATE_COLORS.get(state, 'grey')
            log = module['log']
            kwcolor = {'color': 'grey'} if state == 'DISABLED' else {}
            cprint('     * ', end='', **kwcolor)
            cprint('%-20s ' % module_name, color=state_color, end='')
            cprint(state, color=state_color)
            if state != 'DISABLED' and log:
                cprint('       | Log: %s' % log, color='grey')
    cprint(
        ' | Note: you can look at modules configuration with the command  opsbro packs show',
        color='grey')
Beispiel #9
0
def do_docker_show():
    d = get_opsbro_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.items():
        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.items():
        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)
Beispiel #10
0
def do_detect_run():
    try:
        (code, r) = get_opsbro_local('/agent/detectors/run')
    except get_request_errors() as exp:
        logger.error(exp)
        return
    
    try:
        d = jsoner.loads(r)
    except ValueError as exp:  # bad json
        logger.error('Bad return from the server %s' % exp)
        return
    
    print_info_title('Detectors results')
    all_groups = []
    new_groups = []
    for (k, v) in d.items():
        all_groups.extend(v['groups'])
        new_groups.extend(v['new_groups'])
    e = [('Groups', ','.join(all_groups))]
    e.append(('New groups', {'value': ','.join(new_groups), 'color': 'green'}))
    print_2tab(e)
Beispiel #11
0
def do_info(show_logs):
    try:
        d = get_opsbro_json('/agent/info')
    except get_request_errors() as exp:
        logger.error('Cannot join opsbro agent for info: %s' % exp)
        sys.exit(1)
    logs = d.get('logs')
    version = d.get('version')
    cpu_consumption = d.get('cpu_consumption')
    memory_consumption = d.get('memory_consumption')
    name = d.get('name')
    display_name = d.get('display_name', '')
    # A failback to display name is the name (hostname)
    if not display_name:
        display_name = name
    else:  # show it's a display name
        display_name = '[ ' + display_name + ' ]'
    port = d.get('port')
    local_addr = d.get('local_addr')
    public_addr = d.get('public_addr')
    zone = d.get('zone')
    zone_color = 'green'
    if not zone:
        zone = NO_ZONE_DEFAULT
        zone_color = 'red'

    is_managed_system = d.get('is_managed_system')
    system_distro = d.get('system_distro')
    system_distroversion = d.get('system_distroversion')

    is_zone_protected = d.get('is_zone_protected')
    is_zone_protected_display = ('%s zone have a gossip key' %
                                 CHARACTERS.check,
                                 'green') if is_zone_protected else (
                                     '%s zone do not have a gossip key' %
                                     CHARACTERS.double_exclamation, 'yellow')

    nb_threads = d.get('threads')['nb_threads']
    hosting_drivers_state = d.get('hosting_drivers_state', [])
    httpservers = d.get('httpservers', {'internal': None, 'external': None})
    socket_path = d.get('socket')
    _uuid = d.get('uuid')
    # Modules groking
    modules = d.get('modules', {})
    topics = d.get('topics', {})
    # Get groups as sorted
    groups = d.get('groups')
    groups.sort()
    groups = ','.join(groups)
    collectors = d.get('collectors')
    monitoring = d.get('monitoring')
    compliance = d.get('compliance')
    generators = d.get('generators')
    kv_store = d.get('kv')

    ################### Generic
    __print_topic_header(TOPIC_GENERIC)
    # print_info_title('OpsBro Daemon')

    __print_key_val('Name', name, topic=TOPIC_GENERIC)
    display_name_color = 'green' if (name != display_name) else 'grey'
    __print_key_val('Display name',
                    display_name,
                    color=display_name_color,
                    topic=TOPIC_GENERIC)

    __print_key_val('System',
                    '%s (version %s) ' % (system_distro, system_distroversion),
                    color='green',
                    topic=TOPIC_GENERIC,
                    end_line='')
    if is_managed_system:
        cprint(' %s managed' % CHARACTERS.check, color='grey')
    else:
        cprint(' %s this system is not managed' %
               CHARACTERS.double_exclamation,
               color='yellow')
        __print_more(
            ' Not managed means that configuration automation & system compliance will not be available'
        )

    # We will print modules by modules types
    # cprint(' - Modules: '.ljust(DEFAULT_INFO_COL_SIZE), end='', color='blue')
    modules_by_states = {}
    for module_state in MODULE_STATES:
        modules_by_states[module_state] = []
    for (module_name, module) in modules.items():
        modules_by_states[module['state']].append(module)

    strs = []
    for module_state in MODULE_STATES:
        nb = len(modules_by_states[module_state])
        state_color = MODULE_STATE_COLORS.get(module_state, 'grey')
        color = 'grey' if nb == 0 else state_color
        _s = sprintf('%d %s ' % (nb, module_state), color=color, end='')
        strs.append(_s)
    module_string = sprintf(' / ', color='grey', end='').join(strs)

    __print_key_val('Modules', module_string, topic=TOPIC_GENERIC)

    __print_topic_picto(TOPIC_GENERIC)
    __print_more('opsbro agent modules state')

    ################### Service Discovery
    cprint('')
    __print_topic_header(TOPIC_SERVICE_DISCOVERY)

    __print_key_val('UUID', _uuid, topic=TOPIC_SERVICE_DISCOVERY)
    __print_key_val('Local addr', local_addr, topic=TOPIC_SERVICE_DISCOVERY)
    __print_key_val('Public addr', public_addr, topic=TOPIC_SERVICE_DISCOVERY)

    __print_key_val('UDP port', port, topic=TOPIC_SERVICE_DISCOVERY)

    # Normal agent information
    ext_server = httpservers.get('external')
    int_server = httpservers.get('internal')
    ext_threads = '%d/%d' % (ext_server['nb_threads'] -
                             ext_server['idle_threads'],
                             ext_server['nb_threads'])
    if int_server:
        int_threads = '%d/%d' % (int_server['nb_threads'] -
                                 int_server['idle_threads'],
                                 int_server['nb_threads'])
    else:  # windows case
        int_threads = '(not available on windows)'

    __print_key_val('HTTP threads',
                    'LAN:%s                        Private socket:%s' %
                    (ext_threads, int_threads),
                    topic=TOPIC_SERVICE_DISCOVERY)
    __print_topic_picto(TOPIC_SERVICE_DISCOVERY)
    __print_note(
        '          Listen on the TCP port %s     Listen on the unix socket %s'
        % (port, socket_path))

    __print_key_val('Zone',
                    zone,
                    color=zone_color,
                    topic=TOPIC_SERVICE_DISCOVERY,
                    end_line=False)
    cprint(' (%s)' % is_zone_protected_display[0],
           color=is_zone_protected_display[1])
    __print_topic_picto(TOPIC_SERVICE_DISCOVERY)
    __print_more('opsbro gossip members')

    ################################## Automatic Detection
    cprint('')
    __print_topic_header(TOPIC_AUTOMATIC_DECTECTION)
    __print_key_val('Groups', groups, topic=TOPIC_AUTOMATIC_DECTECTION)

    __print_topic_picto(TOPIC_AUTOMATIC_DECTECTION)
    __print_more('opsbro detectors state')

    # Show hosting drivers, and why we did chose this one
    main_driver_founded = False
    strs = []
    for driver_entry in hosting_drivers_state:
        driver_name = driver_entry['name']
        driver_is_active = driver_entry['is_active']
        _name = driver_name
        if not main_driver_founded and driver_is_active:
            strs.append(
                sprintf('[', color='magenta') + sprintf(_name, color='green') +
                sprintf(']', color='magenta'))
            main_driver_founded = True
        elif driver_is_active:
            strs.append(sprintf(_name, color='green'))
        else:
            strs.append(sprintf(_name, color='grey'))

    _hosting_drivers_state_string = sprintf(' %s ' % CHARACTERS.arrow_left,
                                            color='grey').join(strs)
    __print_key_val('Hosting drivers',
                    _hosting_drivers_state_string,
                    topic=TOPIC_AUTOMATIC_DECTECTION)
    __print_topic_picto(TOPIC_AUTOMATIC_DECTECTION)
    __print_note(
        'first founded valid driver is used as main hosting driver (give uuid, public/private ip, %s)'
        % CHARACTERS.three_dots)

    ################################## Monitoring
    cprint('')
    __print_topic_header(TOPIC_MONITORING)

    monitoring_strings = []
    for check_state in CHECK_STATES:
        count = monitoring[check_state]
        color = STATE_COLORS.get(check_state) if count != 0 else 'grey'
        s = ('%d %s' % (count, check_state.upper())).ljust(15)
        s = sprintf(s, color=color, end='')
        monitoring_strings.append(s)
    monitoring_string = sprintf(' / ', color='grey',
                                end='').join(monitoring_strings)
    __print_key_val('Check states', monitoring_string, topic=TOPIC_MONITORING)
    __print_topic_picto(TOPIC_MONITORING)
    __print_more('opsbro monitoring state')

    ################################## Metrology
    # Now collectors part
    cprint('')
    __print_topic_header(TOPIC_METROLOGY)
    cnames = list(collectors.keys())
    cnames.sort()
    collectors_states = {}
    for collector_state in COLLECTORS_STATES:
        collectors_states[collector_state] = []
    for cname in cnames:
        v = collectors[cname]
        collector_state = v['state']
        collectors_states[collector_state].append(cname)

    strs = []
    for collector_state in COLLECTORS_STATES:
        nb = len(collectors_states[collector_state])
        state_color = COLLECTORS_STATE_COLORS.get(collector_state, 'grey')
        color = 'grey' if nb == 0 else state_color
        _s = ('%d %s' % (nb, collector_state)).ljust(15)
        _s = sprintf(_s, color=color, end='')
        strs.append(_s)
    collector_string = sprintf(' / ', color='grey', end='').join(strs)
    __print_key_val('Collectors', collector_string, topic=TOPIC_METROLOGY)
    __print_topic_picto(TOPIC_METROLOGY)
    __print_more('opsbro collectors state')

    ################################## configuration automation
    cprint('')
    __print_topic_header(TOPIC_CONFIGURATION_AUTOMATION)

    strs = []
    for state in GENERATOR_STATES:
        nb = generators[state]
        state_color = GENERATOR_STATE_COLORS.get(state, 'grey')
        color = 'grey' if nb == 0 else state_color
        _s = ('%d %s' % (nb, state)).ljust(15)
        _s = sprintf(_s, color=color, end='')
        strs.append(_s)
    generator_string = sprintf(' / ', color='grey', end='').join(strs)
    __print_key_val('Generators',
                    generator_string,
                    topic=TOPIC_CONFIGURATION_AUTOMATION)
    __print_topic_picto(TOPIC_CONFIGURATION_AUTOMATION)
    __print_more('opsbro generators state')

    ################################## system compliance
    cprint('')
    __print_topic_header(TOPIC_SYSTEM_COMPLIANCE)

    strs = []
    for state in ALL_COMPLIANCE_STATES:
        nb = compliance[state]
        state_color = COMPLIANCE_STATE_COLORS.get(state, 'grey')
        color = 'grey' if nb == 0 else state_color
        _s = ('%d %s' % (nb, state)).ljust(15)
        _s = sprintf(_s, color=color, end='')
        strs.append(_s)
    collector_string = sprintf(' / ', color='grey', end='').join(strs)
    __print_key_val('Compliance rules',
                    collector_string,
                    topic=TOPIC_SYSTEM_COMPLIANCE)
    __print_topic_picto(TOPIC_SYSTEM_COMPLIANCE)
    __print_more('opsbro compliance state')

    ############### Logs:  Show errors logs if any
    cprint('')
    print_info_title('Technical info')

    cprint(' - Job threads: '.ljust(DEFAULT_INFO_COL_SIZE),
           end='',
           color='blue')
    cprint(nb_threads, color='green')

    if memory_consumption != 0:
        mo_memory_consumption = int(memory_consumption / 1024.0 / 1024.0)
        s = '%dMB' % mo_memory_consumption
        __print_key_val('Memory usage', s)

    if cpu_consumption != 0:
        s = '%.1f%%' % cpu_consumption
        __print_key_val('CPU Usage', s)
        __print_more('opsbro agent internal show-threads')

    kv_store_backend = kv_store.get('backend', None)
    if kv_store_backend:
        cprint(' - KV Backend: '.ljust(DEFAULT_INFO_COL_SIZE),
               end='',
               color='blue')
        cprint(kv_store_backend['name'], color='green')
        cprint('    - size: '.ljust(DEFAULT_INFO_COL_SIZE),
               end='',
               color='blue')
        cprint('%.2fMB' % (kv_store['stats']['size'] / 1024.0 / 1024.0),
               color='green')

        if kv_store_backend['name'] != 'leveldb':
            __print_note(
                'You do not have the fastest lib/backend. Please launch the command: opsbro compliance launch "Install tuning libs"'
            )

        kv_store_error = kv_store['stats']['error']
        if kv_store_error != '':
            cprint('    - error: '.ljust(DEFAULT_INFO_COL_SIZE),
                   color='blue',
                   end='')
            cprint(kv_store_error, color='red')

    cprint(' - Version: '.ljust(DEFAULT_INFO_COL_SIZE), end='', color='blue')
    cprint(version, color='green')

    errors = logs.get('ERROR')
    warnings = logs.get('WARNING')

    cprint(' - Logs: '.ljust(DEFAULT_INFO_COL_SIZE), end='', color='blue')
    # Put warning and errors in red/yellow if need only
    error_color = 'red' if len(errors) > 0 else 'grey'
    warning_color = 'yellow' if len(warnings) > 0 else 'grey'
    cprint('%d errors    ' % len(errors), color=error_color, end='')
    cprint('%d warnings   ' % len(warnings), color=warning_color)

    # If there are errors or warnings, help the user to know it can print them
    if not show_logs and (len(errors) > 0 or len(warnings) > 0):
        __print_note(
            'you can show error & warning logs with the --show-logs options')

    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)