Example #1
0
def main():
    environ["KRB5_CLIENT_KTNAME"] = "/etc/krb5.keytab"
    environ["KRB5CCNAME"] = "MEMORY:"
    framework = object()
    plugins = []
    output = constants.DEFAULT_OUTPUT

    logger.setLevel(logging.INFO)

    options = parse_options(output_registry)

    if options.debug:
        logger.setLevel(logging.DEBUG)

    config = read_config()
    if config is None:
        sys.exit(1)

    if not (options.source
            or options.list_sources) and not is_ipa_configured():
        logging.error("IPA is not configured on this system.")
        sys.exit(1)

    for name, registry in find_registries().items():
        try:
            registry.initialize(framework)
        except Exception as e:
            print("Unable to initialize %s: %s" % (name, e))
            sys.exit(1)
        for plugin in find_plugins(name, registry):
            plugins.append(plugin)

    for out in output_registry.plugins:
        if out.__name__.lower() == options.output:
            output = out(options)

    if options.list_sources:
        return list_sources(plugins)

    if options.infile:
        try:
            with open(options.infile, 'r') as f:
                raw_data = f.read()

            json_data = json.loads(raw_data)
            results = json_to_results(json_data)
            available = ()
        except Exception as e:
            print("Unable to import '%s': %s" % (options.infile, e))
            sys.exit(1)
        if options.source:
            results = limit_results(results, options.source, options.check)
    else:
        results, available = run_service_plugins(plugins, config,
                                                 options.source, options.check)
        results.extend(
            run_plugins(plugins, config, available, options.source,
                        options.check))

    if options.source and len(results.results) == 0:
        for plugin in plugins:
            if not source_or_check_matches(plugin, options.source,
                                           options.check):
                continue

            if not set(plugin.requires).issubset(available):
                print("Source '%s' is missing one or more requirements '%s'" %
                      (options.source, ', '.join(plugin.requires)))
                sys.exit(1)

        if options.check:
            print("Check '%s' not found in Source '%s'" %
                  (options.check, options.source))
        else:
            print("Source '%s' not found" % options.source)
        sys.exit(1)

    try:
        output.render(results)
    except Exception as e:
        logger.error('Output raised %s: %s', e.__class__.__name__, e)

    return_value = 0
    for result in results.results:
        if result.result != constants.SUCCESS:
            return_value = 1
            break

    sys.exit(return_value)
Example #2
0
    def run_healthcheck(self):
        framework = object()
        plugins = []
        output = constants.DEFAULT_OUTPUT

        logger.setLevel(logging.INFO)

        add_default_options(self.parser, self.output_registry,
                            self.default_output)
        add_output_options(self.parser, self.output_registry)
        self.add_options()
        options = parse_options(self.parser)
        self.options = options
        rval = self.validate_options()
        if rval:
            return rval

        if options.debug:
            logger.setLevel(logging.DEBUG)

        config = read_config(self.configfile)
        if config is None:
            return 1

        rval = self.pre_check()
        if rval is not None:
            return rval

        for name, registry in find_registries(self.entry_points).items():
            try:
                registry.initialize(framework, config, options)
            except Exception as e:
                print("Unable to initialize %s: %s" % (name, e))
                return 1
            for plugin in find_plugins(name, registry):
                plugins.append(plugin)

        for out in self.output_registry.plugins:
            if out.__name__.lower() == options.output:
                output = out(options)
                break

        if options.list_sources:
            return list_sources(plugins)

        if 'infile' in options and options.infile:
            try:
                with open(options.infile, 'r') as f:
                    raw_data = f.read()

                json_data = json.loads(raw_data)
                results = json_to_results(json_data)
                available = ()
            except Exception as e:
                print("Unable to import '%s': %s" % (options.infile, e))
                return 1
            if options.source:
                results = limit_results(results, options.source, options.check)
        else:
            results, available = run_service_plugins(plugins, options.source,
                                                     options.check)
            results.extend(
                run_plugins(plugins, available, options.source, options.check))

        if options.source and len(results.results) == 0:
            for plugin in plugins:
                if not source_or_check_matches(plugin, options.source,
                                               options.check):
                    continue

                if not set(plugin.requires).issubset(available):
                    print("Source '%s' is missing one or more requirements "
                          "'%s'" %
                          (options.source, ', '.join(plugin.requires)))
                    return 1

            if options.check:
                print("Check '%s' not found in Source '%s'" %
                      (options.check, options.source))
            else:
                print("Source '%s' not found" % options.source)
            return 1

        try:
            output.render(results)
        except Exception as e:
            logger.error('Output raised %s: %s', e.__class__.__name__, e)

        return_value = 0
        for result in results.results:
            if result.result != constants.SUCCESS:
                return_value = 1
                break

        return return_value
