#!/bin/sh

. ../common/ajax_common


load_alert_stat()
{
    ${FLOWEYE} nev stat | awk -F"=" \
    'BEGIN{
        dot = "";
        printf "{";
    }{
        printf "%s\"%s\":\"%s\"", dot, $1, $2;
        if(dot == "") dot = ",";  
    }END{
        printf "}";
    }'
}


set_alert_stat()
{
    cmdargs="nevttl=${CGI_nevttl} cpu_scan_inter=${CGI_cpu_scan_inter} auth_scan_inter=${CGI_auth_scan_inter} ifbps_scan_inter=${CGI_ifbps_scan_inter}"
    cmdargs="${cmdargs} synackpps_scan_inter=${CGI_synackpps_scan_inter}"
    errmsg=`${FLOWEYE} nev config ${cmdargs}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧܣ${errmsg}"
    else
        sync_floweye "nev config ${cmdargs}"
        WEB_LOGGER "ø澯ģ"
        retjson 0 "ɹ"
    fi
}


set_msgsend_inter()
{
    cmdargs="notify2user=${CGI_notify2user} notifyperiod_week=${CGI_notifyperiod_week}"
    cmdargs="${cmdargs} notifyperiod_hourstart=${CGI_notifyperiod_hourstart} notifyperiod_hourend=${CGI_notifyperiod_hourend}"

    errmsg=`${FLOWEYE} nev config ${cmdargs}`
    [ $? -ne 0 ] &&  retjson 1 "ʧܣ${errmsg}"

    ${WEBUICTL} start "warring" >/dev/null 2>&1 &

    sync_floweye "nev config ${cmdargs}"
    WEB_LOGGER "ø澯ͼ" "${cmdargs}"
    retjson 0 "ɹ"
}


load_policy_list()
{
    ${FLOWEYE} evrule list
}


load_alert_event()
{
    [ "${CGI_type}" = "history" ] && cmdargs="listlog=1"

    ${FLOWEYE} nev list ${cmdargs}
}


get_alert_policy()
{
    [ "${CGI_mtime}" != "" ] && cmdargs="mtime=${CGI_mtime}"

    errmsg=`${FLOWEYE} evrule get id=${CGI_id} show_rawdata=1 "${cmdargs}"`

    if [ $? -ne 0 ]; then
        printf "\"none\""
    else
        printf "{${errmsg}}"
    fi
}


load_addpolicy_env()
{
    . ../common/ajax_object_lib

    printf "{"
    printf "\"wan_list\":`lib_proxy_list wan`"
    printf ",\"interface_list\":`lib_interface_list`"
    printf ",\"iptab_list\":`lib_iptable_list`"
    printf ",\"ippool_list\":`lib_ippool_list`"
    if [ "${CGI_id}" != "" ]; then
        printf ",\"policy\":`get_alert_policy`"
    fi

    printf "}"
}


add_alert_policy()
{
    [ "${CGI_action}" = "add_alert_policy" ] && action="add" || action="set"
    [ "${CGI_nontrig}" = "1" ] && dura=0

    cmdargs="id=${CGI_id} evtype=${CGI_evtype} evattr=${CGI_evattr} optor=${CGI_evoptor} objname=${CGI_objname}"
    cmdargs="${cmdargs} limit1=${CGI_evlimit1} limit2=${CGI_evlimit2} nontrig=${CGI_nontrig} dura=${CGI_dura} notifyinter=${CGI_notifyinter}"

    errmsg=`${FLOWEYE} evrule ${action} ${cmdargs}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧܣ${errmsg}"
    else
        sync_floweye "evrule ${action} ${cmdargs}"
        WEB_LOGGER "ø澯" "${action} evtype=${CGI_evtype} evattr=${CGI_evattr}"
        retjson 0 "ɹ"
    fi
}


