#!/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.7.7.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 1; set c 2} {$i < 19} {incr i; incr c} {
	    grid [label $wname.wri$i -text "WRI$i" -fg blue] -row 0 -column $c
	}
    }
    set labels {wrsPstatsHCPortName
		wrsPstatsHCTXUnderrun
		wrsPstatsHCRXOverrun
		wrsPstatsHCRXInvalidCode
		wrsPstatsHCRXSyncLost
		wrsPstatsHCRXPauseFrames
		wrsPstatsHCRXPfilterDropped
		wrsPstatsHCRXPCSErrors
		wrsPstatsHCRXGiantFrames
		wrsPstatsHCRXRuntFrames
		wrsPstatsHCRXCRCErrors
		wrsPstatsHCRXPclass0
		wrsPstatsHCRXPclass1
		wrsPstatsHCRXPclass2
		wrsPstatsHCRXPclass3
		wrsPstatsHCRXPclass4
		wrsPstatsHCRXPclass5
		wrsPstatsHCRXPclass6
		wrsPstatsHCRXPclass7
		wrsPstatsHCTXFrames
		wrsPstatsHCRXFrames
		wrsPstatsHCRXDropRTUFull
		wrsPstatsHCRXPrio0
		wrsPstatsHCRXPrio1
		wrsPstatsHCRXPrio2
		wrsPstatsHCRXPrio3
		wrsPstatsHCRXPrio4
		wrsPstatsHCRXPrio5
		wrsPstatsHCRXPrio6
		wrsPstatsHCRXPrio7
		wrsPstatsHCRTUValid
		wrsPstatsHCRTUResponses
		wrsPstatsHCRTUDropped
		wrsPstatsHCFastMatchPriority
		wrsPstatsHCFastMatchFastForward
		wrsPstatsHCFastMatchNonForward
		wrsPstatsHCFastMatchRespValid
		wrsPstatsHCFullMatchRespValid
		wrsPstatsHCForwarded
		wrsPstatsHCTRURespValid
    }
    # 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.7.7.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
}
