#!/usr/bin/wish

# Allow user override
if [catch "set cmd \"\$env(SHOWPS_CMD)\""] {
    set cmd "snmpwalk -c public -v 2c -On -Oq HOST .1.3.6.1.4.1.96.100.6.4.1"
}

if ![llength $argv] {
    puts stderr "$argv0: Pass IP number of names of switch(es) as arguments"
    exit 1
}

# A procedure to update the window's content
proc updatew {wname} {
    global w

    set new [expr ![llength [winfo children $wname]]]
    if $new {
	grid [label $wname.name -text "CTR NAME" -fg blue] -row 0 -column 1
	for {set i 0; set c 2} {$i < 18} {incr i; incr c} {
	    grid [label $wname.wr$i -text "WR$i" -fg blue] -row 0 -column $c
	}
    }
    set labels {wrsPstatsPortName
		wrsPstatsTXUnderrun
		wrsPstatsRXOverrun
		wrsPstatsRXInvalidCode
		wrsPstatsRXSyncLost
		wrsPstatsRXPauseFrames
		wrsPstatsRXPfilterDropped
		wrsPstatsRXPCSErrors
		wrsPstatsRXGiantFrames
		wrsPstatsRXRuntFrames
		wrsPstatsRXCRCErrors
		wrsPstatsRXPclass0
		wrsPstatsRXPclass1
		wrsPstatsRXPclass2
		wrsPstatsRXPclass3
		wrsPstatsRXPclass4
		wrsPstatsRXPclass5
		wrsPstatsRXPclass6
		wrsPstatsRXPclass7
		wrsPstatsTXFrames
		wrsPstatsRXFrames
		wrsPstatsRXDropRTUFull
		wrsPstatsRXPrio0
		wrsPstatsRXPrio1
		wrsPstatsRXPrio2
		wrsPstatsRXPrio3
		wrsPstatsRXPrio4
		wrsPstatsRXPrio5
		wrsPstatsRXPrio6
		wrsPstatsRXPrio7
		wrsPstatsRTUValid
		wrsPstatsRTUResponses
		wrsPstatsRTUDropped
		wrsPstatsFastMatchPriority
		wrsPstatsFastMatchFastForward
		wrsPstatsFastMatchNonForward
		wrsPstatsFastMatchRespValid
		wrsPstatsFullMatchRespValid
		wrsPstatsForwarded
		wrsPstatsTRURespValid
    }
    # open a file according to the command given
    set F [open "| $w($wname:cmd)" r]
    while {[gets $F str] > 0} {
	foreach {oid value} $str {
	    # remove the leading part
	    regsub ".1.3.6.1.4.1.96.100.6.4.1." $oid "" oid
	    # extract column and row
	    foreach {col row} [split $oid .] {
		if ![string length $row] break
		set name $wname.${col}_${row}
		if $new {
		    label $name
		} 
		set width "-width 8"
		if {$col == 1} {set width ""}
		eval $name config -text \"$value\" $width
		set row1 [expr $row +1]
		grid $name -row $col -column $row1
	    }
	}
    }
    set n [llength $labels]
    set row 0
    while {$row < $n} {
	set name $wname.1_${row}
	if $new {
	    label $name
	}
	set counter_name [lindex $labels $row]
	eval $name config -text \"$counter_name\"
	set row1 [expr $row +2]
	grid $name -row $row1 -column 1
	incr row
    }
    if [catch "close $F" message] {
	puts stderr "Error in snmpwalk for host $w($wname:host)"
	puts stderr $message
	exit 1
    }

    # our cache is 5 seconds: make sure it changed
    after 6000 "updatew $wname"
}


foreach host $argv {
    # turn "10.0.0.1" into "10_0_0_1"
    regsub -all "\\\." $host "_" win
    wm withdraw .
    toplevel .$win

    # create the command name. Use "w" array, in case we need more info later
    regsub "HOST" $cmd $host  w(.$win:cmd)
    set w(.$win:host) $host

    # get the first batch of items and schedule the next
    updatew .$win
    wm deiconify .$win
}