Example #3
0
    def run_healthcheck(self):
        framework = object()
        plugins = []
        output = constants.DEFAULT_OUTPUT

        logger.setLevel(logging.WARNING)

        add_default_options(self.parser, self.output_registry,
                            self.default_output)
        add_output_options(self.parser, self.output_registry)
        self.add_options()
        options = parse_options(self.parser)
        self.options = options

        if options.version:
            for registry in self.entry_points:
                name = registry.split('.')[0]
                try:
                    version = pkg_resources.get_distribution(name).version
                except pkg_resources.DistributionNotFound:
                    continue
                print('%s: %s' % (name, version))
            return 0

        # pylint: disable=assignment-from-none
        rval = self.validate_options()
        # pylint: enable=assignment-from-none
        if rval is not None:
            return rval

        if options.verbose:
            logger.setLevel(logging.INFO)

        if options.debug:
            logger.setLevel(logging.DEBUG)

        config = read_config(self.configfile)
        if config is None:
            return 1

        # pylint: disable=assignment-from-none
        rval = self.pre_check()
        # pylint: enable=assignment-from-none
        if rval is not None:
            return rval

        # If we have IPA configured without a CA then we want to skip
        # the pkihealthcheck plugins otherwise they will generated a
        # lot of false positives. The IPA plugins are loaded first so
        # which should set ca_configured in its registry to True or
        # False. We will skip the pkihealthcheck plugins only if
        # ca_configured is False which means that it was set by IPA.
        ca_configured = None
        for name, registry in find_registries(self.entry_points).items():
            try:
                registry.initialize(framework, config, options)
            except Exception as e:
                warnings.warn("Trying deprecated initialization API: %s" % e,
                              DeprecationWarning)
                try:
                    registry.initialize(framework, config)
                except Exception as e:
                    logger.error("Unable to initialize %s: %s", name, e)
                    continue
            if hasattr(registry, 'ca_configured'):
                ca_configured = registry.ca_configured
            if 'pkihealthcheck' in name and ca_configured is False:
                logger.debug('IPA CA is not configured, skipping %s', name)
                continue
            for plugin in find_plugins(name, registry):
                plugins.append(plugin)

        for out in self.output_registry.plugins:
            if out.__name__.lower() == options.output:
                output = out(options)
                break

        if options.list_sources:
            return list_sources(plugins)

        if 'infile' in options and options.infile:
            try:
                with open(options.infile, 'r') as f:
                    raw_data = f.read()

                json_data = json.loads(raw_data)
                results = json_to_results(json_data)
                available = ()
            except Exception as e:
                print("Unable to import '%s': %s" % (options.infile, e))
                return 1
            if options.source:
                results = limit_results(results, options.source, options.check)
        else:
            results, available = run_service_plugins(plugins, options.source,
                                                     options.check)
            results.extend(
                run_plugins(plugins, available, options.source, options.check))

        if options.source and len(results.results) == 0:
            for plugin in plugins:
                if not source_or_check_matches(plugin, options.source,
                                               options.check):
                    continue

                if not set(plugin.requires).issubset(available):
                    print("Source '%s' is missing one or more requirements "
                          "'%s'" %
                          (options.source, ', '.join(plugin.requires)))
                    return 1

            if options.check:
                print("Check '%s' not found in Source '%s'" %
                      (options.check, options.source))
            else:
                print("Source '%s' not found" % options.source)
            return 1

        try:
            output.render(results)
        except Exception as e:
            logger.error('Output raised %s: %s', e.__class__.__name__, e)

        return_value = 0
        for result in results.results:
            if result.result != constants.SUCCESS:
                return_value = 1
                break

        return return_value