예제 #1
0
def get_history():

    try:
        query, _, _, _, _, limit, query_time = parse_fields(request.args)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 400

    try:
        history = db.get_history(query=query, limit=limit)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    for alert in history:
        alert['href'] = absolute_url('/alert/' + alert['id'])

    if len(history) > 0:
        return jsonify(
            status="ok",
            history=history,
            lastTime=history[-1]['updateTime']
        )
    else:
        return jsonify(
            status="ok",
            message="not found",
            history=[],
            lastTIme=query_time
        )
예제 #2
0
def get_counts():

    try:
        query, _, _, _, _, _, _ = parse_fields(request.args)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 400

    try:
        severity_count = db.get_counts(query=query,
                                       fields={"severity": 1},
                                       group="severity")
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    try:
        status_count = db.get_counts(query=query,
                                     fields={"status": 1},
                                     group="status")
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    if sum(severity_count.values()):
        return jsonify(status="ok",
                       total=sum(severity_count.values()),
                       severityCounts=severity_count,
                       statusCounts=status_count)
    else:
        return jsonify(status="ok",
                       message="not found",
                       total=0,
                       severityCounts=severity_count,
                       statusCounts=status_count)
예제 #3
0
def get_top10_flapping():

    try:
        query, _, _, group, _, _, _ = parse_fields(request.args)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 400

    try:
        top10 = db.get_topn_flapping(query=query, group=group, limit=10)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    for item in top10:
        for resource in item['resources']:
            resource['href'] = absolute_url('/alert/' + resource['id'])

    if top10:
        return jsonify(status="ok", total=len(top10), top10=top10)
    else:
        return jsonify(
            status="ok",
            message="not found",
            total=0,
            top10=[],
        )
예제 #4
0
파일: views.py 프로젝트: wangbodong/alerta
def get_history():

    try:
        query, _, _, _, _, limit, query_time = parse_fields(request.args)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 400

    try:
        history = db.get_history(query=query, limit=limit)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    for alert in history:
        alert['href'] = absolute_url('/alert/' + alert['id'])

    if len(history) > 0:
        return jsonify(
            status="ok",
            history=history,
            lastTime=history[-1]['updateTime']
        )
    else:
        return jsonify(
            status="ok",
            message="not found",
            history=[],
            lastTIme=query_time
        )
예제 #5
0
파일: views.py 프로젝트: yekeqiang/alerta
def get_top10():

    try:
        query, _, group, _, _ = parse_fields(request)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 400

    try:
        top10 = db.get_topn(query=query, group=group, limit=10)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    for item in top10:
        for resource in item['resources']:
            resource['href'] = "%s/%s" % (request.base_url.replace('alerts/top10', 'alert'), resource['id'])

    if top10:
        return jsonify(
            status="ok",
            total=len(top10),
            top10=top10
        )
    else:
        return jsonify(
            status="ok",
            message="not found",
            total=0,
            top10=[],
        )
예제 #6
0
파일: views.py 프로젝트: yekeqiang/alerta
def get_counts():

    try:
        query, _, _, _, _ = parse_fields(request)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 400

    try:
        severity_count = db.get_counts(query=query, fields={"severity": 1}, group="severity")
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    try:
        status_count = db.get_counts(query=query, fields={"status": 1}, group="status")
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    if sum(severity_count.values()):
        return jsonify(
            status="ok",
            total=sum(severity_count.values()),
            severityCounts=severity_count,
            statusCounts=status_count
        )
    else:
        return jsonify(
            status="ok",
            message="not found",
            total=0,
            severityCounts=severity_count,
            statusCounts=status_count
        )
예제 #7
0
def get_environments(tenant):

    tenant = generateDBName(tenant)

    try:
        query, _, _, _, limit, _ = parse_fields(request)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 400

    try:
        environments = db.get_environments(tenant, query=query, limit=limit)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    if environments:
        return jsonify(
            status="ok",
            total=len(environments),
            environments=environments
        )
    else:
        return jsonify(
            status="ok",
            message="not found",
            total=0,
            environments=[],
        )
