#!/bin/sh
#This script is created by ssparser automatically. The parser first created by MaoShouyan
printf "Content-type: text/html
Cache-Control: no-cache

"
echo -n ""; 
. ../common/common.sh 
[ "${CGI_refresh}" = "" ] && CGI_refresh=0
[ "${CGI_topn}" = "" ] && CGI_topn="30"
[ "${CGI_statype}" = "" ] && CGI_statype="last"
[ "${CGI_bridge}" = "" ] && CGI_bridge="0"
cgipath="/cgi-bin/Monitor/app_topn"
cgiargs="title=${CGI_title}&refresh=${CGI_refresh}&statype=${CGI_statype}&topn=${CGI_topn}&group=${CGI_group}&bridge=${CGI_bridge}"
cgiurl="${cgipath}?${cgiargs}"
[ ${CGI_refresh} -ne 0 ] && afm_load_page  ${CGI_refresh} "${cgiurl}"

echo -n "
<script type=\"text/javascript\" src=\"/img/common.js\"></script>
<script language=\"javascript\">
function ShowDetail(ipaddr)
{
	var url = \"/cgi-bin/Monitor/ip_detail?ipaddr=\" + ipaddr;
	ShowWindow(url, \"\", 500, 640);
}
function ShowFlow(ipaddr)
{
	var url = \"/cgi-bin/Monitor/ip_flow?ipaddr=\" + ipaddr;
	ShowWindow(url, \"\", 650, 700);
}
function onselected()
{
        var i;
	var count;
        var apps;
	count = 0;
        apps = document.getElementsByName(\"appselect\");
        for (i = 0; i < apps.length; i++) {
                if (apps[i].checked) count++;
        }
	document.getElementsByName(\"showselect\")[0].disabled = count > 0 ? false : true;
}
function ShowSelected()
{
        var i;
        var url;
        var apps;
        var datasrc;
        var statype;
        var title;
        datasrc=\"\";
        statype = document.getElementsByName(\"statype\")[0];
        apps = document.getElementsByName(\"appselect\");
        for (i = 0; i < apps.length; i++) {
                if (apps[i].checked) {
                        if (statype.value == \"last\" || statype.value == \"all\") 
                                datasrc = datasrc + \",\" + apps[i].value + \".bps\";
                        else
                        if (statype.value == \"bpsup\") 
                                datasrc = datasrc + \",\" + apps[i].value + \".bpsout\";
                        else
                        if (statype.value == \"bpsdn\") 
                                datasrc = datasrc + \",\" + apps[i].value + \".bpsin\";
                        else
                        if (statype.value == \"flow\") 
                                datasrc = datasrc + \",\" + apps[i].value + \".flow\";
                }
        }
        if (statype.value == \"last\" || statype.value == \"all\")
                title = \"ͼ\"
        else
        if (statype.value == \"bpsup\")
                title = \"ͼ\"
        else
        if (statype.value == \"bpsdn\")
                title = \"ͼ\"
        else
        if (statype.value == \"flow\")
                title = \"ͼ\"
        url = \"/cgi-bin/Monitor/appreport?datasrc=\" + datasrc + \"&title=\" + title;
        ShowWindow(url, \"scrollbars=1,resizable=0\", 670, 850);
}
function showapp(appid, appname)
{
        var url = \"/cgi-bin/Monitor/appview_topip?appid=\" + appid +
              \"&appname=\" + appname;
        ShowWindow(url, \"scrollbars=1,resizable=1\", 740, 800);
}
</script>
<body>
"; cgi_show_title "${CGI_title}" 
echo -n "
<br>
<table width=900 border=0 cellspacing=1 cellpadding=1>
<form method=post action=\"${cgipath}\">
<tr id=tblhdr height=22>
	<td width=120 align=left>&nbsp;
	<select name=bridge value=\"${CGI_bridge}\" style=\"width:80\">";
		for bdg in 0 `${FLOWEYE} bridge list needlog=1`; do
			selected=""
			[ ${bdg} -eq ${CGI_bridge} ] && selected="selected"
			if [ ${bdg} -eq 0 ]; then
				echo "<option value=${bdg} ${selected}></option>"
			else
				echo "<option value=${bdg} ${selected}>${bdg}</option>"
			fi
		done 
echo -n "
	</select>
	</td>
	<td width=222 align=left>&nbsp;
        <select name=statype value=\"${CGI_statype}\" style=\"width:140\"> ";
		if [ "${CGI_statype}" = "last" ]; then
			echo "<option value=last selected>10</option>"
		else
			echo "<option value=last>10</option>"
		fi
		if [ "${CGI_statype}" = "all" ]; then
			echo "<option value=all selected>ۼ</option>"
		else
			echo "<option value=all>ۼ</option>"
		fi
		if [ "${CGI_statype}" = "bpsup" ]; then
			echo "<option value=bpsup selected>bps</option>"
		else
			echo "<option value=bpsup>bps</option>"
		fi
		if [ "${CGI_statype}" = "bpsdn" ]; then
			echo "<option value=bpsdn selected>bps</option>"
		else
			echo "<option value=bpsdn>bps</option>"
		fi 
		if [ "${CGI_statype}" = "flow" ]; then
			echo "<option value=flow selected></option>"
		else
			echo "<option value=flow></option>"
		fi 
	
echo -n "
        </select>&nbsp;</td>
	<td width=252 align=left>ʾǰ<select name=topn value=\"${CGI_topn}\" style=\"width:45\"> ";
		for tm in 10 20 30 50 100 0; do
			if [  ${tm} -eq 0 ]; then
				tmdesc=""
			else
				tmdesc="${tm}"
			fi
			if [ "${CGI_topn}" = "${tm}" ]; then
				echo "<option selected value=${tm}>${tmdesc}</option>"
			else
				echo "<option value=${tm}>${tmdesc}</option>"
			fi
		done 
echo -n "</select>&nbsp;
	<select name=refresh value=\"${CGI_refresh}\" style=\"width:50\">
	";
		for tm in 15 30 45 60 100 0; do
			if [ ${tm} -eq 0 ]; then
				tmdesc="ˢ"
			else
				tmdesc="${tm}"
			fi
			if [ "${CGI_refresh}" = "${tm}" ]; then
				echo "<option selected value=${tm}>${tmdesc}</option>"
			else
				echo "<option value=${tm}>${tmdesc}</option>"
			fi
		done
	
echo -n "
	</select>&nbsp;ˢ</td>
	<td width=* align=right>
		<input type=submit value=\"ˢ  \" style=\"width:70\"></input>&nbsp;&nbsp;
		<input type=button name=showselect disabled value=\"ѡӦöԱȷ\" style=\"width:120\" onclick=\"javascript:ShowSelected()\"></input>
		<input type=hidden name=title value=\"${CGI_title}\"></input>
	</td>
</tr>
</form>
</table>
";
urlpath="${cgipath}?title=${CGI_title}&refresh=${CGI_refresh}&topn=${CGI_topn}&group=${CGI_group}&bridge=${CGI_bridge}"

echo -n "
<table width=900 border=0 cellspacing=1 cellpadding=2>
<tr id=tblhdr height=22>
	<td width=118 align=center>Э</td>
	<td width=60 align=center><a href=\"${urlpath}&statype=flow\"></a></td>
	<td width=75 align=center><a href=\"${urlpath}&statype=bpsup\">bps</a></td>
	<td width=75 align=center><a href=\"${urlpath}&statype=bpsdn\">bps</a></td>
	<td width=250 align=center><a href=\"${urlpath}&statype=all\">ۼ</a></td>
	<td width=* align=center><a href=\"${urlpath}&statype=last\">10</a></td>
</tr>
";
args="sort=${CGI_statype} bridge=${CGI_bridge}"
[ ${CGI_topn} -ne 0 ] && args="${args} topn=${CGI_topn}"
idname="row1"
${FLOWEYE} app stat ${args} | \
while read appid appname flow node aup adown all per1 lup ldown lall per2 rup rdown rall theother
do

echo -n "
	<tr id=${idname}>
	<td align=left>&nbsp;
		<input type=checkbox name=appselect value=\"${appid}\" onclick=\"onselected()\"></input>
		<a href=\"javascript:showapp('${appid}', '${appname}')\">${appname}
	</td>
	<td align=right>${flow}</td>
	<td align=right>${rup}</td>
	<td align=right>${rdown}</td>
	"; wid=`echo "${per1}" |awk '{print ($1 * 90.00) / 100.00}'` 
echo -n "
	<td><table cellpading=0 cellspacing=0>
	    <tr id=${idname}><td width=80 align=right>${all}</td>
	    <td align=left>&nbsp;<img src=\"/img/data_bar.gif\" width=${wid} height=10 alt=\"${per1}%\" border=0>&nbsp;${per1}</td>
	    </tr></table>
	</td>
	"; wid=`echo "${per2}" |awk '{print ($1 * 90.00) / 100.00}'` 
echo -n "
	<td><table cellpading=0 cellspacing=0>
	    <tr id=${idname}><td width=80 align=right>${lall}</td>
	    <td align=left>&nbsp;<img src=\"/img/data_bar.gif\" width=${wid} height=10 alt=\"${per2}%\" border=0>&nbsp;${per2}</td>
	    </tr></table>
	</td>
	</tr>
";
	if [ "${idname}" = "row1" ]; then
		idname="row2"
	else
		idname="row1"
	fi
done

echo -n "
</table>
</body>
</html>
";