Exemple #1
0
    def test_conflicting_modules(self):

        # prepare 2 modconfs:
        modconfA = Module({
            'module_alias': 'whatever',
            'python_name': 'test_module_as_package_dir.modA'
        })
        modconfB = Module({
            'module_alias': '42',
            'python_name': 'test_module_as_package_dir.modB'
        })

        mods = (modconfA, modconfB)

        mm = self.modulemanager = ModulesManager('broker', None)

        mm.load_and_init(mods)

        modA = None
        modB = None
        for _, mod in mm.modules_assoc:
            if mod.__package__ == 'test_module_as_package_dir.modA':
                modA = mod
            elif mod.__package__ == 'test_module_as_package_dir.modB':
                modB = mod

            if mod.properties['type'].startswith("mod"):
                self.assertEqual(mod.expected_helpers_X, mod.helpers.X)
        self.assertIsNotNone(modA)
        self.assertIsNotNone(modB)
        self.assertNotEqual(modA.helpers.X, modB.helpers.X)
Exemple #2
0
    def test_connection_refused(self):
        """ Test module backend connection refused """
        # No backend data defined
        mod = get_instance(
            Module({
                'module_alias': 'web-services',
                'module_types': 'web-services',
                'python_name': 'alignak_module_ws',
            }))
        self.assertFalse(mod.backend_available)

        # Backend bad URL
        mod = get_instance(
            Module({
                'module_alias': 'web-services',
                'module_types': 'web-services',
                'python_name': 'alignak_module_ws',
                'alignak_backend': 'http://bad_url',
                'username': '******',
                'password': '******',
            }))
        self.assertFalse(mod.backend_available)

        # Backend refused login
        mod = get_instance(
            Module({
                'module_alias': 'web-services',
                'module_types': 'web-services',
                'python_name': 'alignak_module_ws',
                'alignak_backend': 'http://127.0.0.1:5000',
                'username': '******',
                'password': '******',
            }))
        self.assertFalse(mod.backend_available)
Exemple #3
0
    def test_module_start_default(self):
        """
        Test the module initialization function, no parameters, using default
        :return:
        """
        self.print_header()
        # Obliged to call to get a self.logger...
        self.setup_with_file('cfg/cfg_default.cfg')
        self.assertTrue(self.conf_is_correct)

        # -----
        # Default initialization
        # -----
        # Clear logs
        self.clear_logs()

        # Create an Alignak module
        mod = Module({
            'module_alias': 'nrpe-booster',
            'module_types': 'nrpe-booster',
            'python_name': 'alignak_module_nrpe_booster'
        })

        instance = alignak_module_nrpe_booster.get_instance(mod)
        self.assertIsInstance(instance, BaseModule)

        self.assert_log_match(
            re.escape("Give an instance of alignak_module_nrpe_booster for "
                      "alias: nrpe-booster"), 0)
Exemple #4
0
    def test_module_start_default(self):
        """Test the module initialization function, no parameters, using default
        :return:
        """
        # Obliged to call to get a self.logger...
        self.setup_with_file('./cfg/alignak.cfg')
        self.assertTrue(self.conf_is_correct)

        # Clear nsca
        self.clear_logs()

        # -----
        # Default initialization
        # -----
        # Create an Alignak module
        mod = Module({
            'module_alias': 'nsca',
            'module_types': 'passive',
            'python_name': 'alignak_module_nsca'
        })

        instance = alignak_module_nsca.get_instance(mod)
        self.assertIsInstance(instance, BaseModule)
        self.show_logs()

        # self.assert_log_match(
        #     re.escape("Give an instance of alignak_module_nsca for alias: nsca"), 0)
        self.assert_log_match(
            re.escape(
                "Give an instance of alignak_module_nsca for alias: nsca"), 0)
        self.assert_log_match(
            re.escape(
                "configuration, allowed hosts : '127.0.0.1'(5667), buffer length: 4096, "
                "payload length: -1, encryption: 0, max packet age: 30, "
                "check future packet: True, backlog: 10"), 1)
Exemple #5
0
    def setUp(self):
        super(TestModule, self).setUp()
        from alignak.objects.module import Module

        self.item = Module(parsing=True)
        print("Item properties:")
        for name in self.item.properties:
            print(("- %s" % name))
    def setUp(self):
        self.maxDiff = None

        # Start arbiter backend module
        modconf = Module()
        modconf.module_alias = "backend_arbiter"
        modconf.username = "******"
        modconf.password = "******"
        modconf.api_url = 'http://127.0.0.1:5000'
        self.arbmodule = AlignakBackendArbiter(modconf)
        self.objects = self.arbmodule.get_objects()

        # Start broker module
        modconf = Module()
        modconf.module_alias = "backend_broker"
        modconf.username = "******"
        modconf.password = "******"
        modconf.api_url = 'http://127.0.0.1:5000'
        self.brokmodule = AlignakBackendBroker(modconf)

        # Set up the broker module
        self.brokmodule.get_refs()
    def _setup_nrpe(self):
        # Create an Alignak module
        mod = Module({
            'module_alias': 'nrpe-booster',
            'module_types': 'nrpe-booster',
            'python_name': 'alignak_module_nrpe_booster'
        })

        # Create the modules manager for a daemon type
        self.modulemanager = ModulesManager('poller', None)
        # Load and initialize the modules
        self.modulemanager.load_and_init([mod])
        my_module = self.modulemanager.instances[0]
        return my_module
Exemple #8
0
    def test_connection_accepted(self):
        """ Test module backend connection accepted """
        # admin user login
        mod = get_instance(
            Module({
                'module_alias': 'web-services',
                'module_types': 'web-services',
                'python_name': 'alignak_module_ws',
                'alignak_backend': 'http://127.0.0.1:5000',
                'username': '******',
                'password': '******',
            }))
        # self.assertTrue(mod.backend_available)

        # test user login
        mod = get_instance(
            Module({
                'module_alias': 'web-services',
                'module_types': 'web-services',
                'python_name': 'alignak_module_ws',
                'alignak_backend': 'http://127.0.0.1:5000',
                'username': '******',
                'password': '******',
            }))
Exemple #9
0
    def test_modulemanager_2(self):
        """ Module manager manages its modules - new form

        Test if the module manager manages correctly all the modules
        :return:
        """
        self.setup_with_file('cfg/cfg_default_with_modules.cfg',
                             'cfg/default_with_modules/alignak.ini')
        assert self.conf_is_correct

        # Create an Alignak module
        mod = Module({
            'name': 'mod-example',
            'type': 'example',
            'python_name': 'alignak_module_example'
        })
        self.run_modulemanager(mod)
Exemple #10
0
    def setUpClass(cls):
        # Set test mode for alignak backend
        os.environ['TEST_ALIGNAK_BACKEND'] = '1'
        os.environ['ALIGNAK_BACKEND_MONGO_DBNAME'] = 'alignak-module-backend-test'

        # Delete used mongo DBs
        print ("Deleting Alignak backend DB...")
        exit_code = subprocess.call(
            shlex.split(
                'mongo %s --eval "db.dropDatabase()"' % os.environ['ALIGNAK_BACKEND_MONGO_DBNAME'])
        )
        assert exit_code == 0

        cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignakbackend:app',
                                  '--socket', '0.0.0.0:5000',
                                  '--protocol=http', '--enable-threads', '--pidfile',
                                  '/tmp/uwsgi.pid'])
        time.sleep(3)

        test_dir = os.path.dirname(os.path.realpath(__file__))
        print(("Current test directory: %s" % test_dir))

        print(("Feeding Alignak backend... %s" % test_dir))
        exit_code = subprocess.call(
            shlex.split(
                'alignak-backend-import --delete %s/cfg/default/_main.cfg' % test_dir)
        )
        assert exit_code == 0

        # Start arbiter module
        modconf = Module()
        modconf.module_alias = "backend_arbiter"
        modconf.username = "******"
        modconf.password = "******"
        modconf.api_url = 'http://127.0.0.1:5000'
        cls.arbmodule = AlignakBackendArbiter(modconf)
        cls.objects = cls.arbmodule.get_objects()
Exemple #11
0
    def test_module_manager(self):
        """
        Test if the module manager manages correctly all the modules
        :return:
        """
        self.print_header()
        self.setup_with_file('cfg/cfg_default.cfg')
        self.assertTrue(self.conf_is_correct)

        time_hacker.set_real_time()

        # Create an Alignak module
        mod = Module({
            'module_alias': 'nrpe-booster',
            'module_types': 'nrpe-booster',
            'python_name': 'alignak_module_nrpe_booster'
        })

        # Create the modules manager for a daemon type
        self.modulemanager = ModulesManager('poller', None)

        # Clear logs
        self.clear_logs()

        # Load and initialize the modules:
        #  - load python module
        #  - get module properties and instances
        self.modulemanager.load_and_init([mod])

        # Loading module logs
        self.assert_log_match(
            re.escape(
                "Importing Python module 'alignak_module_nrpe_booster' for nrpe-booster..."
            ), 0)
        self.assert_log_match(
            re.escape(
                "Module properties: {'daemons': ['poller'], 'phases': ['running'], "
                "'type': 'nrpe_poller', 'external': False, 'worker_capable': True}"
            ), 1)
        self.assert_log_match(
            re.escape(
                "Imported 'alignak_module_nrpe_booster' for nrpe-booster"), 2)
        self.assert_log_match(
            re.escape(
                "Loaded Python module 'alignak_module_nrpe_booster' (nrpe-booster)"
            ), 3)
        self.assert_log_match(
            re.escape(
                "Give an instance of alignak_module_nrpe_booster for alias: nrpe-booster"
            ), 4)

        # Starting internal module logs
        self.assert_log_match("Trying to initialize module: nrpe-booster", 5)
        self.assert_log_match("Initialization of the NRPE poller module", 6)

        my_module = self.modulemanager.instances[0]

        # Get list of not external modules
        self.assertListEqual([my_module],
                             self.modulemanager.get_internal_instances())
        for phase in ['configuration', 'late_configuration', 'retention']:
            self.assertListEqual(
                [], self.modulemanager.get_internal_instances(phase))
        for phase in ['running']:
            self.assertListEqual(
                [my_module], self.modulemanager.get_internal_instances(phase))

        # Get list of external modules
        self.assertListEqual([], self.modulemanager.get_external_instances())
        for phase in [
                'configuration', 'late_configuration', 'running', 'retention'
        ]:
            self.assertListEqual(
                [], self.modulemanager.get_external_instances(phase))

        # Clear logs
        self.clear_logs()

        # Nothing special ...
        self.modulemanager.check_alive_instances()

        # And we clear all now
        self.modulemanager.stop_all()
        # Stopping module logs

        self.assert_log_match("Ending the NRPE poller module", 0)
