Posted to tcl by schelte at Sat Mar 23 15:09:21 GMT 2013view pretty

#!/usr/bin/tclsh
 
package require Tk
 
wm title . "Load AVG"
#wm resizable . 0 0
 
entry .e1 -width 6 -state readonly -justify center -textvariable old(1)
entry .e2 -width 6 -state readonly -justify center -textvariable old(2)
entry .e3 -width 6 -state readonly -justify center -textvariable old(3)
entry .e4 -width 6 -state readonly -justify center -textvariable old(p0)
entry .e5 -width 6 -state readonly -justify center -textvariable old(p1)
button .b1 -text {Exit} -state normal -command {exit}
grid .e1 -row 0 -column 0
grid .e2 -row 1 -column 0
grid .e3 -row 2 -column 0
grid .e4 -row 3 -column 0
grid .e5 -row 4 -column 0
grid .b1 -row 5 -column 0
 
proc StatusInsert {name now entry_name} {
    global old
    if {$now > $old($name)} {
        puts "$now UP"
        $entry_name configure -foreground red
    } elseif {$now == $old($name)} {
        puts "$now EQUAL"
        $entry_name configure -foreground  blue
    } else {
        puts "$now DOWN"
        $entry_name configure -foreground darkgreen
    }
    set old($name) $now
}
 
proc avg_load {} {
    global old
    global p_old
    set lavgFile {/proc/loadavg}
    set lavgFD [open $lavgFile r]
    while {[gets $lavgFD line] >= 0} {
        set result $line
    }
    
    foreach i {1 2 3} value [lrange [split $result { }] 0 2] {
        StatusInsert $i $value .e[expr {$i+1}]
    }
    
    foreach j {0 1} p_value [split [lindex [split $result { }] 3] {/}] {
        StatusInsert p$j $p_value .e[expr {$j+4}]
    }
    
    close $lavgFD
}

proc testcase {} {
    avg_load
    after 1000 testcase
}

testcase