add pctcpu search

git-svn-id: svn://127.0.0.1/Perl/Proc-ProcessTable-Colorizer/trunk@968 0c1c3402-1be1-de11-8092-0022686faf23
This commit is contained in:
Zane C. B-H 2017-10-16 09:54:47 +00:00
parent b8ef193a27
commit c1c6c5d9f0
2 changed files with 168 additions and 3 deletions

28
Proc-ProcessTable-Colorizer/bin/cps Normal file → Executable file
View File

@ -35,8 +35,24 @@ sub main::HELP_MESSAGE {
"-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";
"-u <users> A string search for users.\n".
"-t <time search> A numeric search for CPU time".
"-c <pctcpu search> A numeric search for CPU usage percent\n".
"-w <wait channels> A string search for wait channels.\n".
"\n".
"\n".
"String Search:\n".
"This is a comma seperated list of strings to search for.\n".
"Any items starting with ! will be inverted.\n".
"\n".
"foo Checks if it matches foo.\n".
"!foo Checks if it does not match foo.\n".
"foo,bar Checks if it matches foo or bar.\n.".
"\n".
"\n".
"Numeric Search:\n".
"This is a comma seperated list of values to search for.\n".
"Each item must start with a equality. <, <=, >=, > are all recognized.\n"
}
sub main::VERSION_MESSAGE {
@ -45,7 +61,7 @@ sub main::VERSION_MESSAGE {
#gets the options
my %opts=();
getopts('p:u:zw:st:', \%opts);
getopts('p:u:zw:st:c:', \%opts);
use Proc::ProcessTable::Colorizer;
@ -65,5 +81,11 @@ if ( defined( $opts{t} ) ){
exit $cps->error;
}
}
if ( defined( $opts{c} ) ){
$cps->pctcpuSearchSetString( $opts{c} );
if ($cps->error){
exit $cps->error;
}
}
print $cps->colorize;

View File

@ -52,6 +52,7 @@ sub new {
errorString=>'',
errorExtra=>{
1=>'badTimeString',
2=>'badPctcpuString',
},
colors=>[
'BRIGHT_YELLOW',
@ -91,6 +92,7 @@ sub new {
zombie_search=>0,
swapped_out_search=>0,
time_search=>[],
pctcpu=>[],
};
bless $self;
return $self;
@ -481,6 +483,76 @@ sub colorize{
}
}
#check to see if it needs to search for CPU percent
my $pctcpu_search_array=$self->pctcpuSearchGet;
if ( defined( $pctcpu_search_array->[0] ) ){
$required_hits++;
my $pctcpu_search_int=0;
my $matched=0;
#search while we have a CPU usage defined and it has not already been matched
while(
defined( $pctcpu_search_array->[ $pctcpu_search_int ] ) &&
( $matched == 0 )
){
my $checked=0;
my $to_match=$pctcpu_search_array->[ $pctcpu_search_int ];
my $time=$proc->{pctcpu};
#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++;
}
}
$pctcpu_search_int++;
}
}
#show zombie procs
if ( $self->{zombie_search} ){
@ -760,6 +832,77 @@ sub fieldsSet{
}
=head2 timeSearchGet
Returns the current value for the PCT CPU search.
The return is a array ref.
my $pctcpu_search=$cps->pctcpuSearchGet;
=cut
sub pctcpuSearchGet{
my $self=$_[0];
$self->errorblank;
return $self->{pctcpu_search};
}
=head2 pctcpuSearchSetString
Search for procs based on the CPU usage.
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 procs will be shown.
#search for procs with less than 60% of CPU usage
$cps->pctcpuSearchSetString('<60');
#shows procs with greater than 60% of CPU usage
$cps->waitSearchSetString('>60');
=cut
sub pctcpuSearchSetString{
my $self=$_[0];
my $pctcpu_search_string=$_[1];
$self->errorblank;
my @pctcpu_search_array;
if ( ! defined( $pctcpu_search_string ) ){
$self->{pctcpu_search}=\@pctcpu_search_array;
}else{
@pctcpu_search_array=split(/\,/, $pctcpu_search_string);
foreach my $item ( @pctcpu_search_array ){
if (
( $item !~ /^\>[0123456789]*$/ ) &&
( $item !~ /^\>=[0123456789]*$/ ) &&
( $item !~ /^\<[0123456789]*$/ ) &&
( $item !~ /^\<=[0123456789]*$/ )
){
$self->{error}=2;
$self->{errorString}='"'.$item.'"" is not a valid value for use in a PCT CPU search';
$self->warn;
return undef;
}
}
$self->{pctcpu_search}=\@pctcpu_search_array;
}
return 1;
}
=head2 processColorGet
my $timeColors=$cps->processColorGet;