Script to produce report of High Utilization Processes

Hi,

I have requirement to produce a report on high CPU utilization processes and the processes lying on the CPU for long time (Long running queries). The report should append into the files every 3 minutes. I use prstat to pull top 5 and found the following result.

(USER01)/opt/tuxedo/appsrv_clasuat> prstat -u tuxedo -a -s time -s cpu -n 5 0 1
   PID USERNAME  SIZE   RSS STATE  PRI NICE      TIME  CPU PROCESS/NLWP
  7241 tuxedo    269M  152M sleep   59    0   1:07.36 1.2% cleard/4
 23213 tuxedo    198M   95M sleep   59    0   0:11.15 0.7% cleard/4
 18524 tuxedo    274M  158M sleep   59    0   0:31.10 0.7% cleard/4
 24827 tuxedo    202M  120M sleep   59    0   0:06.46 0.5% WSH/4
 15415 tuxedo    139M   37M sleep   59    0   0:07.45 0.5% cleard/4
 NPROC USERNAME  SIZE   RSS MEMORY      TIME  CPU
    49 tuxedo   7129M 2524M    32%   5:51.53 6.6%

Total: 49 processes, 191 lwps, load averages: 0.46, 0.69, 0.70

Along with this report, I need append the the each tuxedo process (cleard_relcom) for the above process id (7241,23213,18524,24827,24827) in the report as below. Is it possible to produce single report with both of these result?

(USER01)/opt/tuxedo/appsrv_clasuat> ps -ef | grep -v grep | grep 2775
  tuxedo  2775     1  3 12:18:57 ?       100:01 cleard_relcom -C dom=clasuat2 -g 5 -i 21 -u clauat02 -U /opt/tuxedo/appsrv_clasu
(USER01)/opt/tuxedo/appsrv_clasuat>

I need to merge both the report and append to a file. Can you please help me?

Thina

Use > to write output to a file
and >> to append to the file

eg:

$ prstat -u tuxedo -a -s time -s cpu -n 5 0 1 > report.txt
$ ps -ef | grep -v grep | grep 2775 >> report.txt

Hi Chubler_XL,

Thanks for your reply. I need output in the below format. For each process id listed in the prstat command, I need fetch the process detail through ps command and append in the same row of prstat command.

It means, additional column of PROCESS_DETAIL (cleard_relcom -C dom=clasuat2 -g 5 -i 21 -u clauat02) added in each row of the respective process id listed in the prstat command.

Expected output.

   PID USERNAME  SIZE   RSS STATE  PRI NICE      TIME  CPU PROCESS/NLWP PROCESS_DETAIL
  7241 tuxedo    269M  152M sleep   59    0   1:07.36 1.2% cleard/4     cleard_relcom -C dom=clasuat2 -g 5 -i 21 -u clauat02 -U /opt/tuxedo/appsrv_clasu
 23213 tuxedo    198M   95M sleep   59    0   0:11.15 0.7% cleard/4     cleard_batch -C dom=clasuat2 -g 5 -i 21 -u clauat02 -U /opt/tuxedo/appsrv_clasu
 18524 tuxedo    274M  158M sleep   59    0   0:31.10 0.7% cleard/4     cleard_batch -C dom=clasuat2 -g 5 -i 21 -u clauat02 -U /opt/tuxedo/appsrv_clasu
 24827 tuxedo    202M  120M sleep   59    0   0:06.46 0.5% WSH/4        WSH -C dom=clasuat2 -g 5 -i 21 -u clauat02 -U /opt/tuxedo/appsrv_clasu
 15415 tuxedo    139M   37M sleep   59    0   0:07.45 0.5% cleard/4     cleard_cb -C dom=clasuat2 -g 5 -i 21 -u clauat02 -U /opt/tuxedo/appsrv_clasu   
 NPROC USERNAME  SIZE   RSS MEMORY      TIME  CPU
    49 tuxedo   7129M 2524M    32%   5:51.53 6.6%

Total: 49 processes, 191 lwps, load averages: 0.46, 0.69, 0.70

How about this:

addcmdline ()
{
   PID=`echo $1`
   REM=${PID#* }
   PID=${PID% $REM}
   CMD=`ps -p $PID -o cmd | tail -1`
   printf "%-72s%s" "$1" "$CMD"
}
ADD_DET=0
prstat -u tuxedo -a -s time -s cpu -n 5 0 1 | while read
   case "$REPLY" in
       *\ PID\ USER*)
           echo "$REPLY PROCESS_DETAIL"
           ADD_DET=1 ;;
        *\ NPROC\ USER*)
           echo "$REPLY"
           ADD_DET=0 ;;
        *) [ $ADD_DET -eq 1 ] && line=`addcmdline "$REPLY"`
           echo "$REPLY" ;;
    esac
done

Note this uses ps -p 2775 -o cmd to get the command line of a process (2775 in the example). Your ps may have a different code for the -o option, so if it dosn't work check man ps for the correct code to use instead of "cmd".