DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Logmem.sh
continuously prints active memory from /proc/meminfo
if fileentries exceed MAXENTRIES, then file is bzipped
#!/bin/sh
# logmem.sh
# 2007 by Sascha Tayefeh
# continuously prints active memory from /proc/meminfo
# if fileentries exceed MAXENTRIES, then file is bzipped
# created for SUSE 10.2
# some adaptions may be needed for use with other linuxes
#
# usage (OPTIONS ORDER IS CRUTIAL!!!):
# nohup /usr/bin/nice -n 19 logmem.sh [logfile.log] [maximum entries per logfile ] [logging interval in seconds] &
#
# example:
# nohup /usr/bin/nice -n 19 logmem.sh mem.log 1747626 10 &
# nohup /usr/bin/nice -n 19 ~/src/sh/logmem.sh /tmp/mem.log 1747626 1 >& /tmp/logmem.sh.log &
#
# to merge archived files do something like this:
#
# bunzip2 *.bz2
# rm -f mem.merged.log
# find . -name "mem*" -exec cat {} >> mem.merged.log \;
#
# to CUT by TIME:
# cut -c9-29 mem.log
#
# Maximum number of entries per logfile
# When exeeded, file will be archived
pathToBzip="/usr/bin/bzip2"
currentdir=`pwd`
unamestring=`uname -a`
hostname=$HOST
pid=`ps -d | grep logmem | awk '{print $1}'`
echo "********************"
echo logmem.sh 2007 by ST
echo "********************"
echo "unamestring: $unamestring"
echo "host: $hostname"
echo "user: $USER"
echo "date: `date`"
echo "logmem.sh PID: $pid"
if [ $# -lt 3 ];
then
echo
echo "******** ERROR: Too few arguments ********"
echo "******** I need to have EXACTLY 3 arguments !!! ***********"
echo
echo "usage (OPTIONS ORDER IS CRUTIAL!!!):"
echo "nohup /usr/bin/nice -n 19 logmem.sh [logfile.log] [maximum entries per logfile ] [logging interval in seconds] &"
echo
echo "example:"
echo "nohup /usr/bin/nice -n 19 logmem.sh mem.log 1747626 10 &"
echo 'nohup /usr/bin/nice -n 19 ~/src/sh/logmem.sh /tmp/mem.log 1747626 1 >& /tmp/logmem.sh.log &'
echo
exit
else
let interval=$3
let MAXENTRIES=$2
logfile=$1
fi
rm -f $logfile
echo "logfile: $logfile"
echo "loginterval: $interval"
echo "syntax of logfile: [timestamp] [active mem] [active swap]"
echo
echo "to merge archived files do something like this:"
echo
echo " bunzip2 *.bz2"
echo " rm -f mem.merged.log "
echo " find . -name 'mem*' -exec cat {} >> mem.merged.log \;"
echo
echo "logging started until aborted by KILL or something..."
echo
let count=0
# enter endless loop
while [ 0 ];
do
# get properties
let mem=`grep "Active" /proc/meminfo | awk '{print $2}'`
let swapTot=`grep "SwapTotal" /proc/meminfo | awk '{print $2}'`
let swapFree=`grep "SwapFree" /proc/meminfo | awk '{print $2}'`
let swapUsed=$swapTot-$swapFree
datestr=`date +'%Y%m%d %H%M%S'`
# log top file
echo "$datestr $mem $swapUsed " >> $logfile
# Achive file?
if [ $count -ge $MAXENTRIES ];
then
let count=0
$pathToBzip -f -q -9 $logfile
mv "$logfile.bz2" "$logfile.$datestr.bz2"
fi
# fall asleep
sleep $interval
let count=$count+1
done