Exemple #12
0
    def test_module_zzz_host_get(self):
        """Test the module /host API - host creation and get information
        :return:
        """
        self.print_header()
        # Obliged to call to get a self.logger...
        self.setup_with_file('cfg/cfg_default.cfg')
        self.assertTrue(self.conf_is_correct)

        # -----
        # Provide parameters - logger configuration file (exists)
        # -----
        # Clear logs
        self.clear_logs()

        # Create an Alignak module
        mod = Module({
            'module_alias': 'web-services',
            'module_types': 'web-services',
            'python_name': 'alignak_module_ws',
            # Alignak backend
            'alignak_backend': 'http://127.0.0.1:5000',
            'username': '******',
            'password': '******',
            # Do not set a timestamp in the built external commands
            'set_timestamp': '0',
            'give_result': '1',
            'give_feedback': '1',
            # Set Arbiter address as empty to not poll the Arbiter else the test will fail!
            'alignak_host': '',
            'alignak_port': 7770,
            # Allow host/service creation
            'allow_host_creation': '1',
            'allow_service_creation': '1',
            # Errors for unknown host/service
            'ignore_unknown_host': '0',
            'ignore_unknown_service': '0',
        })

        # Create the modules manager for a daemon type
        self.modulemanager = ModulesManager('receiver', None)

        # Load an initialize the modules:
        #  - load python module
        #  - get module properties and instances
        self.modulemanager.load_and_init([mod])

        my_module = self.modulemanager.instances[0]

        # Clear logs
        self.clear_logs()

        # Start external modules
        self.modulemanager.start_external_instances()

        # Starting external module logs
        self.assert_log_match("Trying to initialize module: web-services", 0)
        self.assert_log_match("Starting external module web-services", 1)
        self.assert_log_match(
            "Starting external process for module web-services", 2)
        self.assert_log_match("web-services is now started", 3)

        # Check alive
        self.assertIsNotNone(my_module.process)
        self.assertTrue(my_module.process.is_alive())

        time.sleep(1)

        # Do not allow GET request on /host - not yet authorized!
        response = requests.get('http://127.0.0.1:8888/host')
        self.assertEqual(response.status_code, 401)

        session = requests.Session()

        # Login with username/password (real backend login)
        headers = {'Content-Type': 'application/json'}
        params = {'username': '******', 'password': '******'}
        response = session.post('http://127.0.0.1:8888/login',
                                json=params,
                                headers=headers)
        assert response.status_code == 200
        resp = response.json()

        # -----
        # Get a non-existing host - 1st: use parameters in the request
        response = session.get('http://127.0.0.1:8888/host',
                               auth=self.auth,
                               params={'name': 'new_host_2'})
        result = response.json()
        self.assertEqual(
            result, {
                u'_status': u'ERR',
                u'_result': [],
                u'_issues': ["Requested host 'new_host_2' does not exist"]
            })

        # Get a non-existing host - 2nd: use host name in the URI
        response = session.get('http://127.0.0.1:8888/host/new_host_2',
                               auth=self.auth)
        result = response.json()
        self.assertEqual(
            result, {
                u'_status': u'ERR',
                u'_result': [],
                u'_issues': ["Requested host 'new_host_2' does not exist"]
            })

        # -----
        # Request to create an host - no provided data (default)
        headers = {'Content-Type': 'application/json'}
        data = {
            "name": "new_host_0",
        }
        self.assertEqual(my_module.received_commands, 0)
        response = session.patch('http://127.0.0.1:8888/host',
                                 json=data,
                                 headers=headers)
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(
            result, {
                u'_status':
                u'OK',
                u'_result': [
                    u'new_host_0 is alive :)',
                    u"Requested host 'new_host_0' does not exist.",
                    u"Requested host 'new_host_0' created."
                ],
                u'_feedback': {
                    u'name': u'new_host_0'
                }
            })
        # Host created with default check_command and in default user realm

        # -----
        # Get new host to confirm creation - 1st: use parameters in the request
        response = session.get('http://127.0.0.1:8888/host',
                               auth=self.auth,
                               params={'name': 'new_host_0'})
        result = response.json()
        self.assertEqual(result['_status'], 'OK')
        self.assertIsNot(result['_result'], {})
        self.assertEqual(result['_result'][0]['name'], 'new_host_0')

        # Get new host to confirm creation - 2nd: use host name in the URI
        response = session.get('http://127.0.0.1:8888/host/new_host_0',
                               auth=self.auth)
        result = response.json()
        from pprint import pprint
        pprint(result['_result'])
        # [{u'2d_coords': u'',
        # u'3d_coords': u'',
        # u'_created': u'Thu, 01 Jun 2017 10:58:30 GMT',
        # u'_etag': u'691c3f4a7cc8996c1d047932421759c020d00857',
        # u'_id': u'592ff35606fd4b7eec395625',
        # u'_is_template': False,
        # u'_links': {u'self': {u'href': u'host/592ff35606fd4b7eec395625',
        #                       u'title': u'Host'}},
        # u'_overall_state_id': 3,
        # u'_realm': {u'_all_children': [],
        #             u'_children': [],
        #             u'_created': u'Thu, 01 Jun 2017 10:58:24 GMT',
        #             u'_etag': u'26b3830c017b4fca8553365246f21267aece46a7',
        #             u'_id': u'592ff35006fd4b7eec3955eb',
        #             u'_level': 0,
        #             u'_parent': None,
        #             u'_tree_parents': [],
        #             u'_updated': u'Thu, 01 Jun 2017 10:58:27 GMT',
        #             u'alias': u'',
        #             u'default': True,
        #             u'definition_order': 100,
        #             u'global_critical_threshold': 5,
        #             u'global_warning_threshold': 3,
        #             u'hosts_critical_threshold': 5,
        #             u'hosts_warning_threshold': 3,
        #             u'imported_from': u'unknown',
        #             u'name': u'All',
        #             u'notes': u'',
        #             u'services_critical_threshold': 5,
        #             u'services_warning_threshold': 3},
        # u'_sub_realm': True,
        # u'_template_fields': {},
        # u'_templates': [],
        # u'_templates_with_services': True,
        # u'_updated': u'Thu, 01 Jun 2017 10:58:30 GMT',
        # u'action_url': u'',
        # u'active_checks_enabled': True,
        # u'address': u'',
        # u'address6': u'',
        # u'alias': u'',
        # u'business_impact': 2,
        # u'business_impact_modulations': [],
        # u'business_rule_downtime_as_ack': False,
        # u'business_rule_host_notification_options': [u'd', u'u', u'r', u'f', u's'],
        # u'business_rule_output_template': u'',
        # u'business_rule_service_notification_options': [u'w',
        #                                                 u'u',
        #                                                 u'c',
        #                                                 u'r',
        #                                                 u'f',
        #                                                 u's'],
        # u'business_rule_smart_notifications': False,
        # u'check_command': {u'_created': u'Thu, 01 Jun 2017 10:58:24 GMT',
        #                    u'_etag': u'356d02479ca7dbebe85e22b9b43e95dc9d5d037c',
        #                    u'_id': u'592ff35006fd4b7eec3955f1',
        #                    u'_realm': u'592ff35006fd4b7eec3955eb',
        #                    u'_sub_realm': True,
        #                    u'_updated': u'Thu, 01 Jun 2017 10:58:24 GMT',
        #                    u'alias': u'Host/service is always UP/OK',
        #                    u'command_line': u'_internal_host_up',
        #                    u'definition_order': 100,
        #                    u'enable_environment_macros': False,
        #                    u'imported_from': u'unknown',
        #                    u'module_type': u'fork',
        #                    u'name': u'_internal_host_up',
        #                    u'notes': u'',
        #                    u'poller_tag': u'',
        #                    u'reactionner_tag': u'',
        #                    u'timeout': -1},
        # u'check_command_args': u'',
        # u'check_freshness': False,
        # u'check_interval': 5,
        # u'checkmodulations': [],
        # u'custom_views': [],
        # u'customs': {},
        # u'definition_order': 100,
        # u'display_name': u'',
        # u'escalations': [],
        # u'event_handler': None,
        # u'event_handler_args': u'',
        # u'event_handler_enabled': False,
        # u'failure_prediction_enabled': False,
        # u'first_notification_delay': 0,
        # u'flap_detection_enabled': True,
        # u'flap_detection_options': [u'o', u'd', u'x'],
        # u'freshness_state': u'x',
        # u'freshness_threshold': 0,
        # u'high_flap_threshold': 50,
        # u'icon_image': u'',
        # u'icon_image_alt': u'',
        # u'icon_set': u'',
        # u'imported_from': u'unknown',
        # u'initial_state': u'x',
        # u'labels': [],
        # u'location': {u'coordinates': [48.858293, 2.294601], u'type': u'Point'},
        # u'low_flap_threshold': 25,
        # u'ls_acknowledged': False,
        # u'ls_acknowledgement_type': 1,
        # u'ls_attempt': 0,
        # u'ls_current_attempt': 0,
        # u'ls_downtimed': False,
        # u'ls_execution_time': 0.0,
        # u'ls_grafana': False,
        # u'ls_grafana_panelid': 0,
        # u'ls_impact': False,
        # u'ls_last_check': 0,
        # u'ls_last_hard_state_changed': 0,
        # u'ls_last_notification': 0,
        # u'ls_last_state': u'OK',
        # u'ls_last_state_changed': 0,
        # u'ls_last_state_type': u'HARD',
        # u'ls_last_time_down': 0,
        # u'ls_last_time_unknown': 0,
        # u'ls_last_time_unreachable': 0,
        # u'ls_last_time_up': 0,
        # u'ls_latency': 0.0,
        # u'ls_long_output': u'',
        # u'ls_max_attempts': 0,
        # u'ls_next_check': 0,
        # u'ls_output': u'',
        # u'ls_passive_check': False,
        # u'ls_perf_data': u'',
        # u'ls_state': u'UNREACHABLE',
        # u'ls_state_id': 3,
        # u'ls_state_type': u'HARD',
        # u'macromodulations': [],
        # u'max_check_attempts': 1,
        # u'name': u'new_host_0',
        # u'notes': u'',
        # u'notes_url': u'',
        # u'notification_interval': 60,
        # u'notification_options': [u'd', u'x', u'r', u'f', u's'],
        # u'notifications_enabled': True,
        # u'obsess_over_host': False,
        # u'parents': [],
        # u'passive_checks_enabled': True,
        # u'poller_tag': u'',
        # u'process_perf_data': True,
        # u'reactionner_tag': u'',
        # u'resultmodulations': [],
        # u'retry_interval': 0,
        # u'service_excludes': [],
        # u'service_includes': [],
        # u'service_overrides': [],
        # u'snapshot_criteria': [u'd', u'x'],
        # u'snapshot_enabled': False,
        # u'snapshot_interval': 5,
        # u'stalking_options': [],
        # u'statusmap_image': u'',
        # u'tags': [],
        # u'time_to_orphanage': 300,
        # u'trending_policies': [],
        # u'trigger_broker_raise_enabled': False,
        # u'trigger_name': u'',
        # u'usergroups': [],
        # u'users': [],
        # u'vrml_image': u''}]
        #
        self.assertEqual(result['_status'], 'OK')
        self.assertIsNot(result['_result'], {})
        self.assertEqual(result['_result'][0]['name'], 'new_host_0')

        # -----
        # Logout
        response = session.get('http://127.0.0.1:8888/logout')
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(result['_status'], 'OK')
        self.assertEqual(result['_result'], 'Logged out')

        self.modulemanager.stop_all()
Exemple #13
0
    def setUp(self):
        """Create resources in backend

        :return: None
        """
        self.print_header()
        # Obliged to call to get a self.logger...
        self.setup_with_file('cfg/cfg_default.cfg')
        self.assertTrue(self.conf_is_correct)

        # -----
        # Provide parameters - logger configuration file (exists)
        # -----
        # Clear logs
        self.clear_logs()

        # Create an Alignak module
        mod = Module({
            'module_alias': 'web-services',
            'module_types': 'web-services',
            'python_name': 'alignak_module_ws',
            # Alignak backend
            'alignak_backend': 'http://127.0.0.1:5000',
            'username': '******',
            'password': '******',
            # Do not set a timestamp in the built external commands
            'set_timestamp': '0',
            # Set Arbiter address as empty to not poll the Arbiter else the test will fail!
            'alignak_host': '',
            'alignak_port': 7770,
            # Allow host/service creation
            'allow_host_creation': '1',
            'allow_service_creation': '1'
        })

        # Create the modules manager for a daemon type
        self.modulemanager = ModulesManager('receiver', None)

        # Load an initialize the modules:
        #  - load python module
        #  - get module properties and instances
        self.modulemanager.load_and_init([mod])

        self.my_module = self.modulemanager.instances[0]

        # Clear logs
        self.clear_logs()

        # Start external modules
        self.modulemanager.start_external_instances()

        # Starting external module logs
        self.assert_log_match("Trying to initialize module: web-services", 0)
        self.assert_log_match("Starting external module web-services", 1)
        self.assert_log_match(
            "Starting external process for module web-services", 2)
        self.assert_log_match("web-services is now started", 3)

        # Check alive
        self.assertIsNotNone(self.my_module.process)
        self.assertTrue(self.my_module.process.is_alive())

        time.sleep(1)
