def manage_internal_check(self, hosts, services, check, hostgroups, servicegroups,
                              macromodulations, timeperiods):
        # pylint: disable=too-many-branches, too-many-statements, too-many-locals
        """Manage internal commands such as ::

        * bp_rule
        * _internal_host_up
        * _echo

        :param hosts: Used to create business rules
        :type hosts: alignak.objects.host.Hosts
        :param services: Used to create business rules
        :type services: alignak.objects.service.Services
        :param check: internal check to manage
        :type check: alignak.objects.check.Check
        :return: None
        """
        logger.debug("Internal check: %s - %s", self.get_full_name(), check.command)
        if os.getenv('ALIGNAK_LOG_CHECKS', None):
            logger.info("--ALC-- Internal check: %s - %s", self.get_full_name(), check.command)

        # Default is unknown state
        state = 3

        # Business rule
        if check.command.startswith('bp_'):
            try:
                # Re evaluate the business rule to take into account macro modulation.
                # Caution: We consider that the macro modulation did not
                # change business rule dependency tree. Only Xof: values should
                # be modified by modulation.
                self.create_business_rules(hosts, services, hostgroups, servicegroups,
                                           macromodulations, timeperiods, running=True)
                state = self.business_rule.get_state(hosts, services)
                check.output = self.get_business_rule_output(hosts, services,
                                                             macromodulations, timeperiods)
                if os.getenv('ALIGNAK_LOG_ACTIONS', None):
                    log_function = logger.info
                    if os.getenv('ALIGNAK_LOG_ACTIONS') == 'WARNING':
                        log_function = logger.warning
                    log_function("Resolved BR for '%s', output: %s",
                                 self.get_full_name(), check.output)

            except Exception as exp:  # pylint: disable=broad-except
                # Notifies the error, and return an UNKNOWN state.
                check.output = u"Error while re-evaluating business rule: %s" % exp
                logger.debug("[%s] Error while re-evaluating business rule:\n%s",
                             self.get_name(), traceback.format_exc())
                state = 3

        # _internal_host_up is for putting host as UP
        elif check.command == '_internal_host_up':
            state = 0
            check.execution_time = 0
            check.output = u'Host assumed to be UP'

            if os.getenv('ALIGNAK_LOG_ACTIONS', None):
                log_function = logger.info
                if os.getenv('ALIGNAK_LOG_ACTIONS') == 'WARNING':
                    log_function = logger.warning
                log_function("Set host %s as UP (internal check)", self.get_full_name())

        # Echo is just putting the same state again
        elif check.command == '_echo':
            state = self.state_id
            check.execution_time = 0
            check.output = self.output

            if os.getenv('ALIGNAK_LOG_ACTIONS', None):
                log_function = logger.info
                if os.getenv('ALIGNAK_LOG_ACTIONS') == 'WARNING':
                    log_function = logger.warning
                log_function("Echo the current state (%s - %d) for %s ",
                             self.state, self.state_id, self.get_full_name())

        # _internal_host_check is for having an host check result
        # without running a check plugin
        elif check.command.startswith('_internal_host_check'):
            # Command line contains: state_id;output
            check_result = check.command.split(';')
            if len(check_result) < 2:
                state = 3
                check.output = u'Malformed host internal check'
            else:
                # In SOFT state type, do not change current state - let the new state go to HARD
                if self.state_type == 'SOFT':
                    state = self.state_id
                else:
                    state = check_result[1].strip()
                    # If multiple possible states - choose a random one
                    if ',' in check_result[1]:
                        states = check_result[1].split(',')
                        if len(states) > 5:
                            states = states[0:5]

                        probability = {
                            2: [0.8, 0.2],
                            3: [0.7, 0.2, 0.1],
                            4: [0.6, 0.2, 0.1, 0.1],
                            5: [0.4, 0.2, 0.2, 0.1, 0.1]
                        }
                        probability = probability.get(len(states))

                        try:
                            state = numpy.random.choice(states, p=probability)
                        except Exception as exp:  # pylint: disable=broad-except
                            # If random configuration error, do not change the state
                            logger.warning("Randomly chosen state is not configured correctly "
                                           "for %s: %s", self.get_full_name(), state)
                            state = self.state_id

                    try:
                        state = int(state)
                    except ValueError:
                        pass

                check.output = u'Host internal check result: %d' % state
                if len(check_result) > 2 and check_result[2]:
                    check.output = check_result[2]
                    if '%d' in check.output:
                        check.output = check.output % state

            check.execution_time = 0
            if os.getenv('ALIGNAK_INTERNAL_HOST_PERFDATA', None):
                try:
                    max_range = int(os.getenv('ALIGNAK_INTERNAL_HOST_PERFDATA'))
                except ValueError:
                    max_range = 10
                check.perf_data = "'rnd_metric'=%d" % random.randint(0, max_range)

            if os.getenv('ALIGNAK_LOG_ACTIONS', None):
                log_function = logger.info
                if os.getenv('ALIGNAK_LOG_ACTIONS') == 'WARNING':
                    log_function = logger.warning
                log_function("Host %s internal check: %d - %s",
                             self.get_full_name(), state, check.output)

        # _internal_service_check is for having a service check result
        # without running a check plugin
        elif check.command.startswith('_internal_service_check'):
            # Command line contains: state_id;output
            check_result = check.command.split(';')
            if not check_result:
                state = 3
                check.output = u'Malformed service internal check'
            else:
                # In SOFT state type, do not change current state - let the new state go to HARD
                if self.state_type == 'SOFT':
                    state = self.state_id
                else:
                    state = check_result[1].strip()
                    # If multiple possible states - choose a random one
                    if ',' in check_result[1]:
                        states = check_result[1].split(',')
                        if len(states) > 5:
                            states = states[0:5]

                        probability = {
                            2: [0.8, 0.2],
                            3: [0.7, 0.2, 0.1],
                            4: [0.6, 0.2, 0.1, 0.1],
                            5: [0.4, 0.2, 0.2, 0.1, 0.1]
                        }
                        probability = probability.get(len(states))

                        try:
                            state = numpy.random.choice(states, p=probability)
                        except Exception as exp:  # pylint: disable=broad-except
                            # If random configuration error, do not change the state
                            logger.warning("Randomly chosen state is not configured correctly "
                                           "for %s: %s", self.get_full_name(), state)
                            state = self.state_id

                    try:
                        state = int(state)
                    except ValueError:
                        pass

                check.output = u'Service internal check result: %d' % state
                if len(check_result) > 2 and check_result[2]:
                    check.output = check_result[2]
                    if '%d' in check.output:
                        check.output = check.output % state

            check.execution_time = 0
            if os.getenv('ALIGNAK_INTERNAL_SERVICE_PERFDATA', None):
                try:
                    max_range = int(os.getenv('ALIGNAK_INTERNAL_SERVICE_PERFDATA'))
                except ValueError:
                    max_range = 10
                check.perf_data = "'rnd_metric'=%d" % random.randint(0, max_range)

            if os.getenv('ALIGNAK_LOG_ACTIONS', None):
                log_function = logger.info
                if os.getenv('ALIGNAK_LOG_ACTIONS') == 'WARNING':
                    log_function = logger.warning
                log_function("Service %s internal check: %d - %s",
                             self.get_full_name(), state, check.output)

        check.long_output = check.output
        check.check_time = time.time()
        check.exit_status = state