예제 #1
0
파일: module.py 프로젝트: naparuba/opsbro
    def __export_states_into_shinken(self, nuuid):
        p = self.external_command_file

        v = kvmgr.get_key('__health/%s' % nuuid)
        if v is None or v == '':
            self.logger.error('Cannot access to the checks list for', nuuid)
            return

        lst = jsoner.loads(v)
        for cname in lst:
            v = kvmgr.get_key('__health/%s/%s' % (nuuid, cname))
            if v is None:  # missing check entry? not a real problem
                continue
            check = jsoner.loads(v)
            self.logger.debug('CHECK VALUE %s' % check)
            try:
                mode = 'w' if PY3 else 'a'  # codecs.open got issue with a in python 3
                f = codecs.open(p, mode, encoding="utf-8")
                cmd = '[%s] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n' % (
                    int(time.time()), nuuid, self.sanatize_check_name(cname),
                    check['state_id'], check['output'])
                self.logger.debug('SAVING COMMAND %s' % cmd)
                f.write(cmd)
                f.flush()
                f.close()
            except Exception as exp:
                self.logger.error('Shinken command file write fail: %s' % exp)
                return
예제 #2
0
    def launch(self):
        logger = self.logger
        logger.debug('getRabbitMQStatus: start')

        if not self.is_in_group('rabbitmq'):
            self.set_not_eligible(
                'Please add the rabbitmq group to enable this collector.')
            return

        try:
            uri = self.get_parameter('uri')
            user = self.get_parameter('user')
            password = self.get_parameter('password')
            response = httper.get(uri, timeout=3, user=user, password=password)

        except get_http_exceptions() as e:
            self.set_error('Unable to get RabbitMQ status - HTTPError = %s' %
                           e)
            return False

        except Exception:
            self.set_error('Unable to get RabbitMQ status - Exception = %s' %
                           traceback.format_exc())
            return False

        try:
            status = jsoner.loads(response)
        except Exception as exp:
            self.set_error("Rabbitmq: parsing json: %s" % exp)
            return False

        return status
예제 #3
0
    def get_meta_data(self):
        if self.__meta_data is not None:
            return self.__meta_data

        uri = 'http://metadata.google.internal/computeMetadata/v1/?recursive=true'
        try:
            s = httper.get(uri, headers={'Metadata-Flavor': 'Google'})
        except get_http_exceptions() as exp:
            self.logger.error(
                'Cannot get pubic IP for your Azure instance from %s. Error: %s.Exiting'
                % (uri, exp))
            raise
        raw_data = jsoner.loads(s)

        # We want to merge the structure into a more flatten one between compute and network
        self.__meta_data = raw_data['instance']

        # we want only the short name of the machine type
        self.__meta_data['machineType'] = self.__meta_data[
            'machineType'].split('/')[-1]
        self.__meta_data['zone'] = self.__meta_data['zone'].split('/')[-1]

        del self.__meta_data['serviceAccounts']
        del self.__meta_data['virtualClock']
        del self.__meta_data['licenses']
        del self.__meta_data['disks']

        return self.__meta_data
예제 #4
0
파일: cli.py 프로젝트: naparuba/opsbro
def do_compliance_state(compliance_name=''):
    uri = '/compliance/state'
    try:
        (code, r) = get_opsbro_local(uri)
    except get_request_errors() as exp:
        logger.error(exp)
        return

    try:
        compliances = jsoner.loads(r)
    except ValueError as exp:  # bad json
        logger.error('Bad return from the server %s' % exp)
        return
    print_h1('Compliances')
    packs = {}
    for (cname, compliance) in compliances.items():
        if compliance_name and compliance['name'] != compliance_name:
            continue
        pack_name = compliance['pack_name']
        if pack_name not in packs:
            packs[pack_name] = {}
        packs[pack_name][cname] = compliance
    pnames = list(packs.keys())  # python3
    pnames.sort()
    for pname in pnames:
        pack_entries = packs[pname]
        cprint('* Pack %s' % pname, color='blue')
        cnames = list(pack_entries.keys())  # python3
        cnames.sort()
        for cname in cnames:
            cprint('  - %s' % pname, color='blue', end='')
            compliance = pack_entries[cname]
            __print_compliance_entry(compliance)