Exemple #14
0
    def setUp(self):
        from alignak.objects.module import Module

        self.item = Module(parsing=True)
    def test_module_manager_arbiter(self):
        """
        Test if the module manager manages correctly all the modules
        :return:
        """
        self.print_header()
        self.setup_with_file('cfg/cfg_default.cfg')
        self.assertTrue(self.conf_is_correct)

        time_hacker.set_real_time()

        # Create an Alignak module
        mod = Module({
            'module_alias': 'SnmpBoosterArbiter',
            'module_types': 'checks',
            'python_name': 'alignak_module_snmp_booster.snmpbooster_arbiter',
            'loaded_by': 'arbiter',
            'datasource': './cfg/genDevConfig/example.ini',
            'db_host': 'localhost',
            'db_port': 6379
        })

        # Create the modules manager for a daemon type
        self.modulemanager = ModulesManager('arbiter', None)

        # Clear logs
        self.clear_logs()

        # Load and initialize the modules:
        #  - load python module
        #  - get module properties and instances
        self.modulemanager.load_and_init([mod])

        # Loading module logs
        self.assert_log_match(
            re.escape(
                "Importing Python module 'alignak_module_snmp_booster.snmpbooster_arbiter' for "
                "SnmpBoosterArbiter..."), 0)
        self.assert_log_match(
            re.escape(
                "Module properties: {'daemons': ['arbiter'], 'phases': ['running', "
                "'late_configuration'], 'type': 'snmp_booster', 'external': False, "
                "'worker_capable': True}"), 1)
        self.assert_log_match(
            re.escape(
                "Imported 'alignak_module_snmp_booster.snmpbooster_arbiter' for SnmpBoosterArbiter"
            ), 2)
        self.assert_log_match(
            re.escape(
                "Loaded Python module 'alignak_module_snmp_booster.snmpbooster_arbiter' "
                "(SnmpBoosterArbiter)"), 3)
        self.assert_log_match(
            re.escape(
                "Give an instance of alignak_module_snmp_booster.snmpbooster_arbiter "
                "for alias: SnmpBoosterArbiter"), 4)

        self.assert_log_match(
            re.escape(
                "[SnmpBooster] [code 0101] Loading SNMP Booster module for plugin SnmpBoosterArbiter"
            ), 5)
        self.assert_log_match(
            re.escape(
                "[SnmpBooster] [code 0902] Reading input configuration file: "
                "./cfg/genDevConfig/example.ini"), 6)

        # Starting internal module logs
        self.assert_log_match(
            re.escape("Trying to initialize module: SnmpBoosterArbiter"), 7)
        self.assert_log_match(
            re.escape(
                "[SnmpBooster] [code 1101] Initialization of the SNMP Booster 2.0.0"
            ), 8)

        my_module = self.modulemanager.instances[0]

        # Get list of not external modules
        self.assertListEqual([my_module],
                             self.modulemanager.get_internal_instances())
        for phase in ['configuration', 'retention']:
            self.assertListEqual(
                [], self.modulemanager.get_internal_instances(phase))
        for phase in ['late_configuration', 'running']:
            self.assertListEqual(
                [my_module], self.modulemanager.get_internal_instances(phase))

        # Get list of external modules
        self.assertListEqual([], self.modulemanager.get_external_instances())
        for phase in [
                'configuration', 'late_configuration', 'running', 'retention'
        ]:
            self.assertListEqual(
                [], self.modulemanager.get_external_instances(phase))

        # Clear logs
        self.clear_logs()

        # Nothing special ...
        self.modulemanager.check_alive_instances()

        # And we clear all now
        self.modulemanager.stop_all()
    def setUpClass(cls):
        # Set test mode for alignak backend
        # Uncomment to log the bakend REST API
        # os.environ['TEST_ALIGNAK_BACKEND'] = '1'
        os.environ[
            'ALIGNAK_BACKEND_MONGO_DBNAME'] = 'alignak-module-backend-test'

        # Delete used mongo DBs
        print("Deleting Alignak backend DB...")
        exit_code = subprocess.call(
            shlex.split('mongo %s --eval "db.dropDatabase()"' %
                        os.environ['ALIGNAK_BACKEND_MONGO_DBNAME']))
        assert exit_code == 0

        cls.p = subprocess.Popen([
            'uwsgi', '--plugin', 'python', '-w', 'alignakbackend:app',
            '--socket', '0.0.0.0:5000', '--protocol=http', '--enable-threads',
            '--pidfile', '/tmp/uwsgi.pid'
        ])
        time.sleep(3)

        cls.backend = Backend('http://127.0.0.1:5000')
        cls.backend.login("admin", "admin", "force")
        realms = cls.backend.get_all('realm')
        for cont in realms['_items']:
            cls.realm_all = cont['_id']

        # Get admin user
        users = cls.backend.get_all('user')
        for user in users['_items']:
            if user['name'] == 'admin':
                cls.user_admin = user

        # add commands
        data = json.loads(open('cfg/command_ping.json').read())
        data['_realm'] = cls.realm_all
        data_cmd_ping = cls.backend.post("command", data)
        data = json.loads(open('cfg/command_http.json').read())
        data['_realm'] = cls.realm_all
        data_cmd_http = cls.backend.post("command", data)
        cls.ping_cmd = data_cmd_http['_id']

        # add 1 host
        data = json.loads(open('cfg/host_srv001.json').read())
        data['check_command'] = data_cmd_ping['_id']
        del data['realm']
        data['_realm'] = cls.realm_all
        cls.data_host = cls.backend.post("host", data)

        # add 2 services
        data = json.loads(open('cfg/service_srv001_ping.json').read())
        data['host'] = cls.data_host['_id']
        data['check_command'] = data_cmd_ping['_id']
        data['_realm'] = cls.realm_all
        cls.data_srv_ping = cls.backend.post("service", data)

        data = json.loads(open('cfg/service_srv001_http.json').read())
        data['host'] = cls.data_host['_id']
        data['check_command'] = data_cmd_http['_id']
        data['_realm'] = cls.realm_all
        cls.data_srv_http = cls.backend.post("service", data)

        # Start arbiter module
        modconf = Module()
        modconf.module_alias = "backend_arbiter"
        modconf.username = "******"
        modconf.password = "******"
        modconf.api_url = 'http://127.0.0.1:5000'

        # Update default check timers
        # check every x min if config in backend changed, if yes it will reload it
        # Default, every 5 minutes
        modconf.verify_modification = 1
        # verify_modification     5

        cls.arbmodule = AlignakBackendArbiter(modconf)
        cls.objects = cls.arbmodule.get_objects()
Exemple #17
0
    def test_module_host_livestate_unauthorized(self):
        """Test the module /host API - host livestate - unauthorized access
        :return:
        """
        # Create an Alignak module
        mod = Module({
            'module_alias': 'web-services',
            'module_types': 'web-services',
            'python_name': 'alignak_module_ws',
            # Alignak backend
            'alignak_backend': '',
            'username': '',
            'password': '',
            # Do not set a timestamp in the built external commands
            'set_timestamp': '0',
            # Do not give feedback data
            'give_feedback': '0',
            'give_result': '1',
            # Set Arbiter address as empty to not poll the Arbiter else the test will fail!
            'alignak_host': '',
            'alignak_port': 7770,
            # Set module to listen on all interfaces
            'host': '0.0.0.0',
            'port': 8888,

            # Allow host/service creation
            'allow_host_creation': '1',
            'allow_service_creation': '1',
            # Force Alignak backend update by the module (default is not force!)
            'alignak_backend_livestate_update': '0',

            # Disable authorization
            'authorization': '0'
        })

        # Create a receiver daemon
        args = {'env_file': '', 'daemon_name': 'receiver-master'}
        self._receiver_daemon = Receiver(**args)

        # Create the modules manager for the daemon
        self.modulemanager = ModulesManager(self._receiver_daemon)

        # Load an initialize the modules:
        #  - load python module
        #  - get module properties and instances
        self.modulemanager.load_and_init([mod])

        my_module = self.modulemanager.instances[0]

        # Clear logs
        self.clear_logs()

        # Start external modules
        self.modulemanager.start_external_instances()

        # Starting external module logs
        self.assert_log_match("Trying to initialize module: web-services", 0)
        self.assert_log_match("Starting external module web-services", 1)
        self.assert_log_match(
            "Starting external process for module web-services", 2)
        self.assert_log_match("web-services is now started", 3)

        # Check alive
        self.assertIsNotNone(my_module.process)
        self.assertTrue(my_module.process.is_alive())

        time.sleep(1)

        session = requests.Session()

        headers = {'Content-Type': 'application/json'}
        params = {'username': '******', 'password': '******'}
        response = session.post(self.ws_endpoint + '/login',
                                json=params,
                                headers=headers)
        assert response.status_code == 200
        resp = response.json()

        # -----
        # Update an host with an host livestate (heartbeat / host is alive): livestate
        # Because there is no backend configured, only an external command is raised
        # to Alignak for the host
        data = {
            "name": "new_host_0",
            "livestate": {
                # No timestamp in the livestate
                "state": "UP",
                "output": "Output...",
                "long_output": "Long output...",
                "perf_data": "'counter'=1",
            }
        }
        self.assertEqual(my_module.received_commands, 0)
        response = session.patch(self.ws_endpoint + '/host',
                                 json=data,
                                 headers=headers)
        print(response)
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(
            result, {
                '_status':
                'OK',
                '_result': [
                    'new_host_0 is alive :)',
                    "PROCESS_HOST_CHECK_RESULT;new_host_0;0;Output...|'counter'=1\nLong output...",
                ]
            })
        # No errors - an extrenal command was raised for the host livestate

        # -----
        # Update an host with an host livestate (heartbeat / host is alive): livestate
        # Because there is no backend configured, only an external command is raised
        # to Alignak for the host
        data = {
            "name":
            "new_host_0",
            "livestate": {
                # No timestamp in the livestate
                "state": "UP",
                "output": "Output...",
                "long_output": "Long output...",
                "perf_data": "'counter'=1",
            },
            "services": [{
                "name": "test_svc_0",
                "livestate": {
                    "state": "OK",
                    "output": "Output...",
                    "long_output": "Long output...",
                    "perf_data": "'counter'=1",
                }
            }]
        }
        self.assertEqual(my_module.received_commands, 1)
        response = session.patch(self.ws_endpoint + '/host',
                                 json=data,
                                 headers=headers)
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(
            result, {
                '_status':
                'OK',
                '_result': [
                    'new_host_0 is alive :)',
                    "PROCESS_HOST_CHECK_RESULT;new_host_0;0;Output...|'counter'=1\nLong output...",
                    "PROCESS_SERVICE_CHECK_RESULT;new_host_0;test_svc_0;0;Output...|'counter'=1\nLong output..."
                ]
            })
        # No errors - an extrenal command was raised for the host livestate

        self.modulemanager.stop_all()
