Exemplo n.º 1
0
    def devices_view(self):

        with persistence.SessionContext(p_persistence=self._persistence) as session_context:
            request = flask.request
            with tools.TimingContext(lambda duration: self.measure(p_hostname=request.remote_addr,
                                                                   p_service=request.url_rule, p_duration=duration)):

                devices = self._appcontrol.get_sorted_devices(session_context)
                forms = self.get_devices_forms(p_devices=devices)

                valid_and_submitted = True
                submitted = False

                for form in forms.values():
                    if not form.validate_on_submit():
                        valid_and_submitted = False

                    if form.is_submitted():
                        submitted = True

                if valid_and_submitted:
                    self.save_devices_data(devices, forms)

                    if request.form['submit'] == HTML_KEY_NEW_DEVICE:
                        self._persistence.add_new_device(p_session_context=session_context,
                                                         p_name_pattern=self.gettext("New device {id}"))
                        # TODO: after adding new device Devices window should be opened for new device
                    else:
                        for device in devices:
                            if request.form['submit'] == device.delete_html_key:
                                self._persistence.delete_device(device.id)

                    return flask.redirect(flask.url_for("little_brother.devices"))

                if not submitted:
                    for device in devices:
                        forms[device.device_name].load_from_model(p_model=device)

                return flask.render_template(
                    DEVICES_HTML_TEMPLATE,
                    rel_font_size=self.get_rel_font_size(),
                    devices=devices,
                    authentication=self.get_authenication_info(),
                    forms=forms,
                    new_device_html_key=HTML_KEY_NEW_DEVICE,
                    new_device_submit_value=HTML_KEY_NEW_DEVICE,
                    navigation={
                        'current_view': DEVICES_VIEW_NAME
                    },
                )
Exemplo n.º 2
0
    def index_view(self):

        request = flask.request
        with tools.TimingContext(lambda duration: self.measure(p_hostname=request.remote_addr,
                                                               p_service=request.url_rule, p_duration=duration)):
            with persistence.SessionContext(p_persistence=self._persistence) as session_context:
                user_infos = self._appcontrol.get_user_status_infos(p_session_context=session_context)
                page = flask.render_template(
                    INDEX_HTML_TEMPLATE,
                    rel_font_size=self.get_rel_font_size(),
                    user_infos=user_infos,
                    has_downtime_today=self.has_downtime_today(p_user_infos=user_infos),
                    app_control_config=self._appcontrol._config,
                    authentication=self.get_authenication_info(),
                    navigation={
                        'current_view': INDEX_VIEW_NAME},
                )

        return page
Exemplo n.º 3
0
    def save_devices_data(self, p_devices, p_forms):

        with persistence.SessionContext(p_persistence=self._persistence) as session_context:
            session = session_context.get_session()
            changed = False

            for device in p_devices:
                form = p_forms[device.device_name]
                device = persistence.Device.get_by_device_name(p_session=session, p_device_name=device.device_name)

                if device is not None and form.differs_from_model(p_model=device):
                    form.save_to_model(p_model=device)
                    changed = True

            session.commit()
            session.close()

            if changed:
                self._persistence.clear_cache()
Exemplo n.º 4
0
    def test_process_statistics_get_empty_stat_infos(self):
        start_time = datetime.datetime.utcnow()
        rule_set_configs = test_data.get_dummy_ruleset_configs(
            p_ruleset_config=test_data.RULESET_CONFIGS_USER1_ALL_RESTRICTIONS)
        dummy_persistence = test_persistence.TestPersistence.create_dummy_persistence(
            self._logger)
        session_context = persistence.SessionContext(
            p_persistence=dummy_persistence)
        dummy_persistence.add_new_user(p_session_context=session_context,
                                       p_username=test_data.USER_1)
        migrator = db_migrations.DatabaseMigrations(
            p_logger=self._logger, p_persistence=dummy_persistence)
        migrator.migrate_ruleset_configs(p_ruleset_configs=rule_set_configs)
        sis = process_statistics.get_empty_stat_infos(
            p_user_map=dummy_persistence.user_map(session_context),
            p_reference_time=start_time,
            p_max_lookback_in_days=5,
            p_min_activity_duration=30)

        self.assertIsNotNone(sis)
