Ejemplo n.º 1
0
    def test_reversed_list(self):
        """ Test to ensure new conf is properly merge with different servicegroup definition
        The first conf has all its servicegroup defined servicegroups.cfg and services.cfg
        The second conf has both, so that servicegroups defined ins services.cfg are genretaed by Alignak
        This lead to another generated id witch should be handled properly when regenerating reversed list / merging
        servicegroups definition
        """

        sg = self.sched.servicegroups.find_by_name('servicegroup_01')
        prev_id = sg._id

        reg = Regenerator()
        data = {"instance_id": 0}
        b = Brok('program_status', data)
        b.prepare()
        reg.manage_program_status_brok(b)
        reg.all_done_linking(0)


        self.setup_with_file(["etc/alignak_reversed_list.cfg"])

        reg.all_done_linking(0)

        #for service in self.sched.servicegroups:
        #    assert(service.servicegroup_name in self.sched.servicegroups.reversed_list.keys())
        #    assert(service._id == self.sched.servicegroups.reversed_list[service.servicegroup_name])

        sg = self.sched.servicegroups.find_by_name('servicegroup_01')
        assert(prev_id != sg._id)

        for sname in [u'servicegroup_01', u'ok', u'flap', u'unknown', u'random',
                      u'servicegroup_02', u'servicegroup_03', u'warning', u'critical',
                      u'servicegroup_04', u'servicegroup_05', u'pending', u'mynewgroup']:
            sg = self.sched.servicegroups.find_by_name(sname)
            assert(sname is not None)
    def check_host_brok(self, prop, value):
        # Get an host and its full status brok
        self.objects = self.arbmodule.get_objects()
        my_host = None
        for host in self.objects['hosts']:
            if host['host_name'] == 'srv001':
                my_host = Host(host)
        # Get host status brok
        data = {'uuid': '0123456789'}
        my_host.fill_data_brok_from(data, 'full_status')

        # Update brok data
        if prop not in data:
            print(("Host property '%s' does not exit!" % prop))
            return False
        data[prop] = value
        brok = Brok({'type': 'update_host_status', 'data': data})
        brok.prepare()
        print(("Brok: %s" % brok))

        # Send host status brok
        self.brokmodule.manage_brok(brok)

        # Get host data like an arbiter to check modification
        self.objects = self.arbmodule.get_objects()
        my_changed_host = None
        for host in self.objects['hosts']:
            if host['host_name'] == 'srv001':
                my_changed_host = Host(host)
        # Get host status brok
        data = {'uuid': '0123456789'}
        my_changed_host.fill_data_brok_from(data, 'full_status')
        brok_bis = Brok({'type': 'update_host_status', 'data': data})
        brok_bis.prepare()
        print(("Brok: %s" % brok_bis))

        # Broks data are equal, nothing changed
        self.assertEqual(brok.data, brok_bis.data)

        return True
    def check_service_brok(self, prop, value):
        # Get an service and its full status brok
        self.objects = self.arbmodule.get_objects()
        my_service = None
        for service in self.objects['services']:
            if service['host_name'] == 'srv001' and service['service_description'] == 'ping':
                my_service = Service(service)
        # Get service status brok
        data = {'uuid': '0123456789'}
        my_service.fill_data_brok_from(data, 'full_status')

        # Update brok data
        if prop not in data:
            print(("service property '%s' does not exit!" % prop))
            return False
        data[prop] = value
        brok = Brok({'type': 'update_service_status', 'data': data})
        brok.prepare()
        print(("Before: %s" % brok))

        # Send service status brok
        self.brokmodule.manage_brok(brok)

        # Get service data like an arbiter to check modification
        self.objects = self.arbmodule.get_objects()
        my_changed_service = None
        for service in self.objects['services']:
            if service['host_name'] == 'srv001' and service['service_description'] == 'ping':
                my_changed_service = Service(service)
        # Get service status brok
        data = {'uuid': '0123456789'}
        my_changed_service.fill_data_brok_from(data, 'full_status')
        brok_bis = Brok({'type': 'update_service_status', 'data': data})
        brok_bis.prepare()
        print(("After: %s" % brok_bis))

        # Broks data are equal, nothing changed
        self.assertEqual(brok.data, brok_bis.data)

        return True
    def test_manage_brok_service_check_result(self):
        modconf = Module()
        modconf.module_name = "alignakbackend"
        module = AlignakBackendBrok(modconf)
        data = {
            'state': 'OK',
            'state_type': 'HARD',
            'last_chk': 1440976938,
            'output': 'TCP OK - 0.033 second response time on 93.93.47.83 port 22',
            'long_output': '',
            'perf_data': 'time=0.032536s;;;0.000000;3.000000',
            'problem_has_been_acknowledged': False,
            'service_description': 'check disk',
            'host_name': 'server1'
        }
        brok = Brok(_type='service_check_result', data=data)
        brok.prepare()
        with HTTMock(server_responses):
            module.manage_brok(brok)

        reference = {
            '55d4f7b26376e946db235fc4': {
                '_id': '55d4f8746376e946db235fc8',
                '_etag': 'fff582e398e47bce29e7317f25eb5068aaac3c4b'
            },
            '55d4f7be6376e946db235fc5': {
                '_id': '55d4f8876376e946db235fc9',
                '_etag': '3ed23c329f07c92fabeee465e5c4e59bc5f575f0'
            },
            '55d4f7cc6376e946db235fc6': {
                '_id': '55d4faa26376e946db235fca',
                '_etag': 'b7c4a2563f7382ed86dac2dc975759e20be778fc'
            },
            '55d4f7d76376e946db235fc7': {
                '_id': '55d4fabd6376e946db235fcb',
                '_etag': '9ab70e496605b755836be976d676be17a4bc6fea'
            }
        }
        self.assertEqual(reference, module.ref_live['service'])
    def check_user_brok(self, prop, value):
        # Get a contact and its full status brok
        self.objects = self.arbmodule.get_objects()
        my_user = None
        for user in self.objects['contacts']:
            if user['contact_name'] == 'jeronimo':
                my_user = Contact(user)
        # Get user status brok
        data = {'uuid': '0123456789'}
        my_user.fill_data_brok_from(data, 'full_status')

        # Update brok data
        if prop not in data:
            return False
        data[prop] = value
        brok = Brok({'type': 'update_contact_status', 'data': data})
        brok.prepare()
        print(("Brok: %s" % brok))

        # Send user status brok
        self.brokmodule.manage_brok(brok)

        # Get user data like an arbiter to check modification
        self.objects = self.arbmodule.get_objects()
        my_changed_user = None
        for user in self.objects['contacts']:
            if user['contact_name'] == 'jeronimo':
                my_changed_user = Contact(user)
        # Get user status brok
        data = {'uuid': '0123456789'}
        my_changed_user.fill_data_brok_from(data, 'full_status')
        brok_bis = Brok({'type': 'update_contact_status', 'data': data})
        brok_bis.prepare()
        print(("Brok: %s" % brok_bis))

        # Broks data are equal, nothing changed
        self.assertEqual(brok.data, brok_bis.data)
        return True
    def test_manage_brok_host_check_result(self):
        modconf = Module()
        modconf.module_name = "alignakbackend"
        module = AlignakBackendBrok(modconf)
        data = {
            'state': 'OK',
            'state_type': 'HARD',
            'last_chk': 1440976938,
            'output': 'TCP OK - 0.033 second response time on 93.93.47.83 port 22',
            'long_output': '',
            'perf_data': 'time=0.032536s;;;0.000000;3.000000',
            'problem_has_been_acknowledged': False,
            'host_name': 'server1'
        }
        brok = Brok(_type='host_check_result', data=data)
        brok.prepare()
        with HTTMock(server_responses):
            module.manage_brok(brok)

        reference = {
            '55d4a5246376e946db235fbc': {
                '_etag': 'fff582e398e47bce29e7317f25eb5068aaac3c4a',
                '_id': '55d4e5626376e946db235fc0'
            },
            '55d4a5276376e946db235fbd': {
                '_etag': '3524b87876c1d457bdca7492b9bfc503f3f13b1e',
                '_id': '55d4e57d6376e946db235fc1'
            },
            '55d4a52a6376e946db235fbe': {
                '_etag': 'a457e78fe0dc28c1b427d9b0696096fee73f4a29',
                '_id': '55d4e7d36376e946db235fc2'
            },
            '55d4a52d6376e946db235fbf': {
                '_etag': '2b45425c497d5593bad8ef5413c84e6a0d61cc41',
                '_id': '55d4e8126376e946db235fc3'
            }
        }
        self.assertEqual(reference, module.ref_live['host'])
    def test_03_manage_brok_host(self):

        data = json.loads(open('cfg/brok_host_srv001_up.json').read())
        b = Brok({'data': data, 'type': 'host_check_result'}, False)
        b.prepare()
        self.brokmodule.manage_brok(b)

        params = {'where': '{"name": "srv001"}'}
        r = self.backend.get('host', params)
        self.assertEqual(len(r['_items']), 1)
        number = 0
        for index, item in enumerate(r['_items']):
            self.assertEqual(item['ls_last_state'], 'UNREACHABLE')
            self.assertEqual(item['ls_state'], 'UP')
            self.assertEqual(item['ls_last_state_type'], 'HARD')
            self.assertEqual(item['ls_state_type'], 'HARD')
            self.assertEqual(item['ls_output'], 'PING OK - Packet loss = 0%, RTA = 0.05 ms')
            self.assertEqual(item['ls_perf_data'],
                             'rta=0.049000ms;2.000000;3.000000;0.000000 pl=0%;50;80;0')
            number += 1
        self.assertEqual(1, number)

        r = self.backend.get('service')
        self.assertEqual(len(r['_items']), 2)

        r = self.backend.get('livesynthesis')
        self.assertEqual(len(r['_items']), 1)
        self.assertEqual(r['_items'][0]['hosts_total'], 1)
        self.assertEqual(r['_items'][0]['hosts_up_hard'], 1)
        self.assertEqual(r['_items'][0]['hosts_up_soft'], 0)
        self.assertEqual(r['_items'][0]['hosts_down_hard'], 0)
        self.assertEqual(r['_items'][0]['hosts_down_soft'], 0)
        self.assertEqual(r['_items'][0]['hosts_unreachable_hard'], 0)
        self.assertEqual(r['_items'][0]['hosts_unreachable_soft'], 0)
        self.assertEqual(r['_items'][0]['hosts_acknowledged'], 0)
        self.assertEqual(r['_items'][0]['hosts_in_downtime'], 0)
        self.assertEqual(r['_items'][0]['services_total'], 2)
        self.assertEqual(r['_items'][0]['services_ok_hard'], 0)
        self.assertEqual(r['_items'][0]['services_ok_soft'], 0)
        self.assertEqual(r['_items'][0]['services_warning_hard'], 0)
        self.assertEqual(r['_items'][0]['services_warning_soft'], 0)
        self.assertEqual(r['_items'][0]['services_critical_hard'], 0)
        self.assertEqual(r['_items'][0]['services_critical_soft'], 0)
        self.assertEqual(r['_items'][0]['services_unknown_hard'], 2)
        self.assertEqual(r['_items'][0]['services_unknown_soft'], 0)
        self.assertEqual(r['_items'][0]['services_acknowledged'], 0)
        self.assertEqual(r['_items'][0]['services_in_downtime'], 0)

        # Add down host
        data = json.loads(open('cfg/brok_host_srv001_down.json').read())
        b = Brok({'data': data, 'type': 'host_check_result'}, False)
        b.prepare()
        self.brokmodule.manage_brok(b)

        params = {'where': '{"name": "srv001"}'}
        r = self.backend.get('host', params)
        self.assertEqual(len(r['_items']), 1)
        number = 0
        for index, item in enumerate(r['_items']):
            self.assertEqual(item['ls_last_state'], 'UP')
            self.assertEqual(item['ls_state'], 'DOWN')
            self.assertEqual(item['ls_last_state_type'], 'HARD')
            self.assertEqual(item['ls_state_type'], 'SOFT')
            self.assertEqual(item['ls_output'], 'CRITICAL - Plugin timed out after 10 seconds')
            self.assertEqual(item['ls_perf_data'], '')
            number += 1
        self.assertEqual(1, number)

        r = self.backend.get('livesynthesis')
        self.assertEqual(len(r['_items']), 1)
        self.assertEqual(r['_items'][0]['hosts_total'], 1)
        self.assertEqual(r['_items'][0]['hosts_up_hard'], 0)
        self.assertEqual(r['_items'][0]['hosts_up_soft'], 0)
        self.assertEqual(r['_items'][0]['hosts_down_hard'], 0)
        self.assertEqual(r['_items'][0]['hosts_down_soft'], 1)
        self.assertEqual(r['_items'][0]['hosts_unreachable_hard'], 0)
        self.assertEqual(r['_items'][0]['hosts_unreachable_soft'], 0)
        self.assertEqual(r['_items'][0]['hosts_acknowledged'], 0)
        self.assertEqual(r['_items'][0]['hosts_in_downtime'], 0)
        self.assertEqual(r['_items'][0]['services_total'], 2)
        self.assertEqual(r['_items'][0]['services_ok_hard'], 0)
        self.assertEqual(r['_items'][0]['services_ok_soft'], 0)
        self.assertEqual(r['_items'][0]['services_warning_hard'], 0)
        self.assertEqual(r['_items'][0]['services_warning_soft'], 0)
        self.assertEqual(r['_items'][0]['services_critical_hard'], 0)
        self.assertEqual(r['_items'][0]['services_critical_soft'], 0)
        self.assertEqual(r['_items'][0]['services_unknown_hard'], 2)
        self.assertEqual(r['_items'][0]['services_unknown_soft'], 0)
        self.assertEqual(r['_items'][0]['services_acknowledged'], 0)
        self.assertEqual(r['_items'][0]['services_in_downtime'], 0)
    def test_01_program_status_brok(self):
        """Test with a brok for the program status update

        :return: None
        """
        # Get alignak endpoint resources before the brok
        name = 'my_alignak'
        params = {'sort': '_id', 'where': '{"name": "%s"}' % name}
        all_alignak = self.backend.get_all('alignak', params)
        for alignak_cfg in all_alignak['_items']:
            print(("Alignak cfg: %s" % alignak_cfg))
        # No alignak configuration resource
        self.assertEqual(0, len(all_alignak['_items']))

        # Get a program status brok
        brok_data = {
            # Some general information
            'alignak_name': 'my_alignak',
            'instance_id': '176064a1b30741d39452415097807ab0',
            'instance_name': 'scheduler-master',

            # Some running information
            'program_start': 1493969754,
            'daemon_mode': 1,
            'pid': 68989,
            'last_alive': 1493970641,
            'last_command_check': 1493970641,
            'last_log_rotation': 1493970641,
            'is_running': 1,

            # Some configuration parameters
            'process_performance_data': True,
            'passive_service_checks_enabled': True,
            'event_handlers_enabled': True,
            'command_file': '',
            'global_host_event_handler': None,
            'interval_length': 60,
            'modified_host_attributes': 0,
            'check_external_commands': True,
            'modified_service_attributes': 0,
            'passive_host_checks_enabled': True,
            'global_service_event_handler': 'None',
            'notifications_enabled': True,
            'check_service_freshness': True,
            'check_host_freshness': True,
            'flap_detection_enabled': True,
            'active_service_checks_enabled': True,
            'active_host_checks_enabled': True
        }
        brok = Brok({'type': 'update_program_status', 'data': brok_data})
        brok.prepare()

        # As default, we do not manage this brok !
        assert self.brokmodule.manage_update_program_status is False

        # Send program status brok
        self.brokmodule.manage_brok(brok)

        # Get alignak endpoint resources after the brok
        name = 'my_alignak'
        params = {'sort': '_id', 'where': '{"name": "%s"}' % name}
        all_alignak = self.backend.get_all('alignak', params)
        # Still no alignak configuration resource
        self.assertEqual(0, len(all_alignak['_items']))

        # -------
        # Now we manage this brok !
        self.brokmodule.manage_update_program_status = True

        # Send program status brok
        self.brokmodule.manage_brok(brok)

        # Get alignak endpoint resources after the brok
        name = 'my_alignak'
        params = {'sort': '_id', 'where': '{"name": "%s"}' % name}
        all_alignak = self.backend.get_all('alignak', params)
        # Now we have one resource
        self.assertEqual(1, len(all_alignak['_items']))

        alignak = all_alignak['_items'][0]
        # Remove backend Eve fields and store creation and update timestamp
        _created = alignak.pop('_created')
        _updated = alignak.pop('_updated')
        alignak.pop('_id')
        alignak.pop('_links')
        alignak.pop('_etag')
        alignak.pop('schema_version')
        # TODO need add this new fields in alignak brok creation
        for field_name in ['use_timezone',
                           'illegal_macro_output_chars', 'illegal_object_name_chars',
                           'cleaning_queues_interval', 'max_plugins_output_length',
                           'enable_environment_macros', 'log_initial_states', 'log_active_checks',
                           'log_host_retries', 'log_service_retries', 'log_passive_checks',
                           'log_notifications', 'log_event_handlers', 'log_external_commands',
                           'log_flappings', 'log_snapshots', 'enable_notifications',
                           'notification_timeout', 'timeout_exit_status', 'execute_host_checks',
                           'max_host_check_spread', 'host_check_timeout',
                           'check_for_orphaned_hosts', 'execute_service_checks',
                           'max_service_check_spread', 'service_check_timeout',
                           'check_for_orphaned_services', 'flap_history', 'low_host_flap_threshold',
                           'high_host_flap_threshold', 'low_service_flap_threshold',
                           'high_service_flap_threshold', 'event_handler_timeout',
                           'no_event_handlers_during_downtimes', 'host_perfdata_command',
                           'service_perfdata_command', 'accept_passive_host_checks',
                           'host_freshness_check_interval', 'accept_passive_service_checks',
                           'service_freshness_check_interval', 'additional_freshness_latency']:
            alignak.pop(field_name)

        expected = brok_data.copy()
        expected['name'] = expected.pop('alignak_name')
        # Some fields are valued as default by the backend
        expected['_sub_realm'] = True
        expected['alias'] = expected['name']
        expected['notes'] = ''
        expected['notes_url'] = ''
        expected['_realm'] = self.realm_all
        expected['global_host_event_handler'] = str(expected['global_host_event_handler'])
        expected['global_service_event_handler'] = 'None'
        self.assertEqual(expected, alignak)

        # --- 1
        time.sleep(1)
        # Re-send the same brok
        brok_data = {
            # Some general information
            'alignak_name': 'my_alignak',
            'instance_id': '176064a1b30741d39452415097807ab0',
            'instance_name': 'scheduler-master',

            # Some running information
            'program_start': 1493969754,
            'daemon_mode': 1,
            'pid': 68989,
            'last_alive': 1493970641,
            'last_command_check': 1493970641,
            'last_log_rotation': 1493970641,
            'is_running': 1,

            # Some configuration parameters
            'process_performance_data': True,
            'passive_service_checks_enabled': True,
            'event_handlers_enabled': True,
            'command_file': '',
            'global_host_event_handler': None,
            'interval_length': 60,
            'modified_host_attributes': 0,
            'check_external_commands': True,
            'modified_service_attributes': 0,
            'passive_host_checks_enabled': True,
            'global_service_event_handler': 'None',
            'notifications_enabled': True,
            'check_service_freshness': True,
            'check_host_freshness': True,
            'flap_detection_enabled': True,
            'active_service_checks_enabled': True,
            'active_host_checks_enabled': True
        }
        brok = Brok({'type': 'update_program_status', 'data': brok_data})
        brok.prepare()
        self.brokmodule.manage_brok(brok)

        # Get alignak endpoint resources after the brok
        name = 'my_alignak'
        params = {'sort': '_id', 'where': '{"name": "%s"}' % name}
        all_alignak = self.backend.get_all('alignak', params)
        # We still have one resource
        self.assertEqual(1, len(all_alignak['_items']))

        alignak = all_alignak['_items'][0]
        # Remove backend Eve fields
        # Creation and update timestamps did not change because there was no backend update
        assert _created == alignak.pop('_created')
        assert _updated == alignak.pop('_updated')
        alignak.pop('_id')
        alignak.pop('_links')
        alignak.pop('_etag')
        alignak.pop('schema_version')
        # TODO need add this new fields in alignak brok creation
        for field_name in ['use_timezone',
                           'illegal_macro_output_chars', 'illegal_object_name_chars',
                           'cleaning_queues_interval', 'max_plugins_output_length',
                           'enable_environment_macros', 'log_initial_states', 'log_active_checks',
                           'log_host_retries', 'log_service_retries', 'log_passive_checks',
                           'log_notifications', 'log_event_handlers', 'log_external_commands',
                           'log_flappings', 'log_snapshots', 'enable_notifications',
                           'notification_timeout', 'timeout_exit_status', 'execute_host_checks',
                           'max_host_check_spread', 'host_check_timeout',
                           'check_for_orphaned_hosts', 'execute_service_checks',
                           'max_service_check_spread', 'service_check_timeout',
                           'check_for_orphaned_services', 'flap_history', 'low_host_flap_threshold',
                           'high_host_flap_threshold', 'low_service_flap_threshold',
                           'high_service_flap_threshold', 'event_handler_timeout',
                           'no_event_handlers_during_downtimes', 'host_perfdata_command',
                           'service_perfdata_command', 'accept_passive_host_checks',
                           'host_freshness_check_interval', 'accept_passive_service_checks',
                           'service_freshness_check_interval', 'additional_freshness_latency']:
            alignak.pop(field_name)

        expected = brok_data.copy()
        expected['name'] = expected.pop('alignak_name')
        # Some fields are valued as default by the backend
        expected['_sub_realm'] = True
        expected['alias'] = expected['name']
        expected['notes'] = ''
        expected['notes_url'] = ''
        expected['_realm'] = self.realm_all
        expected['global_host_event_handler'] = str(expected['global_host_event_handler'])
        expected['global_service_event_handler'] = 'None'
        self.assertEqual(expected, alignak)

        # --- 2
        time.sleep(1)
        # Update the program status
        brok_data = {
            # Some general information
            'alignak_name': 'my_alignak',
            'instance_id': '176064a1b30741d39452415097807ab0',
            'instance_name': 'scheduler-master',

            # Some running information
            'program_start': 1493969754,
            'daemon_mode': 1,
            'pid': 68989,
            'last_alive': 1493970641,
            'last_command_check': 1493970641,
            'last_log_rotation': 1493970641,
            'is_running': 1,

            # Some configuration parameters
            'process_performance_data': True,
            'passive_service_checks_enabled': True,
            'event_handlers_enabled': True,
            'command_file': '',
            'global_host_event_handler': None,
            'interval_length': 60,
            'modified_host_attributes': 0,
            'check_external_commands': True,
            'modified_service_attributes': 0,
            'passive_host_checks_enabled': True,
            'global_service_event_handler': 'None',
            'notifications_enabled': True,
            'check_service_freshness': True,
            'check_host_freshness': True,
            'flap_detection_enabled': True,
            'active_service_checks_enabled': True,
            'active_host_checks_enabled': True
        }
        brok_data['flap_detection_enabled'] = False
        brok = Brok({'type': 'update_program_status', 'data': brok_data})
        brok.prepare()
        # Send program status brok
        self.brokmodule.manage_brok(brok)

        # Get alignak endpoint resources after the brok
        name = 'my_alignak'
        params = {'sort': '_id', 'where': '{"name": "%s"}' % name}
        all_alignak = self.backend.get_all('alignak', params)
        # We still have one resource
        self.assertEqual(1, len(all_alignak['_items']))

        alignak = all_alignak['_items'][0]
        # Remove backend Eve fields
        # Creation timestamp did not change
        assert _created == alignak['_created']
        _created = alignak.pop('_created')
        # But update timestamp changed !
        assert _updated != alignak['_updated']
        _updated = alignak.pop('_updated')
        alignak.pop('_id')
        alignak.pop('_links')
        alignak.pop('_etag')
        alignak.pop('schema_version')
        # TODO need add this new fields in alignak brok creation
        for field_name in ['use_timezone',
                           'illegal_macro_output_chars', 'illegal_object_name_chars',
                           'cleaning_queues_interval', 'max_plugins_output_length',
                           'enable_environment_macros', 'log_initial_states', 'log_active_checks',
                           'log_host_retries', 'log_service_retries', 'log_passive_checks',
                           'log_notifications', 'log_event_handlers', 'log_external_commands',
                           'log_flappings', 'log_snapshots', 'enable_notifications',
                           'notification_timeout', 'timeout_exit_status', 'execute_host_checks',
                           'max_host_check_spread', 'host_check_timeout',
                           'check_for_orphaned_hosts', 'execute_service_checks',
                           'max_service_check_spread', 'service_check_timeout',
                           'check_for_orphaned_services', 'flap_history', 'low_host_flap_threshold',
                           'high_host_flap_threshold', 'low_service_flap_threshold',
                           'high_service_flap_threshold', 'event_handler_timeout',
                           'no_event_handlers_during_downtimes', 'host_perfdata_command',
                           'service_perfdata_command', 'accept_passive_host_checks',
                           'host_freshness_check_interval', 'accept_passive_service_checks',
                           'service_freshness_check_interval', 'additional_freshness_latency']:
            alignak.pop(field_name)

        expected = brok_data.copy()
        expected['name'] = expected.pop('alignak_name')
        # Some fields are valued as default by the backend
        expected['_sub_realm'] = True
        expected['alias'] = expected['name']
        expected['notes'] = ''
        expected['notes_url'] = ''
        expected['_realm'] = self.realm_all
        expected['global_host_event_handler'] = str(expected['global_host_event_handler'])
        expected['global_service_event_handler'] = 'None'
        self.assertEqual(expected, alignak)

        # --- 3
        time.sleep(1)
        # Re-send the same brok
        brok_data = {
            # Some general information
            'alignak_name': 'my_alignak',
            'instance_id': '176064a1b30741d39452415097807ab0',
            'instance_name': 'scheduler-master',

            # Some running information
            'program_start': 1493969754,
            'daemon_mode': 1,
            'pid': 68989,
            'last_alive': 1493970641,
            'last_command_check': 1493970641,
            'last_log_rotation': 1493970641,
            'is_running': 1,

            # Some configuration parameters
            'process_performance_data': True,
            'passive_service_checks_enabled': True,
            'event_handlers_enabled': True,
            'command_file': '',
            'global_host_event_handler': None,
            'interval_length': 60,
            'modified_host_attributes': 0,
            'check_external_commands': True,
            'modified_service_attributes': 0,
            'passive_host_checks_enabled': True,
            'global_service_event_handler': 'None',
            'notifications_enabled': True,
            'check_service_freshness': True,
            'check_host_freshness': True,
            'flap_detection_enabled': True,
            'active_service_checks_enabled': True,
            'active_host_checks_enabled': True
        }
        brok_data['flap_detection_enabled'] = False
        brok = Brok({'type': 'update_program_status', 'data': brok_data})
        brok.prepare()
        self.brokmodule.manage_brok(brok)

        # Get alignak endpoint resources after the brok
        name = 'my_alignak'
        params = {'sort': '_id', 'where': '{"name": "%s"}' % name}
        all_alignak = self.backend.get_all('alignak', params)
        # We still have one resource
        self.assertEqual(1, len(all_alignak['_items']))

        alignak = all_alignak['_items'][0]
        # Remove backend Eve fields
        # Creation timestamp do not change
        assert _created == alignak['_created']
        _created = alignak.pop('_created')
        # And update timestamp do not change!
        assert _updated == alignak['_updated']
        _updated = alignak.pop('_updated')
        alignak.pop('_id')
        alignak.pop('_links')
        alignak.pop('_etag')
        alignak.pop('schema_version')
        # TODO need add this new fields in alignak brok creation
        for field_name in ['use_timezone',
                           'illegal_macro_output_chars', 'illegal_object_name_chars',
                           'cleaning_queues_interval', 'max_plugins_output_length',
                           'enable_environment_macros', 'log_initial_states', 'log_active_checks',
                           'log_host_retries', 'log_service_retries', 'log_passive_checks',
                           'log_notifications', 'log_event_handlers', 'log_external_commands',
                           'log_flappings', 'log_snapshots', 'enable_notifications',
                           'notification_timeout', 'timeout_exit_status', 'execute_host_checks',
                           'max_host_check_spread', 'host_check_timeout',
                           'check_for_orphaned_hosts', 'execute_service_checks',
                           'max_service_check_spread', 'service_check_timeout',
                           'check_for_orphaned_services', 'flap_history', 'low_host_flap_threshold',
                           'high_host_flap_threshold', 'low_service_flap_threshold',
                           'high_service_flap_threshold', 'event_handler_timeout',
                           'no_event_handlers_during_downtimes', 'host_perfdata_command',
                           'service_perfdata_command', 'accept_passive_host_checks',
                           'host_freshness_check_interval', 'accept_passive_service_checks',
                           'service_freshness_check_interval', 'additional_freshness_latency']:
            alignak.pop(field_name)

        expected = brok_data.copy()
        expected['name'] = expected.pop('alignak_name')
        # Some fields are valued as default by the backend
        expected['_sub_realm'] = True
        expected['alias'] = expected['name']
        expected['notes'] = ''
        expected['notes_url'] = ''
        expected['_realm'] = self.realm_all
        expected['global_host_event_handler'] = str(expected['global_host_event_handler'])
        expected['global_service_event_handler'] = 'None'
        self.assertEqual(expected, alignak)

        # --- 4
        time.sleep(1)
        # Update only the running properties
        brok_data = {
            # Some general information
            'alignak_name': 'my_alignak',
            'instance_id': '176064a1b30741d39452415097807ab0',
            'instance_name': 'scheduler-master',

            # Some running information
            'program_start': 1493969754,
            'daemon_mode': 1,
            'pid': 68989,
            'last_alive': 1493970641,
            'last_command_check': 1493970641,
            'last_log_rotation': 1493970641,
            'is_running': 1,

            # Some configuration parameters
            'process_performance_data': True,
            'passive_service_checks_enabled': True,
            'event_handlers_enabled': True,
            'command_file': '',
            'global_host_event_handler': None,
            'interval_length': 60,
            'modified_host_attributes': 0,
            'check_external_commands': True,
            'modified_service_attributes': 0,
            'passive_host_checks_enabled': True,
            'global_service_event_handler': 'None',
            'notifications_enabled': True,
            'check_service_freshness': True,
            'check_host_freshness': True,
            'flap_detection_enabled': True,
            'active_service_checks_enabled': True,
            'active_host_checks_enabled': True
        }
        brok_data['flap_detection_enabled'] = False
        brok_data['last_alive'] = 123456789
        brok_data['last_command_check'] = 123456789
        brok_data['last_log_rotation'] = 123456789
        brok = Brok({'type': 'update_program_status', 'data': brok_data})
        brok.prepare()
        self.brokmodule.manage_brok(brok)

        # Get alignak endpoint resources after the brok
        name = 'my_alignak'
        params = {'sort': '_id', 'where': '{"name": "%s"}' % name}
        all_alignak = self.backend.get_all('alignak', params)
        # We still have one resource
        self.assertEqual(1, len(all_alignak['_items']))

        alignak = all_alignak['_items'][0]
        # Remove backend Eve fields
        # Creation timestamp do not change
        assert _created == alignak['_created']
        _created = alignak.pop('_created')
        # And update timestamp do not change!
        assert _updated == alignak['_updated']
        _updated = alignak.pop('_updated')
        alignak.pop('_id')
        alignak.pop('_links')
        alignak.pop('_etag')
        alignak.pop('schema_version')
        # TODO need add this new fields in alignak brok creation
        for field_name in ['use_timezone',
                           'illegal_macro_output_chars', 'illegal_object_name_chars',
                           'cleaning_queues_interval', 'max_plugins_output_length',
                           'enable_environment_macros', 'log_initial_states', 'log_active_checks',
                           'log_host_retries', 'log_service_retries', 'log_passive_checks',
                           'log_notifications', 'log_event_handlers', 'log_external_commands',
                           'log_flappings', 'log_snapshots', 'enable_notifications',
                           'notification_timeout', 'timeout_exit_status', 'execute_host_checks',
                           'max_host_check_spread', 'host_check_timeout',
                           'check_for_orphaned_hosts', 'execute_service_checks',
                           'max_service_check_spread', 'service_check_timeout',
                           'check_for_orphaned_services', 'flap_history', 'low_host_flap_threshold',
                           'high_host_flap_threshold', 'low_service_flap_threshold',
                           'high_service_flap_threshold', 'event_handler_timeout',
                           'no_event_handlers_during_downtimes', 'host_perfdata_command',
                           'service_perfdata_command', 'accept_passive_host_checks',
                           'host_freshness_check_interval', 'accept_passive_service_checks',
                           'service_freshness_check_interval', 'additional_freshness_latency']:
            alignak.pop(field_name)

        expected = brok_data.copy()
        expected['name'] = expected.pop('alignak_name')
        # Some fields are valued as default by the backend
        expected['_sub_realm'] = True
        expected['alias'] = expected['name']
        expected['notes'] = ''
        expected['notes_url'] = ''
        expected['_realm'] = self.realm_all
        expected['global_host_event_handler'] = str(expected['global_host_event_handler'])
        expected['global_service_event_handler'] = 'None'
        self.assertEqual(expected, alignak)
    def test_00_refused_program_status_brok(self):
        """Test with a bad formatted brok for the program status

        :return: None
        """
        # Get alignak endpoint resources before the brok
        name = 'my_alignak'
        params = {'sort': '_id', 'where': '{"name": "%s"}' % name}
        all_alignak = self.backend.get_all('alignak', params)
        for alignak_cfg in all_alignak['_items']:
            print(("Alignak cfg: %s" % alignak_cfg))
        # No alignak configuration resource
        self.assertEqual(0, len(all_alignak['_items']))

        # Get a BAD program status brok
        brok_data = {
            # Some general information

            ### Missing alignak_name property !
            # u'alignak_name': u'my_alignak',
            'instance_id': '176064a1b30741d39452415097807ab0',
            'instance_name': 'scheduler-master',

            # Some running information
            'program_start': 1493969754,
            'daemon_mode': 1,
            'pid': 68989,
            'last_alive': 1493970641,
            'last_command_check': 1493970641,
            'last_log_rotation': 1493970641,
            'is_running': 1,

            # Some configuration parameters
            'process_performance_data': True,
            'passive_service_checks_enabled': True,
            'event_handlers_enabled': True,
            'command_file': '',
            'global_host_event_handler': None,
            'interval_length': 60,
            'modified_host_attributes': 0,
            'check_external_commands': True,
            'modified_service_attributes': 0,
            'passive_host_checks_enabled': True,
            'global_service_event_handler': 'None',
            'notifications_enabled': True,
            'check_service_freshness': True,
            'check_host_freshness': True,
            'flap_detection_enabled': True,
            'active_service_checks_enabled': True,
            'active_host_checks_enabled': True
        }
        brok = Brok({'type': 'update_program_status', 'data': brok_data})
        brok.prepare()

        # Send program status brok
        self.brokmodule.manage_brok(brok)

        # Get alignak endpoint resources after the brok
        name = 'my_alignak'
        params = {'sort': '_id', 'where': '{"name": "%s"}' % name}
        all_alignak = self.backend.get_all('alignak', params)
        # Still no alignak configuration resource
        self.assertEqual(0, len(all_alignak['_items']))

        # Get a GOOD program status brok
        brok_data = {
            # Some general information

            'alignak_name': 'my_alignak',
            'instance_id': '176064a1b30741d39452415097807ab0',
            'instance_name': 'scheduler-master',

            # Some running information
            'program_start': 1493969754,
            'daemon_mode': 1,
            'pid': 68989,
            'last_alive': 1493970641,
            'last_command_check': 1493970641,
            'last_log_rotation': 1493970641,
            'is_running': 1,

            # Some configuration parameters
            'process_performance_data': True,
            'passive_service_checks_enabled': True,
            'event_handlers_enabled': True,
            'command_file': '',
            'global_host_event_handler': None,
            'interval_length': 60,
            'modified_host_attributes': 0,
            'check_external_commands': True,
            'modified_service_attributes': 0,
            'passive_host_checks_enabled': True,
            'global_service_event_handler': 'None',
            'notifications_enabled': True,
            'check_service_freshness': True,
            'check_host_freshness': True,
            'flap_detection_enabled': True,
            'active_service_checks_enabled': True,
            'active_host_checks_enabled': True
        }
        brok = Brok({'type': 'update_program_status', 'data': brok_data})
        brok.prepare()

        # The module has no default realm ... this should never happen !
        self.brokmodule.default_realm = None

        # Send program status brok
        self.brokmodule.manage_brok(brok)

        # Get alignak endpoint resources after the brok
        name = 'my_alignak'
        params = {'sort': '_id', 'where': '{"name": "%s"}' % name}
        all_alignak = self.backend.get_all('alignak', params)
        # Still no alignak configuration resource
        self.assertEqual(0, len(all_alignak['_items']))
