Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorgorjanc committed Feb 6, 2018
2 parents ccf7744 + d4f30fa commit f3ec522
Showing 1 changed file with 54 additions and 17 deletions.
71 changes: 54 additions & 17 deletions cpumemlog
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

### cpumemlog
###----------------------------------------------------------------------------
### ----------------------------------------------------------------------------

NAME=$(basename $0)

Expand All @@ -12,7 +12,7 @@ usage()
$NAME - Monitor CPU and RAM usage for a given process (and its children)
SYNOPSIS
$NAME pid [string] [-t=1]
$NAME pid [string] [-t=1] [-o=<outputfilename>]
DESCRIPTION
pid
Expand All @@ -26,6 +26,10 @@ usage()
Collect information in time intervals equal to x; this is passed to the
sleep command so check its documentation on the format of x; default is
10 seconds.
-o=<path>, --out=<path>
Specify filename of output file. If not given, defaults to
cpumemlog_<pid>[_string].txt
-h, --help
Print this output
Expand Down Expand Up @@ -54,48 +58,81 @@ EOF
}

## Defaults
PID=
STR=
TIME=10
FOUT=

## Get options
for OPTION in $@; do
case "$OPTION" in
# argument parsing : http://mywiki.wooledge.org/BashFAQ/035
position=0
while :; do
case $1 in
-h | --help )
usage
exit 0
;;
-t=* | --time=*)
TIME=$(echo $OPTION | sed -e "s/--time=//" -e "s/-t=//")
TIME=$(echo $1 | sed -e "s/--time=//" -e "s/-t=//")
;;
-o=* | --out=*)
FOUT=$(echo $1 | sed -e "s/--out=//" -e "s/-o=//")
;;
--) # End of all options.
printf 'End of all options?'
shift
break
;;
-?*)
printf 'WARNING: Unknown option (ignored): %s\n' "$1" >&2
;;
*) # Default case: If no more options then break out of the loop.
position=$((position+1))
if [ ${#1} -eq 0 ]; then
break
fi
case $position in
1)
PID=$1
;;
2)
STR=$1
;;
*)
printf 'WARNING: Unknown positional argument (ignored): %s\n' "$1" >&2
exit 1
esac
esac
shift
done


## Collect PID and STRING
if [ "$1" == "" ]; then
if [ -z "$PID" ]; then
echo "Missing PID!"
usage
exit 1
else
PID=$1
fi

if [ "$2" == "" ]; then
STR="${PID}"
else
STR="${PID}_${2}"
if [ -z $FOUT ]; then
if [ -z $STR ]; then
FOUT=cpumemlog_${PID}.txt
else
FOUT=cpumemlog_${PID}_${STR}.txt
fi
fi

rm -f cpumemlog_${STR}.txt
echo "DATE TIME PID PCPU PMEM RSS VSZ ETIME COMMAND" >> cpumemlog_${STR}.txt
rm -f $FOUT
echo "DATE TIME PID PCPU PMEM RSS VSZ ETIME COMMAND" >> $FOUT

printOut()
{
local PID
PID=$1

## Print everything except the header
## echo `date +"%Y-%m-%d %H:%M:%S"` `aps -p $PID -o pid= -o pcpu= -o pmem= -o rss= -o vsz= -o time= -o comm= --no-headers -w -w` >> cpumemlog_${STR}.txt
## echo `date +"%Y-%m-%d %H:%M:%S"` `aps -p $PID -o pid= -o pcpu= -o pmem= -o rss= -o vsz= -o time= -o comm= --no-headers -w -w` >> $FOUT
## BSD always prints the header
echo `date +"%Y-%m-%d %H:%M:%S"` `ps -p $PID -o pid= -o pcpu= -o pmem= -o rss= -o vsz= -o time= -o comm= -w -w | tail -n 1` >> cpumemlog_${STR}.txt
echo `date +"%Y-%m-%d %H:%M:%S"` `ps -p $PID -o pid= -o pcpu= -o pmem= -o rss= -o vsz= -o time= -o comm= -w -w | tail -n 1` >> $FOUT

## Any children?
PIDC=$(pgrep -P $PID)
Expand Down

0 comments on commit f3ec522

Please sign in to comment.