예제 #5
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')
예제 #6
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)
예제 #7
0
파일: module.py 프로젝트: naparuba/opsbro
 def get_ts_values():
     self.logger.info('CALLING /imrane POST')
     try:
         data_raw = request.body.getvalue()
         self.logger.info('POST: get body value: %s' % data_raw)
         data = jsoner.loads(data_raw)
         self.logger.info('POST: get results: %s' % data)
         self._import_data(data)
     except:
         self.logger.error('IMRANE: ERROR %s' % traceback.format_exc())
     return None
예제 #8
0
    def get_data_sources_from_grafana(self):
        uri = '%s/api/datasources' % (self.uri)
        our_data_sources = {}
        try:
            api_return = httper.get(uri, headers=self.__get_headers())
            try:
                all_data_sources = jsoner.loads(api_return)
            except (ValueError, TypeError) as exp:
                self.logger.error(
                    'Cannot load json from grafana datasources: %s' % exp)
                return None
        except get_http_exceptions() as exp:
            self.logger.error('Cannot connect to grafana datasources: %s' %
                              exp)
            return None
        self.logger.debug("All data sources")
        self.logger.debug(str(all_data_sources))
        # Error message is a dict with just a key: message
        if isinstance(all_data_sources, dict):
            error_message = all_data_sources.get('message', '')
            if error_message:
                if error_message == 'Unauthorized':
                    self.logger.error(
                        'Your API key is not autorized to list data sources.')
                    return None
                self.logger.error('Unknown error from grafana API: %s' %
                                  error_message)
                return None

        # A data source will look like this:
        # [{u'name'    : u'SuperBla',
        ##  u'database': u'',
        # u'url': u'http://super:6768',
        #  u'basicAuth': False,
        # u'jsonData': {},
        # u'access': u'proxy',
        # u'typeLogoUrl': u'public/app/plugins/datasource/graphite/img/graphite_logo.png',
        # u'orgId': 1,
        # u'user': u'',
        #  u'password': u'',
        # u'type': u'graphite',
        #  u'id': 1,
        # u'isDefault': False}]
        for data_source in all_data_sources:
            if data_source.get('type', '') != 'graphite':
                continue
            src_name = data_source.get('name', '')
            if '--opsbro--' in src_name:
                elts = src_name.split('--opsbro--')
                if len(elts) == 2:
                    nuuid = elts[1]
                    our_data_sources[nuuid] = data_source
        return our_data_sources
예제 #9
0
def do_evaluator_list(details=False):
    try:
        (code, r) = get_opsbro_local('/agent/evaluator/list')
    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
    
    # Group by
    groups = {}
    for f in d:
        fname = f['name']
        gname = f['group']
        if gname not in groups:
            groups[gname] = {}
        groups[gname][fname] = f
    
    gnames = groups.keys()
    gnames.sort()
    
    for gname in gnames:
        print_h1(gname)
        group_functions = groups[gname]
        fnames = group_functions.keys()
        fnames.sort()
        
        for fname in fnames:
            f = group_functions[fname]
            prototype = f['prototype']
            doc = f['doc']
            
            cprint(fname, color='green', end='')
            cprint('(', end='')
            if prototype:
                _s_args = []
                for arg in prototype:
                    kname = arg[0]
                    def_value = arg[1]
                    if def_value != '__NO_DEFAULT__':
                        _s_args.append('%s=%s' % (kname, def_value))
                    else:
                        _s_args.append('%s' % kname)
                cprint(', '.join(_s_args), color='yellow', end='')
            cprint(')')
            if details:
                cprint(doc, color='grey')
예제 #10
0
    def get_meta_data(self):
        if self.__meta_data is not None:
            return self.__meta_data

        uri = 'http://169.254.169.254/metadata/v1.json'
        try:
            s = httper.get(uri)
        except get_http_exceptions() as exp:
            self.logger.error(
                'Cannot get meta data for your digital ocean instance from %s. Error: %s.Exiting'
                % (uri, exp))
            raise
        self.__meta_data = jsoner.loads(s)
        return self.__meta_data
