Posted to tcl by schelte at Sat Mar 23 15:22:55 GMT 2013view raw

  1. #!/usr/bin/tclsh
  2.  
  3. package require Tk
  4.  
  5. wm title . "Load AVG"
  6. #wm resizable . 0 0
  7.  
  8. entry .e1 -width 6 -state readonly -justify center -textvariable old(.e1)
  9. entry .e2 -width 6 -state readonly -justify center -textvariable old(.e2)
  10. entry .e3 -width 6 -state readonly -justify center -textvariable old(.e3)
  11. entry .e4 -width 6 -state readonly -justify center -textvariable old(.e4)
  12. entry .e5 -width 6 -state readonly -justify center -textvariable old(.e5)
  13. button .b1 -text {Exit} -state normal -command {exit}
  14. grid .e1 -row 0 -column 0
  15. grid .e2 -row 1 -column 0
  16. grid .e3 -row 2 -column 0
  17. grid .e4 -row 3 -column 0
  18. grid .e5 -row 4 -column 0
  19. grid .b1 -row 5 -column 0
  20.  
  21. proc StatusInsert {name now} {
  22. global old
  23. if {$now > $old($name)} {
  24. puts "$now UP"
  25. $name configure -foreground red
  26. } elseif {$now == $old($name)} {
  27. puts "$now EQUAL"
  28. $name configure -foreground blue
  29. } else {
  30. puts "$now DOWN"
  31. $name configure -foreground darkgreen
  32. }
  33. set old($name) $now
  34. }
  35.  
  36. proc avg_load {} {
  37. set lavgFile {/proc/loadavg}
  38. set lavgFD [open $lavgFile r]
  39. while {[gets $lavgFD line] >= 0} {
  40. set result $line
  41. }
  42.  
  43. foreach w {.e1 .e2 .e3} value [lrange [split $result { }] 0 2] {
  44. StatusInsert $w $value
  45. }
  46.  
  47. foreach w {.e4 .e5} value [split [lindex [split $result { }] 3] {/}] {
  48. StatusInsert $w $value
  49. }
  50.  
  51. close $lavgFD
  52. }
  53.  
  54. proc testcase {} {
  55. avg_load
  56. after 1000 testcase
  57. }
  58.  
  59. testcase