Пример #1
0
def main():
    session = sessionmaker(bind=engine_sakuya_db)()

    for row in session.query(Charts):
        if 'spider' in row.name:
            ext = row.get_ext()
            if 'rule' in ext:
                storm.set_rule(row.id, json.dumps(ext['rule']))
                print row.id, row.name
Пример #2
0
def add_chart(cate_id, title, rule_type, host, api):

    if session.query(Charts).filter_by(name=title).count():
        print title, 'exists'
        return

    # make rule
    rule = {
        'datasource':
        'access_log',
        'logging':
        False,
        'filters': [['host', 'equals', False, host],
                    ['request_url', 'regex', False,
                     '.*?%s.*' % api]]
    }
    if rule_type == 'count':
        rule['rule_type'] = 'count'
        rule['field'] = None
    elif rule_type == 'ninety':
        rule['rule_type'] = 'ninety'
        rule['field'] = 'upstream_response_time'
    else:
        assert False

    # make warn
    warn = [["00:00", "08:59", "RANGE", "200", "400", "30", "50", "60", "5"],
            ["09:00", "20:59", "RANGE", "200", "400", "30", "50", "5", "2"],
            ["21:00", "23:59", "RANGE", "200", "400", "30", "50", "60", "5"]]

    # add chart
    row = Charts()
    row.name = title
    row.cate_id = cate_id
    row.owner = 'mingshi'
    row.ext_info = json.dumps({'rule': rule})
    row.alert_enable = 0
    row.createtime = datetime.datetime.now()
    session.add(row)
    session.flush()
    rowid = row.id

    # add follow
    follow = Follows()
    follow.cid = rowid
    follow.follower = 19
    follow.recv_warning = True
    session.add(follow)

    session.commit()

    # storm
    storm.set_rule(rowid, json.dumps(rule))

    print title, 'added'
Пример #3
0
def add_chart(cate_id, title, rule_type, host, api):

    if session.query(Charts).filter_by(name=title).count():
        print title, "exists"
        return

    # make rule
    rule = {
        "datasource": "access_log",
        "logging": False,
        "filters": [["host", "equals", False, host], ["request_url", "regex", False, ".*?%s.*" % api]],
    }
    if rule_type == "count":
        rule["rule_type"] = "count"
        rule["field"] = None
    elif rule_type == "ninety":
        rule["rule_type"] = "ninety"
        rule["field"] = "upstream_response_time"
    else:
        assert False

    # make warn
    warn = [
        ["00:00", "08:59", "RANGE", "200", "400", "30", "50", "60", "5"],
        ["09:00", "20:59", "RANGE", "200", "400", "30", "50", "5", "2"],
        ["21:00", "23:59", "RANGE", "200", "400", "30", "50", "60", "5"],
    ]

    # add chart
    row = Charts()
    row.name = title
    row.cate_id = cate_id
    row.owner = "mingshi"
    row.ext_info = json.dumps({"rule": rule})
    row.alert_enable = 0
    row.createtime = datetime.datetime.now()
    session.add(row)
    session.flush()
    rowid = row.id

    # add follow
    follow = Follows()
    follow.cid = rowid
    follow.follower = 19
    follow.recv_warning = True
    session.add(follow)

    session.commit()

    # storm
    storm.set_rule(rowid, json.dumps(rule))

    print title, "added"
Пример #4
0
def main():
    session = sessionmaker(bind=engine_sakuya_db)()

    for row in session.query(Charts).filter_by(cate_id=200):

        ext = row.get_ext()

        if 'rule' not in ext:
            print chart.id, 'rule not found'
            continue

        if ext['rule']['field'] == 'upstream_response_time':
            ext['rule']['field'] = 'request_time'

        for filter in ext['rule']['filters']:
            if filter[0] == 'upstream_response_time':
                filter[0] = 'request_time'

        row.ext_info = json.dumps(ext)
        session.commit()
        storm.set_rule(row.id, json.dumps(ext['rule']))
        print row.id, 'updated'
