Example #1
0
def json_delete_instance(request):
    data = request.json
    if not data.get('agent_address'):
        raise HTTPError(400, "Agent address field is missing.")
    if not data.get('agent_port'):
        raise HTTPError(400, "Agent port field is missing.")
    delete_instance(request.db_session, **data)
    return {'delete': True}
Example #2
0
def validate_instance_data(data):
    # Submited attributes checking.
    if not data.get('new_agent_address'):
        raise HTTPError(400, "Agent address is missing.")
    check_agent_address(data['new_agent_address'])
    if 'new_agent_port' not in data or data['new_agent_port'] == '':
        raise HTTPError(400, "Agent port is missing.")
    check_agent_port(data['new_agent_port'])
    if 'agent_key' not in data:
        raise HTTPError(400, "Agent key field is missing.")
    if 'groups' not in data:
        raise HTTPError(400, "Groups field is missing.")
    if data['groups'] is not None and type(data['groups']) != list:
        raise HTTPError(400, "Invalid group list.")
Example #3
0
def send_test_email(request):
    data = request.json
    email = data.get('email', None)
    if not email:
        raise HTTPError(400, "Email field is missing.")

    notifications_conf = app.config.notifications
    smtp_host = notifications_conf.smtp_host
    smtp_port = notifications_conf.smtp_port

    if not smtp_host:
        raise HTTPError(500, "SMTP server is not configured")

    send_mail(smtp_host, smtp_port, 'temBoard notification test',
              'This is a test', email)
    return {'message': 'OK'}
Example #4
0
def state_changes(request, name):
    host_id, instance_id = get_request_ids(request)
    start, end = parse_start_end(request)
    key = request.handler.get_argument('key', default=None)
    if name not in check_specs:
        raise HTTPError(404, "Unknown check '%s'" % name)

    query = dedent("""\
    COPY (
        SELECT array_to_json(coalesce(array_agg(json_build_object(
            'datetime', f.datetime,
            'state', f.state,
            'value', f.value,
            'warning', f.warning,
            'critical', f.critical
        )), '{}')) FROM monitoring.get_state_changes(%s, %s, %s, %s, %s, %s) f
    ) TO STDOUT
    """)

    return jsonify(
        sql_json_query(
            request,
            query,
            host_id,
            instance_id,
            name,
            key,
            start,
            end,
        ))
Example #5
0
def group(request, kind, name):
    if 'GET' == request.method:
        if not name:
            raise HTTPError(404)
        group = get_group(request.db_session, name, kind)
        data = dict(
            name=group.group_name,
            kind=kind,
            description=group.group_description,
        )
        if kind == 'instance':
            data['user_groups'] = [
                dict(name=g.group_name, description=g.group_description)
                for g in get_group_list(request.db_session)
            ]
            data['in_groups'] = [a.role_group_name for a in group.ari]
        return data
    else:  # POST
        data = request.json
        if not data.get('new_group_name'):
            raise HTTPError(400, "Missing group name.")
        check_group_name(data['new_group_name'])
        if not data.get('description'):
            raise HTTPError(400, "Missing description")
        check_group_description(data['description'])

        if name:  # Group update
            group = get_group(request.db_session, name, kind)
            if 'instance' == kind:
                for ari in group.ari:
                    delete_role_group_from_instance_group(
                        request.db_session, ari.role_group_name,
                        group.group_name)

            group = update_group(request.db_session, group.group_name, kind,
                                 data['new_group_name'], data['description'])
        else:  # Group creation
            group = add_group(request.db_session, data['new_group_name'],
                              data['description'], kind)

        if 'user_groups' in data and data['user_groups']:
            for group_name in data['user_groups']:
                add_role_group_in_instance_group(request.db_session,
                                                 group_name, group.group_name)

        return {'ok': True}
Example #6
0
def send_test_sms(request):
    data = request.json
    phone = data.get('phone', None)
    if not phone:
        raise HTTPError(400, "Phone field is missing.")

    send_sms(app.config.notifications, 'temBoard notification test', [phone])
    return {'message': 'OK'}
Example #7
0
def check_agent_request(request, hostname, instance):
    key = request.headers.get('X-Key')
    if not key:
        raise HTTPError(401, 'X-Key header missing')

    if instance['available']:
        check_agent_key(request.db_session, hostname,
                        instance['data_directory'], instance['port'], key)
    else:
        # Case when PostgreSQL instance is not started.
        check_host_key(request.db_session, hostname, key)
