示例#1
0
def __get_pack_breadcumb(pack_name, pack_level, end='', topic_picto='large'):
    pack_topics = packer.get_pack_all_topics(pack_name)
    pack_main_topic = 'generic'
    if len(pack_topics) != 0:
        pack_main_topic = pack_topics[0]
    topic_color = topiker.get_color_id_by_topic_string(pack_main_topic)
    if topic_picto == 'large':
        picto = u'%s%s ' % (CHARACTERS.corner_top_left, CHARACTERS.hbar * 2)
    else:
        picto = u'%s ' % CHARACTERS.topic_small_picto
    res = lolcat.get_line(picto, topic_color, spread=None) \
          + sprintf('%-6s' % pack_level, color='blue', end='') \
          + sprintf(' > ', end='') \
          + sprintf('%-15s' % pack_name, color='yellow', end='') \
          + end
    
    return res
示例#2
0
文件: cli.py 项目: naparuba/opsbro
def do_members(detail=False):
    # The information is available only if the agent is started
    wait_for_agent_started(visual_wait=True)

    try:
        members = get_opsbro_json('/agent/members').values()
    except get_request_errors() as exp:
        logger.error('Cannot join opsbro agent to list members: %s' % exp)
        sys.exit(1)
    members = my_sort(members, cmp_f=__sorted_members)
    pprint = libstore.get_pprint()
    logger.debug('Raw members: %s' % (pprint.pformat(members)))
    # If there is a display_name, use it
    max_name_size = max([
        max(len(m['name']),
            len(m.get('display_name', '')) + 4) for m in members
    ])
    max_addr_size = max(
        [len(m['addr']) + len(str(m['port'])) + 1 for m in members])
    zones = set()
    for m in members:
        mzone = m.get('zone', '')
        if mzone == '':
            mzone = NO_ZONE_DEFAULT
        m['zone'] = mzone  # be sure to fix broken zones
        zones.add(mzone)
    zones = list(zones)
    zones.sort()
    for z in zones:
        z_display = z
        if not z:
            z_display = NO_ZONE_DEFAULT
        z_display = z_display.ljust(15)
        title_s = '%s: %s' % (sprintf('Zone', color='yellow', end=''),
                              sprintf(z_display, color='blue', end=''))
        print_h1(title_s, raw_title=True)
        for m in members:
            zone = m.get('zone', NO_ZONE_DEFAULT)
            if zone != z:
                continue
            name = m['name']
            if m.get('display_name', ''):
                name = '[ ' + m.get('display_name') + ' ]'
            groups = m.get('groups', [])
            groups.sort()
            port = m['port']
            addr = m['addr']
            state = m['state']
            is_proxy = m.get('is_proxy', False)
            if not detail:
                cprint('  - %s > ' % zone, color='blue', end='')
                cprint('%s  ' % name.ljust(max_name_size),
                       color='magenta',
                       end='')
            else:
                cprint(' %s  %s  ' % (m['uuid'], name.ljust(max_name_size)),
                       end='')
            c = NODE_STATE_COLORS.get(state, 'cyan')
            state_prefix = NODE_STATE_PREFIXS.get(
                state, CHARACTERS.double_exclamation)
            cprint(('%s %s' % (state_prefix, state)).ljust(9), color=c,
                   end='')  # 7 for the maximum state string + 2 for prefix
            s = ' %s:%s ' % (addr, port)
            s = s.ljust(max_addr_size + 2)  # +2 for the spaces
            cprint(s, end='')
            if is_proxy:
                cprint('proxy ', end='')
            else:
                cprint('      ', end='')
            if detail:
                cprint('%5d' % m['incarnation'], end='')
            cprint(' %s ' % ','.join(groups))
示例#3
0
文件: cli.py 项目: naparuba/opsbro
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)
示例#4
0
##################################       Only root as it's a global system tool.
if os.name != 'nt' and os.getuid() != 0:
    cprint('Setup must be launched as root.', color='red')
    sys.exit(2)

# By default logger should not print anything
core_logger.setLevel('ERROR')
# By maybe we are in verbose more?
if '-v' in sys.argv or os.environ.get('DEBUG_INSTALL', '0') == '1':
    core_logger.setLevel('DEBUG')

core_logger.debug('SCRIPT: install/update script was call with arguments: %s' %
                  orig_sys_argv)

what = 'Installing' if not is_update else 'Updating'
title = sprintf('%s' % what, color='magenta', end='') + sprintf(
    ' OpsBro to version ', end='') + sprintf(
        '%s' % VERSION, color='magenta', end='')

if allow_black_magic:
    print_h1(title, raw_title=False)

##################################       Start to print to the user
if allow_black_magic:
    # If we have a real tty, we can print the delicious banner with lot of BRO
    if is_tty():
        cprint(BANNER)
    else:  # ok you are poor, just got some ascii art then
        cprint(TXT_BANNER)

    # Also print a Bro quote
