Exemplo n.º 1
0
def strategy_update_post():
    sid = request.form['sid'].strip()
    metric = request.form['metric'].strip()
    tags = request.form['tags'].strip()
    max_step = request.form['max_step'].strip()
    priority = request.form['priority'].strip()
    note = request.form['note'].strip()
    func = request.form['func'].strip()
    op = request.form['op'].strip()
    right_value = request.form['right_value'].strip()
    run_begin = request.form['run_begin'].strip()
    run_end = request.form['run_end'].strip()
    tpl_id = request.form['tpl_id'].strip()

    if not metric:
        return jsonify(msg='metric is blank')

    if not note:
        return jsonify(msg='note is blank')

    if metric == 'net.port.listen' and '=' not in tags:
        return jsonify(msg='if metric is net.port.listen, tags should like port=22')

    if sid:
        # update
        Strategy.update_dict(
            {
                'metric': metric,
                'tags': tags,
                'max_step': max_step,
                'priority': priority,
                'func': func,
                'op': op,
                'right_value': right_value,
                'note': note,
                'run_begin': run_begin,
                'run_end': run_end
            },
            'id=%s',
            [sid]
        )
        return jsonify(msg='')

    # insert
    Strategy.insert(
        {
            'metric': metric,
            'tags': tags,
            'max_step': max_step,
            'priority': priority,
            'func': func,
            'op': op,
            'right_value': right_value,
            'note': note,
            'run_begin': run_begin,
            'run_end': run_end,
            'tpl_id': tpl_id
        }
    )
    return jsonify(msg='')
Exemplo n.º 2
0
def strategy_delete_get(sid):
    sid = int(sid)
    s = Strategy.get(sid)
    if not s:
        return jsonify(msg='no such strategy')

    Strategy.delete_one(sid)

    return jsonify(msg='')
Exemplo n.º 3
0
def strategy_delete_get(sid):
    sid = int(sid)
    s = Strategy.get(sid)
    if not s:
        return jsonify(msg='no such strategy')

    Strategy.delete_one(sid)

    return jsonify(msg='')
Exemplo n.º 4
0
def strategy_update_post():
    sid = request.form['sid'].strip()
    metric = request.form['metric'].strip()
    tags = request.form['tags'].strip()
    max_step = request.form['max_step'].strip()
    priority = request.form['priority'].strip()
    note = request.form['note'].strip()
    func = request.form['func'].strip()
    op = request.form['op'].strip()
    right_value = request.form['right_value'].strip()
    run_begin = request.form['run_begin'].strip()
    run_end = request.form['run_end'].strip()
    tpl_id = request.form['tpl_id'].strip()

    if not metric:
        return jsonify(msg='metric is blank')

    if metric == 'net.port.listen' and '=' not in tags:
        return jsonify(
            msg='if metric is net.port.listen, tags should like port=22')

    if sid:
        # update
        Strategy.update_dict(
            {
                'metric': metric,
                'tags': tags,
                'max_step': max_step,
                'priority': priority,
                'func': func,
                'op': op,
                'right_value': right_value,
                'note': note,
                'run_begin': run_begin,
                'run_end': run_end
            }, 'id=%s', [sid])
        return jsonify(msg='')

    # insert
    Strategy.insert({
        'metric': metric,
        'tags': tags,
        'max_step': max_step,
        'priority': priority,
        'func': func,
        'op': op,
        'right_value': right_value,
        'note': note,
        'run_begin': run_begin,
        'run_end': run_end,
        'tpl_id': tpl_id
    })
    return jsonify(msg='')
Exemplo n.º 5
0
def strategy_delete_get(sid):
    sid = int(sid)
    s = Strategy.get(sid)
    data = {'id': sid}
    alarmAdUrl = config.JSONCFG['shortcut']['falconUIC'] + "/api/v1/alarmadjust/whenstrategydeleted"
    if not s:
        return jsonify(msg='no such strategy')

    Strategy.delete_one(sid)
    respCode = post2FeUpdateEventCase(alarmAdUrl, data)
    if respCode != 200:
        log.error(alarmAdUrl + " got " + str(respCode) + " with " + str(data))
    return jsonify(msg='')
Exemplo n.º 6
0
def strategy_delete_get(sid):
    sid = int(sid)
    s = Strategy.get(sid)
    data = {'id': sid}
    alarmAdUrl = config.JSONCFG['shortcut'][
        'falconUIC'] + "/api/v1/alarmadjust/whenstrategydeleted"
    if not s:
        return jsonify(msg='no such strategy')

    Strategy.delete_one(sid)
    respCode = post2FeUpdateEventCase(alarmAdUrl, data)
    if respCode != 200:
        log.error(alarmAdUrl + " got " + str(respCode) + " with " + str(data))
    return jsonify(msg='')