Exemplo n.º 5
0
    def about_view(self):

        with persistence.SessionContext(p_persistence=self._persistence) as session_context:
            request = flask.request
            with tools.TimingContext(lambda duration: self.measure(p_hostname=request.remote_addr,
                                                                   p_service=request.url_rule, p_duration=duration)):
                page = flask.render_template(
                    ABOUT_HTML_TEMPLATE,
                    rel_font_size=self.get_rel_font_size(),
                    user_infos=self._appcontrol.get_user_status_infos(session_context),
                    settings=settings.settings,
                    extended_settings=settings.extended_settings,
                    git_metadata=git.git_metadata,
                    authentication=self.get_authenication_info(),
                    languages=sorted([(a_locale, a_language) for a_locale, a_language in self._languages.items()]),
                    navigation={
                        'current_view': ABOUT_VIEW_NAME}

                )
                return page
    def test_priority(self):
        dummy_persistence = test_persistence.TestPersistence.create_dummy_persistence(self._logger)
        session_context = persistence.SessionContext(p_persistence=dummy_persistence)
        a_rule_handler = self.create_dummy_rule_handler(p_persistence=dummy_persistence)

        migrator = db_migrations.DatabaseMigrations(p_logger=self._logger, p_persistence=dummy_persistence)
        migrator.migrate_ruleset_configs(self.create_dummy_ruleset_configs())

        active_rule_set = a_rule_handler.get_active_ruleset(p_session_context=session_context,
                                                            p_username=TEST_USER, p_reference_date=NORMAL_DAY_1)
        self.assertIsNotNone(active_rule_set)
        self.assertEqual(active_rule_set.context, simple_context_rule_handlers.DEFAULT_CONTEXT_RULE_HANDLER_NAME)

        active_rule_set = a_rule_handler.get_active_ruleset(p_session_context=session_context,
                                                            p_username=TEST_USER, p_reference_date=WEEKEND_DAY_1)
        self.assertIsNotNone(active_rule_set)
        self.assertEqual(active_rule_set.context, simple_context_rule_handlers.WEEKPLAN_CONTEXT_RULE_HANDLER_NAME)

        active_rule_set = a_rule_handler.get_active_ruleset(p_session_context=session_context,
                                                            p_username=TEST_USER, p_reference_date=WEEKEND_DAY_2)
        self.assertIsNotNone(active_rule_set)
        self.assertEqual(active_rule_set.context, simple_context_rule_handlers.WEEKPLAN_CONTEXT_RULE_HANDLER_NAME)

        active_rule_set = a_rule_handler.get_active_ruleset(p_session_context=session_context,
                                                            p_username=TEST_USER,
                                                            p_reference_date=VACATION_DAY_1)
        self.assertIsNotNone(active_rule_set)
        self.assertEqual(active_rule_set.context,
                         german_vacation_context_rule_handler.CALENDAR_CONTEXT_RULE_HANDLER_NAME)

        active_rule_set = a_rule_handler.get_active_ruleset(p_session_context=session_context,
                                                            p_username=TEST_USER,
                                                            p_reference_date=VACATION_DAY_2)
        self.assertIsNotNone(active_rule_set)
        self.assertEqual(active_rule_set.context,
                         german_vacation_context_rule_handler.CALENDAR_CONTEXT_RULE_HANDLER_NAME)

        active_rule_set = a_rule_handler.get_active_ruleset(p_session_context=session_context,
                                                            p_username=TEST_USER, p_reference_date=WEEKEND_DAY_3)
        self.assertIsNotNone(active_rule_set)
        self.assertEqual(active_rule_set.context, simple_context_rule_handlers.WEEKPLAN_CONTEXT_RULE_HANDLER_NAME)
    def test_existing_host(self):
        config = client_device_handler.ClientDeviceHandlerConfigModel()

        device_config = client_device_handler.ClientDeviceConfigModel(
            p_section_name="my_device")
        device_config.username = test_data.USER_1
        device_config.hostname = "localhost"
        device_config.min_activity_duration = 0

        device_configs = {device_config.section_name: device_config}

        dummy_persistence = test_persistence.TestPersistence.create_dummy_persistence(
            self._logger)
        session_context = persistence.SessionContext(
            p_persistence=dummy_persistence)
        dummy_persistence.add_new_user(p_session_context=session_context,
                                       p_username=test_data.USER_1)
        migrator = db_migrations.DatabaseMigrations(
            p_logger=self._logger, p_persistence=dummy_persistence)
        migrator.migrate_client_device_configs(device_configs)
        process_handler = client_device_handler.ClientDeviceHandler(
            p_config=config, p_persistence=dummy_persistence)

        process_handler.scan_processes(
            p_session_context=session_context,
            p_server_group=None,
            p_login_mapping=None,
            p_host_name=None,
            p_process_regex_map=None,
            p_reference_time=datetime.datetime.now())

        events = process_handler.scan_processes(
            p_session_context=session_context,
            p_server_group=None,
            p_login_mapping=None,
            p_host_name=None,
            p_process_regex_map=None,
            p_reference_time=datetime.datetime.now())

        self.check_list_has_n_elements(p_list=events, p_n=1)
    def get_device_info(self, p_device_name):

        with persistence.SessionContext(p_persistence=self._persistence) as session_context:
            device_info = self._device_infos.get(p_device_name)
            device = self._persistence.device_map(session_context).get(p_device_name)

        if device is not None:
            if device_info is None:
                device_info = DeviceInfo(p_device_name=p_device_name,
                                         p_max_active_ping_delay=device.max_active_ping_delay,
                                         p_min_activity_duration=device.min_activity_duration,
                                         p_sample_size=device.sample_size,
                                         p_hostname=device.hostname)
                self._device_infos[p_device_name] = device_info

            else:
                device_info.update_max_active_ping_delay(device.max_active_ping_delay)
                device_info.update_min_activity_duration(device.min_activity_duration)
                device_info.update_hostname(device.hostname)
                device_info.update_sample_size(device.sample_size)

        return device_info
