def test_host_macros(self):
        """Test host macros
        :return:
        """
        (svc, hst) = self.get_hst_svc()
        data = [hst, svc]

        # First group name
        dummy_call = "special_macro!$HOSTGROUPNAME$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert macros_command == 'plugins/nothing allhosts'

        # All group names
        dummy_call = "special_macro!$HOSTGROUPNAMES$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert macros_command == 'plugins/nothing allhosts,hostgroup_01,up'

        # First group alias
        dummy_call = "special_macro!$HOSTGROUPALIAS$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert macros_command == 'plugins/nothing All Hosts'

        # All group aliases
        dummy_call = "special_macro!$HOSTGROUPALIASES$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert macros_command == 'plugins/nothing All Hosts,All Up Hosts,hostgroup_alias_01'
    def test_service_custom_macros(self):
        """
        Test on-demand macros with custom variables for services
        :return:
        """
        (svc, hst) = self.get_hst_svc()

        # Get the second service
        svc2 = self._arbiter.conf.services.find_srv_by_name_and_hostname(
            "test_host_0", "test_another_service"
        )
        data = [hst, svc2]

        # Parse custom macro to get service custom variables based upon a fixed value
        # special_macro is defined as: $USER1$/nothing $ARG1$
        dummy_call = "special_macro!$_SERVICECUSTOM1$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing value' == macros_command

        # Parse custom macro to get service custom variables based upon another macro
        dummy_call = "special_macro!$_SERVICECUSTOM2$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing test_host_0' == macros_command
    def test_unicode_macro(self):
        """
        Call the resolver with a unicode content
        :return:
        """
        (svc, hst) = self.get_hst_svc()
        data = [hst, svc]

        hst.state = 'UP'
        hst.output = u"На берегу пустынных волн"
        dummy_call = "special_macro!$HOSTOUTPUT$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        # Output is correctly restitued
        assert u'plugins/nothing На берегу пустынных волн' == macros_command


        hst.state = 'UP'
        hst.output = 'Père Noël'
        dummy_call = "special_macro!$HOSTOUTPUT$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        # Output is correctly restitued
        assert u'plugins/nothing Père Noël' == macros_command

        hst.state = 'UP'
        hst.output = 'Père Noël'
        dummy_call = "special_macro!$HOSTOUTPUT$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        # Output is correctly restitued
        assert u'plugins/nothing Père Noël' == macros_command
    def test_args_macro(self):
        """
        Test ARGn macros
        :return:
        """
        print("Initial test macros: %d - %s" % (len(self._scheduler.pushed_conf.__class__.macros),
                                                self._scheduler.pushed_conf.__class__.macros))
        print(" - : %s" % (self._scheduler.pushed_conf.__class__.properties['$USER1$']))
        print(" - : %s" % (self._scheduler.pushed_conf.properties['$USER1$']))
        print(" - : %s" % (getattr(self._scheduler.pushed_conf, '$USER1$', None)))
        for key in self._scheduler.pushed_conf.__class__.macros:
            key = self._scheduler.pushed_conf.__class__.macros[key]
            if key:
                value = getattr(self._scheduler.pushed_conf.properties, key, '')
                print(" - %s : %s" % (key, self._scheduler.pushed_conf.properties[key]))
                if value:
                    print("- %s = %s" % (key, value))

        (svc, hst) = self.get_hst_svc()
        data = [hst, svc]

        # command_with_args is defined with 5 arguments as:
        # $PLUGINSDIR$/command -H $HOSTADDRESS$ -t 9 -u -c $ARG1$
        # -a $ARG2$ $ARG3$ $ARG4$ and the last is $ARG5$.

        # No arguments are provided - will be valued as empty strings
        dummy_call = "command_with_args"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        # todo: Test problem is here!
        # Whereas we should get:
        assert macros_command == 'plugins/command -H 127.0.0.1 -t 9 -u -c  -a    and the last is .'
        # We get:
        # assert macros_command == '/var/lib/shinken/libexec/command -H 127.0.0.1 -t 9 -u -c  -a    and the last is .'
        # Outside the test env, everything is ok ! Because some tests executed before the macro
        # do not have the correct value!

        # Extra arguments are provided - will be ignored
        dummy_call = "command_with_args!arg_1!arg_2!arg_3!arg_4!arg_5!extra argument"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert macros_command == 'plugins/command -H 127.0.0.1 -t 9 -u -c arg_1 ' \
                                 '-a arg_2 arg_3 arg_4 and the last is arg_5.'

        # All arguments are provided
        dummy_call = "command_with_args!arg_1!arg_2!arg_3!arg_4!arg_5"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data,  self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert macros_command == 'plugins/command -H 127.0.0.1 -t 9 -u -c arg_1 ' \
                                 '-a arg_2 arg_3 arg_4 and the last is arg_5.'