Exemple #18
0
    def test_module_manager(self):
        """
        Test if the module manager manages correctly all the modules
        :return:
        """
        self.setup_with_file('./cfg/alignak.cfg')
        self.assertTrue(self.conf_is_correct)
        self.clear_logs()

        # Create an Alignak module
        mod = Module({
            'module_alias': 'nsca',
            'module_types': 'nsca',
            'python_name': 'alignak_module_nsca'
        })

        # Create the modules manager for a daemon type
        self.modulemanager = ModulesManager(self._broker_daemon)

        # Load an initialize the modules:
        #  - load python module
        #  - get module properties and instances
        self.modulemanager.load_and_init([mod])

        # Loading module nsca
        print("Load and init")
        self.show_logs()
        i = 0
        self.assert_log_match(
            re.escape(
                "Importing Python module 'alignak_module_nsca' for nsca..."),
            i)
        i += 1
        # Dict order is problematic :/
        # self.assert_log_match(re.escape(
        #     "Module properties: {'daemons': ['broker'], 'phases': ['running'], "
        #     "'type': 'nsca', 'external': True}"
        # ), i)
        i += 1
        self.assert_log_match(
            re.escape("Imported 'alignak_module_nsca' for nsca"), i)
        i += 1
        self.assert_log_match(
            re.escape("Loaded Python module 'alignak_module_nsca' (nsca)"), i)
        i += 1
        self.assert_log_match(re.escape("Alignak starting module 'nsca'"), i)
        i += 1
        self.assert_log_match(
            re.escape(
                "Give an instance of alignak_module_nsca for alias: nsca"), i)
        i += 1
        self.assert_log_match(
            re.escape(
                "configuration, allowed hosts : '127.0.0.1'(5667), buffer length: 4096, "
                "payload length: -1, encryption: 0, max packet age: 30, "
                "check future packet: True, backlog: 10"), i)

        time.sleep(1)
        # Reload the module
        print("Reload")
        self.modulemanager.load([mod])
        self.modulemanager.get_instances()
        #
        # Loading module nsca
        self.show_logs()
        i = 0
        self.assert_log_match(
            re.escape(
                "Importing Python module 'alignak_module_nsca' for nsca..."),
            i)
        i += 1
        # self.assert_log_match(re.escape(
        #     "Module properties: {'daemons': ['broker'], 'phases': ['running'], "
        #     "'type': 'nsca', 'external': True}"
        # ), i)
        i += 1
        self.assert_log_match(
            re.escape("Imported 'alignak_module_nsca' for nsca"), i)
        i += 1
        self.assert_log_match(
            re.escape("Loaded Python module 'alignak_module_nsca' (nsca)"), i)
        i += 1
        self.assert_log_match(re.escape("Alignak starting module 'nsca'"), i)
        i += 1
        self.assert_log_match(
            re.escape(
                "Give an instance of alignak_module_nsca for alias: nsca"), i)
        i += 1
        self.assert_log_match(
            re.escape(
                "configuration, allowed hosts : '127.0.0.1'(5667), buffer length: 4096, "
                "payload length: -1, encryption: 0, max packet age: 30, "
                "check future packet: True, backlog: 10"), i)
        i += 1
        self.assert_log_match(
            re.escape(
                "Importing Python module 'alignak_module_nsca' for nsca..."),
            i)
        i += 1
        # self.assert_log_match(re.escape(
        #     "Module properties: {'daemons': ['broker'], 'phases': ['running'], "
        #     "'type': 'nsca', 'external': True}"
        # ), i)
        i += 1
        self.assert_log_match(
            re.escape("Imported 'alignak_module_nsca' for nsca"), i)
        i += 1
        self.assert_log_match(
            re.escape("Loaded Python module 'alignak_module_nsca' (nsca)"), i)
        i += 1
        self.assert_log_match(
            re.escape("Request external process to stop for nsca"), i)
        i += 1
        self.assert_log_match(re.escape("External process stopped."), i)
        i += 1
        self.assert_log_match(re.escape("Alignak starting module 'nsca'"), i)
        i += 1
        # self.assert_log_match(re.escape(
        #     "Give an instance of alignak_module_nsca for alias: nsca"
        # ), i)
        # i += 1
        self.assert_log_match(
            re.escape(
                "Give an instance of alignak_module_nsca for alias: nsca"), i)
        i += 1
        self.assert_log_match(
            re.escape(
                "configuration, allowed hosts : '127.0.0.1'(5667), buffer length: 4096, "
                "payload length: -1, encryption: 0, max packet age: 30, "
                "check future packet: True, backlog: 10"), i)

        my_module = self.modulemanager.instances[0]

        # Get list of not external modules
        self.assertListEqual([], self.modulemanager.get_internal_instances())
        for phase in [
                'configuration', 'late_configuration', 'running', 'retention'
        ]:
            self.assertListEqual(
                [], self.modulemanager.get_internal_instances(phase))

        # Get list of external modules
        self.assertListEqual([my_module],
                             self.modulemanager.get_external_instances())
        for phase in ['configuration', 'late_configuration', 'retention']:
            self.assertListEqual(
                [], self.modulemanager.get_external_instances(phase))
        for phase in ['running']:
            self.assertListEqual(
                [my_module], self.modulemanager.get_external_instances(phase))

        # Clear nsca
        self.clear_logs()

        # Start external modules
        self.modulemanager.start_external_instances()

        # Starting external module nsca
        self.assert_log_match("Trying to initialize module: nsca", 0)
        self.assert_log_match("Starting external module nsca", 1)
        self.assert_log_match("Starting external process for module nsca", 2)
        self.assert_log_match("nsca is now started", 3)

        # Check alive
        self.assertIsNotNone(my_module.process)
        self.assertTrue(my_module.process.is_alive())

        # Clear nsca
        self.clear_logs()

        # Kill the external module (normal stop is .stop_process)
        my_module.kill()
        time.sleep(0.1)
        index = 0
        self.assert_log_match("Killing external module", index)
        index = index + 1
        # todo: This log is not expected! But it is probably because of the py.test ...
        # Indeed the receiver daemon that the module is attached to is receiving a SIGTERM !!!
        self.assert_log_match(
            re.escape(
                "nsca is still living 10 seconds after a normal kill, I help it to die"
            ), index)
        index = index + 1
        self.assert_log_match("External module killed", index)
        index = index + 1

        # Should be dead (not normally stopped...) but we still know a process for this module!
        self.assertIsNotNone(my_module.process)

        # Nothing special ...
        self.modulemanager.check_alive_instances()
        self.assert_log_match("The external module nsca died unexpectedly!",
                              index)
        index = index + 1
        self.assert_log_match("Setting the module nsca to restart", index)
        index = index + 1

        # # Try to restart the dead modules
        # self.modulemanager.try_to_restart_deads()
        # self.assert_log_match("Trying to restart module: nsca", index)
        # index = index +1
        # self.assert_log_match("Too early to retry initialization, retry period is 5 seconds", index)
        # index = index +1
        #
        # # In fact it's too early, so it won't do it
        # # The module instance is still dead
        # self.assertFalse(my_module.process.is_alive())

        # So we lie, on the restart tries ...
        my_module.last_init_try = -5
        self.modulemanager.check_alive_instances()
        self.modulemanager.try_to_restart_deads()
        self.assert_log_match("Trying to restart module: nsca", index)
        index = index + 1
        self.assert_log_match("Trying to initialize module: nsca", index)
        index = index + 1
        self.assert_log_match("Restarting nsca...", index)
        index = index + 1

        # The module instance is now alive again
        self.assertTrue(my_module.process.is_alive())
        self.assert_log_match("Starting external process for module nsca",
                              index)
        index = index + 1
        self.assert_log_match("nsca is now started", index)
        index = index + 1

        # There is nothing else to restart in the module manager
        self.assertEqual([], self.modulemanager.to_restart)

        # Clear nsca
        self.clear_logs()

        # Let the module start and then kill it again
        time.sleep(3.0)
        my_module.kill()
        # time.sleep(5.0)
        self.show_logs()
        print("My module PID 2: %s" % my_module.process.pid)
        time.sleep(0.2)
        self.assertFalse(my_module.process.is_alive())
        index = 0
        self.assert_log_match("Killing external module", index)
        index = index + 1
        # # todo: This log is not expected! But it is probably because of the py.test ...
        # # Indeed the receiver daemon that the module is attached to is receiving a SIGTERM !!!
        # self.assert_log_match(re.escape("'web-services' is still living 10 seconds after a normal kill, I help it to die"), index)
        # index = index +1
        self.assert_log_match("External module killed", index)
        index = index + 1

        # The module is dead but the modules manager do not know yet!
        self.modulemanager.check_alive_instances()
        self.assert_log_match("The external module nsca died unexpectedly!",
                              index)
        index = index + 1
        self.assert_log_match("Setting the module nsca to restart", index)
        index = index + 1

        self.modulemanager.try_to_restart_deads()
        self.assert_log_match("Trying to restart module: nsca", index)
        index = index + 1
        self.assert_log_match(
            "Too early to retry initialization, retry period is 5 seconds",
            index)
        index = index + 1

        # In fact it's too early, so it won't do it
        # The module instance is still dead
        self.assertFalse(my_module.process.is_alive())

        # So we lie, on the restart tries ...
        my_module.last_init_try = -5
        self.modulemanager.check_alive_instances()
        self.modulemanager.try_to_restart_deads()
        self.assert_log_match("Trying to restart module: nsca", index)
        index = index + 1
        self.assert_log_match("Trying to initialize module: nsca", index)
        index = index + 1
        self.assert_log_match("Restarting nsca...", index)
        index = index + 1

        # The module instance is now alive again
        self.assertTrue(my_module.process.is_alive())
        self.assert_log_match("Starting external process for module nsca",
                              index)
        index = index + 1
        self.assert_log_match("nsca is now started", index)
        index = index + 1
        time.sleep(1.0)
        print("My module PID: %s" % my_module.process.pid)

        # Clear nsca
        self.clear_logs()

        # And we clear all now
        self.modulemanager.stop_all()
        # Stopping module nsca

        index = 0
        self.assert_log_match("Shutting down modules...", index)
        index = index + 1
        self.assert_log_match("Request external process to stop for nsca",
                              index)
        index = index + 1
        self.assert_log_match(re.escape("I'm stopping module 'nsca' (pid="),
                              index)
        index = index + 1
        # self.assert_log_match(re.escape("'nsca' is still living after a normal kill, I help it to die"), index)
        # index = index +1
        self.assert_log_match(re.escape("Killing external module (pid"), index)
        index = index + 1
        self.assert_log_match(re.escape("External module killed"), index)
        index = index + 1
        self.assert_log_match("External process stopped.", index)
        index = index + 1
    def test_delete_host_service_detection(self):
        """Test if the hook detect we have deleted an host / service in the backend
        """
        class Arbiter(object):
            conf = Config()
            pidfile = '/tmp/arbiter.pid'

        arb = Arbiter()

        if os.path.exists('/tmp/arbiter.pid'):
            os.remove('/tmp/arbiter.pid')

        now = timegm(datetime.utcnow().utctimetuple())

        # add 1 host
        data = json.loads(open('cfg/host_srv001.json').read())
        data['check_command'] = self.ping_cmd
        data['name'] = 'srv002'
        del data['realm']
        data['_realm'] = self.realm_all
        srv002 = self.data_host = self.backend.post("host", data)

        # Get our existing host
        hosts = self.backend.get_all('host')
        self.assertEqual(len(hosts['_items']), 3)

        # Start arbiter module
        modconf = Module()
        modconf.module_alias = "backend_arbiter"
        modconf.username = "******"
        modconf.password = "******"
        modconf.api_url = 'http://127.0.0.1:5000'

        # Update default check timers
        # check every x min if config in backend changed, if yes it will reload it
        # Default, every 5 minutes
        modconf.verify_modification = 1
        # verify_modification     5

        self.arbmodule = AlignakBackendArbiter(modconf)
        self.objects = self.arbmodule.get_objects()

        # No configuration check is done ... because it is not yet the moment to check.
        print("No configuration check")
        next_check = self.arbmodule.next_check
        print("Next check: %s" % (next_check))
        assert self.arbmodule.configuration_reload_required is False
        assert self.arbmodule.configuration_reload_changelog == []
        self.arbmodule.hook_tick(arb)
        print("Next check: %s / %s" % (next_check, self.arbmodule.next_check))
        assert self.arbmodule.next_check == next_check
        assert self.arbmodule.configuration_reload_required is False
        assert self.arbmodule.configuration_reload_changelog == []

        # delete the last host
        headers = {
            'Content-Type': 'application/json',
            'If-Match': srv002['_etag']
        }
        self.backend.delete('host/%s' % srv002['_id'], headers)
        hosts = self.backend.get_all('host')
        self.assertEqual(len(hosts['_items']), 2)

        # Set next check in the past to force a configuration check
        # Set check date in the future
        # And configuration has changed...
        print("Configuration check - a host has been deleted changed")
        self.arbmodule.next_check = now - 1
        self.arbmodule.time_loaded_conf = datetime.utcfromtimestamp(
            now + 1).strftime(self.arbmodule.backend_date_format)
        print("Next check: %s" % (next_check))
        assert self.arbmodule.configuration_reload_required is False
        assert self.arbmodule.configuration_reload_changelog == []
        self.arbmodule.hook_tick(arb)
        print("Next check: %s / %s" % (next_check, self.arbmodule.next_check))
        assert self.arbmodule.next_check > now
        assert self.arbmodule.configuration_reload_required is True
        assert len(self.arbmodule.configuration_reload_changelog) > 0
    def setUpClass(cls):
        # Set test mode for alignak backend
        os.environ['TEST_ALIGNAK_BACKEND'] = '1'
        os.environ[
            'ALIGNAK_BACKEND_MONGO_DBNAME'] = 'alignak-module-backend-test'

        # Delete used mongo DBs
        print("Deleting Alignak backend DB...")
        exit_code = subprocess.call(
            shlex.split('mongo %s --eval "db.dropDatabase()"' %
                        os.environ['ALIGNAK_BACKEND_MONGO_DBNAME']))
        assert exit_code == 0

        cls.p = subprocess.Popen([
            'uwsgi', '--plugin', 'python', '-w', 'alignakbackend:app',
            '--socket', '0.0.0.0:5000', '--protocol=http', '--enable-threads',
            '--pidfile', '/tmp/uwsgi.pid'
        ])
        time.sleep(3)
        cls.backend = Backend('http://127.0.0.1:5000')
        cls.backend.login("admin", "admin", "force")
        realms = cls.backend.get_all('realm')
        for cont in realms['_items']:
            cls.realm_all = cont['_id']

        timeperiods = cls.backend.get_all('timeperiod')
        for tp in timeperiods['_items']:
            if tp['name'] == '24x7':
                timeperiods_id = tp['_id']

        # add commands
        data = json.loads(open('cfg/command_ping.json').read())
        data['_realm'] = cls.realm_all
        data_cmd_ping = cls.backend.post("command", data)
        data = json.loads(open('cfg/command_http.json').read())
        data['_realm'] = cls.realm_all
        data_cmd_http = cls.backend.post("command", data)

        # Add some realms
        data = {'name': 'All-A', '_parent': cls.realm_all}
        realm_a = cls.backend.post("realm", data)
        cls.realm_all_a = realm_a['_id']
        data = {'name': 'All-B', '_parent': cls.realm_all}
        cls.backend.post("realm", data)
        data = {'name': 'All-A-1', '_parent': realm_a['_id']}
        cls.backend.post("realm", data)

        # Get admin user
        resp = cls.backend.get_all('user')
        cls.user_admin = resp['_items'][0]

        # Add user
        # User 1
        data = {
            'name':
            'user1',
            'password':
            '******',
            'back_role_super_admin':
            False,
            'host_notification_period':
            cls.user_admin['host_notification_period'],
            'service_notification_period':
            cls.user_admin['service_notification_period'],
            '_realm':
            realm_a['_id'],
            '_sub_realm':
            False
        }
        user1 = cls.backend.post('user', data)

        data = {
            'user': user1['_id'],
            'realm': realm_a['_id'],
            'resource': '*',
            'crud': ['create', 'read', 'update', 'delete']
        }
        cls.backend.post('userrestrictrole', data)

        # Start arbiter backend module
        modconf = Module()
        modconf.module_alias = "backend_arbiter"
        modconf.username = "******"
        modconf.password = "******"
        modconf.api_url = 'http://127.0.0.1:5000'
        cls.arbmodule = AlignakBackendArbiter(modconf)

        modconf2 = Module()
        modconf2.module_alias = "backend_arbiter"
        modconf2.username = "******"
        modconf2.password = "******"
        modconf2.api_url = 'http://127.0.0.1:5000'
        cls.arbmodule2 = AlignakBackendArbiter(modconf2)
