#!/bin/ksh ## ## SCRIPT: ps_cumcpusort_anyhost_bygui ## ## Where: in $FEDIR/scripts where FEDIR=/apps/nns_com/fea ## ############################################################################ ## PURPOSE: ## Lists the top processes on ANY host in terms of the 'ps -ef' TIME ## column (cumulative CPU time). ## ## Uses the 'rsh', 'ps', 'awk', and 'sort' commands. ## ## A Tcl-Tk script presents a GUI that prompts for hostid. ## The entry field is initialized with the hostname of the ## SGI host on which this script is running. ############################################################################ ## CALLED BY: 'perftools' in $FEDIR/scripts -- ## actually by 'perftools.chestdef' in $FEDIR/scripts. ## ## Site SGI toolchest-drawer path is ## ## SGI Toolchest -> ## Handy Tools -> ## Any Host - Perf Tools (Processes) -> ## Processes (CumCPUtime sort) @AnyHost ############################################################################ ## METHOD: ## Uses 'ps -ef' piped to 'awk' and 'sort'. ## ## However, '-f' shows start-time (which is either 1 or 2 fields) before ## the cpu-time field. Needed to use a multi-if-statement awk to ## relocate the CPU-time (to front of recs) for sorting by CPU-time. ## ## Also, '-f' shows start-time, but no mem-sizes. ## Requires using '-l' which introduces other inflexibilities. ## ##------ ## ## Some investigation of 'man ps' AND strings in '/sbin/ps' indicate that ## it would be better (more flexible) to use a '-o' set of options like ## ## ps -e -o 'user ruser pid ppid otime time sz rss cpu pcpu state pri nice tty etime stime args' ## ## Some other options: ## pgid sid class pri opri nice addr wname sz:rss vsz tty comm ## group rgroup uid flag util label ############################################################################ ## MAINTENANCE HISTORY: ## Written by: Blaise Montandon 27Apr2000 Based on 'topps_cumcpu_anyhost' and ## 'ps_starttimes_1host_bygui' in $FEDIR/scripts ## Updated by: Blaise Montandon 08apr2004 Chg 'enter_hostid_optmenu.tk' to ## 'enter_hostid_optmenu_toghelp-scroll.tk'. ## Updated by: Blaise Montandon 13apr2004 Update the trailer of the report. ## E.g. add toolchest-drawer path. ############################################################################ if test "$FEDIR" = "" then FEDIR=/apps/nns_com/fea export FEDIR fi ########################################################################## ## PROMPT FOR HOSTNAME. ########################################################################## WINTITLE="Processes on AnyHost (sorted by Cumulative CPU time)" export WINTITLE WIN_INFO="\ INPUT: Enter the name of an SGI host on the NNS network --- and click OK. (You can enter an IP address, if necessary.) ------ OUTPUT: This utility shows the processes on the specified host --- sorted by Cumulative CPU time (mins:secs). ----- USAGE: This utility is especially helpful to check for processes on the host that may be putting a heavy load on the host (or heavy load on the network) --- perhaps unnecessarily and perhaps for DAYS, SLOWING DOWN YOU & YOUR JOBS. ----- Note that you can do queries on MULTIPLE HOSTS and bring the report windows side-by-side for comparison --- to look for abnormal conditions. OR you may want to start other displays for the SAME HOST -- like 'top' or 'gr_osview' or network-I/O or memory-usage displays. " export WIN_INFO HOST_ID="`hostname`" export HOST_ID # HOST_ID=`$FEDIR/tkGUIs/enter_hostid.tk` # HOST_ID=`$FEDIR/tkGUIs/enter_hostid_optmenu.tk` HOST_ID=`$FEDIR/tkGUIs/enter_hostid_optmenu_toghelp-scroll.tk` if test "$HOST_ID" = "" then exit fi ########################################################################## ## SET OUTPUT FILENAME, local if possible. ########################################################################## . $FEDIR/scripts/set_localoutlist ########################################################################## ## SET REPORT HEADING. ########################################################################## ## HOSTNAME3=`/usr/etc/arp $HOST_ID` ## HOSTNAME2=`echo "$HOSTNAME3" |sed "s|-- no entry||"` echo "\ ..................... `date '+%Y %b %d %a %T%p'` ....................... ALL PROCESSES ON HOST: $HOST_ID (SORTED BY Accumulated-CPU-time, since their respective start times) The meanings of column headings are given at the bottom of this report. TIME min:sec UID PID PPID C STIME TTY CMD -------- ------- -------- -------- - -------- ------- --------------------------- " > $OUTLIST ########################################################################## ## SET INDICATOR OF Local or Remote host. ########################################################################## THISHOST=`hostname` LOCALHOST="N" if test "$HOST_ID" = "$THISHOST" then LOCALHOST="Y" fi ########################################################################## ## PUT 'ps -ef' output into an in-memory variable, PS_OUTPUT. ########################################################################## if test "$LOCALHOST" = "Y" then PS_OUTPUT=`ps -ef` else ############################################## ## MAKE .rhosts FILE FOR THE USER, if needed. ############################################## # echo "+ $USER" > $HOME/.rhosts . $FEDIR/scripts/mak_rhosts PS_OUTPUT=`rsh $HOST_ID ps -ef` # . $FEDIR/scripts/mv_rhosts fi ########################################################################## ## GENERATE REPORT CONTENTS from the PS_OUTPUT in-memory variable. ########################################################################## echo "$PS_OUTPUT" | awk 'BEGIN { } ## Skip the 'ps' header line. NR == 1 {next} ## Handle case when two colons are in the line. (STIME is a "hh:mm:ss" string.) $5 ~ /..:..:../ { printf ("%8s %-8s %8s %8s %1s %8s %-8s %s \n", $7, $1, $2, $3, $4, $5, $6, $8) next } ## Handle case when two colons are NOT in the line. (STIME is a "month day" string.) $5 !~ /..:..:../ { # printf ("%8s %-8s %8s %8s %1s %3s %-4s %-8s %s \n", $8, $1, $2, $3, $4, $5, $6, $7, $9) # printf ("%8s %-8s %8s %8s %1s %3s %4s %-8s %s \n", $8, $1, $2, $3, $4, $5, $6, $7, $9) printf ("%8s %-8s %8s %8s %1s %5s %2s %-8s %s \n", $8, $1, $2, $3, $4, $5, $6, $7, $9) next } ' | sort -t: +0nr -1 +1r -2 >> $OUTLIST ## Sort by mins:secs. ########################################################################## ## ADD REPORT 'TRAILER'. ########################################################################## echo " ..................... `date '+%Y %b %d %a %T%p'` ....................... The output above is from script $0 which was run from host $THISHOST . Commands used: 'ps -ef' piped to 'awk' and 'sort' ............................................................................ Column heading meanings --- in this formatted listing: TIME The cumulative execution time for the process (mins:secs) --- CPU-time, not wall-clock time (which is deducible from STIME). UID The login name of the process owner. PID The process ID of the process. PPID The process ID of the parent process. STIME The starting time of the process, given in hours, minutes, and seconds. (For a process begun more than twenty-four hours before the ps inquiry is executed, start-time is given as month and day.) CMD The command/executable name, without its arguments. (A process that has exited and has a parent, but has not yet been waited for by the parent, is marked .) TTY The controlling terminal for the process. (A question mark, ?, is printed when there is no controlling terminal.) C Processor utilization for scheduling. ............................................................................. IMPLEMENTATION METHODS: This 'Processes (CumCPUtime sort)' script can be accessed via a drawer in a command toolchest-utility, like 'Perf Tools' --- which is available as a drawer in the command/toolchest utility 'Handy Tools'. A site SGI toolchest-drawer path is SGI Toolchest -> Handy Tools -> Any Host - Perf Tools (Processes) -> Processes (CumCPUtime sort) @AnyHost Or, you can implement the script (or the 'perftools' script or the 'handytools' script) as - a command alias, via your .profile file; examples: alias pscpu='$0' alias ptools='$FEDIR/scripts/perftools' alias htools='$FEDIR/scripts/handytools' - a desktop icon, via the 'Find, File QuickFind' tool drawers; - a drawer in the SGI toolchest, via your .auxchestrc file. .......................... `date '+%Y %b %d %a %T%p %Z'` ................. " >> $OUTLIST ########################################################################## ## SHOW REPORT. ########################################################################## $FEDIR/scripts/shofil $OUTLIST ########################################################################## ## END of script. Notes ON 'ps -ef' and 'ps -el' follow. ########################################################################## ## ## NOTES ON 'ps -ef' and 'ps -el' output attempts: ## ## First attempts: (Does not sort well on TIME, for some non-obvious reason?) ## ps -ef | sort +6 -r | head >> $1 ## ps -ef | sort +6 -r >> $1 ## ########################################################################## ## Output of 'ps -ef': ## ## Cols: ## 1-8 9-14 15-20 21-23 24-32 33-39 40-46 47- ## UID PID PPID C STIME TTY TIME COMD ## ## Examples: ## root 1 0 0 Sep 03 ? 0:04 /etc/init ## root 507 1 0 Sep 03 tablet 0:00 /sbin/getty ttyd1 co_9600 ## bmo01 3320 3297 0 08:16:38 pts/9 0:00 -ksh ## ## Importance to this list: ## UID PID PPID C STIME TTY TIME COMD ## ** * * * *** ** ## ## UID PID PPID STIME TIME COMD ## ## ps -ef | cut -c1-8,9-14,15-20,24-29,31-32,40-46,47- | sort -r +4 | \ ## head -$HEADRECS >> $1 ## ########################################################################## ## Output of 'ps -el': ## ## Cols: ##1-2 5-10 11-16 17-22 26-29 33-35 36-47 48-55 56-63 64-70 71- ## 3-4 23-25 30-32 ## F S UID PID PPID C PRI NI P SZ:RSS WCHAN TTY TIME COMD ## ## Examples: ##30 S 0 1 0 0 39 20 * 74:46 881b65b0 ? 0:04 init ##30 S 0 507 1 0 28 20 * 294:103 881b78d0 tablet 0:00 getty ##30 S 2250 3320 3297 0 28 20 * 348:116 881b6a30 pts/9 0:00 ksh ## ## Importance to this list: ## F S UID PID PPID C PRI NI P SZ:RSS WCHAN TTY TIME COMD ## ** ** * * *** ** ## ## UID PID PPID SZ:RSS TIME COMD ## ## ps -el | cut -c5-10,11-16,17-22,36-47,64-70,71- | sort -r +4 | \ ## head -$HEADRECS >> $1 ##########################################################################