#!/bin/sh

. ../common/ajax_common

if [ "${PALANG}" = "en" ] ;then
	LANG001="Error"		#ʧ
	LANG002="Success"	#ɹ
	LANG003="Set_App_Value"	#Э
	LANG004="Add_App_Node"	#Эڵ
	LANG005="Del_App_Node"	#ɾЭڵ
fi


get_app_info()
{
	${FLOWEYE} app get "${CGI_appname}" | awk -F "=" \
	'BEGIN{
		dot = "";
		printf "{";
	}{
		printf "%s\"%s\":\"%s\"", dot, $1, $2;
		if(dot == "") dot = ",";
	}END{
		printf "}";
	}'
}


get_app_usernode()
{
	${FLOWEYE} usernode list | awk -v appname="${CGI_appname}" \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		if($1 != appname) next;

		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"ip\":\"%s\",", $2;
		printf "\"port\":\"%s\"", $3;
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


get_app_config()
{
	printf "{";
	printf "\"info\":`get_app_info`,"
	printf "\"node\":`get_app_usernode`"
	printf "}"
}


set_app_info()
{
	cachesn=0
	ignore_node=0

	[ "${CGI_cachesn}" = "on" ] && cachesn=1
	[ "${CGI_ignore_node}" = "on" ] && ignore_node=1

	cmdargs="${CGI_appname} nodettl=${CGI_nodettl} cachesn=${cachesn} ignore_node=${ignore_node}"
	errmsg=`${FLOWEYE} app set ${cmdargs}`

	if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}:${errmsg}"
	else
		sync_floweye "app set ${cmdargs}"
		WEB_LOGGER "${LANG003:=Э}" "${cmdargs}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


add_app_node()
{
	errmsg=`${FLOWEYE} usernode add ip=${CGI_ip} port=${CGI_port} app=${CGI_appname}`

	if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}:${errmsg}"
	else
		sync_floweye "usernode add ip=${CGI_ip} port=${CGI_port} app=${CGI_appname}"
		WEB_LOGGER "${LANG004:=Эڵ}" "appname=${CGI_appname} ip=${CGI_ip} port=${CGI_port}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


rmv_app_node()
{
	errmsg=`${FLOWEYE} usernode remove ip=${CGI_ip} port=${CGI_port}`

	if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}:${errmsg}"
	else
		sync_floweye "usernode remove ip=${CGI_ip} port=${CGI_port}"
		WEB_LOGGER "${LANG005:=ɾЭڵ}" "appname=${CGI_appname} ip=${CGI_ip} port=${CGI_port}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


get_app_user()
{
	[ "${CGI_appname}" = "" ] && retjson 1 "INV_APPNAME"
	[ "${CGI_sort}" = "" ] && CGI_sort="all"

	end=$((${CGI_page} * ${CGI_limit}))
	start=$((${end} - ${CGI_limit}))

	${FLOWEYE} ipobj listipa  -s ${CGI_sort} -app ${CGI_appname} monitor=1 | grep -i "${CGI_keyword}" | awk \
	-v end=${end} -v start=${start} \
	'BEGIN{
		row = 0;
		dot = "";
		printf "{\"data\":[";
	}{
        row++;
        if(row > end || row < start) next;

		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"ip\":\"%s\",", $(col++);
		printf "\"appname\":\"%s\",", $(col++);
		printf "\"appcname\":\"%s\",", $(col++);
		printf "\"inbps\":\"%s\",", $(col++);
		printf "\"outbps\":\"%s\",", $(col++);
		printf "\"allbps\":\"%s\",", $(col++);
		printf "\"account\":\"%s\"", $(col++);
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "],\"total\":%s}", row;
	}'
}


get_app_flow()
{
	[ "${CGI_appname}" = "" ] && retjson 1 "INV_APPNAME"
	
	showip=0
	[ "${CGI_type}" = "inner" ] && showip=1

	end=$((${CGI_page} * ${CGI_limit}))
	start=$((${end} - ${CGI_limit}))

	${FLOWEYE} flow ipsort app=${CGI_appname} type=${CGI_type} showip=${showip} | grep -i "${CGI_keyword}" | awk \
	-v end=${end} -v start=${start} \
	'BEGIN{
		row = 0;
		dot = "";
		printf "{\"data\":[";
	}{
        row++;
        if(row > end || row < start) next;

		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"ip\":\"%s\",", $(col++);
		printf "\"flow\":\"%s\"", $(col++);
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "],\"total\":%s}", row;
	}'
}