Exemplo n.º 7
0
def strategy_get(sid):
    sid = int(sid)
    s = Strategy.get(sid)
    if not s:
        return jsonify(msg='no such strategy')

    return jsonify(msg='', data=s.to_json())
Exemplo n.º 8
0
def strategy_get(sid):
    sid = int(sid)
    s = Strategy.get(sid)
    if not s:
        return jsonify(msg='no such strategy')

    return jsonify(msg='', data=s.to_json())
Exemplo n.º 9
0
def template_delete_get(tpl_id):
    tpl_id = int(tpl_id)
    t = Template.get(tpl_id)
    if not t:
        return jsonify(msg='no such template')

    if not t.writable(g.user_name):
        return jsonify(msg='no permission')

    Template.delete_one(tpl_id)
    action_id = t.action_id
    if action_id:
        Action.delete_one(action_id)

    Strategy.delete('tpl_id = %s', [tpl_id])

    GrpTpl.unbind_tpl(tpl_id)
    return jsonify(msg='')
Exemplo n.º 10
0
def template_delete_get(tpl_id):
    tpl_id = int(tpl_id)
    t = Template.get(tpl_id)
    if not t:
        return jsonify(msg='no such template')

    if not t.writable(g.user_name):
        return jsonify(msg='no permission')

    Template.delete_one(tpl_id)
    action_id = t.action_id
    if action_id:
        Action.delete_one(action_id)

    Strategy.delete('tpl_id = %s', [tpl_id])

    GrpTpl.unbind_tpl(tpl_id)
    return jsonify(msg='')
Exemplo n.º 11
0
def template_update_get(tpl_id):
    g.menu = 'templates'
    tpl_id = int(tpl_id)
    t = Template.get(tpl_id)
    if not t:
        return jsonify(msg='no such template')

    t.parent = Template.get(t.parent_id)
    ss = Strategy.select_vs(where='tpl_id = %s', params=[tpl_id], order='metric')
    t.action = Action.get(t.action_id)
    return render_template('template/update.html', data={'tpl': t, 'ss': ss, 'uic': UIC_ADDRESS['external']})
Exemplo n.º 12
0
def template_delete_get(tpl_id):
    tpl_id = int(tpl_id)
    t = Template.get(tpl_id)
    data = {'templateId': tpl_id}
    alarmAdUrl = config.JSONCFG['shortcut'][
        'falconUIC'] + "/api/v1/alarmadjust/whentempletedeleted"
    if not t:
        return jsonify(msg='no such template')

    if not t.writable(g.user_name):
        return jsonify(msg='no permission')

    Template.delete_one(tpl_id)
    action_id = t.action_id
    if action_id:
        Action.delete_one(action_id)

    Strategy.delete('tpl_id = %s', [tpl_id])

    GrpTpl.unbind_tpl(tpl_id)
    respCode = post2FeUpdateEventCase(alarmAdUrl, data)
    if respCode != 200:
        log.error(alarmAdUrl + " got " + str(respCode) + " with " + str(data))
    return jsonify(msg='')
Exemplo n.º 13
0
def api_strategies_by_tpl(tpl_id):
    tpls = []
    while True:
        t = Template.get(tpl_id)
        if t is None:
            break
        tpls.append(tpl_id)
        tpl_id = t.parent_id

    strategies = []
    for tpl_id in tpls:
        strategies.extend(Strategy.get_by_tpl(tpl_id))
    strategies = filter(lambda x: x is not None, strategies)
    if len(strategies) == 0:
        return jsonify(msg='can not get strategies by template id:%s' % tpl_id)
    return jsonify(msg='', data=[ss.to_json() for ss in strategies])
Exemplo n.º 14
0
def template_update_get(tpl_id):
    g.menu = 'templates'
    tpl_id = int(tpl_id)
    t = Template.get(tpl_id)
    if not t:
        return jsonify(msg='no such template')

    t.parent = Template.get(t.parent_id)
    ss = Strategy.select_vs(where='tpl_id = %s',
                            params=[tpl_id],
                            order='metric')
    t.action = Action.get(t.action_id)
    return render_template('template/update.html',
                           data={
                               'tpl': t,
                               'ss': ss,
                               'uic': UIC_ADDRESS['external']
                           })
