Skip to content

Instantly share code, notes, and snippets.

@amr
Created October 5, 2009 11:40
Show Gist options
  • Save amr/202063 to your computer and use it in GitHub Desktop.
Save amr/202063 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Print average apache process size
# Licensed under GPL v3 or later
#
# =| USAGE |===================================================================
#
# echo <apache-parent-process-id> | ./apache_avg_procsize.sh
#
# On Debian this translates to:
#
# cat /var/run/apache2.pid | ./apache_avg_procsize.sh
#
# On Redhat:
#
# cat /var/run/httpd.pid | ./apache_avg_procsize.sh
#
if [ ! "$UID" -eq "0" ]
then echo "WARNING: This should be run as root." >&2; fi
read APACHE_PID
MEM_TOTAL=$(cat /proc/meminfo | awk '/MemTotal/ {print $2}')
# Before someone WTFs the next ps line..
# h: omit header
# opid: only print 'pid' column.
# ppid: print only processes whose parent process' id equal to given pid.
HTTPD_AVG_MEM=$(ps --ppid $APACHE_PID h opid |
# Use pmap to retrieve process memory consumption
while read pid; do
pmap -d $pid |
tail -n1 |
sed 's/.*private: \([0-9]*\).*/\1/';
done |
# Get average
awk '{ SUM += $1 } END {print SUM/NR}')
echo "scale=2; $HTTPD_AVG_MEM / 1024" | bc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment