Monitor CPU RAM SWAP HD

http://static.flickr.com/107/256321094_dc56a63b0d.jpg

RJT Monitor usage process with cygwin


 wm title . "Cliente Monitor CPU"
 wm geometry . 400x300+[expr [winfo screenwidth .]/3]+[expr [winfo screenheight .]/3]
 wm maxsize . 1024 768
 wm minsize        .  550 320
 wm attributes . -alpha .65
 wm protocol . WM_DELETE_WINDOW {rerun}


 set varlog 1
 set hc 0
 set tiempo 1000
 set colores {red green blue yellow violet cyan orange pink gold grey white black}
 set color [lindex $colores [expr int(rand()*[llength $colores])]]

 if {[lindex $::tcl_platform(platform)]=="unix"}  {
        set path "./"
 } else  {
        set path ".\\"
 }

 global $path

 destroy .m
 set alto  [winfo height .]
 set ancho [winfo width  .]
 set fuente "arial [expr (($ancho/$alto)*16)] bold"


 pack                 [frame .m -relief raised -borderwidth 0 -bg grey -height 30 ] -expand no -fill both -side top
 pack                 [frame .m.s -relief raised -borderwidth 0 -bg grey -height 30 ] -expand no -fill x -side top
 pack                [label                .m.s.l3                        -text        "Refresco (ms):"]\
                [entry                .m.s.e3                        -textvariable tiempo -width 5 -bg white]\
                [button                .m.s.top                        -text "TOP"                        -command {alltop} ]\
                [button                .m.s.shade        -text "SHADE"        -command {shade} ]\
                [button                .m.s.run                        -text "RUN"                        -command        {run} ]\
                [button                .m.s.bye                        -text "BYE"                        -command {bye} ]\
                [button                .m.s.quit                -text "QUIT"                -command        {exit} ]\
                -expand yes -fill x -side left

 foreach objeto {top shade run bye quit} {
        .m.s.$objeto configure -activebackground red
 }


 pack [frame .m.b -relief groove -borderwidth 2 -bg grey] -expand no -fill x -side top

 pack                 [button         .m.b.cpu                 -text "CPU"         -command        {set varlog 1} ]\
                [button         .m.b.ram                 -text "RAM"         -command        {set varlog 2}        ]\
                [button         .m.b.swp                 -text "SWAP"         -command        {set varlog 3}        ]\
                [button         .m.b.hdd                 -text "DISK"         -command        {set varlog 4}        ]\
                [button  .m.b.pro   -text "PROC"  -command {set varlog 5} ]\
                [button  .m.b.sys   -text "SYS"  -command {set varlog 6} ]\
                -expand yes -fill x -side left

 foreach objeto {cpu ram swp hdd pro sys} {
        .m.b.$objeto configure -activebackground yellow -relief groove
 }


 pack                 [canvas .m.g -background white ] -expand yes -fill both -side top

 bind . <Motion> {wm title . "Monitor de CPU: Rolando José Torres Sánchez" }
 bind . <B1-Motion> {wm title . "Monitor de CPU: %x %y" }
 bind . <B2-Motion> {
         set ancho [winfo width  .]
        set alto  [winfo height .]
        set posimouse [expr (%x+%y)]
        set sumacoor [expr ($ancho+$alto)]
        
        if {$posimouse <= $sumacoor} {
                set trans [expr 0.1+(1.0* $posimouse / $sumacoor)]
                if {($trans > 0.1) & ($trans < 2)} {
                        wm attributes . -alpha $trans
                }
        }
 }
 bind . <B3-Motion> {wm title . "Objeto: %W %x %y" }



 proc Update {  } {
        global varlog servidor puerto ancho alto fuente path tiempo
        
        set alto [winfo height .]
        set ancho [winfo width .]
        set fuentesize [expr int(double($alto/40))]
        if {$fuentesize ==0} { incr fuentesize }
        if {$tiempo <= 500} { set tiempo 500        }
        set fuente "arial $fuentesize bold"
        .m.g configure -width $ancho -height [expr $alto-70]
        if {($varlog>=1)&($varlog<=6)} {
                
                if {$varlog == 1 } {
                        .m.g delete all
                        set hc 0
                        if {[lindex $::tcl_platform(platform)]=="unix"}  {
                                set estado [split [exec vmstat 1 2] \n]
                                set cpulog [lindex [lindex $estado 3]]
                                .m.g create text [expr ($ancho*.125)] [expr $alto-100] -text "Users: [lindex $cpulog 12]"                 -fill black -font $fuente
                                .m.g create text [expr ($ancho*.375)] [expr $alto-100] -text "System: [lindex $cpulog 13]"         -fill black -font $fuente
                                .m.g create text [expr ($ancho*.625)] [expr $alto-100] -text "Idle: [lindex $cpulog 14]"                 -fill black -font $fuente
                                .m.g create text [expr ($ancho*.875)] [expr $alto-100] -text "Wait: [lindex $cpulog 15]"                 -fill black -font $fuente
                                .m.g create rectangle [expr ($ancho*.00)] [expr $alto-120] [expr ($ancho*.25)] [expr ($alto-120)-[expr [lindex $cpulog 12]*($alto-145)*0.01]] -fill orange
                                .m.g create rectangle [expr ($ancho*.25)] [expr $alto-120] [expr ($ancho*.50)] [expr ($alto-120)-[expr [lindex $cpulog 13]*($alto-145)*0.01]] -fill red
                                .m.g create rectangle [expr ($ancho*.50)] [expr $alto-120] [expr ($ancho*.75)] [expr ($alto-120)-[expr [lindex $cpulog 14]*($alto-145)*0.01]] -fill green
                                .m.g create rectangle [expr ($ancho*.75)] [expr $alto-120] [expr ($ancho* 1.)] [expr ($alto-120)-[expr [lindex $cpulog 15]*($alto-145)*0.01]] -fill blue
                        } else  {
                                set estado [split [exec vmstat 1 2] \n]
                                set cpulog [lindex [lindex $estado 3]]
                                .m.g create text [expr ($ancho*1./6)] [expr $alto-100] -text "Users: [lindex $cpulog 13]"         -fill black -font $fuente
                                .m.g create text [expr ($ancho*3./6)] [expr $alto-100] -text "System: [lindex $cpulog 14]"                 -fill black -font $fuente
                                .m.g create text [expr ($ancho*5./6)] [expr $alto-100] -text "Idle: [lindex $cpulog 15]"                 -fill black -font $fuente
                                .m.g create rectangle [expr ($ancho*.00)] [expr $alto-120] [expr ($ancho*2./6)] [expr ($alto-120)-[expr [lindex $cpulog 13]*($alto-145)*0.01]] -fill orange
                                .m.g create rectangle [expr ($ancho*2./6)] [expr $alto-120] [expr ($ancho*4./6)] [expr ($alto-120)-[expr [lindex $cpulog 14]*($alto-145)*0.01]] -fill red
                                .m.g create rectangle [expr ($ancho*4./6)] [expr $alto-120] [expr ($ancho* 1.)] [expr ($alto-120)-[expr [lindex $cpulog 15]*($alto-145)*0.01]] -fill green
                        }
                }
                
                if {$varlog == 2 } {
                        .m.g delete all
                        set hc 0
                        set estado [split [exec free] \n]
                        set allmem [lindex [lindex $estado 1] 1]
                        set usomem [lindex [lindex $estado 1] 2]
                        set fremem [lindex [lindex $estado 1] 3]
                        set usobuf [lindex [lindex $estado 2] 2]
                        set frebuf [lindex [lindex $estado 2] 3]
                        .m.g create text [expr ($ancho*.1)] [expr $alto-100] -text "RAM:\n$allmem"                          -fill black  -font $fuente -justify center
                        .m.g create text [expr ($ancho*.3)] [expr $alto-100] -text "RAM Usada:\n$usomem"          -fill black  -font $fuente -justify center
                        .m.g create text [expr ($ancho*.5)] [expr $alto-100] -text "RAM Libre:\n$fremem"          -fill black  -font $fuente -justify center
                        .m.g create text [expr ($ancho*.7)] [expr $alto-100] -text "Buffer Usado:\n$usobuf"  -fill black  -font $fuente -justify center
                        .m.g create text [expr ($ancho*.9)] [expr $alto-100] -text "Buffer Libre:\n$frebuf"  -fill black  -font  $fuente -justify center
                        .m.g create rectangle [expr ($ancho*.0)] [expr $alto-120] [expr ($ancho*.2)] [expr ($alto-120)-[expr ($allmem./$allmem.)*($alto-140)]] -fill cyan
                        .m.g create rectangle [expr ($ancho*.2)] [expr $alto-120] [expr ($ancho*.4)] [expr ($alto-120)-[expr ($usomem./$allmem.)*($alto-140)]] -fill red
                        .m.g create rectangle [expr ($ancho*.4)] [expr $alto-120] [expr ($ancho*.6)] [expr ($alto-120)-[expr ($fremem./$allmem.)*($alto-140)]] -fill {dark green}
                        .m.g create rectangle [expr ($ancho*.6)] [expr $alto-120] [expr ($ancho*.8)] [expr ($alto-120)-[expr ($usobuf./$allmem.)*($alto-140)]] -fill orange
                        .m.g create rectangle [expr ($ancho*.8)] [expr $alto-120] [expr ($ancho*1.)] [expr ($alto-120)-[expr ($frebuf./$allmem.)*($alto-140)]] -fill yellow
                }
                
                if {$varlog == 3 } {
                        .m.g delete all
                        set hc 0
                        set estado [split [exec free] \n]
                        set allswpmem [lindex [lindex $estado 3] 1]
                        set usoswpmem [lindex [lindex $estado 3] 2]
                        set freswpmem [lindex [lindex $estado 3] 3]
                        .m.g create text                         [expr ($ancho*.166)] [expr $alto-100] -text "SWAP $allswpmem" -fill black -font  $fuente
                        .m.g create text                         [expr ($ancho*.500)] [expr $alto-100] -text "SWAP Usada $usoswpmem" -fill black -font  $fuente
                        .m.g create text                         [expr ($ancho*.833)] [expr $alto-100] -text "SWAP Libre $freswpmem" -fill black -font  $fuente
                        .m.g create rectangle   [expr ($ancho*0.00)] [expr $alto-120] [expr ($ancho*.333)] [expr ($alto-120)-[expr ($allswpmem./$allswpmem.)*($alto-140)]] -fill cyan
                        .m.g create rectangle   [expr ($ancho*.333)] [expr $alto-120] [expr ($ancho*.666)] [expr ($alto-120)-[expr ($usoswpmem./$allswpmem.)*($alto-140)]] -fill red
                        .m.g create rectangle   [expr ($ancho*.666)] [expr $alto-120] [expr ($ancho*1.00)] [expr ($alto-120)-[expr ($freswpmem./$allswpmem.)*($alto-140)]] -fill {dark green}
                }
                
                if {$varlog == 4 } {
                        .m.g delete all
                        set estado [split [exec df -P -k ] \n]
                        set filas [llength $estado]
                        for {set i 0 } {$i<$filas} {incr i} {
                                set particion [lindex [lindex $estado $i] 0]
                                set size        [lindex [lindex $estado $i] 1]
                                set uso [lindex [lindex $estado $i] 2]
                                if {$i >0 } {
                                        set porcenta [expr ($uso*100/$size)]
                                } else  {
                                        set porcenta 0
                                }
                                destroy .m.g.f$i
                                set altofila 20
                                pack                 [frame .m.g.f$i -height $altofila] -fill x -side top
                                pack                 [label .m.g.f$i.c1 -text $particion -width 20 -relief raised -bg white -justify right -font  $fuente ]\
                                                [label .m.g.f$i.c2 -text $size -width 10 -relief raised -bg white -justify right -font  $fuente ]\
                                                [label .m.g.f$i.c3 -text $uso -width 10 -relief raised -bg white -justify right -font  $fuente]\
                                                [label .m.g.f$i.c4 -text $porcenta -width 10 -relief raised -bg white -justify right -font  $fuente] -fill x -side left
                                                
                                pack                 [canvas .m.g.f$i.c5 -width [expr ($porcenta*2)] -height $altofila -bg red -relief flat -bd 0]\
                                                [canvas .m.g.f$i.c6 -width [expr (200-($porcenta*2))] -height $altofila -bg "green" -relief flat -bd 0] -fill x -side left
                        }
                        set varlog 0
                        stop
                }
                
                if {$varlog == 5 } {
                        .m.g delete all
                        set hc 0
                        set estado [split [exec vmstat 1 2] \n]
                        set cpulog [lindex [lindex $estado 3]]
                        .m.g create text [expr ($ancho*.125)] [expr $alto-100] -text "Read: [lindex $cpulog 0]"                 -fill black -font $fuente
                        .m.g create text [expr ($ancho*.485)] [expr $alto-100] -text "Block: [lindex $cpulog 1]"         -fill black -font $fuente
                        .m.g create text [expr ($ancho*.815)] [expr $alto-100] -text "Write: [lindex $cpulog 2]"                 -fill black -font $fuente
                        .m.g create rectangle [expr ($ancho*.00)] [expr $alto-120] [expr ($ancho*.33)] [expr ($alto-120)-[expr [lindex $cpulog 0]*($alto-145)*1]] -fill red
                        .m.g create rectangle [expr ($ancho*.33)] [expr $alto-120] [expr ($ancho*.66)] [expr ($alto-120)-[expr [lindex $cpulog 1]*($alto-145)*1]] -fill yellow
                        .m.g create rectangle [expr ($ancho*.66)] [expr $alto-120] [expr ($ancho*.99)] [expr ($alto-120)-[expr [lindex $cpulog 2]*($alto-145)*1]] -fill green
                }
                
                if {$varlog == 6 } {
                        .m.g delete all
                        set hc 0
                        set estado [split [exec vmstat 1 2] \n]
                        set cpulog [lindex [lindex $estado 3]]
                        set inter [expr [lindex $cpulog 11]]
                        set block [expr [lindex $cpulog 12]]
                        if {$inter >= $block} {
                                set mayor [expr $inter]
                        } else  {
                                set mayor [expr $block]
                        }
                        .m.g create text [expr ($ancho*.25)] [expr $alto-100] -text "Interrup: $inter "                 -fill black -font $fuente
                        .m.g create text [expr ($ancho*.75)] [expr $alto-100] -text "Block: $block"         -fill black -font $fuente
                        .m.g create rectangle [expr ($ancho*.0)]  [expr $alto-120] [expr ($ancho*.5)] [expr ($alto-120)-[expr double(($inter+0.0)/$mayor)*($alto-145) ]] -fill blue
                        .m.g create rectangle [expr ($ancho*.5) ] [expr $alto-120] [expr ($ancho*1)]  [expr ($alto-120)-[expr double(($block+0.0)/$mayor)*($alto-145) ]] -fill yellow
                }
        after $tiempo Update
        }
 }

 proc alltop  {} {
        wm attributes . -topmost yes
        .m.s.top configure -text "NORMAL" -command {subtop}
 }

 proc subtop {} {
        wm attributes . -topmost no
        .m.s.top configure -text "TOP" -command {alltop}
 }

 proc shade {} {
        wm overrideredirect . yes
        .m.s.shade configure -text "UNSHADE" -command {unshade}
 }

 proc unshade {} {
        wm overrideredirect . no
        .m.s.shade configure -text "SHADE" -command {shade}
 }

 proc bye {} {
        global tiempo
        wm withdraw .
        after $tiempo
        wm deiconify .
        .m.s.bye configure -text "HI" -command {
                .m.s.bye configure -text "BYE"
                after 1500
                bye
        }
 }

 proc rerun {} {
        wm withdraw .
        after 5000
        wm deiconify .
 }

 proc run {} {
        global varlog
        .m.s.run configure -text "STOP " -command {stop}
        set varlog 1
        if { [winfo exists .m.g.f1] } {
                catch {
                        destroy .m.g
                        pack                 [canvas .m.g -background white ] -expand yes -fill both -side top
                        .m.g delete all
                }                
        }
        Update
 }

 proc stop {} {
        global varlog
        .m.s.run configure -text "RUN" -command {run}
        set varlog 0
 }


 alltop
 shade
 run