get_app_proxy()
{
	end=$((${CGI_page} * ${CGI_limit}))
	start=$((${end} - ${CGI_limit}))

	${FLOWEYE} app pxylist app=${CGI_appname} | sort ${CGI_sort} | grep -i "${CGI_keyword}" | awk \
	-v end=${end} -v start=${start} \
	'BEGIN{
		row = 0;
		dot = "";
		printf "{\"data\":[";
	}{
        row++;
        if(row > end || row < start) next;

		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"pxyid\":\"%s\",", $(col++);
		printf "\"pxyname\":\"%s\",", $(col++);
		printf "\"up\":\"%s\",", $(col++);
		printf "\"down\":\"%s\",", $(col++);
		printf "\"all\":\"%s\",", $(col++);
		printf "\"flow\":\"%s\"", $(col++);
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "],\"total\":%s}", row;
	}'
}


get_app_policy()
{
	curgrp=`${FLOWEYE} policy getcurgrp | cut -d' ' -f1`

	${FLOWEYE} policy getbyapp ${CGI_appname} | awk \
	-v curgrp=${curgrp} \
	'BEGIN{
		dot = "";
		printf "{\"curgrp\":\"%s\",", curgrp;
		printf "\"data\":[", curgrp;
	}{
		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"polno\":\"%s\",", $(col++);
		printf "\"bridge\":\"%s\",", $(col++);
		printf "\"ifname\":\"%s\",", $(col++);
		printf "\"inif\":\"%s\",", $(col++);
		printf "\"dir\":\"%s\",", $(col++);
		printf "\"appid\":\"%s\",", $(col++);
		printf "\"appname\":\"%s\",", $(col++);
		printf "\"proto\":\"%s\",", $(col++);
		printf "\"from\":\"%s\",", $(col++);
		printf "\"intype\":\"%s\",", $(col++);
		printf "\"inip\":\"%s\",", $(col++);
		printf "\"inport\":\"%s\",", $(col++);
		printf "\"to\":\"%s\",", $(col++);
		printf "\"outtype\":\"%s\",", $(col++);
		printf "\"outip\":\"%s\",", $(col++);
		printf "\"outport\":\"%s\",", $(col++);
		printf "\"action\":\"%s\",", $(col++);
		printf "\"nextstep\":\"%s\",", $(col++);
		printf "\"iprate\":\"%s\",", $(col++);
		printf "\"prclevel\":\"%s\",", $(col++);
		printf "\"pktbps\":\"%s\",", $(col++);
		printf "\"pktbps2\":\"%s\",", $(col++);
		printf "\"tos\":\"%s\",", $(col++);
		printf "\"pri\":\"%s\",", $(col++);
		printf "\"natip\":\"%s\",", $(col++);
		printf "\"linkid\":\"%s\"", $(col++);
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "]}";
	}'
}


case "${CGI_action}" in

	"get_app_info")
		retjson 0 "OK" "`get_app_info`"
		;;

	"get_app_usernode")
		retjson 0 "OK" "`get_app_usernode`"
		;;

	"get_app_config")
		retjson 0 "OK" "`get_app_config`"
		;;

	"set_app_info")
		action_check
		set_app_info
		;;

	"add_app_node")
		action_check
		add_app_node
		;;

	"rmv_app_node")
		action_check
		rmv_app_node
		;;

	"get_app_user")
		retjson 0 "OK" "`get_app_user`"
		;;

	"get_app_flow")
		retjson 0 "OK" "`get_app_flow`"
		;;

	"get_app_proxy")
		retjson 0 "OK" "`get_app_proxy`"
		;;

	"get_app_policy")
		retjson 0 "OK" "`get_app_policy`"
		;;

	*)
		retjson 1 "UNKNOW_ACTION!"
		;;
esac
