Esempio n. 1
0
def do_detect_history():
    uri = '/agent/detectors/history'
    try:
        (code, r) = get_opsbro_local(uri)
    except get_request_errors() as exp:
        logger.error(exp)
        return
    
    try:
        histories = jsoner.loads(r)
    except ValueError as exp:  # bad json
        logger.error('Bad return from the server %s' % exp)
        return
    print_h1('Detected groups history for this node')
    
    for history in histories:
        epoch = history['date']
        # We want only group type events
        entries = [entry for entry in history['entries'] if entry['type'] in ('group-add', 'group-remove')]
        if not entries:
            continue
        print_h2('  Date: %s ' % time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime(epoch)))
        for entry in entries:
            _type = entry['type']
            op = {'group-add': '+', 'group-remove': '-'}.get(_type, '?')
            color = {'group-add': 'green', 'group-remove': 'red'}.get(_type, 'grey')
            cprint(' %s %s' % (op, entry['group']), color=color)
Esempio n. 2
0
def do_collectors_history():
    try:
        history_entries = get_opsbro_json('/agent/collectors/history')
    except get_request_errors() as exp:
        logger.error(exp)
        return

    print_h1('History')
    for history_entry in history_entries:
        epoch_date = history_entry['date']
        entries = history_entry['entries']
        print_h2(
            '  Date: %s ' %
            time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime(epoch_date)))
        for entry in entries:
            name = entry['name']

            cprint(' * ', end='')
            cprint('%s ' % (name.ljust(20)), color='magenta', end='')

            old_state = entry['old_state']
            c = COLLECTORS_STATE_COLORS.get(old_state, 'cyan')
            cprint('%s' % old_state.ljust(10), color=c, end='')

            cprint(' %s ' % CHARACTERS.arrow_left, color='grey', end='')

            state = entry['state']
            c = COLLECTORS_STATE_COLORS.get(state, 'cyan')
            cprint('%s' % state.ljust(10), color=c)

            # Now print output the line under
            log = entry['log']
            if log:
                cprint(' ' * 4 + '| ' + log, color='grey')
Esempio n. 3
0
def do_members_history():
    # The information is available only if the agent is started
    wait_for_agent_started(visual_wait=True)

    try:
        history_entries = get_opsbro_json('/agent/members/history')
    except get_request_errors() as exp:
        logger.error('Cannot join opsbro agent to show member history: %s' %
                     exp)
        sys.exit(1)

    print_h1('History')
    for history_entry in history_entries:
        epoch_date = history_entry['date']
        # We want only group type events
        entries = [
            entry for entry in history_entry['entries']
            if entry['type'] == 'node-state-change'
        ]
        if not entries:
            continue
        print_h2(
            '  Date: %s ' %
            time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime(epoch_date)))
        for entry in entries:
            name = entry['name']
            if entry.get('display_name', ''):
                name = '[ ' + entry.get('display_name') + ' ]'
            old_state = entry['old_state']
            new_state = entry['state']

            old_color = NODE_STATE_COLORS.get(old_state, 'cyan')
            old_state_prefix = NODE_STATE_PREFIXS.get(
                old_state, CHARACTERS.double_exclamation)

            new_color = NODE_STATE_COLORS.get(new_state, 'cyan')
            new_state_prefix = NODE_STATE_PREFIXS.get(
                new_state, CHARACTERS.double_exclamation)

            cprint('%s  ' % name.ljust(20), color='magenta', end='')
            cprint(('%s %s' % (old_state_prefix, old_state)).ljust(9),
                   color=old_color,
                   end='')  # 7 for the maximum state string + 2 for prefix

            cprint(' %s ' % CHARACTERS.arrow_left, color='grey', end='')

            cprint(('%s %s' % (new_state_prefix, new_state)).ljust(9),
                   color=new_color
                   )  # 7 for the maximum state string + 2 for prefix