rmv_alert_policy()
{
    errmsg=`${FLOWEYE} evrule remove id=${CGI_id}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧܣ${errmsg}"
    else
        sync_floweye "evrule remove id=${CGI_id}"
        WEB_LOGGER "ɾ澯" "id=${CGI_id} evtype=${CGI_evtype}"
        retjson 0 "ɹ"
    fi
}


enable_alert_policy()
{
    errmsg=`${FLOWEYE} evrule set id=${CGI_id} enable=1`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧܣ${errmsg}"
    else
        sync_floweye "evrule set id=${CGI_id} enable=1"
        WEB_LOGGER "ø澯" "id=${CGI_id} evtype=${CGI_evtype}"
        retjson 0 "ɹ"
    fi
}


disable_alert_policy()
{
    errmsg=`${FLOWEYE} evrule set id=${CGI_id} enable=0`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧܣ${errmsg}"
    else
        sync_floweye "evrule set id=${CGI_id} enable=0"
        WEB_LOGGER "ø澯" "id=${CGI_id} evtype=${CGI_evtype}"
        retjson 0 "ɹ"
    fi
}


btnrmv_alert_policy()
{
    num=0

    for id in `echo ${CGI_items} | tr ";" " "`
    do
        errmsg=`${FLOWEYE} evrule remove id=${id}`
        [ $? -ne 0 ] && retjson 1 "ʧܣ${errmsg}"
        sync_floweye "evrule remove id=${id}"
        num=$((${num} + 1))
    done

    WEB_LOGGER "ɾ澯" "num=${num}"
    retjson 0 "ɹ"
}


btnenable_alert_policy()
{
    num=0

    for id in `echo ${CGI_items} | tr ";" " "`
    do
        errmsg=`${FLOWEYE} evrule set id=${id} enable=1`
        [ $? -ne 0 ] && retjson 1 "ʧܣ${errmsg}"
        sync_floweye "evrule set id=${id} enable=1"
        num=$((${num} + 1))
    done

    WEB_LOGGER "ø澯" "num=${num}"
    retjson 0 "ɹ"
}


btndisable_alert_policy()
{
    num=0

    for id in `echo ${CGI_items} | tr ";" " "`
    do
        errmsg=`${FLOWEYE} evrule set id=${id} enable=0`
        [ $? -ne 0 ] && retjson 1 "ʧܣ${errmsg}"
        sync_floweye "evrule set id=${id} enable=0"
        num=$((${num} + 1))
    done

    WEB_LOGGER "ø澯" "num=${num}"
    retjson 0 "ɹ"
}


export_alert_policy()
{
	file_name="system_alert_rule.conf"

	mkdir -p ${WEB_DOWNLOAD}

	awk '{
		if($1 == "evrule")
            print $0;
	}' ${PGETC}/panabit.conf >> ${WEB_DOWNLOAD}/${file_name}

	retjson 0 "OK" "\"${file_name}\""
}


import_alert_policy()
{
    [ ! -f "${CGI_file}" ] && retjson 1 "INV_FILE"

    ok=0
    err=0

    while read types conf
    do
        [ "${types}" != "evrule" ] && continue

        ${FLOWEYE} evrule add ${conf}

        if [ $? -ne 0 -a "${CGI_type}" = "cover" ]; then
            ${FLOWEYE} evrule set ${conf}
            if [ $? -ne 0 ]; then
                err=$((${err} + 1))
            else
                ok=$((${ok} + 1))
            fi
        else
            ok=$((${ok} + 1))
        fi
    done <<EOF
`cat ${CGI_file}`
EOF

    WEB_LOGGER "澯"
    retjson 0 "ɣɹ:${ok}ʧ:${err}"
}


rmv_history_event()
{
    errmsg=`${FLOWEYE} nev remove logidstr=${CGI_id}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧܣ${errmsg}"
    else
        sync_floweye "nev remove logidstr=${CGI_id}"
        WEB_LOGGER "ɾʷ澯¼" "id=${CGI_id}"
        retjson 0 "ɹ"
    fi
}


case "${CGI_action}" in
    "load_alert_stat")
        retjson 0 "OK" "`load_alert_stat`"
        ;;

    "set_alert_stat")
        action_check
        set_alert_stat
        ;;

    "set_msgsend_inter")
        action_check
        set_msgsend_inter
        ;;

    "load_policy_list")
        retjson 0 "OK" "`load_policy_list`"
        ;;
    
    "load_alert_event")
        retjson 0 "OK" "`load_alert_event`"
        ;;

    "load_addpolicy_env")
        retjson 0 "OK" "`load_addpolicy_env`"
        ;;

    "add_alert_policy"|"edit_alert_policy")
        action_check
        add_alert_policy
        ;;

    "rmv_alert_policy")
        action_check
        rmv_alert_policy
        ;;

    "enable_alert_policy")
        action_check
        enable_alert_policy
        ;;
    
    "disable_alert_policy")
        action_check
        disable_alert_policy
        ;;

    "btnrmv_alert_policy")
        action_check
        btnrmv_alert_policy
        ;;

    "btnenable_alert_policy")
        action_check
        btnenable_alert_policy
        ;;

    "btndisable_alert_policy")
        action_check
        btndisable_alert_policy
        ;;

    "export_alert_policy")
        action_check
        export_alert_policy
        ;;

    "import_alert_policy")
        action_check
        import_alert_policy
        ;;

    "rmv_history_event")
        action_check
        rmv_history_event
        ;;

    *)
        retjson 1 "UNKNOW_ACTION"
        ;;
esac
