admin-stuff/swap_usage.sh
2017-02-21 02:14:51 +01:00

90 lines
2.1 KiB
Bash
Executable File

#!/usr/bin/env bash
function usage() {
echo
[ -n "$1" ] && echo -e "Error: $1\n"
cat<<EOF
Usage: ` basename $0` < -a | -A |-p <program name to filter output> >
Shows swap-usage either for all programs (-a/-A) or filtered for
a given program name (-p program_name).
Options:
-A
Show swap-usage for all running processes, even if no swap
is in use. Not realy useul, but gives a short overview about
running processes.
-a
Show swap-usage for all running processes that have swap space allocated.
-p <programm_name>
Show swap-usage for all processes of the given program name.
`basename $0` -p amavisd
EOF
exit 1
}
[ $# -eq "0" ] && usage "wrong number of arguments"
all=false
while getopts Aahp: opt ; do
case $opt in
A) all=true ;;
a) ;;
h) usage ;;
p) filter_prog_name=$OPTARG ;;
\?) usage
esac
done
shift `expr $OPTIND - 1`
[ $# -eq "0" ] || usage "wrong number of arguments"
declare -i total_prog=0
declare -i total=0
for _dir in `find /proc/ -maxdepth 1 -type d | egrep "^/proc/[0-9]"` ; do
#_pid=`basename $_dir`
_pid=`echo $_dir | cut -d "/" -f 3`
_prog_name=`ps -p $_pid -o comm --no-headers`
if [ -n "$filter_prog_name" ]; then
[ "$_prog_name" != "$filter_prog_name" ] && continue
fi
for _mem in `grep Swap $_dir/smaps 2>/dev/null| awk '{print$2}'` ; do
[ $_mem -eq 0 ] && ! $all ] && continue
let total_prog=$total_prog+$_mem
let tmp=$tmp+$_pid
done
[ $total_prog -eq 0 ] && ! $all && continue
echo "PID = $_pid - Swap used: $total_prog kb - ( $_prog_name )"
let total=$total+$total_prog
total_prog=0
done
if [ $total -eq 0 ]; then
if [ -z $filter_prog_name ] ; then
echo -e "\n\tNo swap space alocated.\n"
else
echo -e "\n\tNo swap space alocated by \"$filter_prog_name\" processes.\n"
fi
else
if [ -z $filter_prog_name ] ; then
echo -e "\n\tTotal allocated swap space: $total kb\n"
else
echo -e "\n\tAllocated swap space by \"$filter_prog_name\" processes: $total kb\n"
fi
fi
exit 0