#!/bin/bash # Memonit create graph of memory usage and CPU for a command line or a process # Copyright: Patrice Ferlet © 2009-2010 # Version: 0.0.1-alpha # Licence: GNU/GPL v3 # # call -h option for usage # # Warning, time base is strange, you can give -s 0.1 to check memory every 100 ms but # I don't know why: it doesn't... It is **about** every 100 ms... # Don't forget it's a bash script ! # # You need to install "gnuplot" to generate graph # I use also "bc" and "ps" commands, please check if you get them max=0 grid=0 mega="o" STOP=0 title="" scale=0.1 autoscale=0 gplotfile="/tmp/_plotfile"$RANDOM".gp" datafile="/tmp/_plotdatas"$RANDOM format="640x480" stop_monitor(){ echo "Signal trapped" STOP=1 } trap stop_monitor SIGINT SIGTERM printHelp(){ cmd=$(basename $0) cat 1>&1 << EOF $cmd [-aghmk] [-s timer] [-p pid] [-o output] [-t title] [-f W,H] -- [command] Options: -a set autoscale On -g set grid to On -p pid to monitor if not given, command has to be given -h print this help -o img output filename (must be terminated by png, svg or jpg) -t title (double quoted) -m get memory in mega (default octet) -k get memory in ko (default octet) -s get memory every n second (n could be float to get miliseconds) default is 0.1 second -f format width,height if -o option given (size could be dynamically changed if gnuplot term used) EOF exit $1 } while getopts "aghkmvrcs:f:t:p:o:" flag do #echo "$flag" $OPTIND $OPTARG case $flag in a) autoscale=1;; g) grid=1;; t) title=$OPTARG;; p) pid=$OPTARG;; s) scale=$OPTARG;; h) printHelp 0;; o) output=$OPTARG;; k) mega="k";; m) mega="m";; f) format=$OPTARG;; esac if [ $OPTIND -gt $max ] then max=$OPTIND fi done shift $max #work on options [[ "$mega" == "o" ]] && mega=1 && divide="octets" [[ "$mega" == "k" ]] && mega=1024 && divide="Ko" [[ "$mega" == "m" ]] && mega=1048576 && divide="Mo" if [[ "$pid" == "" ]]; then if [[ "$@" == "" ]]; then echo "No command or pid given" printHelp 1 fi #run program and get pid $@ & pid=$! fi echo "program running with pid $pid" if [[ "$title" == "" ]]; then title="Monitoring $pid" fi #do the work now rm -f $datafile 2>/dev/null rm -f $gplotfile 2>/dev/null i=0; retval=0 while [[ $retval == "0" && "$STOP" == "0" ]] do usage=`ps -eo pid,rss,vsize,pcpu | grep "$pid"` retval=$? usage=`echo $usage | sed "s/\n/_/g" |sed -r "s/([0-9]+) +/\1;/g"` #echo $usage #get memory usages vmz=`echo $usage | cut -f3 -d";"` rss=`echo $usage | cut -f2 -d";"` cpu=`echo $usage | cut -f4 -d";"` #echo "get: $vmz $rss $cpu" for line in `ps -eo ppid,rss,vsize,pcpu | grep "$pid"|sed -r "s/([0-9]+) +/\1;/g"` do #cumulation v=`echo $line | cut -f3 -d";"` r=`echo $line | cut -f2 -d";"` c=`echo $line | cut -f4 -d";"` vmz=`echo "$vmz + $v" | bc -l` rss=`echo "$rss + $r" | bc -l` cpu=`echo "$cpu + $c" | bc -l` done #transform in megaoctets vmz=`echo "$vmz/$mega" | bc -l` rss=`echo "$rss/$mega" | bc -l` echo "$i $vmz $rss $cpu" >> $datafile sleep $scale i=`echo "$i+0.01"|bc -l` done echo "process terminated" echo "generate graph" if [[ "$output" != "" ]]; then ext=`echo $output | awk -F . '{print $NF}'` echo "set terminal $ext size $format" >> $gplotfile echo "set output \"/dev/null\"" >> $gplotfile # echo "set output \"$output\"" >> $gplotfile fi cat 1>> $gplotfile <> $gplotfile fi echo "replot" >> $gplotfile if [[ "$output" == "" ]]; then echo >> $gplotfile echo "pause -1 \"Press key ...\"" >> $gplotfile echo >> $gplotfile fi if [[ "$output" != "" ]]; then echo "set output \"$output\"" >> $gplotfile echo "replot" >> $gplotfile fi #generate plot now gnuplot $gplotfile rm -f $gplotfile 2>/dev/null rm -f $datafile 2>/dev/null #show graph #eog $png exit 0