Exemple #21
0
    def test_module_zzz_command_unauthorized(self):
        """ Test the WS /command endpoint - unauthorized access mode
        :return:
        """
        # Create an Alignak module
        mod = Module({
            'module_alias': 'web-services',
            'module_types': 'web-services',
            'python_name': 'alignak_module_ws',
            # Alignak backend - not configured
            'alignak_backend': '',
            'username': '',
            'password': '',
            # Set Arbiter address as empty to not poll the Arbiter else the test will fail!
            'alignak_host': '',
            'alignak_port': 7770,
            # Disable authorization
            'authorization': '0'
        })

        # Create a receiver daemon
        args = {'env_file': '', 'daemon_name': 'receiver-master'}
        self._receiver_daemon = Receiver(**args)

        # Create the modules manager for the daemon
        self.modulemanager = ModulesManager(self._receiver_daemon)

        # Load an initialize the modules:
        #  - load python module
        #  - get module properties and instances
        self.modulemanager.load_and_init([mod])

        my_module = self.modulemanager.instances[0]

        # Clear logs
        self.clear_logs()

        # Start external modules
        self.modulemanager.start_external_instances()

        # Starting external module logs
        self.assert_log_match("Trying to initialize module: web-services", 0)
        self.assert_log_match("Starting external module web-services", 1)
        self.assert_log_match(
            "Starting external process for module web-services", 2)
        self.assert_log_match("web-services is now started", 3)

        # Check alive
        self.assertIsNotNone(my_module.process)
        self.assertTrue(my_module.process.is_alive())

        time.sleep(1)

        session = requests.Session()

        # You must have parameters when POSTing on /command
        headers = {'Content-Type': 'application/json'}
        data = {}
        response = session.post(self.ws_endpoint + '/command',
                                json=data,
                                headers=headers)
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(result['_status'], 'ERR')
        self.assertEqual(result['_error'],
                         'You must POST parameters on this endpoint.')

        self.assertEqual(my_module.received_commands, 0)

        # You must have a command parameter when POSTing on /command
        headers = {'Content-Type': 'application/json'}
        data = {
            # "command": "Command",
            "element": "test_host",
            "parameters": "abc;1"
        }
        self.assertEqual(my_module.received_commands, 0)
        response = session.post(self.ws_endpoint + '/command',
                                json=data,
                                headers=headers)
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(result['_status'], 'ERR')
        # Result error message
        self.assertEqual(result['_error'], 'Missing command parameter')

        # Request to execute an external command
        headers = {'Content-Type': 'application/json'}
        data = {
            "command": "Command",
            "element": "test_host",
            "parameters": "abc;1"
        }
        self.assertEqual(my_module.received_commands, 0)
        response = session.post(self.ws_endpoint + '/command',
                                json=data,
                                headers=headers)
        self.assertEqual(my_module.received_commands, 1)
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(result['_status'], 'OK')
        # Result is uppercase command, parameters are ordered
        self.assertEqual(result['_command'], 'COMMAND;test_host;abc;1')

        # Request to execute an external command with timestamp - bad value
        headers = {'Content-Type': 'application/json'}
        data = {
            "command": "Command",
            "timestamp": "text",
            "element": "test_host",
            "parameters": "abc;1"
        }
        response = session.post(self.ws_endpoint + '/command',
                                json=data,
                                headers=headers)
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(result, {
            '_status': 'ERR',
            '_error': 'Timestamp must be an integer value'
        })
        self.assertEqual(my_module.received_commands, 1)

        # Request to execute an external command with timestamp
        headers = {'Content-Type': 'application/json'}
        data = {
            "command": "command_command",
            "timestamp": "1234",
            "element": "test_host;test_service",
            "parameters": "1;abc;2"
        }
        response = session.post(self.ws_endpoint + '/command',
                                json=data,
                                headers=headers)
        self.assertEqual(my_module.received_commands, 2)
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(result['_status'], 'OK')
        # Result is uppercase command, parameters are ordered
        self.assertEqual(
            result['_command'],
            '[1234] COMMAND_COMMAND;test_host;test_service;1;abc;2')

        # Request to execute an external command
        headers = {'Content-Type': 'application/json'}
        data = {
            "command": "command_command",
            "element": "test_host;test_service",
            "parameters": "1;abc;2"
        }
        response = session.post(self.ws_endpoint + '/command',
                                json=data,
                                headers=headers)
        self.assertEqual(my_module.received_commands, 3)
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(result['_status'], 'OK')
        # Result is uppercase command, parameters are ordered
        self.assertEqual(result['_command'],
                         'COMMAND_COMMAND;test_host;test_service;1;abc;2')

        # Request to execute an external command
        headers = {'Content-Type': 'application/json'}
        data = {
            "command": "command_command",
            "element":
            "test_host/test_service",  # Accept / as an host/service separator
            "parameters": "1;abc;2"
        }
        response = session.post(self.ws_endpoint + '/command',
                                json=data,
                                headers=headers)
        self.assertEqual(my_module.received_commands, 4)
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(result['_status'], 'OK')
        # Result is uppercase command, parameters are ordered
        self.assertEqual(result['_command'],
                         'COMMAND_COMMAND;test_host;test_service;1;abc;2')

        # Request to execute an external command (Alignak modern syntax)
        headers = {'Content-Type': 'application/json'}
        data = {
            "command": "command_command",
            "host": "test_host",
            "service": "test_service",
            "parameters": "1;abc;2"
        }
        response = session.post(self.ws_endpoint + '/command',
                                json=data,
                                headers=headers)
        self.assertEqual(my_module.received_commands, 5)
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(result['_status'], 'OK')
        # Result is uppercase command, parameters are ordered
        self.assertEqual(result['_command'],
                         'COMMAND_COMMAND;test_host;test_service;1;abc;2')

        # Request to execute an external command (Alignak modern syntax)
        headers = {'Content-Type': 'application/json'}
        data = {
            "command": "command_command",
            "host": "test_host",
            "service": "test_service",
            "user": "******",
            "parameters": "1;abc;2"
        }
        response = session.post(self.ws_endpoint + '/command',
                                json=data,
                                headers=headers)
        self.assertEqual(my_module.received_commands, 6)
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(result['_status'], 'OK')
        # Result is uppercase command, parameters are ordered
        self.assertEqual(
            result['_command'],
            'COMMAND_COMMAND;test_host;test_service;test_user;1;abc;2')

        self.modulemanager.stop_all()
    def setUpClass(cls):
        """This method:
          * deletes mongodb database
          * starts the backend with uwsgi
          * logs in the backend and gets the token

        :return: None
        """
        # Set test mode for alignak backend
        os.environ['TEST_ALIGNAK_BACKEND'] = '1'
        os.environ[
            'ALIGNAK_BACKEND_MONGO_DBNAME'] = 'alignak-module-backend-test'

        # Delete used mongo DBs
        print("Deleting Alignak backend DB...")
        exit_code = subprocess.call(
            shlex.split('mongo %s --eval "db.dropDatabase()"' %
                        os.environ['ALIGNAK_BACKEND_MONGO_DBNAME']))
        assert exit_code == 0

        cls.p = subprocess.Popen([
            'uwsgi', '--plugin', 'python', '-w', 'alignakbackend:app',
            '--socket', '0.0.0.0:5000', '--protocol=http', '--enable-threads',
            '--pidfile', '/tmp/uwsgi.pid'
        ])
        time.sleep(3)

        cls.backend = Backend('http://127.0.0.1:5000')
        cls.backend.login("admin", "admin", "force")
        realms = cls.backend.get_all('realm')
        for cont in realms['_items']:
            cls.realm_all = cont['_id']

        # add commands
        data = json.loads(open('cfg/command_ping.json').read())
        data['name'] = 'other-ping'
        data['_realm'] = cls.realm_all
        data_cmd_ping = cls.backend.post("command", data)
        data = json.loads(open('cfg/command_http.json').read())
        data['_realm'] = cls.realm_all
        data_cmd_http = cls.backend.post("command", data)
        # add host
        data = json.loads(open('cfg/host_srv001.json').read())
        data['check_command'] = data_cmd_ping['_id']
        del data['realm']
        data['_realm'] = cls.realm_all
        cls.data_host = cls.backend.post("host", data)
        # add 2 services
        data = json.loads(open('cfg/service_srv001_ping.json').read())
        data['host'] = cls.data_host['_id']
        data['check_command'] = data_cmd_ping['_id']
        data['_realm'] = cls.realm_all
        cls.data_srv_ping = cls.backend.post("service", data)

        data = json.loads(open('cfg/service_srv001_http.json').read())
        data['host'] = cls.data_host['_id']
        data['check_command'] = data_cmd_http['_id']
        data['_realm'] = cls.realm_all
        cls.data_srv_http = cls.backend.post("service", data)

        # Get admin user
        users = cls.backend.get_all('user')
        cls.user_id = users['_items'][0]['_id']

        # Start scheduler module
        modconf = Module()
        modconf.module_alias = "backend_scheduler"
        modconf.username = "******"
        modconf.password = "******"
        modconf.api_url = 'http://127.0.0.1:5000'
        cls.schedmodule = AlignakBackendScheduler(modconf)

        class scheduler(object):
            """
            Fake scheduler class used to save and load retention
            """
            def __init__(self):
                self.data = None

            def get_retention_data(self):
                """
                Get host and service data for save in backend

                :return: properties of host and services
                :rtype: dict
                """
                all_data = {'hosts': {}, 'services': {}}
                all_data['hosts']['srv001'] = {
                    'latency': 0,
                    'last_state_type': 'HARD',
                    'state': 'UP',
                    'last_chk': 0,
                }
                all_data['hosts']['srv002'] = {
                    'latency': 0,
                    'last_state_type': 'HARD',
                    'state': 'UP',
                    'last_chk': 0,
                }

                all_data['services'][('srv001', 'check_http')] = {
                    'latency': 0,
                    'last_state_type': 'HARD',
                    'state': 'OK',
                    'last_chk': 0,
                }
                all_data['services'][('srv002', 'check_https')] = {
                    'latency': 0,
                    'last_state_type': 'HARD',
                    'state': 'WARNING',
                    'last_chk': 0,
                }
                return all_data

            def restore_retention_data(self, data):
                """
                Load hoas and servcie from backend retention

                :param data: dict of hosts and service from retention backend
                :type data: dict
                """
                self.data = data

        cls.sched = scheduler()
