def test_evaluate_1(self):
     """evaluate: test de evaluacion 1 con resultado correcto y expresion simple"""
     expr = 'a>3 and b<=7'
     values = {'a': 5, 'b': 5, 'c': 0, 'd': 2}
     ev = ExpressionEvaluator(expr)
     res = ev.evaluate(values)
     assert_true(res)
 def test_evaluate_2(self):
     """evaluate: test de evaluacion 2 con resultado correcto y expresion con parentesis"""
     expr = '(a>3 and b<7) and (c=0 or d=1)'
     values = {'a': 5, 'b': 5, 'c': 0, 'd': 2}
     ev = ExpressionEvaluator(expr)
     res = ev.evaluate(values)
     assert_true(res)
 def test_evaluate_11(self):
     """evaluate: test de evaluacion 11 con resultado correcto y expresion mixta booleana y algebraica"""
     expr = '(a+3)<b'
     values = {'a': 2, 'b': 10}
     ev = ExpressionEvaluator(expr)
     res = ev.evaluate(values)
     assert_true(res)
 def test_evaluate_16(self):
     """evaluate: test de evaluacion 16 con resultado correcto devolviendo literal"""
     expr = '"hola"'
     values = {}
     ev = ExpressionEvaluator(expr)
     res = ev.evaluate(values)
     assert_equal(res, 'hola')
 def test_evaluate_8(self):
     """evaluate: test de evaluacion 8 con resultado correcto y expresion icontains"""
     expr = '"HOLAMUNDO" icontains "hola"'
     values = dict()
     ev = ExpressionEvaluator(expr)
     res = ev.evaluate(values)
     assert_true(res)
 def test_evaluate_10(self):
     """evaluate: test de evaluacion 10 con resultado correcto y expresion algebraica compleja"""
     expr = '(3+5)*a-4*3+10/5'
     values = {'a': 2}
     ev = ExpressionEvaluator(expr)
     res = ev.evaluate(values)
     assert_equal(6, res)
 def test_evaluate_4(self):
     """evaluate: test de evaluacion 4 con resultado correcto y expresion con funcion simple, operadores
     de un argumento y comparaciones con booleanos"""
     expr = 'length[a]=9 and (not xxx and a contains "hola") and (b<10 or c=6) and xxx=False'
     values = {'a': 'holamundo', 'b': 8, 'c': 6, 'xxx': False}
     ev = ExpressionEvaluator(expr)
     res = ev.evaluate(values)
     assert_true(res)
 def test_evaluate_9(self):
     """evaluate: test de evaluacion 9 con resultado correcto y expresion algebraica simple"""
     expr = '3+5'
     # values = {'a': 'holamundo', 'b': 8, 'c': 6, 'xxx': False}
     values = dict()
     ev = ExpressionEvaluator(expr)
     res = ev.evaluate(values)
     assert_equal(8, res)
 def test_evaluate_7(self):
     """evaluate: test de evaluacion 7 con resultado correcto y expresion con funciones multiples, operadores
     de un argumento y comparaciones con booleanos"""
     expr = 'have_passed_time["2017-01-01 00:00:00", 60, weeks]'
     values = {'a': 'holamundo', 'b': 8, 'c': 6, 'xxx': False}
     ev = ExpressionEvaluator(expr)
     res = ev.evaluate(values)
     assert_true(res)
Esempio n. 10
0
 def test_evaluate_6(self):
     """evaluate: test de evaluacion 6 con resultado correcto y expresion con funciones multiples, operadores
     de un argumento y comparaciones con booleanos"""
     expr = 'length["hola"]'
     values = {'a': 'holamundo', 'b': 8, 'c': 6, 'xxx': False}
     ev = ExpressionEvaluator(expr)
     res = ev.evaluate(values)
     assert_true(res)
Esempio n. 11
0
 def test_evaluate_12(self):
     """evaluate: test de evaluacion 12 con resultado correcto y expresion con funciones multiples, operadores
     de un argumento, expresiones algebraicas y comparaciones con booleanos"""
     expr = 'length[a]=9 and (not xxx and a contains "hola") and (b<10 or c=6) and (xxx=False and ' \
            'True=have_passed_time["2017-01-01 00:00:00",60,weeks]) and ((2*c)<b or (b+c)=50)'
     values = {'a': 'holamundo', 'b': 8, 'c': 6, 'xxx': False}
     ev = ExpressionEvaluator(expr)
     res = ev.evaluate(values)
     assert_false(res)
