This script logs CPU load for a given process, every n seconds into a file
Requires : bash, ps, awk
Examples :
psmon <process name> [delay [logfile]]
psmon sshd→ logs %CPU load from process sshd, every 5 seconds, to file psmon.log.[day]
psmon q3ded 10 q3ded.log → logs %CPU load from process q3ded, every 10 seconds, to file q3ded.log
- #!/bin/bash
- if [ -z $1 ] ; then
- echo "Monitor a cpu load for a given process, every n seconds"
- echo "Usage : `basename $0` <process name> [delay [logfile]]"
- echo " [delay] : defaults to 5 seconds"
- echo " [logfile] : defaults to psmon.log.yymmdd.hhmmss"
- echo " if you specify a logfile, you MUST specify a delay"
- echo "Example : psmon q3ded 10 q3dedmonitor.log"
- echo "Example : psmon q3ded"
- exit 0
- fi
- PROCESS=$1
- if $(ps -C "$PROCESS">/dev/null) ; then
- # process exists
- WARNING=""
- else
- # process doesnt exist
- WARNING="Warning : $PROCESS NOT running at the moment !"
- fi
- LOGFILE=$3
- if [ -z $3 ] ; then
- LOGFILE=psmon.log.$(date +%Y%m%d.%H%M%S)
- fi
- DELAY=$2
- if [ -z $2 ] ; then
- DELAY=5
- fi
- C="\033[1m"
- N="\033[0m"
- echo -e "${N}Monitoring $C$PROCESS$N cpu load, every $C$DELAY$N s, to file $C$LOGFILE"
- echo -e $WARNING
- echo "# psmon logfile" > $LOGFILE
- echo "# Process name : $PROCESS" >> $LOGFILE
- echo "# Started : $(date +%D\ \@\ %X)" >> $LOGFILE
- echo "" >> $LOGFILE
- # Now logging the stuff
- while true; do
- ps -C "$PROCESS" -o "pcpu" | awk '{a+=$1} END {print a}' >> $LOGFILE
- sleep $DELAY
- done
Shorter URL
Want to share or tweet this post? Please use this short URL: http://ozh.in/r
FirstPost
(au cas ou)