Exemple #23
0
    def test_module_zzz_event(self):
        """Test the module /event endpoint
        :return:
        """
        # Create an Alignak module
        mod = Module({
            'module_alias': 'web-services',
            'module_types': 'web-services',
            'python_name': 'alignak_module_ws',
            # Alignak backend
            'alignak_backend': 'http://127.0.0.1:5000',
            'username': '******',
            'password': '******',
            # Set Arbiter address as empty to not poll the Arbiter else the test will fail!
            'alignak_host': '',
            'alignak_port': 7770,
            'authorization': '1',
        })

        # Create a receiver daemon
        args = {'env_file': '', 'daemon_name': 'receiver-master'}
        self._receiver_daemon = Receiver(**args)

        # Create the modules manager for the daemon
        self.modulemanager = ModulesManager(self._receiver_daemon)

        # Load an initialize the modules:
        #  - load python module
        #  - get module properties and instances
        self.modulemanager.load_and_init([mod])

        my_module = self.modulemanager.instances[0]

        # Clear logs
        self.clear_logs()

        # Start external modules
        self.modulemanager.start_external_instances()

        # Starting external module logs
        self.assert_log_match("Trying to initialize module: web-services", 0)
        self.assert_log_match("Starting external module web-services", 1)
        self.assert_log_match("Starting external process for module web-services", 2)
        self.assert_log_match("web-services is now started", 3)

        # Check alive
        self.assertIsNotNone(my_module.process)
        self.assertTrue(my_module.process.is_alive())

        time.sleep(1)

        # ---
        # Prepare the backend content...
        self.endpoint = 'http://127.0.0.1:5000'

        headers = {'Content-Type': 'application/json'}
        params = {'username': '******', 'password': '******'}
        # get token
        response = requests.post(self.endpoint + '/login', json=params, headers=headers)
        resp = response.json()
        self.token = resp['token']
        self.auth = requests.auth.HTTPBasicAuth(self.token, '')

        # Get default realm
        response = requests.get(self.endpoint + '/realm', auth=self.auth)
        resp = response.json()
        self.realm_all = resp['_items'][0]['_id']
        # ---

        # Do not allow GET request on /event - not yet authorized
        response = requests.get(self.ws_endpoint + '/event')
        self.assertEqual(response.status_code, 401)

        session = requests.Session()

        # Login with username/password (real backend login)
        headers = {'Content-Type': 'application/json'}
        params = {'username': '******', 'password': '******'}
        response = session.post(self.ws_endpoint + '/login', json=params, headers=headers)
        assert response.status_code == 200
        resp = response.json()

        # Do not allow GET request on /event
        response = session.get(self.ws_endpoint + '/event')
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(result['_status'], 'ERR')
        self.assertEqual(result['_issues'], ['You must only POST on this endpoint.'])

        self.assertEqual(my_module.received_commands, 0)

        # You must have parameters when POSTing on /event
        headers = {'Content-Type': 'application/json'}
        data = {}
        response = session.post(self.ws_endpoint + '/event', json=data, headers=headers)
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(result['_status'], 'ERR')
        self.assertEqual(result['_issues'], ['You must POST parameters on this endpoint.'])

        self.assertEqual(my_module.received_commands, 0)

        # Notify an host event - missing host or service
        headers = {'Content-Type': 'application/json'}
        data = {
            "fake": ""
        }
        self.assertEqual(my_module.received_commands, 0)
        response = session.post(self.ws_endpoint + '/event', json=data, headers=headers)
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(result, {'_status': 'ERR', '_issues': ['Missing host and/or service parameter.']})

        # Notify an host event - missing comment
        headers = {'Content-Type': 'application/json'}
        data = {
            "host": "test_host",
        }
        response = session.post(self.ws_endpoint + '/event', json=data, headers=headers)
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(result, {'_status': 'ERR',
                                  '_issues': ['Missing comment. If you do not have any comment, '
                                              'do not comment ;)']})

        # Notify an host event - default author
        headers = {'Content-Type': 'application/json'}
        data = {
            "host": "test_host",
            "comment": "My comment"
        }
        response = session.post(self.ws_endpoint + '/event', json=data, headers=headers)
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(result, {'_status': 'OK',
                                  '_result': ['ADD_HOST_COMMENT;test_host;1;'
                                              'Alignak WS;My comment']})

        # Notify an host event - default author and timestamp
        headers = {'Content-Type': 'application/json'}
        data = {
            "timestamp": 1234567890,
            "host": "test_host",
            "author": "Me",
            "comment": "My comment"
        }
        response = session.post(self.ws_endpoint + '/event', json=data, headers=headers)
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(result, {'_status': 'OK',
                                  '_result': ['[1234567890] ADD_HOST_COMMENT;test_host;1;'
                                              'Me;My comment']})

        # Notify a service event - default author
        headers = {'Content-Type': 'application/json'}
        data = {
            "host": "test_host",
            "service": "test_service",
            "comment": "My comment"
        }
        response = session.post(self.ws_endpoint + '/event', json=data, headers=headers)
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(result, {'_status': 'OK',
                                  '_result': ['ADD_SVC_COMMENT;test_host;test_service;1;'
                                              'Alignak WS;My comment']})

        # Notify a service event - default author and timestamp
        headers = {'Content-Type': 'application/json'}
        data = {
            "timestamp": 1234567890,
            "host": "test_host",
            "service": "test_service",
            "author": "Me",
            "comment": "My comment"
        }
        response = session.post(self.ws_endpoint + '/event', json=data, headers=headers)
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(result, {'_status': 'OK',
                                  '_result': ['[1234567890] ADD_SVC_COMMENT;test_host;test_service;'
                                              '1;Me;My comment']})

        # Get history to confirm that backend is ready
        # ---
        response = session.get(self.endpoint + '/history', auth=self.auth,
                               params={"sort": "-_id", "max_results": 25, "page": 1})
        resp = response.json()
        print(("Response: %s" % resp))
        for item in resp['_items']:
            assert item['type'] in ['webui.comment']
        # Got 4 notified events, so we get 4 comments in the backend
        self.assertEqual(len(resp['_items']), 4)
        # ---

        # Logout
        response = session.get(self.ws_endpoint + '/logout')
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(result['_status'], 'OK')
        self.assertEqual(result['_result'], 'Logged out')

        self.modulemanager.stop_all()
    def _get_history(self, username, password):

        # Obliged to call to get a self.logger...
        self.setup_with_file('cfg/cfg_default.cfg')
        self.assertTrue(self.conf_is_correct)

        # -----
        # Provide parameters - logger configuration file (exists)
        # -----
        # Clear logs
        self.clear_logs()

        # Create an Alignak module
        mod = Module({
            'module_alias': 'web-services',
            'module_types': 'web-services',
            'python_name': 'alignak_module_ws',
            # Set Arbiter address as empty to not poll the Arbiter else the test will fail!
            'alignak_host': '',
            'alignak_port': 7770,
            # Alignak backend URL
            'alignak_backend': 'http://127.0.0.1:5000',
            'username': '******',
            'password': '******',
            # Activate CherryPy file logs
            'log_access': '/tmp/alignak-module-ws-access.log',
            'log_error': '/tmp/alignak-module-ws-error.log',
        })

        # Create the modules manager for a daemon type
        self.modulemanager = ModulesManager('receiver', None)

        # Load an initialize the modules:
        #  - load python module
        #  - get module properties and instances
        self.modulemanager.load_and_init([mod])

        my_module = self.modulemanager.instances[0]

        # Clear logs
        self.clear_logs()

        # Start external modules
        self.modulemanager.start_external_instances()

        # Starting external module logs
        self.assert_log_match("Trying to initialize module: web-services", 0)
        self.assert_log_match("Starting external module web-services", 1)
        self.assert_log_match("Starting external process for module web-services", 2)
        self.assert_log_match("web-services is now started", 3)

        time.sleep(1)

        # Check alive
        self.assertIsNotNone(my_module.process)
        self.assertTrue(my_module.process.is_alive())

        time.sleep(1)

        # ---
        # Prepare the backend content...
        self.endpoint = 'http://127.0.0.1:5000'

        headers = {'Content-Type': 'application/json'}
        params = {'username': '******', 'password': '******'}
        # get token
        response = requests.post(self.endpoint + '/login', json=params, headers=headers)
        resp = response.json()
        self.token = resp['token']
        self.auth = requests.auth.HTTPBasicAuth(self.token, '')

        # Get default realm
        response = requests.get(self.endpoint + '/realm', auth=self.auth)
        resp = response.json()
        self.realm_all = resp['_items'][0]['_id']
        # ---

        # -------------------------------------------
        # Add a check result for an host
        data = {
            "last_check": 1496332753,
            "host": self.rh[0]['_id'],
            "service": None,
            'acknowledged': False,
            'state_id': 0,
            'state': 'UP',
            'state_type': 'HARD',
            'last_state_id': 0,
            'last_state': 'UP',
            'last_state_type': 'HARD',
            'state_changed': False,
            'latency': 0,
            'execution_time': 0.12,
            'output': 'Check output',
            'long_output': 'Check long_output',
            'perf_data': 'perf_data',
            "_realm": self.realm_all
        }
        response = requests.post(
            self.endpoint + '/logcheckresult', json=data, headers=headers, auth=self.auth
        )
        resp = response.json()
        self.assertEqual(resp['_status'], 'OK')

        # -------------------------------------------
        # Add a check result for a service
        data = {
            "last_check": 1496332754,
            "host": self.rh[0]['_id'],
            "service": self.rs[0]['_id'],
            'acknowledged': False,
            'state_id': 0,
            'state': 'UP',
            'state_type': 'HARD',
            'last_state_id': 0,
            'last_state': 'UP',
            'last_state_type': 'HARD',
            'state_changed': False,
            'latency': 0,
            'execution_time': 0.12,
            'output': 'Check output',
            'long_output': 'Check long_output',
            'perf_data': 'perf_data',
            "_realm": self.realm_all
        }
        response = requests.post(
            self.endpoint + '/logcheckresult', json=data, headers=headers, auth=self.auth
        )
        resp = response.json()
        self.assertEqual(resp['_status'], 'OK')
        # Add an history event
        data = {
            "host_name": "chazay",
            "service_name": "Processus",
            "user_name": "Alignak",
            "type": "check.result",
            "message": "OK[HARD] (False,False): All is ok",
            "_realm": self.realm_all,
            "_sub_realm": True
        }
        time.sleep(1)
        requests.post(self.endpoint + '/history', json=data, headers=headers, auth=self.auth)

        # Add an history event
        time.sleep(1)
        data = {
            "host_name": "denice",
            "service_name": "Zombies",
            "user_name": "Alignak",
            "type": "check.result",
            "message": "OK[HARD] (False,False): All is ok",
            "_realm": self.realm_all,
            "_sub_realm": True
        }
        requests.post(self.endpoint + '/history', json=data, headers=headers, auth=self.auth)

        # Add an history event
        time.sleep(1)
        data = {
            "host_name": "denice",
            "user_name": "Me",
            "type": "monitoring.alert",
            "message": "HOST ALERT ....",
            "_realm": self.realm_all,
            "_sub_realm": True
        }
        requests.post(self.endpoint + '/history', json=data, headers=headers, auth=self.auth)
        # ---

        # ---
        # Get history to confirm that backend is ready
        # ---
        response = requests.get(self.endpoint + '/history', auth=self.auth,
                                params={"sort": "-_id", "max_results": 25, "page": 1, 'embedded': json.dumps({"logcheckresult": 1})})
        resp = response.json()
        pprint(resp['_items'])
        self.assertEqual(len(resp['_items']), 5)

        # Backend real history
        # The commented fields are the one existing in the backend but filtered by the WS
        backend_real_history = [
            {
                u'_created': u'Thu, 01 Jun 2017 15:59:16 GMT',
                # u'_etag': u'9f07c7285b37bb3d336a96ede3d3fd2a774c4c4c',
                u'_id': u'593039d406fd4b3bf0e27d9f',
                # u'_links': {u'self': {u'href': u'history/593039d406fd4b3bf0e27d9f',
                #                       u'title': u'History'}},
                # u'_realm': u'593039cc06fd4b3bf0e27d88',
                # u'_sub_realm': True,
                # u'_updated': u'Thu, 01 Jun 2017 15:59:16 GMT',
                u'host_name': u'denice',
                u'message': u'HOST ALERT ....',
                u'type': u'monitoring.alert',
                u'user_name': u'Me'
            },
            {
                u'_created': u'Thu, 01 Jun 2017 15:59:15 GMT',
                # u'_etag': u'24cd486a1a28859a0177fbe15d1ead61f78f7b2c',
                u'_id': u'593039d306fd4b3bf0e27d9e',
                # u'_links': {u'self': {u'href': u'history/593039d306fd4b3bf0e27d9e',
                #                       u'title': u'History'}},
                # u'_realm': u'593039cc06fd4b3bf0e27d88',
                # u'_sub_realm': True,
                # u'_updated': u'Thu, 01 Jun 2017 15:59:15 GMT',
                u'host_name': u'denice',
                u'message': u'OK[HARD] (False,False): All is ok',
                u'service_name': u'Zombies',
                u'type': u'check.result',
                u'user_name': u'Alignak'
            },
            {
                u'_created': u'Thu, 01 Jun 2017 15:59:14 GMT',
                # u'_etag': u'4c4ee43a4fac0b91dcfddb011619007dedb1cd95',
                u'_id': u'593039d206fd4b3bf0e27d9d',
                # u'_links': {u'self': {u'href': u'history/593039d206fd4b3bf0e27d9d',
                #                       u'title': u'History'}},
                # u'_realm': u'593039cc06fd4b3bf0e27d88',
                # u'_sub_realm': True,
                # u'_updated': u'Thu, 01 Jun 2017 15:59:14 GMT',
                u'host_name': u'chazay',
                u'message': u'OK[HARD] (False,False): All is ok',
                u'service_name': u'Processus',
                u'type': u'check.result',
                u'user_name': u'Alignak'
            },
            {u'_created': u'Thu, 01 Jun 2017 15:59:13 GMT',
             # u'_etag': u'76dd35f575244848dd41f67ad3109cf6f1f9a33c',
             u'_id': u'593039d106fd4b3bf0e27d9c',
             # u'_links': {u'self': {u'href': u'history/593039d106fd4b3bf0e27d9c',
             #                       u'title': u'History'}},
             # u'_realm': u'593039cc06fd4b3bf0e27d88',
             # u'_sub_realm': True,
             # u'_updated': u'Thu, 01 Jun 2017 15:59:13 GMT',
             # u'host': u'593039cc06fd4b3bf0e27d90',
             u'host_name': u'srv001',
             u'logcheckresult': {
                 u'_created': u'Thu, 01 Jun 2017 15:59:13 GMT',
                 # u'_etag': u'10a3935b1158fe4c8f62962a14b1050fef32df4b',
                 # u'_id': u'593039d106fd4b3bf0e27d9b',
                 # u'_realm': u'593039cc06fd4b3bf0e27d88',
                 # u'_sub_realm': True,
                 # u'_updated': u'Thu, 01 Jun 2017 15:59:13 GMT',
                 u'acknowledged': False,
                 u'acknowledgement_type': 1,
                 u'downtimed': False,
                 u'execution_time': 0.12,
                 # u'host': u'593039cc06fd4b3bf0e27d90',
                 # u'host_name': u'srv001',
                 u'last_check': 1496332753,
                 u'last_state': u'UP',
                 u'last_state_changed': 0,
                 u'last_state_id': 0,
                 u'last_state_type': u'HARD',
                 u'latency': 0.0,
                 u'long_output': u'Check long_output',
                 u'output': u'Check output',
                 u'passive_check': False,
                 u'perf_data': u'perf_data',
                 # u'service': u'593039cf06fd4b3bf0e27d98',
                 # u'service_name': u'ping',
                 u'state': u'UP',
                 u'state_changed': False,
                 u'state_id': 0,
                 u'state_type': u'HARD'
             },
             u'message': u'UP[HARD] (False/False): Check output',
             # u'service': u'593039cf06fd4b3bf0e27d98',
             u'service_name': u'ping',
             u'type': u'check.result',
             # u'user': None,
             u'user_name': u'Alignak'
             },
            {u'_created': u'Thu, 01 Jun 2017 15:59:13 GMT',
             # u'_etag': u'c3cd29587ad328325dc48af677b3a36157361a84',
             u'_id': u'593039d106fd4b3bf0e27d9a',
             # u'_links': {u'self': {u'href': u'history/593039d106fd4b3bf0e27d9a',
             #                       u'title': u'History'}},
             # u'_realm': u'593039cc06fd4b3bf0e27d88',
             # u'_sub_realm': True,
             # u'_updated': u'Thu, 01 Jun 2017 15:59:13 GMT',
             # u'host': u'593039cc06fd4b3bf0e27d90',
             u'host_name': u'srv001',
             u'logcheckresult': {
                 u'_created': u'Thu, 01 Jun 2017 15:59:13 GMT',
                 # u'_etag': u'0ea4c16f1e651a02772aa2bfa83070b47e7f6531',
                 # u'_id': u'593039d106fd4b3bf0e27d99',
                 # u'_realm': u'593039cc06fd4b3bf0e27d88',
                 # u'_sub_realm': True,
                 # u'_updated': u'Thu, 01 Jun 2017 15:59:13 GMT',
                 u'acknowledged': False,
                 u'acknowledgement_type': 1,
                 u'downtimed': False,
                 u'execution_time': 0.12,
                 # u'host': u'593039cc06fd4b3bf0e27d90',
                 # u'host_name': u'srv001',
                 u'last_check': 1496332754,
                 u'last_state': u'UP',
                 u'last_state_changed': 0,
                 u'last_state_id': 0,
                 u'last_state_type': u'HARD',
                 u'latency': 0.0,
                 u'long_output': u'Check long_output',
                 u'output': u'Check output',
                 u'passive_check': False,
                 u'perf_data': u'perf_data',
                 # u'service': None,
                 # u'service_name': u'',
                 u'state': u'UP',
                 u'state_changed': False,
                 u'state_id': 0,
                 u'state_type': u'HARD'
             },
             u'message': u'UP[HARD] (False/False): Check output',
             # u'service': None,
             u'service_name': u'',
             u'type': u'check.result',
             # u'user': None,
             u'user_name': u'Alignak'
             }
        ]
        # ---

        # ---
        # # Directly call the module function
        # search = {
        #     'page': 1,
        #     'max_results': 25
        # }
        # result = my_module.getBackendHistory(search)
        # print(result)
        # print("Page: %d, got: %d items" % (search["page"], len(result['items'])))
        # for item in result['items']:
        #     print(item)
        # assert len(result['items']) == 5

        # ---
        # Do not allow GET request on /alignak_logs - not yet authorized!
        response = requests.get('http://127.0.0.1:8888/alignak_logs')
        self.assertEqual(response.status_code, 401)

        session = requests.Session()

        # Login with username/password (real backend login)
        headers = {'Content-Type': 'application/json'}
        params = {'username': username, 'password': password}
        response = session.post('http://127.0.0.1:8888/login', json=params, headers=headers)
        assert response.status_code == 200
        resp = response.json()

        # ---
        # Get the alignak default history
        response = session.get('http://127.0.0.1:8888/alignak_logs')
        self.assertEqual(response.status_code, 200)
        result = response.json()
        # Remove fields that will obviously be different!
        for item in result['items']:
            del(item['_id'])
            del(item['_created'])
            # if 'logcheckresult' in item:
            #     del (item['logcheckresult']['_created'])
        for item in backend_real_history:
            del(item['_id'])
            del(item['_created'])
            if 'logcheckresult' in item:
                del (item['logcheckresult']['_created'])
        self.assertEqual(len(result['items']), 5)
        # Too complex comparison!!!
        # self.assertEqual(backend_real_history, result['items'])
        # assert cmp(backend_real_history, result['items']) == 0
        # ---

        # ---
        # Get the alignak default history, filter to get only check.result
        response = session.get('http://127.0.0.1:8888/alignak_logs?search=type:check.result')
        self.assertEqual(response.status_code, 200)
        result = response.json()
        for item in result['items']:
            print(item)
        self.assertEqual(len(result['items']), 4)
        # ---

        # ---
        # Get the alignak default history, filter to get only for a user
        response = session.get('http://127.0.0.1:8888/alignak_logs?search=user_name:Alignak')
        self.assertEqual(response.status_code, 200)
        result = response.json()
        for item in result['items']:
            print(item)
        self.assertEqual(len(result['items']), 4)
        response = session.get('http://127.0.0.1:8888/alignak_logs?search=user_name:Me')
        self.assertEqual(response.status_code, 200)
        result = response.json()
        for item in result['items']:
            print(item)
        self.assertEqual(len(result['items']), 1)
        # ---

        # ---
        # Get the alignak default history, filter to get only for an host
        response = session.get('http://127.0.0.1:8888/alignak_logs?search=host_name:chazay')
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(len(result['items']), 1)
        # Implicit host_name
        response = session.get('http://127.0.0.1:8888/alignak_logs?search=chazay')
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(len(result['items']), 1)
        # Unknown search field
        response = session.get('http://127.0.0.1:8888/alignak_logs?search=name:chazay')
        self.assertEqual(response.status_code, 200)
        result = response.json()
        # All history items because name is not aknown search field! So we get all items...
        self.assertEqual(len(result['items']), 5)

        # Some other hosts...
        response = session.get('http://127.0.0.1:8888/alignak_logs?search=host_name:denice')
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(len(result['items']), 2)
        response = session.get('http://127.0.0.1:8888/alignak_logs?search=host_name:srv001')
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(len(result['items']), 2)

        # Several hosts...
        response = session.get('http://127.0.0.1:8888/alignak_logs?search=host_name:denice host_name:srv001')
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(len(result['items']), 4)   # 2 for each host

        # Not an host...
        # TODO: looks that ths criteria is not correctly implemented :(
        # response = session.get('http://127.0.0.1:8888/alignak_logs?search=host_name:!denice')
        # self.assertEqual(response.status_code, 200)
        # result = response.json()
        # self.assertEqual(len(result['items']), 3)
        # ---

        # ---
        # Get the alignak default history, NOT for an host
        # todo: temporarily skipped
        # response = requests.get('http://127.0.0.1:8888/alignak_logs?search=host_name:!Chazay')
        # self.assertEqual(response.status_code, 200)
        # result = response.json()
        # for item in result['items']:
        #     print(item)
        # self.assertEqual(len(result['items']), 2)
        # ---

        # ---
        # Get the alignak default history, only for a service
        response = session.get('http://127.0.0.1:8888/alignak_logs?search=service_name:Processus')
        self.assertEqual(response.status_code, 200)
        result = response.json()
        for item in result['items']:
            print(item)
        self.assertEqual(len(result['items']), 1)
        # ---

        # ---
        # Get the alignak default history, for an host and a service
        # todo multi search query to be improved!
        # response = session.get('http://127.0.0.1:8888/alignak_logs?search="host_name:chazay service_name=Processus"')
        # self.assertEqual(response.status_code, 200)
        # result = response.json()
        # for item in result['items']:
        #     print(item)
        # self.assertEqual(len(result['items']), 3)
        # ---

        # ---
        # Get the alignak default history, unknown event type
        response = session.get('http://127.0.0.1:8888/alignak_logs?search=type:XXX')
        self.assertEqual(response.status_code, 200)
        result = response.json()
        for item in result['items']:
            print(item)
        self.assertEqual(len(result['items']), 0)
        # ---

        # ---
        # Get the alignak default history, page count
        response = session.get('http://127.0.0.1:8888/alignak_logs?start=0&count=1')
        self.assertEqual(response.status_code, 200)
        result = response.json()
        for item in result['items']:
            print(item)
        self.assertEqual(len(result['items']), 1)
        response = session.get('http://127.0.0.1:8888/alignak_logs?start=1&count=1')
        self.assertEqual(response.status_code, 200)
        result = response.json()
        for item in result['items']:
            print(item)
        self.assertEqual(len(result['items']), 1)
        response = session.get('http://127.0.0.1:8888/alignak_logs?start=2&count=1')
        self.assertEqual(response.status_code, 200)
        result = response.json()
        for item in result['items']:
            print(item)
        self.assertEqual(len(result['items']), 1)
        response = session.get('http://127.0.0.1:8888/alignak_logs?start=3&count=1')
        self.assertEqual(response.status_code, 200)
        result = response.json()
        for item in result['items']:
            print(item)
        self.assertEqual(len(result['items']), 1)
        response = session.get('http://127.0.0.1:8888/alignak_logs?start=4&count=1')
        self.assertEqual(response.status_code, 200)
        result = response.json()
        for item in result['items']:
            print(item)
        self.assertEqual(len(result['items']), 1)
        # Over the limits !
        response = session.get('http://127.0.0.1:8888/alignak_logs?start=5&count=1')
        self.assertEqual(response.status_code, 200)
        result = response.json()
        for item in result['items']:
            print(item)
        self.assertEqual(len(result['items']), 0)
        response = session.get('http://127.0.0.1:8888/alignak_logs?start=50&count=50')
        self.assertEqual(response.status_code, 200)
        result = response.json()
        for item in result['items']:
            print(item)
        self.assertEqual(len(result['items']), 0)
        # ---

        # ---
        # Get the alignak history, page count greater than the number of items
        response = session.get('http://127.0.0.1:8888/alignak_logs?start=1&count=25')
        self.assertEqual(response.status_code, 200)
        result = response.json()
        pprint(result)
        self.assertEqual(len(result['items']), 5)   # Got 5 items
        self.assertEqual(result['_meta']['max_results'], 25)
        self.assertEqual(result['_meta']['page'], 1)
        self.assertEqual(result['_meta']['total'], 5)

        response = session.get('http://127.0.0.1:8888/alignak_logs?start=0&count=50')
        self.assertEqual(response.status_code, 200)
        result = response.json()
        pprint(result)
        self.assertEqual(len(result['items']), 5)   # Got 5 items
        self.assertEqual(result['_meta']['max_results'], 50)
        self.assertEqual(result['_meta']['page'], 1)
        self.assertEqual(result['_meta']['total'], 5)

        # ---

        # Logout
        response = session.get('http://127.0.0.1:8888/logout')
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertEqual(result['_status'], 'OK')
        self.assertEqual(result['_result'], 'Logged out')
