Proc-ProcessTable-ncps/Proc-ProcessTable-ncps/bin/ncps

271 lines
5.1 KiB
Perl
Executable File

#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long;
use Proc::ProcessTable::ncps;
sub version{
print "ncps v. 0.0.0\n";
}
sub help{
print '
-c <regex> Search procs using the matching regex.
--ci Invert the command search.
--cf Show children minor faults.
--cF Show children major faults.
-m <pctmem> Memory usage percent to search for.
--mi Invert the memory usage search.
-f Show minor faults.
-F Show major faults.
-n Show number of threads.
-p <pctcpu> CPU usage percent to search for.
--pi Invert the CPU usage search.
--pid <pids> PIDs to search for.
--pidi Invert the PID search.
-r <RSSs> A comma seperated list of RSS values to search for.
--ri Invert the RSS search.
-s Show swapped out procs.
--si Invert the swapped out search.
--st <states> A comma seperated list of states to search for.
--sti Invert the state search.
-t <times> A comma seperated value of time, in seconds, to search for.
--ti Invert the time search.
-w <wchans> A string search for wait channels.
--wi Invert the wait channel search.
-z Show zombies procs.
';
}
# defaults
my $wait_channels_string;
my $wait_channels_invert=0;
my $zombie=0,
my $swapped=0,
my $swapped_invert=0;
my $version;
my $help;
my $commands_string;
my $commands_invert=0;
my $pids_string;
my $pids_invert=0;
my $cpu_string;
my $cpu_invert=0;
my $mem_string;
my $mem_invert=0;
my $rss_string;
my $rss_invert=0;
my $time_string;
my $time_invert=0;
my $states_string;
my $states_invert=0;
my $minor_faults=0;
my $major_faults=0;
my $cminor_faults=0;
my $cmajor_faults=0;
my $numthr=0;
# get the commandline options
Getopt::Long::Configure ('no_ignore_case');
Getopt::Long::Configure ('bundling');
GetOptions(
'w=s' => \$wait_channels_string,
'wi' => \$wait_channels_invert,
'h' => \$help,
'help' => \$help,
'v' => \$version,
'version' => \$version,
'z'=> \$zombie,
's'=> \$swapped,
'si' => \$swapped_invert,
'c=s' => \$commands_string,
'ci' => \$commands_invert,
'pid=s' => \$pids_string,
'pidi' => \$pids_invert,
'p=s' => \$cpu_string,
'pi' => \$cpu_invert,
'm=s' => \$mem_string,
'mi' => \$mem_invert,
'r=s' => \$rss_string,
'ri' => \$rss_invert,
't=s' => \$time_string,
'ti' => \$time_invert,
'st=s' => \$states_string,
'sti' => \$states_invert,
'f' => \$minor_faults,
'F' => \$major_faults,
'cf' => \$cminor_faults,
'cF' => \$cmajor_faults,
'n' => \$numthr,
);
# print the version info if requested
if ( $version ){
&version;
exit;
}
if ( $help ){
&version;
&help;
exit;
}
my @filters;
#
# handles wait channels
#
if ( defined( $wait_channels_string ) ){
my @wchans=split(/\,/, $wait_channels_string );
push( @filters, {
type=>'WChan',
invert=>$wait_channels_invert,
args=>{
wchans=>\@wchans,
},
});
}
#
# handles swappped procs search
#
if ( $swapped ){
push( @filters, {
type=>'Swapped',
invert=>$swapped_invert,
args=>{},
});
}
#
# handles the commands search
#
if ( defined( $commands_string ) ){
my @commands=split(/\,/, $commands_string );
push( @filters, {
type=>'Command',
invert=>$commands_invert,
args=>{
commands=>\@commands,
},
});
}
#
# handles the PIDs search
#
if ( defined( $pids_string ) ){
my @pids=split(/\,/, $pids_string );
push( @filters, {
type=>'PID',
invert=>$pids_invert,
args=>{
pids=>\@pids,
},
});
}
#
# handles the CPU search
#
if ( defined( $cpu_string ) ){
my @cpus=split(/\,/, $cpu_string );
push( @filters, {
type=>'PctCPU',
invert=>$cpu_invert,
args=>{
pctcpus=>\@cpus,
},
});
}
#
# handles the memory search
#
if ( defined( $mem_string ) ){
my @mems=split(/\,/, $mem_string );
push( @filters, {
type=>'PctMem',
invert=>$mem_invert,
args=>{
pctmems=>\@mems,
},
});
}
#
# handles the RSS search
#
if ( defined( $rss_string ) ){
my @rss=split(/\,/, $rss_string );
push( @filters, {
type=>'RSS',
invert=>$rss_invert,
args=>{
rss=>\@rss,
},
});
}
#
# handles the time search
#
if ( defined( $time_string ) ){
my @times=split(/\,/, $time_string );
push( @filters, {
type=>'Time',
invert=>$time_invert,
args=>{
times=>\@times,
},
});
}
#
# handles the time search
#
if ( defined( $states_string ) ){
my @states=split(/\,/, $states_string );
push( @filters, {
type=>'State',
invert=>$states_invert,
args=>{
states=>\@states,
},
});
}
my $args={
invert=>0,
cmajor_faults=>$cmajor_faults,
cminor_faults=>$cminor_faults,
major_faults=>$major_faults,
minor_faults=>$minor_faults,
numthr=>$numthr,
match=>{
checks=>\@filters,
}
};
my $ncps=Proc::ProcessTable::ncps->new( $args );
print $ncps->run;
exit 0;