예제 #11
0
파일: cli.py 프로젝트: naparuba/opsbro
def do_wait_ok(check_name, timeout=30):
    import itertools
    spinners = itertools.cycle(CHARACTERS.spinners)
    
    name = check_name
    
    state_string = 'UNKNOWN'
    for i in range(timeout):
        
        uri = '/monitoring/state'
        try:
            (code, r) = get_opsbro_local(uri)
        except get_request_errors() as exp:
            logger.error(exp)
            return
        
        try:
            states = jsoner.loads(r)
        except ValueError as exp:  # bad json
            logger.error('Bad return from the server %s' % exp)
            return
        checks = states['checks']
        check = None
        for (cname, c) in checks.items():
            if c['display_name'] == name:
                check = c
        if not check:
            logger.error("Cannot find the check '%s'" % name)
            sys.exit(2)
        state_id = check['state_id']
        
        c = STATE_ID_COLORS.get(state_id, 'cyan')
        state_string = STATE_ID_STRINGS.get(state_id, 'UNKNOWN')
        
        cprint('\r %s ' % next(spinners), color='blue', end='')
        cprint('%s' % name, color='magenta', end='')
        cprint(' is ', end='')
        cprint('%s' % state_string.ljust(10), color=c, end='')
        cprint(' (%d/%d)' % (i, timeout), end='')
        # As we did not \n, we must flush stdout to print it
        sys.stdout.flush()
        if state_id == 0:
            cprint("\nThe check %s is OK" % name)
            sys.exit(0)
        logger.debug("Current state %s" % state_id)
        
        time.sleep(1)
    cprint("\nThe check %s is not OK after %s seconds (currently %s)" % (name, timeout, state_string))
    sys.exit(2)
예제 #12
0
    def get_conf(self):
        if self.conf is not None:
            return self.conf

        uri = 'http://169.254.42.42/conf?format=json'
        try:
            s = httper.get(uri)
        except get_http_exceptions() as exp:
            self.logger.error(
                'Cannot get pubic IP for your Scaleway instance from %s. Error: %s.Exiting'
                % (uri, exp))
            raise
        self.conf = jsoner.loads(s)
        self.logger.info('Get scaleway conf: %s' % self.conf)
        return self.conf
예제 #13
0
def parse_json_file(path):
    """**parse_json_file(path)** -> return the object read from the file at path

 * path: (string) path of the json file to parse

<code>
    Example:
        parse_json_file('/tmp/my_file.json')
    Returns:
        {'key':'value'}
</code>
    """
    with codecs.open(path, 'r', 'utf8') as f:
        o = jsoner.loads(f.read())
    return o
예제 #14
0
파일: cli.py 프로젝트: naparuba/opsbro
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")
예제 #15
0
파일: cli.py 프로젝트: naparuba/opsbro
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')
예제 #16
0
파일: cli.py 프로젝트: naparuba/opsbro
def do_generators_wait_compliant(generator_name, timeout=30):
    import itertools
    spinners = itertools.cycle(CHARACTERS.spinners)

    current_state = 'UNKNOWN'
    for i in range(timeout):
        uri = '/generators/state'
        try:
            (code, r) = get_opsbro_local(uri)
        except get_request_errors() as exp:
            logger.error(exp)
            return

        try:
            generators = jsoner.loads(r)
        except ValueError as exp:  # bad json
            logger.error('Bad return from the server %s' % exp)
            return
        generator = None
        for (cname, c) in generators.items():
            if c['name'] == generator_name:
                generator = c
        if not generator:
            logger.error("Cannot find the generator '%s'" % generator_name)
            sys.exit(2)
        current_state = generator['state']
        cprint('\r %s ' % next(spinners), color='blue', end='')
        cprint('%s' % generator_name, color='magenta', end='')
        cprint(' is ', end='')
        cprint('%15s ' % current_state,
               color=GENERATOR_STATE_COLORS.get(current_state, 'cyan'),
               end='')
        cprint(' (%d/%d)' % (i, timeout), end='')
        # As we did not \n, we must flush stdout to print it
        sys.stdout.flush()
        if current_state == 'COMPLIANT':
            cprint("\nThe generator %s is compliant" % generator_name)
            sys.exit(0)
        logger.debug("Current state %s" % current_state)

        time.sleep(1)
    cprint(
        "\nThe generator %s is not compliant after %s seconds (currently %s)" %
        (generator_name, timeout, current_state))
    sys.exit(2)
