#! ../scotty -nf
##
## Simply print the rstat information given by an rstatd.
##


##
## Compute the diff between two rstat calls.
##

proc rstat_diff {l1 l2 period} {
    set len [llength $l1]
    set res ""
    for {set i "0"} {$i<$len} {incr i} {
        set el1 [lindex $l1 $i]
        set el2 [lindex $l2 $i]
        set tmp [lindex $el1 2]
        if {[lindex $el1 1]=="Counter"} {
            set tmp [expr {[lindex $el1 2]-[lindex $el2 2]}]
	    set tmp [expr {"$tmp.0" / $period}]
	}	
        if {[lindex $el1 1]=="Gauge"} {
            set tmp [expr {[lindex $el1 2]/256.0}]
        }
        lappend res [format "%-12s %-12s %16.3f" \
		     [lindex $el1 0] [lindex $el1 1] $tmp]
    }
    return $res
}

proc rstat {host period} {
    set stat [rpc stat $host]
    sleep $period
    set newstat [rpc stat $host]
    set res ""
    foreach s [rstat_diff $newstat $stat $period] { lappend res $s }
    return $res
}

##
## Check the commandline and start reporting.
##

proc usage {} {
    puts stderr "usage: rstat hostname intervall"
    exit
}

if {[llength $argv] != 2} { usage } else {
    set ip [lindex $argv 0]
    set intervall [lindex $argv 1]
    set intervall [expr {$intervall<1 ? 1 : $intervall}]
    while {1} {
	if {[catch {rstat $ip $intervall} result]} {
	    puts "rstat: $result"
	    return
	}
	puts ""
	foreach elem $result {
	    puts $elem
	}
    }
}