예제 #8
0
def get_services(tenant):

    tenant = generateDBName(tenant)

    try:
        query, _, _, _, limit, _ = parse_fields(request)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 400

    try:
        services = db.get_services(tenant, query=query, limit=limit)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    if services:
        return jsonify(
            status="ok",
            total=len(services),
            services=services
        )
    else:
        return jsonify(
            status="ok",
            message="not found",
            total=0,
            services=[],
        )
예제 #9
0
def get_top10(tenant):

    tenant = generateDBName(tenant)

    try:
        query, _, group, _, _, _ = parse_fields(request)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 400

    try:
        top10 = db.get_topn(tenant, query=query, group=group, limit=10)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    for item in top10:
        for resource in item['resources']:
            resource['href'] = "%s/%s" % (request.base_url.replace('alerts/top10', 'alert'), resource['id'])

    if top10:
        return jsonify(
            status="ok",
            total=len(top10),
            top10=top10
        )
    else:
        return jsonify(
            status="ok",
            message="not found",
            total=0,
            top10=[],
        )
예제 #10
0
def get_history(tenant):

    tenant = generateDBName(tenant)

    try:
        query, _, _, _, limit, query_time = parse_fields(request)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 400

    try:
        history = db.get_history(tenant, query=query, limit=limit)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    for alert in history:
        alert['href'] = "%s/%s" % (request.base_url.replace('alerts/history', 'alert'), alert['id'])

    if len(history) > 0:
        return jsonify(
            status="ok",
            history=history,
            lastTime=history[-1]['updateTime']
        )
    else:
        return jsonify(
            status="ok",
            message="not found",
            history=[],
            lastTIme=query_time
        )
예제 #11
0
파일: views.py 프로젝트: yekeqiang/alerta
def get_environments():

    try:
        query, _, _, limit, _ = parse_fields(request)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 400

    try:
        environments = db.get_environments(query=query, limit=limit)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    if environments:
        return jsonify(
            status="ok",
            total=len(environments),
            environments=environments
        )
    else:
        return jsonify(
            status="ok",
            message="not found",
            total=0,
            environments=[],
        )
예제 #12
0
def get_services():

    try:
        query, _, _, _, _, limit, _ = parse_fields(request.args)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 400

    try:
        services = db.get_services(query=query, limit=limit)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    if services:
        return jsonify(
            status="ok",
            total=len(services),
            services=services
        )
    else:
        return jsonify(
            status="ok",
            message="not found",
            total=0,
            services=[],
        )
예제 #13
0
def get_top10_flapping():

    try:
        query, _, _, group, _, _, _ = parse_fields(request.args)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 400

    try:
        top10 = db.get_topn_flapping(query=query, group=group, limit=10)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    for item in top10:
        for resource in item['resources']:
            resource['href'] = absolute_url('/alert/' + resource['id'])

    if top10:
        return jsonify(
            status="ok",
            total=len(top10),
            top10=top10
        )
    else:
        return jsonify(
            status="ok",
            message="not found",
            total=0,
            top10=[],
        )
예제 #14
0
def get_alerts():

    gets_started = gets_timer.start_timer()
    try:
        query, sort, _, limit, query_time = parse_fields(request)
    except Exception, e:
        gets_timer.stop_timer(gets_started)
        return jsonify(status="error", message=str(e)), 400
예제 #15
0
파일: views.py 프로젝트: yekeqiang/alerta
def get_alerts():

    gets_started = gets_timer.start_timer()
    try:
        query, sort, _, limit, query_time = parse_fields(request)
    except Exception, e:
        gets_timer.stop_timer(gets_started)
        return jsonify(status="error", message=str(e)), 400
예제 #16
0
def get_services():

    try:
        query, _, _, limit, _ = parse_fields(request)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 400

    try:
        services = db.get_services(query=query, limit=limit)
    except Exception, e:
        return jsonify(status="error", message=str(e)), 500