Пример #5
0
def main():
    session = sessionmaker(bind=engine_sakuya_db)()

    for row in session.query(Charts).filter_by(cate_id=200):

        ext = row.get_ext()

        if 'rule' not in ext:
            print chart.id, 'rule not found'
            continue

        if ext['rule']['field'] == 'upstream_response_time':
            ext['rule']['field'] = 'request_time'

        for filter in ext['rule']['filters']:
            if filter[0] == 'upstream_response_time':
                filter[0] = 'request_time'

        row.ext_info = json.dumps(ext)
        session.commit()
        storm.set_rule(row.id, json.dumps(ext['rule']))
        print row.id, 'updated'
Пример #6
0
def add(sakuya_db):

    rule = None
    warn = None

    def error(msg):
        rt = _rt(sakuya_db)
        rt['forms'] = request.forms
        rt['forms']['rule'] = dump_rule(rule)
        rt['forms']['warn'] = dump_warn(warn)
        rt['error_msg'] = msg
        rt['warn_only'] = False
        rt['is_haopan'] = False
        return rt

    try:
        use_storm = request.forms['use_storm'] == '1'
        if use_storm:
            rule = make_rule()
            if not rule['datasource'] or not rule['filters']:
                raise ValueError

        alert_enable = request.forms['alert_enable'] == '1'
        warn = make_warn()
        if alert_enable and not warn:
            raise ValueError

        name = request.forms['name']
        if not name:
            raise ValueError

        cid = int(request.forms['cid'])
        if not sakuya_db.\
               query(Categories).\
               filter_by(id=cid, is_parent=False).\
               count():
            raise ValueError

        owner = request.forms['owner']
        if not sakuya_db.\
               query(Users).\
               filter_by(username=owner).\
               count():
            raise ValueError

    except Exception:
        return error('参数错误。')

    # add chart
    try:
        row = Charts()
        row.name = name
        row.owner = owner
        row.cate_id = cid
        row.root_category = get_parent_category(sakuya_db, cid)
        row.ds_id = 0
        row.ds_tbl_name = ''
        row.ext_info = ''
        row.followers = 0
        row.createtime = datetime.datetime.now()
        row.alert_enable = int(alert_enable)

        ext = {}
        if use_storm:
            ext['rule'] = rule
        row.ext_info = json.dumps(ext)

        sakuya_db.add(row)
        sakuya_db.flush()
        rowid = row.id
        sakuya_db.commit()

    except Exception:
        traceback.print_exc()
        sakuya_db.rollback()
        return error('图表添加失败。')

    process_warn_rules(sakuya_db, rowid, warn)

    if use_storm:
        storm.set_rule(rowid, json.dumps(rule))
        redirect('/admin/category/%d' % cid)
    else:
        redirect('/admin/chart/success/%d' % rowid)