예제 #17
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')
예제 #18
0
파일: cli.py 프로젝트: genothomas/opsbro
def do_exec(group='*', cmd='uname -a'):
    if cmd == '':
        logger.error('Missing command')
        return
    # The information is available only if the agent is started
    wait_for_agent_started(visual_wait=True)

    try:
        (code, r) = get_opsbro_local('/exec/%s?cmd=%s' % (group, cmd))
    except get_request_errors() as exp:
        logger.error(exp)
        return
    print(r)
    cid = r
    print("Command group launch as cid", cid)
    time.sleep(5)  # TODO: manage a real way to get the result..
    try:
        (code, r) = get_opsbro_local('/exec-get/%s' % cid)
    except get_request_errors() as exp:
        logger.error(exp)
        return
    j = jsoner.loads(r)

    res = j['res']
    for (uuid, e) in res.items():
        node = e['node']
        nname = node['name']
        color = {
            'alive': 'green',
            'dead': 'red',
            'suspect': 'yellow',
            'leave': 'cyan'
        }.get(node['state'], 'cyan')
        cprint(nname, color=color)
        cprint('Return code:', end='')
        color = {0: 'green', 1: 'yellow', 2: 'red'}.get(e['rc'], 'cyan')
        cprint(e['rc'], color=color)
        cprint('Output:', end='')
        cprint(e['output'].strip(), color=color)
        if e['err']:
            cprint('Error:', end='')
            cprint(e['err'].strip(), color='red')
        cprint('')
예제 #19
0
    def get_meta_data(self):
        if self.__meta_data is not None:
            return self.__meta_data

        uri = 'http://169.254.169.254/metadata/instance?api-version=2017-08-01'
        try:
            s = httper.get(uri, headers={'Metadata': 'True'})
        except get_http_exceptions() as exp:
            self.logger.error(
                'Cannot get pubic IP for your Azure instance from %s. Error: %s.Exiting'
                % (uri, exp))
            raise
        raw_data = jsoner.loads(s)

        # We want to merge the structure into a more flatten one between compute and network
        self.__meta_data = raw_data['compute']
        first_network_interface = raw_data['network']['interface'][0]
        self.__meta_data.update(first_network_interface)

        return self.__meta_data
예제 #20
0
파일: cli.py 프로젝트: naparuba/opsbro
def do_join(seed=''):
    if seed == '':
        logger.error('Missing target argument. For example 192.168.0.1:6768')
        return
    # The information is available only if the agent is started
    wait_for_agent_started(visual_wait=True)

    try:
        (code, r) = get_opsbro_local('/agent/join/%s' % seed)
    except get_request_errors() as exp:
        logger.error(exp)
        return
    try:
        b = jsoner.loads(r)
    except ValueError as exp:  # bad json
        logger.error('Bad return from the server %s' % exp)
        return
    cprint('Joining %s : ' % seed, end='')
    if b:
        cprint('OK', color='green')
    else:
        cprint('FAILED', color='red')
예제 #21
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)
예제 #22
0
파일: cli.py 프로젝트: naparuba/opsbro
def do_generators_state(show_diff=False):
    uri = '/generators/state'
    try:
        (code, r) = get_opsbro_local(uri)
    except get_request_errors() as exp:
        logger.error(exp)
        return

    try:
        generators = jsoner.loads(r)
    except ValueError as exp:  # bad json
        logger.error('Bad return from the server %s' % exp)
        return
    print_h1('Generators')
    packs = {}
    for (cname, generator) in generators.items():
        pack_name = generator['pack_name']
        if pack_name not in packs:
            packs[pack_name] = {}
        packs[pack_name][cname] = generator
    pnames = list(packs.keys())
    pnames.sort()
    for pname in pnames:
        pack_entries = packs[pname]
        cprint('* Pack %s' % pname, color='blue')
        cnames = list(pack_entries.keys())
        cnames.sort()
        for cname in cnames:
            cprint('  - %s' % pname, color='blue', end='')
            generator = pack_entries[cname]
            __print_generator_entry(generator, show_diff=show_diff)

    if not show_diff:
        cprint('')
        cprint(
            '  | Note: you can see the modification diff with the --show-diff parameter',
            color='grey')