Exemplo n.º 9
0
    def admin_view(self):

        request = flask.request
        with tools.TimingContext(lambda duration: self.measure(p_hostname=request.remote_addr,
                                                               p_service=request.url_rule, p_duration=duration)):
            with persistence.SessionContext(p_persistence=self._persistence) as session_context:

                admin_infos = self._appcontrol.get_admin_infos(p_session_context=session_context)
                forms = self.get_admin_forms(p_admin_infos=admin_infos)

                valid_and_submitted = True
                submitted = False

                for form in forms.values():
                    if not form.validate_on_submit():
                        valid_and_submitted = False

                    if form.is_submitted():
                        submitted = True

                if valid_and_submitted:
                    self.save_admin_data(admin_infos, forms)
                    return flask.redirect(flask.url_for("little_brother.admin"))

                if not submitted:
                    for admin_info in admin_infos:
                        for day_info in admin_info.day_infos:
                            forms[day_info.html_key].load_from_model(p_model=day_info.override)

                return flask.render_template(
                    ADMIN_HTML_TEMPLATE,
                    rel_font_size=self.get_rel_font_size(),
                    admin_infos=admin_infos,
                    authentication=self.get_authenication_info(),
                    forms=forms,
                    navigation={
                        'current_view': ADMIN_VIEW_NAME},
                )
Exemplo n.º 10
0
    def save_users_data(self, p_users, p_forms):

        with persistence.SessionContext(p_persistence=self._persistence) as session_context:
            session = session_context.get_session()
            changed = False

            for user in p_users:
                form = p_forms[user.html_key]
                persistent_user = persistence.User.get_by_username(p_session=session, p_username=user.username)

                if persistent_user is not None and form.differs_from_model(p_model=persistent_user):
                    form.save_to_model(p_model=persistent_user)
                    changed = True

                for ruleset in user.rulesets:
                    form = p_forms[ruleset.html_key]
                    persistent_ruleset = persistence.RuleSet.get_by_id(p_session=session, p_id=ruleset.id)

                    if persistent_ruleset is not None and form.differs_from_model(p_model=persistent_ruleset):
                        form.save_to_model(p_model=persistent_ruleset)
                        changed = True

                for user2device in user.devices:
                    form = p_forms[user2device.html_key]
                    persistent_user2device = persistence.User2Device.get_by_id(p_session=session, p_id=user2device.id)

                    if persistent_user2device is not None and form.differs_from_model(p_model=persistent_user2device):
                        form.save_to_model(p_model=persistent_user2device)
                        changed = True

            if changed:
                session.commit()

                self._persistence.clear_cache()
                self._appcontrol.send_config_to_all_slaves()
                self._appcontrol.reset_users(p_session_context=session_context)
    def scan_processes(self, p_session_context, p_reference_time, p_server_group, p_login_mapping, p_host_name,
                       p_process_regex_map):

        events = []

        with persistence.SessionContext(p_persistence=self._persistence) as session_context:

            for device in self._persistence.devices(p_session_context):
                self.ping_device(p_reference_time=p_reference_time, p_device=device)

            for device_info in self._device_infos.values():
                if device_info.device_name not in self._persistence.device_map(session_context):
                    # Clear statistics for old device names so that they are correctly reported in Prometheus
                    device_info.clear_moving_average()

            for device in self._persistence.devices(session_context):
                device_info = self.get_device_info(p_device_name=device.device_name)

                if device_info.requires_process_start_event(p_reference_time=p_reference_time):
                    # Send process start event for monitored users (if required)
                    for user2device in device.users:
                        current_pinfo = self.get_current_active_pinfo(device.hostname, p_username=user2device.user.username)

                        if user2device.active:
                            if current_pinfo is None:
                                event = admin_event.AdminEvent(
                                    p_event_type=admin_event.EVENT_TYPE_PROCESS_START,
                                    p_hostname=device_info.hostname,
                                    p_hostlabel=device_info.device_name,
                                    p_processhandler=self.id,
                                    p_username=user2device.user.username,
                                    p_percent=user2device.percent,
                                    p_process_start_time=p_reference_time)
                                events.append(event)

                        else:
                            if current_pinfo is not None:
                                event = self.create_admin_event_process_end_from_pinfo(
                                    p_pinfo=current_pinfo,
                                    p_reference_time=p_reference_time)
                                events.append(event)


                else:
                    # Send process stop event for non monitored users (if required)
                    for user2device in device.users:
                        current_pinfo = self.get_current_active_pinfo(device.hostname, p_username=user2device.user.username)

                        if current_pinfo is not None:
                            event = self.create_admin_event_process_end_from_pinfo(
                                p_pinfo=current_pinfo,
                                p_reference_time=p_reference_time)
                            events.append(event)

            active_hostnames = [device_info.hostname for device_info in self._device_infos.values() if device_info.is_up]

            for pinfo in self._process_infos.values():
                # If the end time of a current entry is None AND the process was started on the local host AND
                # the process is no longer running THEN send an EVENT_TYPE_PROCESS_END event!
                if pinfo.end_time is None and pinfo.hostname not in active_hostnames:
                    event = self.create_admin_event_process_end_from_pinfo(
                        p_pinfo=pinfo,
                        p_reference_time=p_reference_time)
                    events.append(event)

        return events
    def get_number_of_monitored_devices(self):

        with persistence.SessionContext(p_persistence=self._persistence) as session_context:
            return len(self._persistence.devices(session_context))