예제 #17
0
파일: views.py 프로젝트: yekeqiang/alerta
def get_services():

    try:
        query, _, _, limit, _ = parse_fields(request)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 400

    try:
        services = db.get_services(query=query, limit=limit)
    except Exception, e:
        return jsonify(status="error", message=str(e)), 500
예제 #18
0
파일: views.py 프로젝트: wangbodong/alerta
def oembed(format):

    oembed_started = oembed_timer.start_timer()

    if format != 'json':
        oembed_timer.stop_timer(oembed_started)
        return jsonify(status="error", message="unsupported format: %s" % format), 400

    if 'url' not in request.args or 'maxwidth' not in request.args \
            or 'maxheight' not in request.args:
        oembed_timer.stop_timer(oembed_started)
        return jsonify(status="error", message="missing default parameters: url, maxwidth, maxheight"), 400

    try:
        url = request.args['url']
        width = int(request.args['maxwidth'])
        height = int(request.args['maxheight'])
        title = request.args.get('title', 'Alerts')
    except Exception as e:
        oembed_timer.stop_timer(oembed_started)
        return jsonify(status="error", message=str(e)), 400

    try:
        o = urlparse(url)
        params = ImmutableMultiDict(urls.url_decode(o.query))
        query, _, _, _, _, _, _ = parse_fields(params)
    except Exception as e:
        oembed_timer.stop_timer(oembed_started)
        return jsonify(status="error", message=str(e)), 500

    if o.path.endswith('/alerts/count'):
        try:
            severity_count = db.get_counts(query=query, fields={"severity": 1}, group="severity")
        except Exception as e:
            return jsonify(status="error", message=str(e)), 500

        max = 'normal'
        if severity_count.get('warning', 0) > 0:
            max = 'warning'
        if severity_count.get('minor', 0) > 0:
            max = 'minor'
        if severity_count.get('major', 0) > 0:
            max = 'major'
        if severity_count.get('critical', 0) > 0:
            max = 'critical'

        html = render_template(
            'oembed/counts.html',
            title=title,
            width=width,
            height=height,
            max=max,
            counts=severity_count
        )
        oembed_timer.stop_timer(oembed_started)
        return jsonify(version="1.0", type="rich", width=width, height=height, title=title,  provider_name="Alerta", provider_url=request.url_root, html=html)

    elif o.path.endswith('/alerts/top10/count'):
        # TODO: support top10 oembed widget
        pass
    else:
        oembed_timer.stop_timer(oembed_started)
        return jsonify(status="error", message="unsupported oEmbed URL scheme"), 400
예제 #19
0
def get_counts():

    try:
        query, _, _, query_time = parse_fields(request)
    except Exception, e:
        return jsonify(response={"status": "error", "message": str(e)})
예제 #20
0
def get_alerts():

    try:
        query, sort, limit, query_time = parse_fields(request)
    except Exception, e:
        return jsonify(status="error", message=str(e))
예제 #21
0
파일: views.py 프로젝트: yekeqiang/alerta
def get_history():

    try:
        query, _, _, limit, query_time = parse_fields(request)
    except Exception, e:
        return jsonify(status="error", message=str(e)), 400