Esempio n. 4
0
def do_compliance_history():
    uri = '/compliance/history'
    try:
        (code, r) = get_opsbro_local(uri)
    except get_request_errors() as exp:
        logger.error(exp)
        return

    try:
        histories = jsoner.loads(r)
    except ValueError as exp:  # bad json
        logger.error('Bad return from the server %s' % exp)
        return
    print_h1('Compliance history')

    for history in histories:
        epoch = history['date']
        entries = history['entries']
        print_h2('  Date: %s ' %
                 time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime(epoch)))
        entries_by_compliances = {}
        for entry in entries:
            compliance_name = entry['compliance_name']
            pack_name = entry['pack_name']
            mode = entry['mode']
            if compliance_name not in entries_by_compliances:
                entries_by_compliances[compliance_name] = {
                    'pack_name': pack_name,
                    'mode': mode,
                    'entries': []
                }
            entries_by_compliances[compliance_name]['entries'].append(entry)
        for (compliance_name, d) in entries_by_compliances.items():
            pack_name = d['pack_name']
            mode = d['mode']
            entries = d['entries']
            cprint('  - %s' % pack_name, color='blue', end='')
            cprint(' > compliance > ', color='grey', end='')
            cprint('[%s] ' % (compliance_name.ljust(30)),
                   color='magenta',
                   end='')
            cprint('[mode:%10s] ' % mode, color='magenta')
            for entry in entries:
                # rule_name = entry['name']
                # cprint('[%s] ' % (rule_name.ljust(30)), color='magenta', end='')
                __print_rule_entry(entry)
        cprint("\n")
Esempio n. 5
0
def do_history():
    uri = '/monitoring/history/checks'
    try:
        (code, r) = get_opsbro_local(uri)
    except get_request_errors() as exp:
        logger.error(exp)
        return
    
    try:
        history_entries = jsoner.loads(r)
    except ValueError as exp:  # bad json
        logger.error('Bad return from the server %s' % exp)
        return
    
    print_h1('History')
    for history_entry in history_entries:
        epoch_date = history_entry['date']
        entries = history_entry['entries']
        print_h2('  Date: %s ' % time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime(epoch_date)))
        for entry in entries:
            pname = entry['pack_name']
            
            check_display_name = entry['display_name']
            
            cprint('  - %s' % pname, color='blue', end='')
            cprint(' > checks > ', color='grey', end='')
            cprint('%s ' % (check_display_name.ljust(30)), color='magenta', end='')
            
            old_state = entry['old_state_id']
            c = STATE_ID_COLORS.get(old_state, 'cyan')
            old_state = STATE_ID_STRINGS.get(old_state, 'UNKNOWN')
            cprint('%s' % old_state.ljust(10), color=c, end='')
            
            cprint(' %s ' % CHARACTERS.arrow_left, color='grey', end='')
            
            state = entry['state_id']
            c = STATE_ID_COLORS.get(state, 'cyan')
            state = STATE_ID_STRINGS.get(state, 'UNKNOWN')
            cprint('%s' % state.ljust(10), color=c)
            
            # Now print output the line under
            output = entry['output']
            output_lines = output.strip().splitlines()
            for line in output_lines:
                cprint(' ' * 4 + '| ' + line, color='grey')
Esempio n. 6
0
def do_packs_list():
    from opsbro.packer import packer
    packs = packer.get_packs()
    all_pack_names = set()
    for (level, packs_in_level) in packs.items():
        for (pname, _) in packs_in_level.items():
            all_pack_names.add(pname)

    print_h2('Legend (topics)')
    for topic_id in VERY_ALL_TOPICS:
        color_id = topiker.get_color_id_by_topic_id(topic_id)
        label = TOPICS_LABELS[topic_id]
        s = u'%s %s %s' % (CHARACTERS.topic_small_picto, CHARACTERS.arrow_left,
                           label)
        color_s = lolcat.get_line(s, color_id, spread=None)
        cprint(color_s)

    print_h1('Packs')

    pnames = list(all_pack_names)
    pnames.sort()
    for pname in pnames:
        present_before = False
        keywords = []  # useless but make lint code check happy
        for level in ('core', 'global', 'zone', 'local'):
            if pname in packs[level]:
                (pack, _) = packs[level][pname]
                if present_before:
                    cprint('(overloaded by %s) ' % CHARACTERS.arrow_left,
                           color='green',
                           end='')
                __print_pack_breadcumb(pname,
                                       level,
                                       end='',
                                       topic_picto='small')
                keywords = pack['keywords']
            present_before = True
        cprint('[keywords: %s]' % (','.join(keywords)), color='magenta')
Esempio n. 7
0
def do_generators_history():
    uri = '/generators/history'
    try:
        (code, r) = get_opsbro_local(uri)
    except get_request_errors() as exp:
        logger.error(exp)
        return

    try:
        histories = jsoner.loads(r)
    except ValueError as exp:  # bad json
        logger.error('Bad return from the server %s' % exp)
        return
    print_h1('Generators history')

    for history in histories:
        epoch = history['date']
        entries = history['entries']
        print_h2('  Date: %s ' %
                 time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime(epoch)))
        for entry in entries:
            cprint('  - %s' % entry['pack_name'], color='blue', end='')
            __print_generator_entry(entry, show_diff=True)
        cprint("\n")