示例#5
0
文件: cli.py 项目: naparuba/opsbro
def do_packs_show():
    logger.setLevel('ERROR')
    # We should already have load the configuration, so just dump it
    # now we read them, set it in our object

    from opsbro.packer import packer
    packs = {'core': {}, 'global': {}, 'zone': {}, 'local': {}}
    for level in packer.packs:
        for pname in packer.packs[level]:
            packs[level][pname] = {
                'checks': {},
                'module': None,
                'collectors': {},
                'generators': {}
            }

    from opsbro.monitoring import monitoringmgr
    checks = monitoringmgr.checks
    for cname, check in checks.items():
        pack_name = check['pack_name']
        pack_level = check['pack_level']
        packs[pack_level][pack_name]['checks'][cname] = check

    from opsbro.modulemanager import modulemanager
    modules = modulemanager.modules
    for module in modules:
        pack_name = module.pack_name
        pack_level = module.pack_level
        packs[pack_level][pack_name]['module'] = module

    from opsbro.collectormanager import collectormgr
    collectors = collectormgr.collectors
    for colname, collector in collectors.items():
        pack_name = collector['inst'].pack_name
        pack_level = collector['inst'].pack_level
        packs[pack_level][pack_name]['collectors'][colname] = collector

    from opsbro.generatormgr import generatormgr
    generators = generatormgr.generators
    for gname, generator in generators.items():
        pack_name = generator.pack_name
        pack_level = generator.pack_level
        packs[pack_level][pack_name]['generators'][gname] = generator

    for level in ('core', 'global', 'zone', 'local'):
        s1 = sprintf('Packs at level ', color='yellow', end='')
        s2 = sprintf(level, color='blue', end='')
        print_h1(s1 + s2, raw_title=True)
        pack_names = list(packs[level].keys())
        pack_names.sort()
        if len(pack_names) == 0:
            cprint('  No packs are available at the level %s' % level,
                   color='grey')
            continue
        for pack_name in pack_names:
            pack_entry = packs[level][pack_name]
            pack_breadcumb_s = __get_pack_breadcumb(pack_name, level)
            cprint(pack_breadcumb_s)

            main_topic, secondary_topics = packer.get_pack_main_and_secondary_topics(
                pack_name)
            main_topic_color = topiker.get_color_id_by_topic_string(main_topic)
            if main_topic != 'generic':

                __print_line_header(main_topic_color)
                cprint(u' * Main topic: ', color='grey', end='')
                s = lolcat.get_line(main_topic, main_topic_color, spread=None)
                cprint(s)
            if secondary_topics:
                _numeral = 's' if len(secondary_topics) > 1 else ''
                s = u' * Secondary topic%s: %s' % (_numeral,
                                                   ', '.join(secondary_topics))
                __print_line_header(main_topic_color)
                cprint(s, color='grey')

            #### Now loop over objects
            # * checks
            # * module
            # * collectors
            # * handlers
            # * generators
            no_such_objects = []
            checks = pack_entry['checks']
            if len(checks) == 0:
                no_such_objects.append('checks')
            else:
                __print_line_header(main_topic_color)
                print_element_breadcumb(pack_name, pack_level, 'checks')
                cprint(' (%d)' % len(checks), color='magenta')
                for cname, check in checks.items():
                    __print_line_header(main_topic_color)
                    cprint('  - ', end='')
                    cprint('checks > %-15s' % cname.split(os.sep)[-1],
                           color='cyan',
                           end='')
                    cprint(' if_group=%s' % (check['if_group']))

            # Module
            module = pack_entry['module']
            if module is None:
                no_such_objects.append('module')
            else:
                __print_line_header(main_topic_color)
                print_element_breadcumb(pack_name, pack_level, 'module')
                # cprint(' : configuration=', end='')
                cprint('')
                offset = 0
                __print_element_parameters(module, pack_name, pack_level,
                                           main_topic_color, 'parameters',
                                           offset)

            # collectors
            collectors = pack_entry['collectors']
            if len(collectors) == 0:
                no_such_objects.append('collectors')
            else:
                __print_line_header(main_topic_color)
                print_element_breadcumb(pack_name, pack_level, 'collectors')
                cprint(' (%d)' % len(collectors), color='magenta')
                for colname, collector_d in collectors.items():
                    __print_line_header(main_topic_color)
                    collector = collector_d['inst']
                    cprint('  - ', end='')
                    cprint('collectors > %-15s' % colname,
                           end='',
                           color='cyan')
                    cprint('')
                    offset = 1
                    __print_element_parameters(collector, pack_name,
                                               pack_level, main_topic_color,
                                               'parameters', offset)

            # generators
            generators = pack_entry['generators']
            if len(generators) == 0:
                no_such_objects.append('generators')
            else:
                __print_line_header(main_topic_color)
                print_element_breadcumb(pack_name, pack_level, 'generators')
                cprint(' (%d)' % len(generators), color='magenta')
                for gname, generator in generators.items():
                    __print_line_header(main_topic_color)
                    cprint('  - ', end='')
                    cprint('generators > %-15s' % gname.split(os.sep)[-1],
                           color='cyan',
                           end='')
                    cprint(' generate_if=%s' % generator.generate_if)

            # Display what the pack do not manage (for info)
            if no_such_objects:
                __print_line_header(main_topic_color)
                cprint(' * The pack do not provide objects: %s' %
                       ','.join(no_such_objects),
                       color='grey')
            cprint('')