Example #8
0
def enable_instance_plugins(db_session, instance, plugins, loaded_plugins):
    for plugin_name in plugins or []:
        # 'administration' plugin case: the plugin is not currently
        # implemented on UI side
        if plugin_name == 'administration':
            continue
        if plugin_name not in loaded_plugins:
            raise HTTPError(404, "Unknown plugin %s." % plugin_name)

        add_instance_plugin(db_session, instance.agent_address,
                            instance.agent_port, plugin_name)
Example #9
0
def parse_start_end(request):
    start = request.handler.get_argument('start', default=None)
    end = request.handler.get_argument('end', default=None)
    try:
        if start:
            start = parse_datetime.parse(start)
        if end:
            end = parse_datetime.parse(end)
    except ValueError:
        raise HTTPError(406, 'Datetime not valid.')

    return start, end
Example #10
0
def states(request, name):
    host_id, instance_id = get_request_ids(request)
    if name not in check_specs:
        raise HTTPError(404, "Unknown check '%s'" % name)

    detail = check_state_detail(request.db_session, host_id, instance_id, name)
    for d in detail:
        spec = check_specs[name]
        if 'value_type' in spec:
            d['value_type'] = spec['value_type']

    return jsonify(detail)
Example #11
0
def collector(request):
    data = request.json

    try:
        # Ignore legacy multi-instance.
        instance = data['instances'][0]
        hostinfo = data['hostinfo']
        hostname = hostinfo['hostname']
        port = instance['port']
        metrics_data = data['data']
        n_cpu = hostinfo['cpu_count']
        if 'max_connections' in instance:
            data['data']['max_connections'] = instance['max_connections']
    except (KeyError, IndexError) as e:
        logger.exception(str(e))
        raise HTTPError(409, "Not valid data")

    check_agent_request(request, hostname, instance)

    # Update the inventory
    host = merge_agent_info(request.db_session, hostinfo, instance)

    # Send the write SQL commands to the database because the metrics are
    # inserted with queries not the orm. Tables must be there.
    request.db_session.commit()

    insert_availability(
        request.db_session, host, data, logger, hostname, port)
    insert_metrics(
        request.db_session, host, metrics_data, logger, hostname, port)

    # ALERTING PART

    host_id = get_host_id(request.db_session, hostname)
    instance_id = get_instance_id(request.db_session, host_id, port)
    populate_host_checks(request.db_session, host_id, instance_id,
                         dict(n_cpu=n_cpu))
    request.db_session.commit()

    # Create new task for checking preprocessed values
    task_options = build_check_task_options(
        request, host_id, instance_id,
        get_instance_checks(request.db_session, instance_id),
    )
    request.handler.application.temboard_app.scheduler.schedule_task(
        'check_data_worker',
        options=task_options,
        expire=0,
    )

    return {'done': True}
Example #12
0
def data_metric(request, metric_name):
    key = request.handler.get_argument('key', default=None)
    host_id, instance_id = get_request_ids(request)
    start, end = parse_start_end(request)
    try:
        data = get_metric_data_csv(
            request.db_session, metric_name,
            start, end,
            host_id=host_id,
            instance_id=instance_id,
            key=key,
        )
    except IndexError:
        raise HTTPError(404, 'Unknown metric.')

    return csvify(data=data)
Example #13
0
def json_user(request, username):
    if 'GET' == request.method:
        if username is None:
            raise HTTPError(500, "Username is missing")
        role = get_role(request.db_session, username)
        groups = get_group_list(request.db_session)

        return {
            'role_name': role.role_name,
            'role_email': role.role_email,
            'role_phone': role.role_phone,
            'is_active': role.is_active,
            'is_admin': role.is_admin,
            'in_groups': [group.group_name for group in role.groups],
            'groups': [{
                'name': group.group_name,
                'description': group.group_description
            } for group in groups]
        }
    elif 'POST' == request.method:  # update
        data = request.json
        role = get_role(request.db_session, username)
        validate_user_data(data, role)

        h_passwd = handle_password(data)

        # First step is to remove user from the groups he belongs to.
        role_groups = get_groups_by_role(request.db_session,
                                         role.role_name)
        if role_groups:
            for role_group in role_groups:
                delete_role_from_group(request.db_session, role.role_name,
                                       role_group.group_name)
        role = update_role(
            request.db_session,
            role.role_name,
            data['new_username'],
            h_passwd,
            data['email'],
            data['is_active'],
            data['is_admin'],
            data['phone'])

        add_user_to_groups(request, data, role)

        return {"message": "OK"}