Пример #7
0
def update(sakuya_db, id):

    row = sakuya_db.query(Charts).get(id)
    if row is None:
        abort(404)

    is_haopan = haopan.is_haopan(row.cate_id)

    if auth.is_role('sa'):
        warn_only = False
    elif auth.is_role('haopan') and is_haopan:
        warn_only = True
    elif auth.get_user()['username'] == row.owner:
        warn_only = True
    else:
        abort(403)

    rule = None
    warn = None

    def error(msg):
        rt = _rt(sakuya_db)
        rt['id'] = id
        rt['editing'] = True
        rt['warn_only'] = warn_only
        rt['is_haopan'] = is_haopan
        rt['forms'] = request.forms
        rt['forms']['rule'] = dump_rule(rule)
        rt['forms']['warn'] = dump_warn(warn)
        rt['error_msg'] = msg
        return rt

    try:

        alert_enable = request.forms['alert_enable'] == '1'
        warn = make_warn()
        if alert_enable and not warn:
            raise ValueError

        if not warn_only and not is_haopan:

            use_storm = request.forms['use_storm'] == '1'
            if use_storm:
                rule = make_rule()
                if not rule['datasource'] or not rule['filters']:
                    raise ValueError

            name = request.forms['name']
            if not name:
                raise ValueError

            cid = int(request.forms['cid'])
            if not sakuya_db.\
                   query(Categories).\
                   filter_by(id=cid, is_parent=False).\
                   count():
                raise ValueError

            owner = request.forms['owner']
            if not sakuya_db.\
                   query(Users).\
                   filter_by(username=owner).\
                   count():
                raise ValueError

    except Exception:
        return error('参数错误。')

    try:
        row.alert_enable = int(alert_enable)
        row.warnings = 0
        row.criticals = 0
        process_warn_rules(sakuya_db, id, warn)

        if not warn_only and not is_haopan:
            row.name = name
            row.cate_id = cid
            row.root_category = get_parent_category(sakuya_db, cid)
            row.owner = owner

            ext = row.get_ext()
            if use_storm:
                ext['rule'] = rule
                storm.set_rule(id, json.dumps(rule))
            else:
                ext.pop('rule', None)
                storm.delete_rule(id)
            row.ext_info = json.dumps(ext)

        sakuya_db.commit()

    except Exception:
        traceback.print_exc()
        sakuya_db.rollback()
        return error('图表更新失败。')

    if warn_only or is_haopan:
        redirect('/chart/%d' % id)
    redirect('/admin/category/%d' % cid)
Пример #8
0
def add(sakuya_db):

    rule = None
    warn = None

    def error(msg):
        rt = _rt(sakuya_db)
        rt['forms'] = request.forms
        rt['forms']['rule'] = dump_rule(rule)
        rt['forms']['warn'] = dump_warn(warn)
        rt['error_msg'] = msg
        rt['warn_only'] = False
        rt['is_haopan'] = False
        return rt

    try:
        use_storm = request.forms['use_storm'] == '1'
        if use_storm:
            rule = make_rule()
            if not rule['datasource'] or not rule['filters']:
                raise ValueError

        alert_enable = request.forms['alert_enable'] == '1'
        warn = make_warn()
        if alert_enable and not warn:
            raise ValueError

        name = request.forms['name']
        if not name:
            raise ValueError

        cid = int(request.forms['cid'])
        if not sakuya_db.\
               query(Categories).\
               filter_by(id=cid, is_parent=False).\
               count():
            raise ValueError

        owner = request.forms['owner']
        if not sakuya_db.\
               query(Users).\
               filter_by(username=owner).\
               count():
            raise ValueError

    except Exception:
        return error('参数错误。')

    # add chart
    try:
        row = Charts()
        row.name = name
        row.owner = owner
        row.cate_id = cid
        row.root_category = get_parent_category(sakuya_db, cid)
        row.ds_id = 0
        row.ds_tbl_name = ''
        row.ext_info = ''
        row.followers = 0
        row.createtime = datetime.datetime.now()
        row.alert_enable = int(alert_enable)

        ext = {}
        if use_storm:
            ext['rule'] = rule
        row.ext_info = json.dumps(ext)

        sakuya_db.add(row)
        sakuya_db.flush()
        rowid = row.id
        sakuya_db.commit()

    except Exception:
        traceback.print_exc()
        sakuya_db.rollback()
        return error('图表添加失败。')

    process_warn_rules(sakuya_db, rowid, warn)

    if use_storm:
        storm.set_rule(rowid, json.dumps(rule))
        redirect('/admin/category/%d' % cid)
    else:
        redirect('/admin/chart/success/%d' % rowid)