Esempio n. 12
0
 def evaluate(self, expr: str, values: dict, insert_additional_data: bool):
     ev = ExpressionEvaluator(expr)
     ev.set_additional_data(
         AdditionalServerData('local', self.ssh_host, self.ssh_port,
                              self.ssh_username, self.ssh_password))
     ev.add_function_package(
         'common.infra_modules.monit_module.conditions_monit_module')
     result = ev.evaluate(values)
     return result
Esempio n. 13
0
 def test_evaluate_15(self):
     """evaluate: test de evaluacion 15 con resultado incorrecto por variable no encontrada"""
     expr = 'a>3 and b=7'
     values = {'a': 5}
     ev = ExpressionEvaluator(expr)
     ev.evaluate(values)
Esempio n. 14
0
 def test_evaluate_14(self):
     """evaluate: test de evaluacion 14 con resultado incorrecto por expresion mal formada"""
     expr = 'a>3 and b 7'
     values = {'a': 5, 'b': 5, 'c': 0, 'd': 2}
     ev = ExpressionEvaluator(expr)
     ev.evaluate(values)
Esempio n. 15
0
 def test_evaluate_13(self):
     """evaluate: test de evaluacion 13 con resultado incorrecto por expresion mal formada"""
     expr = 'have_passed_time[2017-01-01 00:00:00", 60, weeks]'
     values = {'a': 'holamundo', 'b': 8, 'c': 6, 'xxx': False}
     ev = ExpressionEvaluator(expr)
     ev.evaluate(values)
Esempio n. 16
0
    def initialize(self):
        """
        This method create and initialize all variables and resources needed
        :return: None
        """

        # Load system_commands_module
        try:
            self.scm = common.module_manager.get_module('system_commands_module')
        except Exception as e:
            logger.error('Can not import system_commands_module')
            logger.exception(e)

        # Load specific config file
        sections = config.get_sections(config_type=MODULE_NAME)

        # Recover rules
        all_rules = dict()
        for section in sections:
            if section.startswith(RULE_PREFIX):
                rule_name = section[section.index(RULE_PREFIX) + len(RULE_PREFIX):]
                condition = config.get_value(section, 'condition', MODULE_NAME)
                action_ok = config.get_value(section, 'action_ok', MODULE_NAME)
                action_ko = config.get_value(section, 'action_ko', MODULE_NAME)

                rule_data = RuleData(rule_name, condition, action_ok, action_ko)
                rule_data.condition_evaluator = ExpressionEvaluator(condition)
                rule_data.condition_evaluator.add_function_package(CONDITIONS_MODULE)
                rule_data.action_ok_evaluator = ExpressionEvaluator(action_ok)
                rule_data.action_ok_evaluator.add_function_package(ACTIONS_MODULE)
                rule_data.action_ko_evaluator = ExpressionEvaluator(action_ko)
                rule_data.action_ko_evaluator.add_function_package(ACTIONS_MODULE)
                all_rules[rule_name] = rule_data

        # Create server data with associated rules
        servers = dict()
        for section in sections:
            if not section.startswith(RULE_PREFIX):
                server_name = section
                host = config.get_value(section, 'host', MODULE_NAME)
                port = config.get_value(section, 'port', MODULE_NAME)
                username = config.get_value(section, 'username', MODULE_NAME)
                password = config.get_value(section, 'password', MODULE_NAME)
                interval = int(config.get_value(section, 'interval', MODULE_NAME))
                applied_rules = striplist(config.get_value(section, 'rules', MODULE_NAME).split(','))
                rules = [all_rules[x] for x in applied_rules]

                server_data = ServerData(server_name, host, port, username, password, interval, rules)
                server_data.host_evaluator = ExpressionEvaluator(host)
                server_data.host_evaluator.add_function_package(CONDITIONS_MODULE)
                servers[server_name] = server_data

        # Init and execute threads. Store task in dictionary indexed by server name
        self.check_tasks = dict()
        for server in servers.values():
            server_check_task = self.ServerCheckTask(server)
            self.check_tasks[server.name] = server_check_task
            server_check_task.set_interval(server.interval)
            server_check_task.setName(server.name)
            server_check_task.set_initial_delay(int(config.get_value(MODULE_NAME, 'initial_delay')))
            server_check_task.start()

        self.register_url(MonitModuleView, '/monit')
        self.register_url(ShutdownMonitModuleView, '/shutdown')