Exemplo n.º 13
0
    def users_view(self):

        with persistence.SessionContext(p_persistence=self._persistence) as session_context:
            request = flask.request
            with tools.TimingContext(lambda duration: self.measure(p_hostname=request.remote_addr,
                                                                   p_service=request.url_rule, p_duration=duration)):

                users = self._appcontrol.get_sorted_users(session_context)
                forms = self.get_users_forms(p_users=users, p_session_context=session_context)

                valid_and_submitted = True
                submitted = False

                for form in forms.values():
                    if not form.validate_on_submit():
                        valid_and_submitted = False

                    if form.is_submitted():
                        submitted = True

                if valid_and_submitted:
                    self.save_users_data(users, forms)

                    if request.form['submit'] == HTML_KEY_NEW_USER:
                        username = forms[HTML_KEY_NEW_USER].username.data
                        self._appcontrol.add_new_user(p_session_context=session_context,
                                                      p_username=username, p_locale=self._locale_helper.locale)
                        # TODO: after adding new user Users window should be opened for new user

                    else:
                        for user in users:
                            if request.form['submit'] == user.delete_html_key:
                                self._persistence.delete_user(user.username)
                                self._appcontrol.send_config_to_all_slaves()

                            elif request.form['submit'] == user.new_ruleset_html_key:
                                self._persistence.add_ruleset(user.username)

                            elif request.form['submit'] == user.new_device_html_key:
                                device_id = int(forms[user.new_device_html_key].device_id.data)
                                self._persistence.add_device(p_device_id=device_id, p_username=user.username)

                            else:
                                for ruleset in user.rulesets:
                                    if request.form['submit'] == ruleset.delete_html_key:
                                        self._persistence.delete_ruleset(p_ruleset_id=ruleset.id)

                                    elif request.form['submit'] == ruleset.move_down_html_key:
                                        self._persistence.move_down_ruleset(p_ruleset_id=ruleset.id)

                                    elif request.form['submit'] == ruleset.move_up_html_key:
                                        self._persistence.move_up_ruleset(p_ruleset_id=ruleset.id)

                                for user2device in user.devices:
                                    if request.form['submit'] == user2device.delete_html_key:
                                        self._persistence.delete_user2device(p_user2device_id=user2device.id)

                    return flask.redirect(flask.url_for("little_brother.users"))

                if not submitted:
                    for user in users:
                        forms[user.html_key].load_from_model(p_model=user)

                        for ruleset in user.rulesets:
                            forms[ruleset.html_key].load_from_model(p_model=ruleset)
                            # provide a callback function so that the RuleSet can retrieve context summaries
                            ruleset.get_context_rule_handler = self._appcontrol.get_context_rule_handler

                        for user2device in user.devices:
                            forms[user2device.html_key].load_from_model(p_model=user2device)

                return flask.render_template(
                    USERS_HTML_TEMPLATE,
                    rel_font_size=self.get_rel_font_size(),
                    users=users,
                    authentication=self.get_authenication_info(),
                    forms=forms,
                    new_user_html_key=HTML_KEY_NEW_USER,
                    new_user_submit_value=HTML_KEY_NEW_USER,
                    navigation={
                        'current_view': USERS_VIEW_NAME
                    },
                )