Ejemplo n.º 10
0
    def test_alignak_configuration(self):
        """Test alignak configuration reading

        :return:
        """
        # 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)

        # Get a program status brok
        brok_data = {
            # Some general information
            'alignak_name': 'my_alignak',
            'instance_id': '176064a1b30741d39452415097807ab0',
            'instance_name': 'scheduler-master',

            # Some running information
            'program_start': 1493969754,
            'daemon_mode': 1,
            'pid': 68989,
            'last_alive': 1493970641,
            'last_command_check': 1493970641,
            'last_log_rotation': 1493970641,
            'is_running': 1,

            # Some configuration parameters
            'process_performance_data': True,
            'passive_service_checks_enabled': True,
            'event_handlers_enabled': True,
            'command_file': '',
            'global_host_event_handler': None,
            'interval_length': 60,
            'modified_host_attributes': 0,
            'check_external_commands': True,
            'modified_service_attributes': 0,
            'passive_host_checks_enabled': True,
            'global_service_event_handler': None,
            'notifications_enabled': True,
            'check_service_freshness': True,
            'check_host_freshness': True,
            'flap_detection_enabled': True,
            'active_service_checks_enabled': True,
            'active_host_checks_enabled': True
        }
        brok = Brok({'type': 'update_program_status', 'data': brok_data})
        brok.prepare()

        # -------
        # Configure to manage this brok (default is to ignore...) !
        self.brokmodule.manage_update_program_status = True

        # Send program status brok
        self.brokmodule.manage_brok(brok)
        # This has created an `alignak` resource...

        # Now we call the Arbiter hook function to get this created configuration
        # Will get all the `alignak` resources because no arbiter name is defined ...
        fake_arb = Arbiter()
        self.arbmodule.hook_read_configuration(fake_arb)
        configuration = self.arbmodule.get_alignak_configuration()
        print(("Configuration: %s" % configuration))
        expected = brok_data.copy()
        print(("Expected: %s" % expected))
        expected['name'] = expected.pop('alignak_name')
        # Some fields are valued as default by the backend
        configuration.pop('_created')
        configuration.pop('_updated')
        configuration.pop('_id')
        configuration.pop('_etag')
        configuration.pop('_realm')
        configuration.pop('_sub_realm')
        configuration.pop('_links')
        configuration.pop('schema_version')
        # TODO need add this new fields in alignak brok creation
        for field_name in ['use_timezone',
                           'illegal_macro_output_chars', 'illegal_object_name_chars',
                           'cleaning_queues_interval', 'max_plugins_output_length',
                           'enable_environment_macros', 'log_initial_states', 'log_active_checks',
                           'log_host_retries', 'log_service_retries', 'log_passive_checks',
                           'log_notifications', 'log_event_handlers', 'log_external_commands',
                           'log_flappings', 'log_snapshots', 'enable_notifications',
                           'notification_timeout', 'timeout_exit_status', 'execute_host_checks',
                           'max_host_check_spread', 'host_check_timeout',
                           'check_for_orphaned_hosts', 'execute_service_checks',
                           'max_service_check_spread', 'service_check_timeout',
                           'check_for_orphaned_services', 'flap_history', 'low_host_flap_threshold',
                           'high_host_flap_threshold', 'low_service_flap_threshold',
                           'high_service_flap_threshold', 'event_handler_timeout',
                           'no_event_handlers_during_downtimes', 'host_perfdata_command',
                           'service_perfdata_command', 'accept_passive_host_checks',
                           'host_freshness_check_interval', 'accept_passive_service_checks',
                           'service_freshness_check_interval', 'additional_freshness_latency']:
            configuration.pop(field_name)
        expected['alias'] = expected['name']
        expected['notes'] = ''
        expected['notes_url'] = ''
        expected['global_host_event_handler'] = str(expected['global_host_event_handler'])
        expected['global_service_event_handler'] = 'None'
        self.assertEqual(configuration, expected)

        # Get another program status brok
        brok_data = {
            # Some general information
            'alignak_name': 'my_alignak_2',
            'instance_id': '176064a1b30741d39452415097807ab0',
            'instance_name': 'scheduler-master',

            # Some running information
            'program_start': 1493969754,
            'daemon_mode': 1,
            'pid': 68989,
            'last_alive': 1493970641,
            'last_command_check': 1493970641,
            'last_log_rotation': 1493970641,
            'is_running': 1,

            # Some configuration parameters
            'process_performance_data': True,
            'passive_service_checks_enabled': True,
            'event_handlers_enabled': True,
            'command_file': '',
            'global_host_event_handler': 'None',
            'interval_length': 60,
            'modified_host_attributes': 0,
            'check_external_commands': True,
            'modified_service_attributes': 0,
            'passive_host_checks_enabled': True,
            'global_service_event_handler': 'None',
            'notifications_enabled': True,
            'check_service_freshness': True,
            'check_host_freshness': True,
            'flap_detection_enabled': True,
            'active_service_checks_enabled': True,
            'active_host_checks_enabled': True
        }
        brok = Brok({'type': 'update_program_status', 'data': brok_data})
        brok.prepare()

        # Send program status brok
        self.brokmodule.manage_brok(brok)
        # This has created an `alignak` resource...

        # Now we call the Arbiter hook function to get this created configuration
        # Get the configuration for a specific arbiter / alignak
        # It will be the first one created
        fake_arb = Arbiter(arbiter_name='my_alignak')
        self.arbmodule.hook_read_configuration(fake_arb)
        configuration = self.arbmodule.get_alignak_configuration()
        # Some fields are valued as default by the backend
        configuration.pop('_created')
        configuration.pop('_updated')
        configuration.pop('_id')
        configuration.pop('_etag')
        configuration.pop('_realm')
        configuration.pop('_sub_realm')
        configuration.pop('_links')
        configuration.pop('schema_version')
        # TODO need add this new fields in alignak brok creation
        for field_name in ['use_timezone',
                           'illegal_macro_output_chars', 'illegal_object_name_chars',
                           'cleaning_queues_interval', 'max_plugins_output_length',
                           'enable_environment_macros', 'log_initial_states', 'log_active_checks',
                           'log_host_retries', 'log_service_retries', 'log_passive_checks',
                           'log_notifications', 'log_event_handlers', 'log_external_commands',
                           'log_flappings', 'log_snapshots', 'enable_notifications',
                           'notification_timeout', 'timeout_exit_status', 'execute_host_checks',
                           'max_host_check_spread', 'host_check_timeout',
                           'check_for_orphaned_hosts', 'execute_service_checks',
                           'max_service_check_spread', 'service_check_timeout',
                           'check_for_orphaned_services', 'flap_history', 'low_host_flap_threshold',
                           'high_host_flap_threshold', 'low_service_flap_threshold',
                           'high_service_flap_threshold', 'event_handler_timeout',
                           'no_event_handlers_during_downtimes', 'host_perfdata_command',
                           'service_perfdata_command', 'accept_passive_host_checks',
                           'host_freshness_check_interval', 'accept_passive_service_checks',
                           'service_freshness_check_interval', 'additional_freshness_latency']:
            configuration.pop(field_name)
        self.assertEqual(configuration, expected)