#! /bin/csh -f

# Process arguments
set usage = "Usage: tnstat [-c] [-b <html-browser-cmmand>] <eth>"

set cont = 1
set browser = ""
set cmd = "sudo `which tnregs`"
set Parameter = "Value"

if ($#argv < 1) then
	echo "Usage: $usage"
	exit(1)
endif

while ($#argv > 0)
	if ("$1" == "-c") then
		set cont = 0
	else if ("$1" == "-b") then
		set browser = $2
		shift argv
	else
		set eth = $1
	endif
	shift argv
end

# Verify the eth is valid and TN4010 NIC is found
set res = `sudo ifconfig $eth | grep error` 

if ("$res" == "") then
	exit(1)
endif

set res = `$cmd $eth info 1`
if ("$res" != "0x1 (1)") then
	echo "Not a NT4010 based device found on $eth"
	exit(1)
endif

# Open browser if requested
if ("$browser" != "") then
	$browser rmonstats.html &
endif

# Main loop
while (1)
	clear

	# Read counters
	@ etherStatsDropEvents =           `$cmd $eth read_reg 0x6120` + `$cmd $eth read_reg 0x7250` + (`$cmd $eth read_reg 0x7258` * 0)
	@ etherStatsMulticastPkts =        `$cmd $eth read_reg 0x60E8` + `$cmd $eth read_reg 0x60A8`
	@ etherStatsOctets =               `$cmd $eth read_reg 0x6128`
	@ etherStatsPkts =                 `$cmd $eth read_reg 0x6130`
	@ etherStatsBroadcastPkts =        `$cmd $eth read_reg 0x60F0`
	@ etherStatsCRCAlignErrors =       `$cmd $eth read_reg 0x6090`
	@ etherStatsUndersizePkts =        `$cmd $eth read_reg 0x6138`
	@ etherStatsFragments =            `$cmd $eth read_reg 0x6188`
	@ etherStatsOversizePkts =         `$cmd $eth read_reg 0x6178`
	@ etherStatsJabbers =              `$cmd $eth read_reg 0x6180`
	@ etherStatsPkts64Octets =         `$cmd $eth read_reg 0x6140`
	@ etherStatsPkts65to127Octets =    `$cmd $eth read_reg 0x6148`
	@ etherStatsPkts128to255Octets =   `$cmd $eth read_reg 0x6150`
	@ etherStatsPkts256to511Octets =   `$cmd $eth read_reg 0x6158`
	@ etherStatsPkts512to1023Octets =  `$cmd $eth read_reg 0x6160`
	@ etherStatsPkts1024to1518Octets = `$cmd $eth read_reg 0x6168`
	@ etherStatsPkts1519toX =          `$cmd $eth read_reg 0x6170`

	# Generate HTML header
	echo "<html>" >! rmonstats.html
	echo "" >> rmonstats.html
	echo "<head>" >> rmonstats.html
	echo "   <meta http-equiv="refresh" content="1" >" >> rmonstats.html
	echo "</head>" >> rmonstats.html
	echo "" >> rmonstats.html
	echo "<p class=MsoNormal dir=LTR style='text-align:left;direction:ltr;unicode-bidi: embed'><span style='font-size:17.0pt;line-height:115%;color:black'>Tehuti Networks LTD</span></p>" >> rmonstats.html
	echo "<p class=MsoNormal dir=LTR style='text-align:left;direction:ltr;unicode-bidi: embed'><span style='font-size:17.0pt;line-height:115%;color:black'>RFC2819 RMON MIB statistics</span></p>" >> rmonstats.html
	echo "<p class=MsoNormal dir=LTR style='text-align:left;direction:ltr;unicode-bidi: embed'>For NIC at $eth</p>" >> rmonstats.html
	echo "" >> rmonstats.html
	echo "<table class=MsoTableGrid border=1 cellspacing=0 cellpadding=0 style='border-collapse:collapse;border:none'>" >> rmonstats.html
	echo "" >> rmonstats.html

	# Generate table entries
	foreach var (Parameter etherStatsDropEvents etherStatsOctets etherStatsPkts etherStatsBroadcastPkts etherStatsMulticastPkts etherStatsCRCAlignErrors etherStatsUndersizePkts etherStatsFragments etherStatsOversizePkts etherStatsJabbers etherStatsPkts64Octets etherStatsPkts65to127Octets etherStatsPkts128to255Octets etherStatsPkts256to511Octets etherStatsPkts512to1023Octets etherStatsPkts1024to1518Octets etherStatsPkts1519toX)
		printf "%-30s = " $var
		eval `echo echo \$$var`

		echo "   <tr>" >> rmonstats.html
		echo "      <td width=295 valign=top style='width:221.4pt;border:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt'>" >> rmonstats.html
		echo "         <p class=MsoNormal dir=LTR style='margin-bottom:0cm;margin-bottom:.0001pt;text-align:left;line-height:normal;direction:ltr;unicode-bidi:embed'><b>$var</b></p>" >> rmonstats.html
		echo "      </td>" >> rmonstats.html
		echo "      <td width=295 valign=top style='width:221.4pt;border:solid windowtext 1.0pt;border-left:none;padding:0cm 5.4pt 0cm 5.4pt'>" >> rmonstats.html
		echo -n "         <p class=MsoNormal dir=LTR style='margin-bottom:0cm;margin-bottom:.0001pt;text-align:left;line-height:normal;direction:ltr;unicode-bidi:embed'><b>" >> rmonstats.html
		eval `echo -n echo \$$var` >> rmonstats.html
		echo "</b></p>" >> rmonstats.html
		echo "      </td>" >> rmonstats.html
		echo "   </tr>" >> rmonstats.html
		echo "" >> rmonstats.html
	end

	# Generate HTML trailer
	echo "</table>" >> rmonstats.html
	echo "" >> rmonstats.html
	echo "</body>" >> rmonstats.html
	echo "" >> rmonstats.html
	echo "</html>" >> rmonstats.html

	if ($cont == 1) then
		exit(0)
	else
		sleep 5
	endif

end

exit(0)
