fix time decoding and implement time search
git-svn-id: svn://127.0.0.1/Perl/Proc-ProcessTable-Colorizer/trunk@967 0c1c3402-1be1-de11-8092-0022686faf23
This commit is contained in:
parent
82e36d1752
commit
b8ef193a27
|
@ -31,20 +31,39 @@ $Getopt::Std::STANDARD_HELP_VERSION = 1;
|
|||
|
||||
#print help
|
||||
sub main::HELP_MESSAGE {
|
||||
print "";
|
||||
print "\n".
|
||||
"-z Show zombies procs.\n".
|
||||
"-s Show swapped out procs.\n".
|
||||
"-p <regex> Search procs using the matching regex.\n".
|
||||
"-u <users> A comma seperated list of users to search for. A ! inverts the users.\n".
|
||||
"-w <wait channels> A comma seperated list of wait channel to search for. A ! inverts it.\n";
|
||||
}
|
||||
|
||||
sub main::VERSION_MESSAGE {
|
||||
print "cps v. 0.0.0\n";
|
||||
}
|
||||
|
||||
#gets the options
|
||||
my %opts=();
|
||||
getopts('p:u:', \%opts);
|
||||
getopts('p:u:zw:st:', \%opts);
|
||||
|
||||
|
||||
use Proc::ProcessTable::Colorizer;
|
||||
my $cps = Proc::ProcessTable::Colorizer->new;
|
||||
$cps->procSearchSet( $opts{p} );
|
||||
|
||||
$cps->zombieSearchSet( $opts{z} );
|
||||
$cps->swappedOutSearchSet( $opts{s} );
|
||||
if ( defined( $opts{u} ) ){
|
||||
$cps->userSearchSetString( $opts{u} );
|
||||
}
|
||||
if ( defined( $opts{w} ) ){
|
||||
$cps->waitSearchSetString( $opts{w} );
|
||||
}
|
||||
if ( defined( $opts{t} ) ){
|
||||
$cps->timeSearchSetString( $opts{t} );
|
||||
if ($cps->error){
|
||||
exit $cps->error;
|
||||
}
|
||||
}
|
||||
|
||||
print $cps->colorize;
|
||||
|
|
|
@ -51,9 +51,7 @@ sub new {
|
|||
error=>undef,
|
||||
errorString=>'',
|
||||
errorExtra=>{
|
||||
flags=>{
|
||||
1=>'noUserStringDefined',
|
||||
},
|
||||
1=>'badTimeString',
|
||||
},
|
||||
colors=>[
|
||||
'BRIGHT_YELLOW',
|
||||
|
@ -88,7 +86,11 @@ sub new {
|
|||
showIdle=>0,
|
||||
proc_search=>undef,
|
||||
user_search=>[],
|
||||
wait_search=>[],
|
||||
self_ignore=>2,
|
||||
zombie_search=>0,
|
||||
swapped_out_search=>0,
|
||||
time_search=>[],
|
||||
};
|
||||
bless $self;
|
||||
return $self;
|
||||
|
@ -372,6 +374,132 @@ sub colorize{
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
#check to see if it needs to search for wait channels
|
||||
my $wait_search_array=$self->waitSearchGet;
|
||||
if ( defined( $wait_search_array->[0] ) ){
|
||||
$required_hits++;
|
||||
my $wait_search_int=0;
|
||||
my $matched=0;
|
||||
#search while we have a wait channel defined and it has not already been matched
|
||||
while(
|
||||
defined( $wait_search_array->[ $wait_search_int ] ) &&
|
||||
( $matched == 0 )
|
||||
){
|
||||
my $to_match=$wait_search_array->[ $wait_search_int ];
|
||||
my $to_invert=0;
|
||||
if ( $to_match=~ /^\!/ ){
|
||||
$to_invert=1;
|
||||
$to_match=~s/^\!//;
|
||||
}
|
||||
|
||||
#check if it matches
|
||||
if ( $to_invert ){
|
||||
if ( $to_match ne $proc->{wchan} ){
|
||||
$hits++;
|
||||
$matched=1;
|
||||
}
|
||||
}else{
|
||||
if ( $to_match eq $proc->{wchan} ){
|
||||
$hits++;
|
||||
$matched=1;
|
||||
}
|
||||
}
|
||||
|
||||
$wait_search_int++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#check to see if it needs to search for CPU time usage
|
||||
my $time_search_array=$self->timeSearchGet;
|
||||
if ( defined( $time_search_array->[0] ) ){
|
||||
$required_hits++;
|
||||
my $time_search_int=0;
|
||||
my $matched=0;
|
||||
#search while we have a CPU time defined and it has not already been matched
|
||||
while(
|
||||
defined( $time_search_array->[ $time_search_int ] ) &&
|
||||
( $matched == 0 )
|
||||
){
|
||||
my $checked=0;
|
||||
my $to_match=$time_search_array->[ $time_search_int ];
|
||||
my $time=$proc->{time};
|
||||
#checks for less than or equal
|
||||
if (
|
||||
( $to_match =~ /^\<\=/ ) &&
|
||||
( $checked == 0 )
|
||||
){
|
||||
$checked++;
|
||||
$to_match =~ s/^\<\=//;
|
||||
if ( $time <= $to_match ){
|
||||
$hits++;
|
||||
$matched++;
|
||||
}
|
||||
}
|
||||
|
||||
#checks for less than
|
||||
if (
|
||||
( $to_match =~ /^\</ ) &&
|
||||
( $checked == 0 )
|
||||
){
|
||||
$checked++;
|
||||
$to_match =~ s/^\<//;
|
||||
if ( $time < $to_match ){
|
||||
$hits++;
|
||||
$matched++;
|
||||
}
|
||||
}
|
||||
|
||||
#checks for greater than or equal
|
||||
if (
|
||||
( $to_match =~ /^\>=/ ) &&
|
||||
( $checked == 0 )
|
||||
){
|
||||
$checked++;
|
||||
$to_match =~ s/^\>\=//;
|
||||
if ( $time >= $to_match ){
|
||||
$hits++;
|
||||
$matched++;
|
||||
}
|
||||
}
|
||||
|
||||
#checks for greater than
|
||||
if (
|
||||
( $to_match =~ /^\>/ ) &&
|
||||
( $checked == 0 )
|
||||
){
|
||||
$checked++;
|
||||
$to_match =~ s/^\>//;
|
||||
if ( $time > $to_match ){
|
||||
$hits++;
|
||||
$matched++;
|
||||
}
|
||||
}
|
||||
|
||||
$time_search_int++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#show zombie procs
|
||||
if ( $self->{zombie_search} ){
|
||||
$required_hits++;
|
||||
if ( $proc->{state} eq 'zombie' ){
|
||||
$hits++;
|
||||
}
|
||||
}
|
||||
|
||||
#show swapped out procs
|
||||
if ( $self->{swapped_out_search} ){
|
||||
$required_hits++;
|
||||
if (
|
||||
( $proc->{state} ne 'zombie' ) &&
|
||||
( $proc->{rss} == '0' )
|
||||
){
|
||||
$hits++;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $required_hits == $hits ){
|
||||
$show=1;
|
||||
|
@ -779,6 +907,46 @@ sub startString{
|
|||
return sprintf('%02d', $hour).':'.sprintf('%02d', $min);
|
||||
}
|
||||
|
||||
=head2 swappedOutSearchGet
|
||||
|
||||
Returns the current value for the swapped out search.
|
||||
|
||||
The return is a Perl boolean.
|
||||
|
||||
my $swappedOut_search=$cps->swappedOutSearchGet;
|
||||
if ( $swappedOut_search ){
|
||||
print "only swapped out procs will be shown";
|
||||
}
|
||||
|
||||
=cut
|
||||
|
||||
sub swappedOutSearchGet{
|
||||
my $self=$_[0];
|
||||
$self->errorblank;
|
||||
|
||||
return $self->{swapped_out_search};
|
||||
}
|
||||
|
||||
=head2 swappedOutSearchSet
|
||||
|
||||
Sets the swapped out search value.
|
||||
|
||||
The value taken is a Perl boolean.
|
||||
|
||||
$cps->swappedOutSearchSet( 1 );
|
||||
|
||||
=cut
|
||||
|
||||
sub swappedOutSearchSet{
|
||||
my $self=$_[0];
|
||||
my $swapped_out_search=$_[1];
|
||||
$self->errorblank;
|
||||
|
||||
$self->{swapped_out_search}=$swapped_out_search;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
=head2 timeColorsGet
|
||||
|
||||
my $timeColors=$cps->timeColorsGet;
|
||||
|
@ -792,6 +960,77 @@ sub timeColorsGet{
|
|||
return $self->{timeColors};
|
||||
}
|
||||
|
||||
=head2 timeSearchGet
|
||||
|
||||
Returns the current value for the time search.
|
||||
|
||||
The return is a array ref.
|
||||
|
||||
my $time_search=$cps->waitSearchGet;
|
||||
|
||||
=cut
|
||||
|
||||
sub timeSearchGet{
|
||||
my $self=$_[0];
|
||||
$self->errorblank;
|
||||
|
||||
return $self->{time_search};
|
||||
}
|
||||
|
||||
=head2 timeSearchSetString
|
||||
|
||||
Search for procs based on the CPU time value.
|
||||
|
||||
The following equalities are understood.
|
||||
|
||||
<=
|
||||
<
|
||||
>
|
||||
>=
|
||||
|
||||
The string may contain multiple values seperated by a comma. Checking will stop after the first hit.
|
||||
|
||||
If the string is undef, all wait channels will be shown.
|
||||
|
||||
#search for procs with less than 60 seconds of CPU time
|
||||
$cps->waitSearchSetString('<69');
|
||||
#shows procs with less than 60 seconds and greater 120 seconds
|
||||
$cps->waitSearchSetString('<60,>120');
|
||||
|
||||
=cut
|
||||
|
||||
sub timeSearchSetString{
|
||||
my $self=$_[0];
|
||||
my $time_search_string=$_[1];
|
||||
$self->errorblank;
|
||||
|
||||
my @time_search_array;
|
||||
if ( ! defined( $time_search_string ) ){
|
||||
$self->{time_search}=\@time_search_array;
|
||||
}else{
|
||||
@time_search_array=split(/\,/, $time_search_string);
|
||||
|
||||
foreach my $item ( @time_search_array ){
|
||||
if (
|
||||
( $item !~ /^\>[0123456789]*$/ ) &&
|
||||
( $item !~ /^\>=[0123456789]*$/ ) &&
|
||||
( $item !~ /^\<[0123456789]*$/ ) &&
|
||||
( $item !~ /^\<=[0123456789]*$/ )
|
||||
){
|
||||
$self->{error}=1;
|
||||
$self->{errorString}='"'.$item.'"" is not a valid value for use in a time search';
|
||||
$self->warn;
|
||||
return undef;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$self->{time_search}=\@time_search_array;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
=head2 timeString
|
||||
|
||||
Turns the raw run string into something usable.
|
||||
|
@ -809,16 +1048,21 @@ sub timeString{
|
|||
|
||||
my $colors=$self->timeColorsGet;
|
||||
|
||||
my $hours = $time / 3600;
|
||||
my $hours=0;
|
||||
if ( $time >= 3600 ){
|
||||
$hours = $time / 3600;
|
||||
}
|
||||
my $loSeconds = $time % 3600;
|
||||
my $minutes = $loSeconds / 60;
|
||||
my $minutes=0;
|
||||
if ( $time >= 60 ){
|
||||
$minutes = $loSeconds / 60;
|
||||
}
|
||||
my $seconds = $loSeconds % 60;
|
||||
|
||||
#nicely format it
|
||||
$hours=~s/\..*//;
|
||||
$minutes=~s/\..*//;
|
||||
$seconds=sprintf('%.1f',$seconds);
|
||||
$seconds=~s/\.0//;
|
||||
$seconds=sprintf('%.f',$seconds);
|
||||
|
||||
#this will be returned
|
||||
my $toReturn='';
|
||||
|
@ -868,18 +1112,20 @@ sub userSearchGet{
|
|||
|
||||
=head1 userSearchSetString
|
||||
|
||||
This gets the user to be searched for and if it should be inverted or not.
|
||||
|
||||
This returns an array reference of users to search for.
|
||||
This takes a string to set the user search for.
|
||||
|
||||
An selection can be inverted via !.
|
||||
|
||||
The string may contain multiple users seperated by a comma.
|
||||
|
||||
If the string is undef, all users will be shown.
|
||||
|
||||
#search for user foo and bar
|
||||
$cps->userSearchSetString('foo,bar');
|
||||
#show users not matching foo
|
||||
$cps->userSearchSetString('!foo');
|
||||
#show all users, clearing any previous settings
|
||||
$cps->userSearchSetString;
|
||||
|
||||
=cut
|
||||
|
||||
|
@ -888,17 +1134,105 @@ sub userSearchSetString{
|
|||
my $user_search_string=$_[1];
|
||||
$self->errorblank;
|
||||
|
||||
my @user_search_array;
|
||||
if ( ! defined( $user_search_string ) ){
|
||||
$self->{errorStirng}=1;
|
||||
$self->{error}='No user search string defined.';
|
||||
$self->warn;
|
||||
return undef;
|
||||
$self->{user_search}=\@user_search_array;
|
||||
}else{
|
||||
@user_search_array=split(/\,/, $user_search_string);
|
||||
$self->{user_search}=\@user_search_array;
|
||||
}
|
||||
|
||||
my @user_search_array=split(/\,/, $user_search_string);
|
||||
|
||||
|
||||
$self->{user_search}=\@user_search_array;
|
||||
return 1;
|
||||
}
|
||||
|
||||
=head2 waitSearchGet
|
||||
|
||||
Returns the current value for the wait search.
|
||||
|
||||
The return is a array ref.
|
||||
|
||||
my $wait_search=$cps->waitSearchGet;
|
||||
|
||||
=cut
|
||||
|
||||
sub waitSearchGet{
|
||||
my $self=$_[0];
|
||||
$self->errorblank;
|
||||
|
||||
return $self->{wait_search};
|
||||
}
|
||||
|
||||
=head2 waitSearchSetString
|
||||
|
||||
This takes a string to set the wait channel search for.
|
||||
|
||||
An selection can be inverted via !.
|
||||
|
||||
The string may contain multiple users seperated by a comma.
|
||||
|
||||
If the string is undef, all wait channels will be shown.
|
||||
|
||||
#search for wait channel wait and sleep
|
||||
$cps->waitSearchSetString('wait,sleep');
|
||||
#shows wait channels not matching sbwait
|
||||
$cps->waitSearchSetString('!sbwait');
|
||||
#show all users, clearing any previous settings
|
||||
$cps->waitSearchSetString;
|
||||
|
||||
=cut
|
||||
|
||||
sub waitSearchSetString{
|
||||
my $self=$_[0];
|
||||
my $wait_search_string=$_[1];
|
||||
$self->errorblank;
|
||||
|
||||
my @wait_search_array;
|
||||
if ( ! defined( $wait_search_string ) ){
|
||||
$self->{wait_search}=\@wait_search_array;
|
||||
}else{
|
||||
@wait_search_array=split(/\,/, $wait_search_string);
|
||||
$self->{wait_search}=\@wait_search_array;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
=head2 zombieSearchGet
|
||||
|
||||
Returns the current value for the zombie search.
|
||||
|
||||
The return is a Perl boolean.
|
||||
|
||||
my $zombie_search=$cps->zombieSearchGet;
|
||||
if ( $zombie_search ){
|
||||
print "only zombie procs will be shown";
|
||||
}
|
||||
|
||||
=cut
|
||||
|
||||
sub zombieSearchGet{
|
||||
my $self=$_[0];
|
||||
$self->errorblank;
|
||||
|
||||
return $self->{zombie_search};
|
||||
}
|
||||
|
||||
=head2 zombieSearchSet
|
||||
|
||||
Sets the zombie search value.
|
||||
|
||||
The value taken is a Perl boolean.
|
||||
|
||||
$cps->zombieSearchSet( 1 );
|
||||
|
||||
=cut
|
||||
|
||||
sub zombieSearchSet{
|
||||
my $self=$_[0];
|
||||
my $zombie_search=$_[1];
|
||||
$self->errorblank;
|
||||
|
||||
$self->{zombie_search}=$zombie_search;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
@ -931,9 +1265,9 @@ The default is as below.
|
|||
|
||||
=head1 ERROR CODES/FLAGS
|
||||
|
||||
=head2 1 / noUserStringDefined
|
||||
=head2 1 / badTimeString
|
||||
|
||||
No string string of users to search for defined.
|
||||
The time search string contains errors.
|
||||
|
||||
=head1 AUTHOR
|
||||
|
||||
|
|
Loading…
Reference in New Issue