Ejemplo n.º 1
0
def home(request):
    role = request.current_user
    instances = get_instances_by_role_name(request.db_session, role.role_name)
    groups = get_instance_groups_by_role(request.db_session, role.role_name)
    return render_template(
        'home.html',
        nav=True,
        role=role,
        instance_list=instances,
        groups=groups,
    )
Ejemplo n.º 2
0
 def get_home(self):
     self.logger.info("Loading home.")
     self.setUp()
     role = self.current_user
     if not role:
         raise TemboardUIError(302, 'Current role unknown.')
     instance_list = get_instances_by_role_name(self.db_session,
                                                role.role_name)
     self.tearDown(commit=False)
     self.logger.info("Done.")
     return HTMLAsyncResult(http_code=200,
                            template_file='home.html',
                            data={
                                'nav': True,
                                'role': role,
                                'instance_list': instance_list
                            })
Ejemplo n.º 3
0
 def get_home(self):
     try:
         self.logger.info("Loading home.")
         self.load_auth_cookie()
         self.start_db_session()
         role = self.current_user
         if not role:
             raise TemboardUIError(302, 'Current role unknown.')
         instance_list = get_instances_by_role_name(self.db_session,
                                                    role.role_name)
         self.db_session.expunge_all()
         self.db_session.commit()
         self.db_session.close()
         self.logger.info("Done.")
         return HTMLAsyncResult(http_code=200,
                                template_file='home.html',
                                data={
                                    'nav': True,
                                    'role': role,
                                    'instance_list': instance_list
                                })
     except (TemboardUIError, Exception) as e:
         self.logger.traceback(get_tb())
         self.logger.error(str(e))
         self.logger.info("Failed.")
         try:
             self.db_session.expunge_all()
             self.db_session.rollback()
             self.db_session.close()
         except Exception:
             pass
         if isinstance(e, TemboardUIError):
             if e.code == 302:
                 return HTMLAsyncResult(302, '/login')
             elif e.code == 401:
                 return HTMLAsyncResult(401,
                                        None, {'nav': False},
                                        template_file='unauthorized.html')
         return HTMLAsyncResult(500,
                                None, {
                                    'nav': False,
                                    'error': e.message
                                },
                                template_file='error.html')
Ejemplo n.º 4
0
def home_instances(request):
    role = request.current_user

    # get the instances for which current user has access to
    instances = get_instances_by_role_name(request.db_session, role.role_name)
    instances = [{
        'hostname':
        instance.hostname,
        'agent_address':
        instance.agent_address,
        'agent_port':
        instance.agent_port,
        'pg_data':
        instance.pg_data,
        'pg_port':
        instance.pg_port,
        'pg_version':
        instance.pg_version,
        'pg_version_summary':
        instance.pg_version_summary,
        'groups': [group.group_name for group in instance.groups],
        'plugins': [plugin.plugin_name for plugin in instance.plugins],
    } for instance in instances]

    # Get availability for all monitored instances
    cur = request.db_session.connection().connection.cursor()
    cur.execute("SET search_path TO monitoring")
    sql = """
        SELECT
          distinct(ia.instance_id),
          i.port,
          h.hostname,
          first_value(available) OVER
            (PARTITION BY ia.instance_id ORDER BY datetime desc) AS available
        FROM monitoring.instance_availability AS ia
        JOIN monitoring.instances AS i
        ON i.instance_id = ia.instance_id
        JOIN monitoring.hosts AS h
        ON i.host_id = h.host_id;
    """
    all_availability = request.db_session.execute(sql, {}).fetchall()
    all_availability = {(i.hostname, i.port): i.available
                        for i in all_availability}

    # set instances availability if known
    for instance in instances:
        try:
            available = all_availability.get(
                (instance['hostname'], instance['pg_port']))
        except KeyError:
            # Instance may not be monitored yet
            # because it has been added recently or monitor plugin is not
            # activated
            available = None
        instance['available'] = available

    sql = """
        WITH states_by_key AS (
            SELECT
                check_id,
                json_agg(json_build_object('key', key, 'state', state))
                  as state_by_key
            FROM monitoring.check_states
            GROUP BY check_id
        )
        SELECT h.hostname, i.port,
        json_agg(
            json_build_object(
                'name', c.name,
                'warning', c.warning,
                'critical', c.critical,
                'description', c.description,
                'enabled', c.enabled,
                'state_by_key', states_by_key.state_by_key
        )) AS checks
        FROM monitoring.checks c
        JOIN states_by_key ON c.check_id = states_by_key.check_id
        JOIN monitoring.instances i
          ON c.host_id = i.host_id AND c.instance_id = i.instance_id
        JOIN monitoring.hosts h ON h.host_id = i.host_id
        GROUP BY 1,2;
    """
    all_checks = request.db_session.execute(sql, {}).fetchall()
    all_checks = {(i.hostname, i.port): [{
        'name':
        check['name'],
        'state':
        get_highest_state([s['state'] for s in check['state_by_key']]),
        'description':
        check['description']
    } for check in i.checks]
                  for i in all_checks}

    # set instances checks if any
    for instance in instances:
        try:
            checks = all_checks[(instance['hostname'], instance['pg_port'])]
        except KeyError:
            # Instance may not be monitored yet
            # because it has been added recently or monitor plugin is not
            # activated
            checks = []
        instance['checks'] = checks

    return jsonify(instances)