コード例 #5
0
    def __init__(self, params=None, parsing=True):
        if params is None:
            params = {}

        # At deserialization, thoses are dict
        # TODO: Separate parsing instance from recreated ones
        if 'check_command' in params and isinstance(params['check_command'],
                                                    dict):
            # We recreate the object
            self.check_command = CommandCall(params['check_command'])
            # And remove prop, to prevent from being overridden
            del params['check_command']

        super(CheckModulation, self).__init__(params, parsing=parsing)
コード例 #6
0
    def create_commandcall(prop, commands, command):
        """
        Create CommandCall object with command

        :param prop: property
        :type prop: str
        :param commands: all commands
        :type commands: fusionsupervision.objects.command.Commands
        :param command: a command object
        :type command: str
        :return: a commandCall object
        :rtype: fusionsupervision.objects.commandcallitem.CommandCall
        """
        cc = {
            'commands': commands,
            'call': command
        }

        if hasattr(prop, 'enable_environment_macros'):
            cc['enable_environment_macros'] = prop.enable_environment_macros

        if hasattr(prop, 'poller_tag'):
            cc['poller_tag'] = prop.poller_tag
        elif hasattr(prop, 'reactionner_tag'):
            cc['reactionner_tag'] = prop.reactionner_tag

        return CommandCall(cc)
コード例 #7
0
    def test_commands_pack(self):
        """ Test commands pack build

        :return: None
        """
        t = {
            'command_name': 'check_command_test',
            'command_line': '/tmp/dummy_command.sh $ARG1$ $ARG2$',
            'module_type': 'nrpe-booster',
            'poller_tag': 'DMZ',
            'reactionner_tag': 'REAC'
        }
        c = Command(t)

        # now create a commands packs
        cs = Commands([c])
        dummy_call = "check_command_test!titi!toto"
        cc = CommandCall({"commands": cs, "call": dummy_call})
        assert True == cc.is_valid()
        assert c == cc.command
        assert 'DMZ' == cc.poller_tag
        assert 'REAC' == cc.reactionner_tag
    def test_host_custom_macros(self):
        """
        Test on-demand macros with custom variables for hosts
        :return:
        """
        hst = self._scheduler.hosts.find_by_name("test_macro_host")
        # The host has custom variables, thus we may use them in a macro
        assert hst.customs is not []
        assert '_CUSTOM1' in hst.customs
        assert '_CUSTOM2' in hst.customs
        # Force declare an integer customs variable
        hst.customs['_CUSTOM3'] = 10
        print((hst.customs))
        data = [hst]

        # Parse custom macro to get host custom variables based upon a fixed value
        # host has a custom variable defined as _custom1 = value
        dummy_call = "special_macro!$_HOSTCUSTOM1$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing value' == macros_command

        # Parse custom macro to get host custom variables based upon another macro
        # host has a custom variable defined as _custom2 = $HOSTNAME$
        dummy_call = "special_macro!$_HOSTCUSTOM2$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing test_macro_host' == macros_command

        # Parse custom macro to get host custom variables based upon another macro
        # host has a custom variable defined as _custom2 = $HOSTNAME$
        dummy_call = "special_macro!$_HOSTCUSTOM3$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        print(("Command: %s" % macros_command))
        assert 'plugins/nothing 10' == macros_command
    def test_hostadressX_macros(self):
        """
        Host addresses macros
        :return:
        """
        (svc, hst) = self.get_hst_svc()
        data = [hst, svc]

        # Ok sample host call
        dummy_call = "special_macro!$HOSTADDRESS$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 127.0.0.1' == macros_command
 def test_escape_macro(self):
     """
     Call the resolver with an empty macro ($$)
     :return:
     """
     (svc, hst) = self.get_hst_svc()
     data = [hst, svc]
     hst.state = 'UP'
     dummy_call = "special_macro!$$"
     cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
     macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                         self._scheduler.timeperiods)
     # Not a macro but $$ is transformed as $
     assert 'plugins/nothing $' == macros_command
    def test_contact_custom_macros(self):
        """
        Test on-demand macros with custom variables for contacts
        :return:
        """
        contact = self._scheduler.contacts.find_by_name("test_macro_contact")
        data = [contact]

        # Parse custom macro to get contact custom variables based upon a fixed value
        # contact has a custom variable defined as _custom1 = value
        dummy_call = "special_macro!$_CONTACTCUSTOM1$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing value' == macros_command

        # Parse custom macro to get service custom variables based upon another macro
        # host has a custom variable defined as _custom2 = $CONTACTNAME$
        dummy_call = "special_macro!$_CONTACTCUSTOM2$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing test_macro_contact' == macros_command
 def test_special_macros_realm(self):
     """
     Call the resolver with a special macro HOSTREALM
     :return:
     """
     (svc, hst) = self.get_hst_svc()
     data = [hst, svc]
     hst.state = 'UP'
     dummy_call = "special_macro!$HOSTREALM$"
     cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
     macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                         self._scheduler.timeperiods)
     # Macro raised the default realm (All)
     assert 'plugins/nothing All' == macros_command
