vBulletin Search Engine Optimization
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| Hi all, Dont know if this will help anybody but heres a bit of perl i just did to match the processes in pstat to the output of ps aux on AIX 4.3.3. Just wrote it as we had a performance problem and I could see a kproc that was clocking up loads of cpu and wanted to know what it was. If you specify a pid then it will only display that pid, if you dont it will match all kprocs. Tony #!/usr/bin/perl #--------------------------------------------------------------- # # Script Name: kproc_list # By : Tony Shrimpton # Date : 10/12/2003 # Version : 1.0 #--------------------------------------------------------------- # Quick Perl script to match hex output from pstat to ps aux output. for Kproc processes # #----------------------------------------------------------------- #--- Read Command line for a given pid if ( $ARGV[0] ne "" ) { $GetPid = $ARGV[0] } #Get PSTAT info local $/; open PSTAT,"pstat -A | awk {\'print \$1,\",\",\$4,\",\",\$9'} |"; $PstatData = <PSTAT>; close PSTAT; #Get Ps aux Info $Command = join("","ps aux | grep kproc | grep -v grep | awk {'print \$2,\",\",\$12'}"); open Check,"$Command |"; $CheckRes=<Check>; close Check; #split ps aux info in to a hash for (split/^/,$CheckRes) { @SplitRes=split(/\,/,$_); $SplitRes[0] =~ s/^\s+//; $SplitRes[0] =~ s/\s+$//; $ResHash{$SplitRes[0]}=$SplitRes[1]; } #read through pstat and match data for (split/^/,$PstatData) { @Row=split(/\,/,$_); $Row[1] =~ s/ //g; $Pid = hex($Row[1]); chomp $Row[2]; $Row[2] =~ s/^\s+//; # Delete leading whitespace. $Row[2] =~ s/\s+$//; # Delete trailing whitespace. if ( $GetPid ne "" ) { if ( $GetPid == $Pid ) { print "Kproc ($Pid) is $Row[2]\n"; exit; } } else { if ( $ResHash{$Pid} ) { print "Kproc ($Pid) is $Row[2]\n"; } } } |
| ||||
| Tony <ts@relson.net> wrote: T> Hi all, T> Dont know if this will help anybody but heres a bit of perl i just did T> to match the processes in pstat to the output of ps aux on AIX 4.3.3. T> Just wrote it as we had a performance problem and I could see a kproc T> that was clocking up loads of cpu and wanted to know what it was. T> If you specify a pid then it will only display that pid, if you dont T> it will match all kprocs. A few suggestions. T> #!/usr/bin/perl #!/usr/bin/perl -w Warnings are your friend. T> if ( $ARGV[0] ne "" ) { $GetPid = $ARGV[0] } $GetPid = $ARGV[0] if @ARGV == 1; T> #Get PSTAT info T> local $/; T> open PSTAT,"pstat -A | awk {\'print \$1,\",\",\$4,\",\",\$9'} |"; T> $PstatData = <PSTAT>; T> close PSTAT; Why use external commands when Perl already contains features of awk? Also, you should *always* check the return value of open() and inform the user if the function doesn't succeed. open PSTAT, "pstat -A " or die "cannot open pstat -A: $!"; T> #Get Ps aux Info T> T> $Command = join("","ps aux | grep kproc | grep -v grep | awk T> {'print \$2,\",\",\$12'}"); If you're dead-set on calling external commands, I'd suggest the shorter and equally as effective: $Command = join("", "ps aux | grep [k]proc | awk . . ."); The brackets in "[k]proc" will ignore the lines that contain both "grep" and "kproc" in the ps output. T> open Check,"$Command |"; open Check, "$Command |" or die "cannot open $Command: $!"; You do the user a disservice when you don't check for and report non-successful return values from open(). Regards, Nick -- "Why shouldn't I top-post?" http://www.aglami.com/tpfaq.html "Meanings are another story." http://www.ifas.org/wa/glossolalia.html |