예제 #23
0
파일: cli.py 프로젝트: naparuba/opsbro
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")
예제 #24
0
파일: cli.py 프로젝트: naparuba/opsbro
def do_compliance_wait_compliant(compliance_name, timeout=30, exit_if_ok=True):
    import itertools
    spinners = itertools.cycle(CHARACTERS.spinners)

    current_state = COMPLIANCE_STATES.UNKNOWN
    for i in range(timeout):  # no xrange in python3
        uri = '/compliance/state'
        try:
            (code, r) = get_opsbro_local(uri)
        except get_request_errors() as exp:
            logger.error(exp)
            return

        try:
            compliances = jsoner.loads(r)
        except ValueError as exp:  # bad json
            logger.error('Bad return from the server %s' % exp)
            return
        compliance = None
        for (cname, c) in compliances.items():
            if c['name'] == compliance_name:
                compliance = c
        if not compliance:
            logger.error("Cannot find the compliance '%s'" % compliance_name)
            sys.exit(2)
        logger.debug('Current compliance data', compliance)
        current_step = ''
        current_state = compliance['state']
        if compliance['is_running']:
            current_state = COMPLIANCE_STATES.RUNNING
            current_step = compliance['current_step']
        # Clean line
        try:
            terminal_height, terminal_width = get_terminal_size()
        except:  # beware, maybe we don't have a tty
            terminal_width = 100
        cprint('\r' + ' ' * terminal_width, end='')
        cprint('\r %s ' % next(spinners), color='blue',
               end='')  # no spinnner.next in python3
        cprint('%s' % compliance_name, color='magenta', end='')
        cprint(' is ', end='')
        cprint('%15s ' % current_state,
               color=COMPLIANCE_STATE_COLORS.get(current_state, 'cyan'),
               end='')
        cprint(' (%d/%d)' % (i, timeout), end='')
        if current_step:
            cprint(' [ in step ', end='')
            cprint(current_step, color='magenta', end='')
            cprint(' ]', end='')
        # As we did not \n, we must flush stdout to print it
        sys.stdout.flush()
        if current_state == COMPLIANCE_STATES.COMPLIANT:
            cprint("\nThe compliance rule %s is compliant" % compliance_name)
            if exit_if_ok:
                sys.exit(0)
            else:
                return
        logger.debug("Current state %s" % current_state)

        time.sleep(1)
    cprint(
        "\nThe compliance rule %s is not compliant after %s seconds (currently %s)"
        % (compliance_name, timeout, current_state))
    sys.exit(2)
예제 #25
0
 def do_render(targets, _from):
     response.content_type = 'application/json'
     
     if not targets:
         return abort(400, 'Invalid target')
     # Default past values, round at an hour
     now = int(time.time())
     pastraw = int(time.time()) - 86400
     past = divmod(pastraw, 3600)[0] * 3600
     
     found = False
     # Try -Xd
     m = re.match(r'-(\d*)d', _from, re.M | re.I)
     if m:
         found = True
         nbdays = int(m.group(1))
         pastraw = int(time.time()) - (nbdays * 86400)
         past = divmod(pastraw, 86400)[0] * 86400
     # Try -Xh
     m = re.match(r'-(\d*)h', _from, re.M | re.I)
     if m:
         found = True
         nbhours = int(m.group(1))
         pastraw = int(time.time()) - (nbhours * 3600)
         past = divmod(pastraw, 3600)[0] * 3600
     # Try -Xhours
     if not found:
         m = re.match(r'-(\d*)hours', _from, re.M | re.I)
         if m:
             found = True
             nbhours = int(m.group(1))
             pastraw = int(time.time()) - (nbhours * 3600)
             past = divmod(pastraw, 3600)[0] * 3600
     # Try -Xmin
     if not found:
         m = re.match(r'-(\d*)min', _from, re.M | re.I)
         if m:
             found = True
             nbminutes = int(m.group(1))
             pastraw = int(time.time()) - (nbminutes * 60)
             past = divmod(pastraw, 60)[0] * 60
     # absolute value maybe?
     if not found:
         m = re.match(r'(\d*)', _from, re.M | re.I)
         if m:
             found = True
             past = divmod(int(m.group(1)), 3600)[0] * 3600
     
     if not found:
         return abort(400, 'Invalid range')
     
     # Ok now got the good values
     res = []
     for target in targets:
         
         nuuid = gossiper.find_group_node('ts', target)
         n = None
         if nuuid:
             n = gossiper.get(nuuid)
         nname = ''
         if n:
             nname = n['name']
         self.logger.debug('HTTP ts: target %s is managed by %s(%s)' % (target, nname, nuuid))
         # that's me or the other is no more there?
         if nuuid == gossiper.uuid or n is None:
             self.logger.debug('HTTP ts: /render, my job to manage %s' % target)
             
             # Maybe I am also the TS manager of these data? if so, get the TS backend data for this
             min_e = hour_e = day_e = None
             
             self.logger.debug('HTTP RENDER founded TS %s' % tsmgr.tsb.data)
             min_e = tsmgr.tsb.data.get('min::%s' % target, None)
             hour_e = tsmgr.tsb.data.get('hour::%s' % target, None)
             day_e = tsmgr.tsb.data.get('day::%s' % target, None)
             self.logger.debug('HTTP TS RENDER, FOUNDED TS data %s %s %s' % (min_e, hour_e, day_e))
             
             # Get from the past, but start at the good hours offset
             t = past
             r = []
             
             while t < now:
                 # Maybe the time match a hour we got in memory, if so take there
                 if hour_e and hour_e['hour'] == t:
                     self.logger.debug('HTTP TS RENDER match memory HOUR, take this value instead')
                     raw_values = hour_e['values'][:]  # copy instead of cherrypick, because it can move/append
                     for i in range(60):
                         # Get teh value and the time
                         e = raw_values[i]
                         tt = t + 60 * i
                         r.append((e, tt))
                         if e:
                             self.logger.debug('GOT NOT NULL VALUE from RENDER MEMORY cache %s:%s' % (e, tt))
                 else:  # no memory match, got look in the KS part
                     ukey = '%s::h%d' % (target, t)
                     raw64 = kvmgr.get_key(ukey)
                     if raw64 is None:
                         for i in range(60):
                             # Get the value and the time
                             tt = t + 60 * i
                             r.append((None, tt))
                     else:
                         raw = base64.b64decode(raw64)
                         v = pickle.loads(raw)
                         raw_values = v['values']
                         for i in range(60):
                             # Get teh value and the time
                             e = raw_values[i]
                             tt = t + 60 * i
                             r.append((e, tt))
                 # Ok now the new hour :)
                 t += 3600
             # Now build the final thing
             res.append({"target": target, "datapoints": r})
         else:  # someone else job, rely the question
             uri = 'http://%s:%s/render/?target=%s&from=%s' % (n['addr'], n['port'], target, _from)
             try:
                 self.logger.debug('TS: (get /render) relaying to %s: %s' % (n['name'], uri))
                 r = httper.get(uri)
                 self.logger.debug('TS: get /render founded (%d)' % len(r))
                 v = jsoner.loads(r)
                 self.logger.debug("TS /render relay GOT RETURN", v, "AND RES", res)
                 res.extend(v)
                 self.logger.debug("TS /render res is now", res)
             except get_http_exceptions() as exp:
                 self.logger.debug('TS: /render relay error asking to %s: %s' % (n['name'], str(exp)))
                 continue
     
     self.logger.debug('TS RENDER FINALLY RETURN', res)
     return jsoner.dumps(res)
