#!/usr/bin/perl use warnings; use strict; use IO::Pipe; use Tk; use Tk::Dialog; # 1 setting for big or default font, see lines 41 - 42 #mouse button 1 click on an app, will ask if you want to kill it my $mw = new MainWindow; my $tframe = $mw->Frame(-bg=>'black')->pack(-fill=>'x'); $tframe->Label(-text=> 'RAM ------',-bg=>'black',-fg=>'red')->pack(-side=>'left'); $tframe->Label(-text=> 'CPU ------',-bg=>'black',-fg=>'green')->pack(-side=>'left'); my $canvas = $mw->Scrolled('Canvas', -bg =>'black', -height => 500, -width => 600, -scrollregion => [0,0,400,500], -scrollbars => 'osoe' )->pack(-expand=>1,-fill=>'both'); my $realcan = $canvas->Subwidget('scrolled'); #my $font = 'default'; my $font = $mw->fontCreate('font', -family=>'arial', -weight=>'bold', -size=>int(-18*18/14)); my $fonttest = $canvas->createText(0,0, -fill => 'black', -text => 'W', -font => $font, ); my ($bx,$by,$bx1,$by1) = $canvas->bbox($fonttest); my $f_w = $bx1 - $bx; my $f_h = $by1 - $by; $canvas->delete($fonttest); # setup ipc for top #$pid = open(FH, "top -b |") or die "$!\n"; #$mw->fileevent($fh, 'readable', [\&refresh]); my $id = Tk::After->new($mw, 2000,'repeat',\&refresh); refresh(); MainLoop; sub refresh{ my $fh = new IO::Pipe; $fh->reader("top -b -n 1"); my @data = <$fh>; splice(@data,0,7); #remove headers # print @data,"\n"; $fh->close; my %pid; $canvas->delete('all'); foreach my $line(@data){ $line =~ s/^\s+//; if($line =~ /^\d+/){ my @p = split(/\s+/, $line); my $pid = $p[0]; $pid{$pid}{'user'} = $p[1]; $pid{$pid}{'command'} = $p[11]; $pid{$pid}{'mem'} = $p[9]; $pid{$pid}{'cpu'} = $p[8]; } } my $count = 1; foreach my $key(sort {$a<=>$b} keys %pid){ my $string = ' ' x ( 7 - length( $key ) ) . $key; $pid{$key}{'user'} .= ' ' x ( 15 - length( $pid{$key}{'user'} ) ); $string .= ' '.$pid{$key}{'user'}.' '.$pid{$key}{'mem'}.' '. $pid{$key}{'cpu'}.' '.$pid{$key}{'command'}; my $text = $canvas->createText(0, $count * $f_h , -fill => 'orange', -text => $string, -font => $font, -anchor => 'nw', -justify => 'left', -tags => [$key,'string'], ); #500 pixel max width, so 500 = 1.0 or 100% $canvas->createLine($f_w, $count * $f_h - 2, $f_w + $pid{$key}{'cpu'} * 5 , $count * $f_h - 2 , -fill => 'green', -width =>5, # -stipple =>'gray75', ); $canvas->createLine($f_w, $count * $f_h + $f_h , $f_w + $pid{$key}{'mem'} * 5 , $count * $f_h + $f_h , -fill => 'red', -width =>5, # -stipple =>'gray75', ); $canvas->createLine($f_w, $count * $f_h + 1.5*$f_h, $f_w + 500 , $count * $f_h + 1.5*$f_h , -fill => 'white', -dash => '- -', -width =>1, ); $count +=2; } $canvas->CanvasBind("",sub{ my ($tag) = grep /\d+/, $canvas->gettags("current"); if( defined $tag){ &do_dialog($tag) } } ); $canvas->bind( 'string', '' => sub{ my ($tag) = grep /\d+/, $canvas->gettags("current"); if( defined $tag){ my ($current) = $canvas->find('withtag','current'); $canvas->itemconfigure($current,-fill=>'white'); } }); $canvas->bind( 'string', '' => sub{ my ($tag) = grep /\d+/, $canvas->gettags("current"); if( defined $tag){ my ($current) = $canvas->find('withtag','current'); $canvas->itemconfigure($current,-fill=>'orange'); } }); my (undef,undef,$x,$y) = $realcan->bbox("all"); if( defined $x){ $realcan->configure(-scrollregion => [0,0,$x + 30, $y+30 ] ); } } ################################################################## sub do_dialog { my $pid = shift; $pid += 0; #make numeric my $dlg = $mw->Dialog( -title=>"Kill pid # $pid?", -buttons => ["Cancel", "No", "Yes"], -default_button => "No", -text => "Kill pid # $pid ?", -font => $font ); my $result = $dlg->Show(); if($result eq 'Yes'){ kill 9, $pid } } #####################################################################