Linux Memory Usage Summary Script

I was having trouble with a webserver this week, which I’d just set up for a client. When it went live, it seemed OK initially, but when we left it overnight it curled up its toes and died. It was so dead that I couldn’t even ssh in to reboot it, so we had to do a remote reboot. Crunch.

Anyway, on getting it back up, I poked around in the logs and found that it was running out of memory, which it really shouldn’t do given that it was a fairly low load on the webserver, and only apache, mysql and php were running on it. I looked around the Interwebs and found an excellent resource called Troubleshooting Memory Usage, which gave me some pointers about how to rein in apache and stop it from eating up memory so quickly. Thanks!

Anyway on the page was a script for summarising memory usage, which I thought was a good idea, so i grabbed that one and adapted it to my own purposes. I thought I’d make the results available here, so feel free to grab it and adapt it to your own needs.

#!/bin/bash
# Mem-info.sh. Version 1.5 2008-12-20
# Adapted from various sources around the Internet.
# Requires mutt to be installed to use the email function. 

#################### Functions ####################
function print_help {
	echo "  Usage:"
	echo "    $0 [-a full|summary] -f /tmp/memoryreport.txt"
	echo "    Parameters:"
	echo "    -a              Specify full or summary report"
	echo "    -f              Specify alternate filename "
	echo "    -e       Send report to specified email address"             
	echo "    -h | --help     This usage information"
	echo "    Running the script with no parameters writes a summary report to ./output.txt"
}

function summary_report {
	echo "=========================== SUMMARY ============================" > $mem_outputfile
	date >> $mem_outputfile
	echo  >> $mem_outputfile
	echo "=========================== uptime ==============================" >> $mem_outputfile
	uptime >> $mem_outputfile
	echo  >> $mem_outputfile
	echo "========================== free -m ==============================" >> $mem_outputfile
	free -m >> $mem_outputfile
	echo  >> $mem_outputfile
	echo "========================= vmstat 1.5 ============================" >> $mem_outputfile
	vmstat 1 5 >> $mem_outputfile
	echo  >> $mem_outputfile
	echo "================== ps top 20 Processes by CPU ===================" >> $mem_outputfile
	ps -eo user,%mem,%cpu,pid,cmd --sort -%cpu | head -n 20 >> $mem_outputfile
	echo  >> $mem_outputfile

}

function full_report {
	summary_report
	echo "========================= FULL DETAIL ===========================" >> $mem_outputfile
	echo "======================== top raw output =========================" >> $mem_outputfile
	top -b -n 1 >> $mem_outputfile
	echo " " >> $mem_outputfile
	echo "======================= ps auxf raw output ======================" >> $mem_outputfile
	ps auxf --width=200 >> $mem_outputfile
	echo  >> $mem_outputfile
	echo "=========================== end ================================ " >> $mem_outputfile
	echo  >> $mem_outputfile
	echo  >> $mem_outputfile
}

#################### Process Command line ####################

while [ "$1" != "" ]; do
    case $1 in
        -f )           	shift
                                mem_outputfile=$1
                                ;;
        -a )    	shift
				mem_action=$1
                                ;;
        -e )            shift
				email_to=$1
				;;
        -h | --help )           print_help
                                exit
                                ;;
        * )                     print_help
                                exit
				;;
    esac
    shift
done

######################### Set Defaults #########################
if [ -z "${mem_action}" ]; then 
    echo "    No action Specified. Defaulting to summary report"
    mem_action="summary"
fi
if [ -z "${mem_outputfile}" ]; then 
    echo "    No output file specified. Writing to `pwd`/output.txt"
    mem_outputfile="output.txt"
fi

######################### Do Stuff #########################

if [ "$mem_action" = "full" ]; then
		full_report
	else
		summary_report
fi

if [ "$email_to" = "" ]; then
		echo "    No email address specified. Not emailing report."
	else
		mutt -s "Memory Usage Report" $email_to < $mem_outputfile
fi

Or you can download the file mem-info1 if the formatting above is all messed up. Make sure you go to the page I linked to above for excellent advice on interpreting the report.

Leave a Comment