コード例 #13
0
    def __init__(self, params=None, parsing=True):
        if params is None:
            params = {}

        # At deserialization, thoses are dict
        # TODO: Separate parsing instance from recreated ones
        for prop in ['service_notification_commands', 'host_notification_commands']:
            if prop in params and isinstance(params[prop], list) and params[prop] \
                    and isinstance(params[prop][0], dict):
                new_list = [CommandCall(elem, parsing=parsing) for elem in params[prop]]
                # We recreate the object
                setattr(self, prop, new_list)
                # And remove prop, to prevent from being overridden
                del params[prop]
        super(NotificationWay, self).__init__(params, parsing=parsing)
    def test_resource_file(self):
        """
        Test macros defined in configuration files
        :return:
        """
        (svc, hst) = self.get_hst_svc()
        data = [hst, svc]

        # $USER1$ macro is defined as 'plugins' in the configuration file
        dummy_call = "special_macro!$USER1$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing plugins' == macros_command

        # $PLUGINSDIR$ macro is defined as $USER1$ in the configuration file
        dummy_call = "special_macro!$PLUGINSDIR$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing plugins' == macros_command

        # $INTERESTINGVARIABLE$ macro is defined as 'interesting_value' in the configuration file
        dummy_call = "special_macro!$INTERESTINGVARIABLE$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing interesting_value' == macros_command

        # Look for multiple = in lines, should split the first
        # and keep others in the macro value
        dummy_call = "special_macro!$ANOTHERVALUE$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing first=second' == macros_command
    def test_datetime_macros(self):
        """ Test date / time macros: SHORTDATETIME, LONGDATETIME, DATE, TIME, ...

        :return:
        """
        (svc, hst) = self.get_hst_svc()
        data = [hst, svc]
        hst.state = 'UP'

        # Long and short datetime
        dummy_call = "special_macro!$LONGDATETIME$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        dummy_call = "special_macro!$SHORTDATETIME$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        dummy_call = "special_macro!$DATE$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        dummy_call = "special_macro!$TIME$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        dummy_call = "special_macro!$TIMET$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        # Do not check that the output of these macro is correct
        # because there is no specific macro code for those functions ;)

        # Process and event start time
        dummy_call = "special_macro!$PROCESSSTARTTIME$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing n/a' == macros_command
        dummy_call = "special_macro!$EVENTSTARTTIME$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing n/a' == macros_command
    def test_illegal_macro_output_chars(self):
        """ Check output macros are cleaned from illegal macro characters

        $HOSTOUTPUT$, $HOSTPERFDATA$, $HOSTACKAUTHOR$, $HOSTACKCOMMENT$,
        $SERVICEOUTPUT$, $SERVICEPERFDATA$, $SERVICEACKAUTHOR$, $SERVICEACKCOMMENT$
        """
        (svc, hst) = self.get_hst_svc()
        data = [hst, svc]
        illegal_macro_output_chars = \
            self._scheduler.pushed_conf.illegal_macro_output_chars
        print("Illegal macros caracters:", illegal_macro_output_chars)
        hst.output = 'fake output'
        dummy_call = "special_macro!$HOSTOUTPUT$"

        for c in illegal_macro_output_chars:
            hst.output = 'fake output' + c
            cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
            macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                                self._scheduler.timeperiods)
            print(macros_command)
            assert 'plugins/nothing fake output' == macros_command