Example #14
0
def delete_group_handler(request, kind):
    name = request.json.get('group_name')
    if not name:
        raise HTTPError(400)
    delete_group(request.db_session, name, kind)
    return {'delete': True}
Example #15
0
def checks(request):
    host_id, instance_id = get_request_ids(request)
    if 'GET' == request.method:
        data = checks_info(request.db_session, host_id, instance_id)
        for datum in data:
            spec = check_specs[datum['name']]
            if 'value_type' in spec:
                datum['value_type'] = spec['value_type']
        return jsonify(data)
    else:
        post = tornado.escape.json_decode(request.body)
        if 'checks' not in post or type(post.get('checks')) is not list:
            raise HTTPError(400, "Post data not valid.")

        for row in post['checks']:
            if row.get('name') not in check_specs:
                raise HTTPError(404, "Unknown check '%s'" % row.get('name'))

        for row in post['checks']:
            # Find the check from its name
            check = request.db_session.query(Check).filter(
                Check.name == str(row.get('name')), Check.host_id == host_id,
                Check.instance_id == instance_id).first()
            enabled_before = check.enabled

            if u'enabled' in row:
                enabled_after = bool(row.get(u'enabled'))
                check.enabled = enabled_after
                # detect any change from enabled to disabled
                is_getting_disabled = enabled_before and not enabled_after
            if u'warning' in row:
                warning = row.get(u'warning')
                if type(warning) not in (int, float):
                    raise HTTPError(400, "Post data not valid.")
                check.warning = warning
            if u'critical' in row:
                critical = row.get(u'critical')
                if type(critical) not in (int, float):
                    raise HTTPError(400, "Post data not valid.")
                check.critical = critical
            if u'description' in row:
                check.description = row.get(u'description')

            request.db_session.merge(check)

            if is_getting_disabled:
                cs = request.db_session.query(CheckState).filter(
                    CheckState.check_id == check.check_id, )
                for i in cs:
                    i.state = str('UNDEF')
                    request.db_session.merge(i)
                    request.db_session.execute(
                        "SELECT monitoring.append_state_changes(:d, :i,"
                        ":s, :k, :v, :w, :c)", {
                            'd': datetime.utcnow(),
                            'i': check.check_id,
                            's': 'UNDEF',
                            'k': i.key,
                            'v': None,
                            'w': check.warning,
                            'c': check.critical
                        })

        request.db_session.commit()

        return {}
Example #16
0
def validate_instance_data(data):
    # Submited attributes checking.
    if not data.get('new_agent_address'):
        raise HTTPError(400, "Agent address is missing.")
    check_agent_address(data['new_agent_address'])
    if 'new_agent_port' not in data or data['new_agent_port'] == '':
        raise HTTPError(400, "Agent port is missing.")
    check_agent_port(data['new_agent_port'])
    if 'agent_key' not in data:
        raise HTTPError(400, "Agent key field is missing.")
    if 'hostname' not in data:
        raise HTTPError(400, "Hostname field is missing.")
    if 'cpu' not in data:
        raise HTTPError(400, "CPU field is missing.")
    if 'memory_size' not in data:
        raise HTTPError(400, "Memory size field is missing.")
    if 'pg_port' not in data:
        raise HTTPError(400, "PostgreSQL port field is missing.")
    if 'pg_version' not in data:
        raise HTTPError(400, "PostgreSQL version field is missing.")
    if 'pg_data' not in data:
        raise HTTPError(400, "PostgreSQL data directory field is missing.")
    if 'groups' not in data:
        raise HTTPError(400, "Groups field is missing.")
    if data['groups'] is not None and type(data['groups']) != list:
        raise HTTPError(400, "Invalid group list.")
Example #17
0
def delete_user(request):
    data = request.json
    if not data.get('username'):
        raise HTTPError(400, "Username field is missing.")
    delete_role(request.db_session, data['username'])
    return {'delete': True}