Exemple #25
0
    def test_modulemanager_several_modules(self):
        """ Module manager manages its modules

        Test if the module manager manages correctly all the modules

        Configured with several modules
        :return:
        """
        self.setup_with_file('cfg/cfg_default_with_modules.cfg',
                             'cfg/default_with_modules/alignak.ini')
        assert self.conf_is_correct

        # for mod in self._arbiter.conf.modules:
        #     print (mod.__dict__)

        # Create an Alignak module
        mod = Module({
            'module_alias': 'mod-example',
            'module_types': 'example',
            'python_name': 'alignak_module_example',
            'option1': 'foo',
            'option2': 'bar',
            'option3': 1
        })
        mod2 = Module({
            'module_alias': 'mod-example-2',
            'module_types': 'example',
            'python_name': 'alignak_module_example',
            'option1': 'faa',
            'option2': 'bor',
            'option3': 1
        })

        # Force the daemon SyncManager to None for unit tests!
        self._broker_daemon.sync_manager = None
        # Create the modules manager for a daemon type
        self.modules_manager = ModulesManager(self._broker_daemon)
        print("Modules: %s" % self._broker_daemon.modules)

        # Load an initialize the modules:
        #  - load python module
        #  - get module properties and instances
        assert self.modules_manager.load_and_init([mod, mod2])
        print("I correctly loaded my modules: [%s]" %
              ','.join([inst.name for inst in self.modules_manager.instances]))
        self.show_logs()

        self.assert_any_log_match(
            re.escape(
                "Importing Python module 'alignak_module_example' for mod-example..."
            ))
        self.assert_any_log_match(
            re.escape("Imported 'alignak_module_example' for mod-example"))
        self.assert_any_log_match(
            re.escape(
                "Loaded Python module 'alignak_module_example' (mod-example)"))
        self.assert_any_log_match(
            re.escape(
                "Importing Python module 'alignak_module_example' for mod-example-2..."
            ))
        self.assert_any_log_match(
            re.escape("Imported 'alignak_module_example' for mod-example-2"))
        self.assert_any_log_match(
            re.escape(
                "Loaded Python module 'alignak_module_example' (mod-example-2)"
            ))
        self.assert_any_log_match(
            re.escape(
                "Give an instance of alignak_module_example for alias: mod-example"
            ))
        self.assert_any_log_match(re.escape("configuration, foo, bar, 1"))
        self.assert_any_log_match(
            re.escape(
                "Give an instance of alignak_module_example for alias: mod-example-2"
            ))
        self.assert_any_log_match(re.escape("configuration, faa, bor, 1"))
        # Loading module logs
        self.assert_any_log_match(
            re.escape(
                "Importing Python module 'alignak_module_example' for mod-example..."
            ))

        my_module = self.modules_manager.instances[0]
        my_module2 = self.modules_manager.instances[1]
        assert my_module.is_external
        assert my_module2.is_external

        # Get list of not external modules
        assert [] == self.modules_manager.get_internal_instances()
        for phase in [
                'configuration', 'late_configuration', 'running', 'retention'
        ]:
            assert [] == self.modules_manager.get_internal_instances(phase)

        # Get list of external modules
        assert [my_module,
                my_module2] == self.modules_manager.get_external_instances()
        for phase in [
                'configuration', 'late_configuration', 'running', 'retention'
        ]:
            assert [my_module, my_module2
                    ] == self.modules_manager.get_external_instances(phase)

        # Start external modules
        self.modules_manager.start_external_instances()
        self.modules_manager.start_external_instances()

        # Starting external module logs
        self.assert_any_log_match(
            re.escape("Starting external module mod-example"))
        self.assert_any_log_match(
            re.escape("Starting external process for module mod-example"))
        self.assert_any_log_match(
            re.escape("mod-example is now started (pid="))

        # Check alive
        assert my_module.process is not None
        assert my_module.process.is_alive()

        assert my_module2.process is not None
        assert my_module2.process.is_alive()

        # Kill the external module (normal stop is .stop_process)
        self.clear_logs()
        print("Killing a module")
        my_module.kill()
        time.sleep(0.1)
        self.show_logs()
        # Stopping module logs
        self.assert_any_log_match(re.escape("Killing external module "))
        self.assert_any_log_match(re.escape("External module killed"))
        # Should be dead (not normally stopped...) but we still know a process for this module!
        assert my_module.process is not None

        self.clear_logs()
        print("Killing another module")
        my_module2.kill()
        time.sleep(0.1)
        self.show_logs()
        # Stopping module logs
        self.assert_any_log_match(re.escape("Killing external module "))
        self.assert_any_log_match(re.escape("External module killed"))
        # Should be dead (not normally stopped...) but we still know a process for this module!
        assert my_module.process is not None

        # Nothing special ...
        self.clear_logs()
        self.modules_manager.check_alive_instances()

        # Try to restart the dead modules
        print("Trying to restart dead modules")
        # We lie on the last restart try time
        my_module.last_init_try = time.time()
        my_module2.last_init_try = time.time()
        self.modules_manager.try_to_restart_deads()
        self.show_logs()
        # In fact it's too early, so it won't do it

        # Here the module instances should still be dead
        assert not my_module.process.is_alive()
        assert not my_module2.process.is_alive()

        # We lie on the last restart try time
        my_module.last_init_try = 0
        my_module2.last_init_try = 0
        self.modules_manager.check_alive_instances()
        self.modules_manager.try_to_restart_deads()

        # Here the module instances should be alive again
        assert my_module.process.is_alive()
        assert my_module2.process.is_alive()

        # Kill the module again
        self.clear_logs()
        my_module.kill()
        self.show_logs()
        time.sleep(0.2)
        assert not my_module.process.is_alive()

        # And we clear all now
        self.modules_manager.stop_all()
        # Stopping module logs
        self.assert_any_log_match(re.escape("I'm stopping module "))
    def _get_from_backend(self, backend_url='http://127.0.01:5000',
                          host_name='test', file_name='history.json'):
        """Get the all history for an host in an existing backend.
        Dumps the resulting information as a JSON array to a file

        :return:
        """
        self.print_header()
        # Obliged to call to get a self.logger...
        self.setup_with_file('cfg/cfg_default.cfg')
        self.assertTrue(self.conf_is_correct)

        # -----
        # Provide parameters - logger configuration file (exists)
        # -----
        # Clear logs
        self.clear_logs()

        # Create an Alignak module
        mod = Module({
            'module_alias': 'web-services',
            'module_types': 'web-services',
            'python_name': 'alignak_module_ws',
            # Set Arbiter address as empty to not poll the Arbiter else the test will fail!
            'alignak_host': '',
            'alignak_port': 7770,
            # Alignak backend URL
            'alignak_backend': backend_url,
            'username': '******',
            'password': '******',
        })

        # Create the modules manager for a daemon type
        self.modulemanager = ModulesManager('receiver', None)

        # Load an initialize the modules:
        #  - load python module
        #  - get module properties and instances
        self.modulemanager.load_and_init([mod])

        my_module = self.modulemanager.instances[0]

        # Clear logs
        self.clear_logs()

        # Start external modules
        self.modulemanager.start_external_instances()

        # Starting external module logs
        self.assert_log_match("Trying to initialize module: web-services", 0)
        self.assert_log_match("Starting external module web-services", 1)
        self.assert_log_match("Starting external process for module web-services", 2)
        self.assert_log_match("web-services is now started", 3)

        time.sleep(2)

        # Check alive
        self.assertIsNotNone(my_module.process)
        self.assertTrue(my_module.process.is_alive())

        time.sleep(1)

        # ---

        # Start with first page ... max_results=3
        page = 1
        count = 25
        items = []
        search = {
            'page': page,
            'max_results': count
        }
        where = Helper.decode_search(host_name)
        if where:
            search.update({'where': json.dumps(where)})

        while count > 0:
            result = my_module.getBackendHistory(search)
            count = len(result['items'])
            print("Page: %d, got: %d items" % (search["page"], count))
            for item in result['items']:
                sys.stdout.write('.')
                # print(item)
            items.extend(result['items'])
            search["page"] += 1
        print("Got: %d items" % len(items))

        self.folder = '/tmp'
        self.file_dump(items, file_name)