Пример #9
0
def update(sakuya_db, id):

    row = sakuya_db.query(Charts).get(id)
    if row is None:
        abort(404)

    is_haopan = haopan.is_haopan(row.cate_id)

    if auth.is_role('sa'):
        warn_only = False
    elif auth.is_role('haopan') and is_haopan:
        warn_only = True
    elif auth.get_user()['username'] == row.owner:
        warn_only = True
    else:
        abort(403)

    rule = None
    warn = None

    def error(msg):
        rt = _rt(sakuya_db)
        rt['id'] = id
        rt['editing'] = True
        rt['warn_only'] = warn_only
        rt['is_haopan'] = is_haopan
        rt['forms'] = request.forms
        rt['forms']['rule'] = dump_rule(rule)
        rt['forms']['warn'] = dump_warn(warn)
        rt['error_msg'] = msg
        return rt

    try:

        alert_enable = request.forms['alert_enable'] == '1'
        warn = make_warn()
        if alert_enable and not warn:
            raise ValueError

        if not warn_only and not is_haopan:

            use_storm = request.forms['use_storm'] == '1'
            if use_storm:
                rule = make_rule()
                if not rule['datasource'] or not rule['filters']:
                    raise ValueError

            name = request.forms['name']
            if not name:
                raise ValueError

            cid = int(request.forms['cid'])
            if not sakuya_db.\
                   query(Categories).\
                   filter_by(id=cid, is_parent=False).\
                   count():
                raise ValueError

            owner = request.forms['owner']
            if not sakuya_db.\
                   query(Users).\
                   filter_by(username=owner).\
                   count():
                raise ValueError

    except Exception:
        return error('参数错误。')

    try:
        row.alert_enable = int(alert_enable)
        row.warnings = 0
        row.criticals = 0
        process_warn_rules(sakuya_db, id, warn)

        if not warn_only and not is_haopan:
            row.name = name
            row.cate_id = cid
            row.root_category = get_parent_category(sakuya_db, cid)
            row.owner = owner

            ext = row.get_ext()
            if use_storm:
                ext['rule'] = rule
                storm.set_rule(id, json.dumps(rule))
            else:
                ext.pop('rule', None)
                storm.delete_rule(id)
            row.ext_info = json.dumps(ext)

        sakuya_db.commit()

    except Exception:
        traceback.print_exc()
        sakuya_db.rollback()
        return error('图表更新失败。')

    if warn_only or is_haopan:
        redirect('/chart/%d' % id)
    redirect('/admin/category/%d' % cid)
Пример #10
0
        rule['filters'].append([filter[0], filter[1], False, filter[2]])

    # add to database
    row = Charts()
    row.name = title
    row.owner = '系统'
    row.cate_id = SOLR_CATE_ID
    row.ext_info = json.dumps({'rule': rule})
    row.createtime = datetime.datetime.now()
    sakuya_db.add(row)
    sakuya_db.flush()
    id = row.id
    sakuya_db.commit()

    # set storm
    storm.set_rule(id, json.dumps(rule))

    # return id
    return util.output('ok', id=id)

@app.get('/api/solr/delete/<id:int>')
def solr_delete(sakuya_db, id):

    row = sakuya_db.query(Charts).filter_by(id=id, cate_id=SOLR_CATE_ID).first()
    if row is None:
        return util.output('error', msg='Chart not found.')

    sakuya_db.delete(row)
    sakuya_db.commit()

    storm.delete_rule(id)
Пример #11
0
        rule['filters'].append([filter[0], filter[1], False, filter[2]])

    # add to database
    row = Charts()
    row.name = title
    row.owner = '系统'
    row.cate_id = SOLR_CATE_ID
    row.ext_info = json.dumps({'rule': rule})
    row.createtime = datetime.datetime.now()
    sakuya_db.add(row)
    sakuya_db.flush()
    id = row.id
    sakuya_db.commit()

    # set storm
    storm.set_rule(id, json.dumps(rule))

    # return id
    return util.output('ok', id=id)


@app.get('/api/solr/delete/<id:int>')
def solr_delete(sakuya_db, id):

    row = sakuya_db.query(Charts).filter_by(id=id,
                                            cate_id=SOLR_CATE_ID).first()
    if row is None:
        return util.output('error', msg='Chart not found.')

    sakuya_db.delete(row)
    sakuya_db.commit()