Exemplo n.º 15
0
def strategy_update_post():
    sid = request.form['sid'].strip()
    metric = request.form['metric'].strip()
    tags = request.form['tags'].strip()
    max_step = request.form['max_step'].strip()
    priority = request.form['priority'].strip()
    note = request.form['note'].strip()
    func = request.form['func'].strip()
    op = request.form['op'].strip()
    right_value = request.form['right_value'].strip()
    run_begin = request.form['run_begin'].strip()
    run_end = request.form['run_end'].strip()
    tpl_id = request.form['tpl_id'].strip()
    data = {'id': sid}
    alarmAdUrl = config.JSONCFG['shortcut'][
        'falconUIC'] + "/api/v1/alarmadjust/whenstrategyupdated"
    if not metric:
        return jsonify(msg='metric is blank')

    if not note:
        return jsonify(msg='note is blank')

    if metric == 'net.port.listen' and '=' not in tags:
        return jsonify(
            msg='if metric is net.port.listen, tags should like port=22')
    need_reset = False
    if sid:
        st = Strategy.get(sid)
        if (st.func != func or st.right_value != right_value or st.op != op
                or st.metric != metric or st.tags != tags):
            need_reset = True
        log.debug("need_reset: " + str(need_reset))
        log.debug(str(st.to_json()))
    if sid:
        # update
        Strategy.update_dict(
            {
                'metric': metric,
                'tags': tags,
                'max_step': max_step,
                'priority': priority,
                'func': func,
                'op': op,
                'right_value': right_value,
                'note': note,
                'run_begin': run_begin,
                'run_end': run_end
            }, 'id=%s', [sid])
        if need_reset:
            respCode = post2FeUpdateEventCase(alarmAdUrl, data)
            if respCode != 200:
                log.error(alarmAdUrl + " got " + str(respCode) + " with " +
                          str(data))
        return jsonify(msg='')

    # insert
    Strategy.insert({
        'metric': metric,
        'tags': tags,
        'max_step': max_step,
        'priority': priority,
        'func': func,
        'op': op,
        'right_value': right_value,
        'note': note,
        'run_begin': run_begin,
        'run_end': run_end,
        'tpl_id': tpl_id
    })
    respCode = post2FeUpdateEventCase(alarmAdUrl, data)
    if respCode != 200:
        log.error(alarmAdUrl + " got " + str(respCode) + " with " + str(data))
    return jsonify(msg='')
Exemplo n.º 16
0
def api_strategy_get(s_id):
    s_id = int(s_id)
    s = Strategy.get(s_id)
    if not s:
        return jsonify(msg="no such strategy")
    return jsonify(msg='', data=s.to_json())
Exemplo n.º 17
0
def strategy_update_post():
    sid = request.form['sid'].strip()
    metric = request.form['metric'].strip()
    tags = request.form['tags'].strip()
    max_step = request.form['max_step'].strip()
    priority = request.form['priority'].strip()
    note = request.form['note'].strip()
    func = request.form['func'].strip()
    op = request.form['op'].strip()
    right_value = request.form['right_value'].strip()
    run_begin = request.form['run_begin'].strip()
    run_end = request.form['run_end'].strip()
    tpl_id = request.form['tpl_id'].strip()
    data = {'id': sid}
    alarmAdUrl = config.JSONCFG['shortcut']['falconUIC'] + "/api/v1/alarmadjust/whenstrategyupdated"
    if not metric:
        return jsonify(msg='metric is blank')

    if not note:
        return jsonify(msg='note is blank')

    if metric == 'net.port.listen' and '=' not in tags:
        return jsonify(msg='if metric is net.port.listen, tags should like port=22')
    need_reset = False
    if sid:
        st = Strategy.get(sid)
        if (st.func != func or st.right_value != right_value or st.op != op or st.metric != metric or st.tags !=
                tags):
            need_reset = True
        log.debug("need_reset: " + str(need_reset))
        log.debug(str(st.to_json()))
    if sid:
        # update
        Strategy.update_dict(
            {
                'metric': metric,
                'tags': tags,
                'max_step': max_step,
                'priority': priority,
                'func': func,
                'op': op,
                'right_value': right_value,
                'note': note,
                'run_begin': run_begin,
                'run_end': run_end
            },
            'id=%s',
            [sid]
        )
        if need_reset:
            respCode = post2FeUpdateEventCase(alarmAdUrl, data)
            if respCode != 200:
                log.error(alarmAdUrl + " got " + str(respCode) + " with " + str(data))
        return jsonify(msg='')

    # insert
    Strategy.insert(
        {
            'metric': metric,
            'tags': tags,
            'max_step': max_step,
            'priority': priority,
            'func': func,
            'op': op,
            'right_value': right_value,
            'note': note,
            'run_begin': run_begin,
            'run_end': run_end,
            'tpl_id': tpl_id
        }
    )
    respCode = post2FeUpdateEventCase(alarmAdUrl, data)
    if respCode != 200:
        log.error(alarmAdUrl + " got " + str(respCode) + " with " + str(data))
    return jsonify(msg='')
Exemplo n.º 18
0
def api_strategy_get(s_id):
    s_id = int(s_id)
    s = Strategy.get(s_id)
    if not s:
        return jsonify(msg="no such strategy")
    return jsonify(msg='', data=s.to_json())