예제 #22
0
def get_alerts():

    gets_started = gets_timer.start_timer()
    try:
        query, fields, sort, _, page, limit, query_time = parse_fields(request.args)
    except Exception as e:
        gets_timer.stop_timer(gets_started)
        return jsonify(status="error", message=str(e)), 400

    try:
        severity_count = db.get_counts(query=query, fields={"severity": 1}, group="severity")
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    try:
        status_count = db.get_counts(query=query, fields={"status": 1}, group="status")
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    if limit < 1:
        return jsonify(status="error", message="page 'limit' of %s is not valid" % limit), 416

    total = sum(severity_count.values())
    pages = ((total - 1) // limit) + 1

    if total and page > pages or page < 0:
        return jsonify(status="error", message="page out of range: 1-%s" % pages), 416

    if 'history' not in fields:
        fields['history'] = {'$slice': app.config['HISTORY_LIMIT']}

    try:
        alerts = db.get_alerts(query=query, fields=fields, sort=sort, page=page, limit=limit)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    alert_response = list()
    if len(alerts) > 0:

        last_time = None

        for alert in alerts:
            body = alert.get_body()
            body['href'] = absolute_url('/alert/' + alert.id)

            if not last_time:
                last_time = body['lastReceiveTime']
            elif body['lastReceiveTime'] > last_time:
                last_time = body['lastReceiveTime']

            alert_response.append(body)

        gets_timer.stop_timer(gets_started)
        return jsonify(
            status="ok",
            total=total,
            page=page,
            pageSize=limit,
            pages=pages,
            more=page < pages,
            alerts=alert_response,
            severityCounts=severity_count,
            statusCounts=status_count,
            lastTime=last_time,
            autoRefresh=Switch.get('auto-refresh-allow').is_on(),
        )
    else:
        gets_timer.stop_timer(gets_started)
        return jsonify(
            status="ok",
            message="not found",
            total=total,
            page=page,
            pageSize=limit,
            pages=pages,
            more=False,
            alerts=[],
            severityCounts=severity_count,
            statusCounts=status_count,
            lastTime=query_time,
            autoRefresh=Switch.get('auto-refresh-allow').is_on()
        )
예제 #23
0
def get_alerts():

    gets_started = gets_timer.start_timer()
    try:
        query, fields, sort, _, page, limit, query_time = parse_fields(
            request.args)
    except Exception as e:
        gets_timer.stop_timer(gets_started)
        return jsonify(status="error", message=str(e)), 400

    try:
        severity_count = db.get_counts(query=query,
                                       fields={"severity": 1},
                                       group="severity")
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    try:
        status_count = db.get_counts(query=query,
                                     fields={"status": 1},
                                     group="status")
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    if limit < 1:
        return jsonify(status="error",
                       message="page 'limit' of %s is not valid" % limit), 416

    total = sum(severity_count.values())
    pages = ((total - 1) // limit) + 1

    if total and page > pages or page < 0:
        return jsonify(status="error",
                       message="page out of range: 1-%s" % pages), 416

    if 'history' not in fields:
        fields['history'] = {'$slice': app.config['HISTORY_LIMIT']}

    try:
        alerts = db.get_alerts(query=query,
                               fields=fields,
                               sort=sort,
                               page=page,
                               limit=limit)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    alert_response = list()
    if len(alerts) > 0:

        last_time = None

        for alert in alerts:
            body = alert.get_body()
            body['href'] = absolute_url('/alert/' + alert.id)

            if not last_time:
                last_time = body['lastReceiveTime']
            elif body['lastReceiveTime'] > last_time:
                last_time = body['lastReceiveTime']

            alert_response.append(body)

        gets_timer.stop_timer(gets_started)
        return jsonify(
            status="ok",
            total=total,
            page=page,
            pageSize=limit,
            pages=pages,
            more=page < pages,
            alerts=alert_response,
            severityCounts=severity_count,
            statusCounts=status_count,
            lastTime=last_time,
            autoRefresh=Switch.get('auto-refresh-allow').is_on(),
        )
    else:
        gets_timer.stop_timer(gets_started)
        return jsonify(status="ok",
                       message="not found",
                       total=total,
                       page=page,
                       pageSize=limit,
                       pages=pages,
                       more=False,
                       alerts=[],
                       severityCounts=severity_count,
                       statusCounts=status_count,
                       lastTime=query_time,
                       autoRefresh=Switch.get('auto-refresh-allow').is_on())
예제 #24
0
def get_history():

    try:
        query, _, _, limit, query_time = parse_fields(request)
    except Exception, e:
        return jsonify(status="error", message=str(e)), 400