コード例 #17
0
class CheckModulation(Item):
    """CheckModulation class is simply a modulation of the check command (of a Host/Service)
    during a check_period.

    """
    my_type = 'checkmodulation'

    properties = Item.properties.copy()
    properties.update({
        'checkmodulation_name':
        StringProp(fill_brok=['full_status']),
        'check_command':
        StringProp(fill_brok=['full_status']),
        'check_period':
        StringProp(brok_transformation=to_name_if_possible,
                   fill_brok=['full_status']),
    })

    running_properties = Item.running_properties.copy()

    special_properties = ('check_period', )

    macros = {}

    def __init__(self, params=None, parsing=True):
        if params is None:
            params = {}

        # At deserialization, thoses are dict
        # TODO: Separate parsing instance from recreated ones
        if 'check_command' in params and isinstance(params['check_command'],
                                                    dict):
            # We recreate the object
            self.check_command = CommandCall(params['check_command'])
            # And remove prop, to prevent from being overridden
            del params['check_command']

        super(CheckModulation, self).__init__(params, parsing=parsing)

    def serialize(self):
        res = super(CheckModulation, self).serialize()
        res['check_command'] = None
        if getattr(self, 'check_command', None):
            res['check_command'] = self.check_command.serialize()
        return res

    def get_name(self):
        """Accessor to checkmodulation_name attribute

        :return: check modulation name
        :rtype: str
        """
        if hasattr(self, 'checkmodulation_name'):
            return self.checkmodulation_name
        return 'Unnamed'

    def get_check_command(self, timeperiods, t_to_go):
        """Get the check_command if we are in the check period modulation

        :param t_to_go: time to check if we are in the timeperiod
        :type t_to_go:
        :return: A check command if we are in the check period, None otherwise
        :rtype: fusionsupervision.objects.command.Command
        """
        if not self.check_period or timeperiods[
                self.check_period].is_time_valid(t_to_go):
            return self.check_command
        return None

    def is_correct(self):
        """Check if this object configuration is correct ::

        * Check our own specific properties
        * Call our parent class is_correct checker

        :return: True if the configuration is correct, otherwise False
        :rtype: bool
        """
        state = True

        # Internal checks before executing inherited function...
        if not hasattr(self, 'check_command'):
            msg = "[checkmodulation::%s] do not have any check_command defined" % (
                self.get_name())
            self.add_error(msg)
            state = False
        else:
            if self.check_command is None:
                msg = "[checkmodulation::%s] a check_command is missing" % (
                    self.get_name())
                self.add_error(msg)
                state = False
            if self.check_command and not self.check_command.is_valid():
                msg = "[checkmodulation::%s] a check_command is invalid" % (
                    self.get_name())
                self.add_error(msg)
                state = False

        # Ok just put None as check_period, means 24x7
        if not hasattr(self, 'check_period'):
            self.check_period = None

        return super(CheckModulation, self).is_correct() and state
    def test_host_count_services_macros(self):
        """Test services count for an hostmacros
        :return:
        """
        (svc, hst) = self.get_hst_svc()
        data = [hst, svc]
        hst.state = 'UP'

        # Get another service
        svc2 = self._scheduler.pushed_conf.services.find_srv_by_name_and_hostname(
            "test_host_0", "test_another_service"
        )
        svc2.output = 'you should not pass'

        # Total
        svc.output = 'you should not pass'
        data = [hst, svc]
        dummy_call = "special_macro!$TOTALHOSTSERVICES$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 2' == macros_command

        # Services states
        svc.state_id = 0
        svc.state = 'OK'
        svc2.state_id = 1
        svc2.state = 'WARNING'

        # Ok
        svc.output = 'you should not pass'
        data = [hst, svc]
        dummy_call = "special_macro!$TOTALHOSTSERVICESOK$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 1' == macros_command

        # Warning
        svc.output = 'you should not pass'
        data = [hst, svc]
        dummy_call = "special_macro!$TOTALHOSTSERVICESWARNING$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 1' == macros_command

        # Critical
        svc.output = 'you should not pass'
        data = [hst, svc]
        dummy_call = "special_macro!$TOTALHOSTSERVICESCRITICAL$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 0' == macros_command

        # Unknown
        svc.output = 'you should not pass'
        data = [hst, svc]
        dummy_call = "special_macro!$TOTALHOSTSERVICESUNKNOWN$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 0' == macros_command

        # Unreachable
        svc.output = 'you should not pass'
        data = [hst, svc]
        dummy_call = "special_macro!$TOTALHOSTSERVICESUNREACHABLE$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 0' == macros_command

        # Change states
        svc.state_id = 2
        svc.state = 'CRITICAL'
        svc2.state_id = 3
        svc2.state = 'UNKNOWN'

        # Ok
        svc.output = 'you should not pass'
        data = [hst, svc]
        dummy_call = "special_macro!$TOTALHOSTSERVICESOK$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 0' == macros_command

        # Warning
        svc.output = 'you should not pass'
        data = [hst, svc]
        dummy_call = "special_macro!$TOTALHOSTSERVICESWARNING$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 0' == macros_command

        # Critical
        svc.output = 'you should not pass'
        data = [hst, svc]
        dummy_call = "special_macro!$TOTALHOSTSERVICESCRITICAL$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 1' == macros_command

        # Unknown
        svc.output = 'you should not pass'
        data = [hst, svc]
        dummy_call = "special_macro!$TOTALHOSTSERVICESUNKNOWN$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 1' == macros_command

        # Unreachable
        svc.output = 'you should not pass'
        data = [hst, svc]
        dummy_call = "special_macro!$TOTALHOSTSERVICESUNREACHABLE$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 0' == macros_command
    def test_ondemand_macros(self):
        """Test on-demand macros
        :return:
        """
        (svc, hst) = self.get_hst_svc()
        data = [hst, svc]
        hst.state = 'UP'
        svc.state = 'UNKNOWN'

        # Get another service
        svc2 = self._scheduler.pushed_conf.services.find_srv_by_name_and_hostname(
            "test_host_0", "test_another_service"
        )
        svc2.output = 'you should not pass'

        # Request a not existing macro
        dummy_call = "special_macro!$HOSTXXX:test_host_0$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing' == macros_command

        # Request a specific host state
        dummy_call = "special_macro!$HOSTSTATE:test_host_0$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing UP' == macros_command

        # Call with a void host name, means : myhost
        data = [hst]
        dummy_call = "special_macro!$HOSTSTATE:$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing UP' == macros_command

        # Now with a service, for our implicit host state
        data = [hst, svc]
        dummy_call = "special_macro!$HOSTSTATE:test_host_0$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing UP' == macros_command
                                                        
        # Now with a service, for our implicit host state (missing host ...)
        data = [hst, svc]
        dummy_call = "special_macro!$HOSTSTATE:$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing UP' == macros_command

        # Now call this data from our previous service - get service state
        data = [hst, svc2]
        dummy_call = "special_macro!$SERVICESTATE:test_host_0:test_another_service$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing OK' == macros_command

        # Now call this data from our previous service - get service output
        data = [hst, svc2]
        dummy_call = "special_macro!$SERVICEOUTPUT:test_host_0:test_another_service$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing you should not pass' == macros_command

        # Ok now with a host implicit way
        svc2.output = 'you should not pass'
        data = [hst, svc2]
        dummy_call = "special_macro!$SERVICEOUTPUT::test_another_service$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing you should not pass' == macros_command
    def test_summary_macros(self):
        """ Test summary macros: TOTALHOSTSUP, TOTALHOSTDOWN, ...

        :return:
        """
        (svc, hst) = self.get_hst_svc()
        data = [hst, svc]
        hst.state = 'UP'

        # Number of hosts UP / DOWN / UNREACHABLE
        dummy_call = "special_macro!$TOTALHOSTSUP$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        # All 3 hosts are UP
        assert 'plugins/nothing 3' == macros_command
        dummy_call = "special_macro!$TOTALHOSTPROBLEMS$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 0' == macros_command
        dummy_call = "special_macro!$TOTALHOSTPROBLEMSUNHANDLED$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 0' == macros_command

        # Now my host is DOWN and not yet handled
        hst.state = 'DOWN'
        hst.is_problem = True
        hst.problem_has_been_acknowledged = False
        dummy_call = "special_macro!$TOTALHOSTSDOWN$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 1' == macros_command
        dummy_call = "special_macro!$TOTALHOSTSDOWNUNHANDLED$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 1' == macros_command
        # Now my host is DOWN but handled
        hst.problem_has_been_acknowledged = True
        dummy_call = "special_macro!$TOTALHOSTSDOWNUNHANDLED$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 0' == macros_command

        # Now my host is UNREACHABLE and not yet handled
        hst.state = 'UNREACHABLE'
        hst.is_problem = True
        hst.problem_has_been_acknowledged = False
        dummy_call = "special_macro!$TOTALHOSTSUNREACHABLE$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 1' == macros_command
        dummy_call = "special_macro!$TOTALHOSTSUNREACHABLEUNHANDLED$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 1' == macros_command
        # Now my host is UNREACHABLE but handled
        hst.problem_has_been_acknowledged = True
        dummy_call = "special_macro!$TOTALHOSTSUNREACHABLEUNHANDLED$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 0' == macros_command

        # Now my host is DOWN and not yet handled
        hst.state = 'DOWN'
        hst.is_problem = True
        hst.problem_has_been_acknowledged = False
        dummy_call = "special_macro!$TOTALHOSTPROBLEMS$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 1' == macros_command
        dummy_call = "special_macro!$TOTALHOSTPROBLEMSUNHANDLED$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 1' == macros_command

        # Now my host is UP and no more a problem
        hst.state = 'UP'
        hst.is_problem = False
        hst.problem_has_been_acknowledged = False
        dummy_call = "special_macro!$TOTALHOSTPROBLEMS$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 0' == macros_command
        dummy_call = "special_macro!$TOTALHOSTPROBLEMSUNHANDLED$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 0' == macros_command

        # Number of services OK / WARNING / CRITICAL / UNKNOWN
        dummy_call = "special_macro!$TOTALSERVICESOK$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 2' == macros_command

        # Now my service is WARNING and not handled
        svc.state = 'WARNING'
        svc.is_problem = True
        svc.problem_has_been_acknowledged = False
        dummy_call = "special_macro!$TOTALSERVICESWARNING$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 1' == macros_command
        dummy_call = "special_macro!$TOTALSERVICESWARNINGUNHANDLED$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 1' == macros_command
        # Now my service problem is handled
        svc.problem_has_been_acknowledged = True
        dummy_call = "special_macro!$TOTALSERVICESWARNINGUNHANDLED$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 0' == macros_command

        # Now my service is CRITICAL and not handled
        svc.state = 'CRITICAL'
        svc.is_problem = True
        svc.problem_has_been_acknowledged = False
        dummy_call = "special_macro!$TOTALSERVICESCRITICAL$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 1' == macros_command
        dummy_call = "special_macro!$TOTALSERVICESCRITICALUNHANDLED$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 1' == macros_command
        # Now my service problem is handled
        svc.problem_has_been_acknowledged = True
        dummy_call = "special_macro!$TOTALSERVICESCRITICALUNHANDLED$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 0' == macros_command

        # Now my service is UNKNOWN and not handled
        svc.state = 'UNKNOWN'
        svc.is_problem = True
        svc.problem_has_been_acknowledged = False
        dummy_call = "special_macro!$TOTALSERVICESUNKNOWN$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 1' == macros_command
        dummy_call = "special_macro!$TOTALSERVICESUNKNOWNUNHANDLED$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 1' == macros_command
        # Now my service problem is handled
        svc.problem_has_been_acknowledged = True
        dummy_call = "special_macro!$TOTALSERVICESUNKNOWNUNHANDLED$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 0' == macros_command

        # Now my service is WARNING and not handled
        svc.state = 'WARNING'
        svc.is_problem = True
        svc.problem_has_been_acknowledged = False
        dummy_call = "special_macro!$TOTALSERVICEPROBLEMS$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 1' == macros_command
        dummy_call = "special_macro!$TOTALSERVICEPROBLEMSUNHANDLED$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 1' == macros_command

        # Now my service is OK and no more a problem
        svc.state = 'OK'
        svc.is_problem = False
        svc.problem_has_been_acknowledged = False
        dummy_call = "special_macro!$TOTALSERVICEPROBLEMS$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 0' == macros_command
        dummy_call = "special_macro!$TOTALSERVICEPROBLEMSUNHANDLED$"
        cc = CommandCall({"commands": self._arbiter.conf.commands, "call": dummy_call})
        macros_command = self.mr.resolve_command(cc, data, self._scheduler.macromodulations,
                                            self._scheduler.timeperiods)
        assert 'plugins/nothing 0' == macros_command