Posted to tcl by schelte at Sat Mar 23 15:09:21 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(1)
  9. entry .e2 -width 6 -state readonly -justify center -textvariable old(2)
  10. entry .e3 -width 6 -state readonly -justify center -textvariable old(3)
  11. entry .e4 -width 6 -state readonly -justify center -textvariable old(p0)
  12. entry .e5 -width 6 -state readonly -justify center -textvariable old(p1)
  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 entry_name} {
  22. global old
  23. if {$now > $old($name)} {
  24. puts "$now UP"
  25. $entry_name configure -foreground red
  26. } elseif {$now == $old($name)} {
  27. puts "$now EQUAL"
  28. $entry_name configure -foreground blue
  29. } else {
  30. puts "$now DOWN"
  31. $entry_name configure -foreground darkgreen
  32. }
  33. set old($name) $now
  34. }
  35.  
  36. proc avg_load {} {
  37. global old
  38. global p_old
  39. set lavgFile {/proc/loadavg}
  40. set lavgFD [open $lavgFile r]
  41. while {[gets $lavgFD line] >= 0} {
  42. set result $line
  43. }
  44.  
  45. foreach i {1 2 3} value [lrange [split $result { }] 0 2] {
  46. StatusInsert $i $value .e[expr {$i+1}]
  47. }
  48.  
  49. foreach j {0 1} p_value [split [lindex [split $result { }] 3] {/}] {
  50. StatusInsert p$j $p_value .e[expr {$j+4}]
  51. }
  52.  
  53. close $lavgFD
  54. }
  55.  
  56. proc testcase {} {
  57. avg_load
  58. after 1000 testcase
  59. }
  60.  
  61. testcase