#!/bin/bash -p
# pstool v1.3, Finn Magnusson, finn.magnusson@ericsson.com
# Type "pstool" for help.

######################################################################################
#                                                                                    #
#  Ericsson AB 2001-2024    - All Rights Reserved                                   #
#                                                                                    #
# The copyright to the computer program(s) herein is the property   of Ericsson AB,  #
# Sweden. The programs may be used and/or copied only with the written permission    #
# from Ericsson AB or in accordance with the terms and  conditions stipulated in the #
# agreement/contract under which the program(s) have been supplied.                  #
#                                                                                    #
######################################################################################

moshelldir=`dirname "$0"`
if [[ $moshelldir != /* ]] ; then moshelldir=`pwd`/$moshelldir ; fi
vobsinstallation=0
unamea=$(uname -a)
gawkext=""
gawklib=""
if [[ $unamea = [Ll][iI][nN][uU][xX]*x86_64* ]] ; then gawklib="lin64"   ; if [[ $vobsinstallation = 1 ]] ; then gawkext=".lin64" ; fi
elif [[ $unamea = [Ll][iI][nN][uU][xX]* ]]      ; then gawklib="linux"   ; if [[ $vobsinstallation = 1 ]] ; then gawkext=".linux" ; fi
elif [[ $unamea = SunOS*sparc* ]]               ; then gawklib="solaris" ; if [[ $vobsinstallation = 1 ]] ; then gawkext=".solaris" ; fi
elif [[ $unamea = SunOS* ]]                     ; then gawklib="sol86"   ; if [[ $vobsinstallation = 1 ]] ; then gawkext=".sol86" ; fi
elif [[ $unamea = Darwin* ]]                    ; then gawklib="app"     ; if [[ $vobsinstallation = 1 ]] ; then gawkext=".app"   ; fi
else gawklib="cygwin" ; gawkext=".exe"
fi
gawkprog="gawk${gawkext}" 
gawk="$moshelldir/$gawkprog"

#special case where moshell has been installed on a linux 32 bit machine but should be run on linux 64 sharing the same file system
if [[ $unamea = [Ll][iI][nN][uU][xX]*x86_64* && $vobsinstallation != 1 && `file "$moshelldir/gawk"` = *32-bit* ]] ; then gawklib="linux" ; fi

filefunc="$moshelldir/commonjars/lib/${gawklib}/filefuncs"
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}$moshelldir/commonjars/lib/${gawklib}
export LANG=C
export LC_ALL=C

user=""
monitor=0
kill=0
old=0
elapsed=0
while getopts "u:km9oe:" options
do
case $options in
k) kill=15 ;;
9) kill=9 ;;
m) monitor=1 ;;
u) user=$OPTARG ;;
e) elapsed=$OPTARG ;;
o) old=1 ;;
esac
done
shift $(($OPTIND - 1))

function print_usage()
{
cat <<EOF
Usage: pstool [-m] [-o] [-u <userid>] [-e <nrdays>] [-k|-9] list|detail|<process-filter>|<process-id>

This utility shows the resources consumed by a process and its children:
 - NCh    = Number of Children processes spawned by that process
 - %CPU   = Percentage of the CPU used by the process and its children
 - %MEM   = Percentage of the RAM memory used by the process and its children
 - RSS    = Resident Set Size, the amount of RAM memory used by the process and its children, in MB
 - VSZ    = Virtual Memory Size, the amount of Virtual memory (= RAM + swap) used by the process and its children, in MB
 - ELAPSED= The time for which a process has been running, expressed in days/hours/minutes/seconds. Eg: 345-12:04:07 = 345 days, 12h, 4m, 7s
 - aXXX   = The Average usage value of a resource
 - pXXX   = The highest (Peak) usage value of a resource

Arguments:
 list: to show the resource consumption of all moshell sessions currently running on the workstation. Eg: "pstool list"
 detail: same as "list" but shows the resource consumption of all processes spawned by each moshell session. Eg: "pstool detail"
 <process-id>: to show the resource consumption of a process and its children, given by its PID. Eg: "pstool 5874"
 <process-filter>: same as above but using a regular expression, matching the command that started the process (if contain spaces, replace spaces with .*)

Options:
- The "-m" option is for continuous monitoring of the resources consumed by the process and its children. 
- The "-u" option is to specify the owner of the process. Only needed in case the person running the pstool is different than the person who owns the process.
- The "-e" option is to specify the number of elapsed days. Eg "-e 5" to only display/kill the sessions older than 5 days.
- The "-k" option is to send the "kill" command to a process and all its children. 
- The "-9" option is to send the "kill -9" command to a process and all its children (to be used only in case the standard kill did not work).
- The "-o" option is for printing with the old format (moshell 7.1m and before)

Examples: 
>> pstool moshell.*rnc11            --> show the process tree for a process started by a command matching "moshell.*rnc11"
>> pstool -m 3214                   --> monitor resource consumption for process with pid 3214
>> pstool -u root httpd             --> show process tree for a process called "httpd" and owned by the user "root"
>> pstool -k 3214		    --> kill process 3214 and all its children (standard kill).
>> pstool -9 3214		    --> force kill process 3214 and all its children (kill -9).
>> pstool -e 10 -9 .                --> force kill all sessions older than 5 days.
>> pstool list | sort -nk 7         --> show all running moshell sessions, sorted by RSS (= RAM usage)
>> pstool list | sort -nk 8         --> show all running moshell sessions, sorted by VSZ (= RAM+swap)
>> pstool detail                    --> show all running moshell sessions, sorted by number of spawned processes
EOF
}

function pstool()
{
$gawk -v pstoolpid=$1 -v monitor=$monitor -v kill=$kill -v moshelldir="$moshelldir" -v user=$user -v old=$old -v elapsed=$elapsed -l "$filefunc" -f $moshelldir/commonjars/pstool.awk
}

if [[ $# -eq 1 ]] ; then
	pstool $1
else
	print_usage
fi

