lots of misc work

git-svn-id: svn://127.0.0.1/Perl/Proc-ProcessTable-Colorizer/trunk@961 0c1c3402-1be1-de11-8092-0022686faf23
This commit is contained in:
Zane C. B-H 2017-10-10 08:59:54 +00:00
parent c51a744a65
commit 58a62ae2b0
1 changed files with 251 additions and 54 deletions

View File

@ -82,6 +82,9 @@ sub new {
'proc',
],
header=>1,
resolveUser=>1,
nextColor=>0,
showIdle=>0,
};
bless $self;
return $self;
@ -89,44 +92,6 @@ sub new {
=head2 colorize
=head3 args hash
=head4 columns
A list of columns to print.
=head2 processColor
This is the color to use for the process. This is by default 'WHITE'.
=head2 fields
The fields to show. By default is as below.
[
'pid'
'uid',
'vmsize',
'rss',
'state',
'wchan',
'nice',
'start',
'time',
'proc',
]
=head2 timeColors
These are the colors for seconds, minutes, hour, 10+ hours.
[
'GREEN',
'BRIGHT_GREEN',
'RED',
'BRIGHT_RED'
]
=cut
sub colorize{
@ -172,39 +137,162 @@ sub colorize{
$fieldInt++;
}
push( @colored, @header );
push( @colored, \@header );
}
#get the process table
my $pt=Proc::ProcessTable->new;
my $procs=$pt->table;
#goes through it all and puts it together
my $PIDint=0;
while( defined( $procs->[$PIDint] ) ){
my @line;
#an array of procs
my @procs;
#goes through it all and gathers the information
foreach my $proc ( @{$pt->table} ){
#process the requested fields
$fieldInt=0;
my %values;
while ( defined( $fields->[$fieldInt] ) ){
my $field=$fields->[$fieldInt];
if (
($^O =~ /bsd/) &&
( $field =~ /pctcpu/ )
){
my $bproc=BSD::Process::info( $proc->pid );
$values{pctcpu}=$bproc->{pctcpu};
}elsif(
($^O =~ /bsd/) &&
( $field =~ /pctmem/ )
){
my $rss=$proc->rss;
my $physmem=`/sbin/sysctl -a hw.physmem`;
chomp($physmem);
$physmem=~s/^.* //;
$values{pctmem}=($rss / $physmem)*100;
}elsif(
($^O =~ /bsd/) &&
( $field =~ /vmsize/ )
){
my $bproc=BSD::Process::info( $proc->pid );
$values{vmsize}=$bproc->{size};
}elsif(
$field =~ /proc/
){
my $fname=$proc->fname;
my $cmndline=$proc->cmndline;
if ( $cmndline =~ /^$/ ){
$values{'proc'}='['.$fname.']';
if ( $fname eq 'idle' ){
$values{'idle'}=1;
}
}else{
$values{'proc'}=$fname;
}
}else{
$values{$field}=$proc->$field;
}
$fieldInt++;
}
if ( ! defined( $values{pctmem} ) ){
$values{pctmem} = 0;
}
if ( ! defined( $values{pctcpu} ) ){
$values{pctcpu} = 0;
}
if ( ! defined( $values{vmsize} ) ){
$values{vmsize} = 0;
}
$values{pctcpu}=$values{pctcpu}/20;
$values{pctmem}=sprintf('%.1f', $values{pctmem});
$values{pctcpu}=sprintf('%.1f', $values{pctcpu});
$values{rss}=$values{rss}/1024;
$values{vmsize}=$values{vmsize}/1024;
push( @procs, \%values );
$PIDint++;
}
#sort by CPU percent and then RAM
@procs=sort {
$a->{pctcpu} <=> $b->{pctcpu} or
$a->{pctmem} <=> $b->{pctmem}
} @procs;
@procs=reverse(@procs);
foreach my $proc (@procs){
my @line;
$self->nextColorReset;
my $show=1;
#checks if it is the idle proc and if it should show it
if (
defined ( $proc->{idle} ) &&
( ! $self->{showIdle} )
){
$show = 0;
}
if (
( $show )
){
foreach my $field ( @{$fields} ){
my $item=$proc->{$field};
if ( $field eq 'start' ){
$item=$self->startString($item);
}
if (
( $field eq 'uid' ) &&
$self->{resolveUser}
){
$item=getpwuid($item);
}
#colorizes it
if ( $field eq 'time' ){
$item=$self->timeString($item);
}elsif( $field eq 'proc' ){
$item=color($self->processColorGet).$item;
}else{
$item=color($self->nextColor).$item;
}
push( @line, $item.color('reset') );
}
push( @colored, \@line );
}
}
my $tb = Text::Table->new;
return $tb->load( \@colored );
return $tb->load( @colored );
}
=head2 timeString
=head2 startString
Generates a short time string based on the supplied unix time.
=cut
sub timeString{
sub startString{
my $self=$_[0];
my $startTime=$_[1];
$self->errorblank;
@ -220,17 +308,17 @@ sub timeString{
#find the most common one and return it
if ( $year ne $cyear ){
return $year.$mon.$mday.'-'/$hour.$min;
return $year.sprintf('%02d', $mon).sprintf('%02d', $mday).'-'/sprintf('%02d', $hour).sprintf('%02d', $min);
}
if ( $mon ne $cmon ){
return $mon.$mday.'-'.$hour.$min;
return sprintf('%02d', $mon).sprintf('%02d', $mday).'-'.sprintf('%02d', $hour).sprintf('%02d', $min);
}
if ( $mday ne $cmday ){
return $mday.'-'.$hour.$min;
return sprintf('%02d', $mday).'-'.sprintf('%02d', $hour).sprintf('%02d', $min);
}
#just return this for anything less
return $hour.':'.$min;
return sprintf('%02d', $hour).':'.sprintf('%02d', $min);
}
=head2 fields
@ -276,6 +364,49 @@ sub fieldsGet{
return $self->{fields};
}
=head2 nextColor
Returns the next color.
my $nextColor=$cps->nextColor;
=cut
sub nextColor{
my $self=$_[0];
$self->errorblank;
my $color;
if( defined( $self->{colors}[ $self->{nextColor} ] ) ){
$color=$self->{colors}[ $self->{nextColor} ];
$self->{nextColor}++;
}else{
$self->{nextColor}=0;
$color=$self->{colors}[ $self->{nextColor} ];
$self->{nextColor}++;
}
return $color;
}
=head2 nextColor
Resets the next color to the first one.
my $nextColor=$cps->nextColor;
=cut
sub nextColorReset{
my $self=$_[0];
$self->errorblank;
$self->{nextColor}=0;
return 1;
}
=head2 fieldsSet
Gets the currently set fields.
@ -293,9 +424,22 @@ sub fieldsSet{
}
=head2 processColorGet
my $timeColors=$cps->processColorGet;
=cut
sub processColorGet{
my $self=$_[0];
$self->errorblank;
return $self->{processColor};
}
=head2 timeColorsGet
my @timeColors=$cps->timeColorsGet;
my $timeColors=$cps->timeColorsGet;
=cut
@ -303,7 +447,60 @@ sub timeColorsGet{
my $self=$_[0];
$self->errorblank;
return $self->{fields};
return $self->{timeColors};
}
=head2 timeString
Turns the raw run string into something usable.
This returns a colorized item.
my $time=$cps->timeString( $seconds );
=cut
sub timeString{
my $self=$_[0];
my $time=$_[1];
$self->errorblank;
my $colors=$self->timeColorsGet;
my $hours = $time / 3600;
my $loSeconds = $time % 3600;
my $minutes = $loSeconds / 60;
my $seconds = $loSeconds % 60;
#nicely format it
$hours=~s/\..*//;
$minutes=~s/\..*//;
$seconds=sprintf('%.1f',$seconds);
$seconds=~s/\.0//;
#this will be returned
my $toReturn='';
#process the hours bit
if ( $hours == 0 ){
#don't do anything if time is 0
}elsif(
$hours >= 10
){
$toReturn=color($colors->[3]).$hours.':';
}else{
$toReturn=color($colors->[2]).$hours.':';
}
#process the minutes bit
if (
( $hours > 0 ) ||
( $minutes > 0 )
){
$toReturn=$toReturn.color( $colors->[1] ). $minutes.':';
}
return $toReturn.color( $colors->[0] ). $seconds ;
}
=head1 COLORS