예제 #26
0
파일: cli.py 프로젝트: naparuba/opsbro
def do_state(name=''):
    uri = '/monitoring/state/%s' % name
    if not name:
        uri = '/monitoring/state'
    try:
        (code, r) = get_opsbro_local(uri)
    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
    
    services = d['services']
    print_h1('Services')
    if len(services) == 0:
        cprint('No services', color='grey')
    else:
        for (sname, service) in services.items():
            state = service['state_id']
            cprint('\t%s ' % sname.ljust(20), end='')
            c = {0: 'green', 2: 'red', 1: 'yellow', 3: 'cyan'}.get(state, 'cyan')
            state = {0: 'OK', 2: 'CRITICAL', 1: 'WARNING', 3: 'UNKNOWN'}.get(state, 'UNKNOWN')
            cprint('%s - ' % state.ljust(8), color=c, end='')
            output = service['check']['output']
            cprint(output.strip(), color='grey')
    
    checks = d['checks']
    if len(checks) == 0:
        cprint('No checks', color='grey')
        return  # nothing to do more
    
    print_h1('Checks')
    packs = {}
    for (cname, check) in checks.items():
        pack_name = check['pack_name']
        if pack_name not in packs:
            packs[pack_name] = {}
        packs[pack_name][cname] = check
    pnames = list(packs.keys())
    pnames.sort()
    for pname in pnames:
        pack_entries = packs[pname]
        cprint('* Pack %s' % pname, color='blue')
        cnames = list(pack_entries.keys())
        cnames.sort()
        for cname in cnames:
            check = pack_entries[cname]
            check_display_name = check['display_name']
            
            cprint('  - %s' % pname, color='blue', end='')
            cprint(' > checks > ', color='grey', end='')
            cprint('%s ' % (check_display_name.ljust(30)), color='magenta', end='')
            
            state = check['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 = check['output']
            output_lines = output.strip().splitlines()
            for line in output_lines:
                cprint(' ' * 4 + '| ' + line, color='grey')
예제 #27
0
파일: __init__.py 프로젝트: naparuba/opsbro
 def __init__(self, body):
     self.raw = body
     self.body = jsoner.loads(body)
     self.successful = self.body['ok']
     self.error = self.body.get('error')